Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 83 latest episode block #266

Merged
merged 7 commits into from
Jan 18, 2024
Merged
38 changes: 37 additions & 1 deletion assets/js/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal block libraries
*/
import { __ } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import { registerBlockType, registerBlockVariation } from '@wordpress/blocks';

// Split the Edit component out.
import Edit from './edit';
Expand Down Expand Up @@ -105,3 +105,39 @@ export default registerBlockType(
},
},
);

const VARIATION_NAME = 'podcasting/latest-episode';

registerBlockVariation('core/query', {
name: VARIATION_NAME,
title: 'Latest Podcast Episode',
description: 'Displays the latest podcast episode.',
isActive: ['simple-podcasting'],
icon: 'microphone',
attributes: {
namespace: VARIATION_NAME,
query: {
postType: 'post',
podcastingQuery: 'not_empty',
},
},
allowedControls: [ ],
scope: [ 'inserter' ],
innerBlocks: [
[
'core/post-template',
{},
[ [
'core/group',
{ className: 'podcasting-latest-episode' },
[
[ 'core/post-featured-image' ],
[ 'core/group', { className: 'podcasting-latest-episode__content' }, [
[ 'core/post-title' ], [ 'core/post-date' ], [ 'core/post-excerpt' ]
] ],
]
] ],
],
[ 'core/query-no-results' ],
],
});
1 change: 1 addition & 0 deletions assets/js/blocks/latest-episode/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './index.scss';
51 changes: 51 additions & 0 deletions assets/js/blocks/latest-episode/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
.podcasting-latest-episode {
display: flex;
flex-direction: column;
justify-content: end;
min-height: 20rem;
overflow: hidden;
position: relative;

& .wp-block-post-featured-image {
height: 100%;
object-fit: fill;
object-position: center;
position: absolute;
width: 100%;

&::after {
background-color: rgb(0 0 0 / 75%);
content: "";
display: block;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
}
}

.podcasting-latest-episode__content {
color: #fff;
padding: 3rem;
position: relative;
z-index: 1;

@media (min-width: 768px) {
padding: 3rem;
}

& .wp-block-post-excerpt,
& .wp-block-post-excerpt__more-text {
margin-top: 0.25rem;
}

& .wp-block-post-excerpt__more-link {
color: #fff;
}
}

.editor-styles-wrapper .wp-block-post-content .podcasting-latest-episode__content .wp-block-post-excerpt__more-link:where(:not(.wp-element-button)) {
color: #fff;
}
59 changes: 59 additions & 0 deletions includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,62 @@ function( $platform ) {
wp_send_json_success( $result );
}
add_action( 'wp_ajax_get_podcast_platforms', __NAMESPACE__ . '\ajax_get_podcast_platforms' );

/**
* Latest podcast query for front-end.
*/
function latest_episode_query_loop($query) {

// update query to only return posts that have a podcast selected
return [
'post_type' => 'post',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => [
[
'taxonomy' => 'podcasting_podcasts',
'field' => 'term_id',
'operator' => 'EXISTS'
]
],
];
}

/**
* Latest podcast check.
*/
function latest_episode_check( $pre_render, $parsed_block, $parent_block ) {

if ( isset( $parsed_block[ 'attrs' ][ 'namespace' ] ) && 'podcasting/latest-episode' === $parsed_block[ 'attrs' ][ 'namespace' ] ) {
add_action( 'query_loop_block_query_vars', __NAMESPACE__ . '\latest_episode_query_loop' );
}
}
add_filter( 'pre_render_block', __NAMESPACE__ . '\latest_episode_check', 10, 3 );

/**
* Latest podcast query in editor.
*/
function latest_episode_query_api( $args, $request ) {

$podcasting_podcasts = $request->get_param( 'podcastingQuery' );

if ( 'not_empty' === $podcasting_podcasts ) {
$args = [
'post_type' => 'post',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => [
[
'taxonomy' => 'podcasting_podcasts',
'field' => 'term_id',
'operator' => 'EXISTS'
]
],
];
}

return $args;
}
add_filter( 'rest_post_query', __NAMESPACE__ . '\latest_episode_query_api', 10, 2);
45 changes: 45 additions & 0 deletions simple-podcasting.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,48 @@ function setup_edit_screen() {
}
}
add_action( 'admin_init', __NAMESPACE__ . '\setup_edit_screen' );

/**
* Registers block assets for Latest Episode.
*/
function register_latest_episode_assets() {
if ( ! file_exists( PODCASTING_PATH . 'dist/latest-episode.asset.php' ) ) {
return;
}

$block_asset = require PODCASTING_PATH . 'dist/latest-episode.asset.php';

wp_register_style(
'latest-episode-block',
PODCASTING_URL . 'dist/latest-episode.css',
array(),
$block_asset['version'],
'all'
);

wp_enqueue_style( 'latest-episode-block' );
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\register_latest_episode_assets' );

/**
* Registers block assets for Latest Episode in admin.
*
*/
function register_latest_episode_assets_admin() {
if ( ! file_exists( PODCASTING_PATH . 'dist/latest-episode.asset.php' ) ) {
return;
}

$block_asset = require PODCASTING_PATH . 'dist/latest-episode.asset.php';

wp_register_style(
'latest-episode-block',
PODCASTING_URL . 'dist/latest-episode.css',
array(),
$block_asset['version'],
'all'
);

wp_enqueue_style( 'latest-episode-block' );
}
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\register_latest_episode_assets_admin' );
5 changes: 5 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ module.exports = {
'assets/js',
'create-podcast-show.js'
),
'latest-episode': path.resolve(
process.cwd(),
'assets/js/blocks/latest-episode',
'index.js'
),
},
plugins: [
...defaultConfig.plugins,
Expand Down
Loading