feat: add editorial query blocks
This commit is contained in:
24 files changed
+1421
No files matched your search
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* Dynamic editorial block registration and shared rendering helpers.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
/**
|
||||
* Allowlist a semantic heading level.
|
||||
*
|
||||
* @param mixed $value Requested level.
|
||||
* @param int $fallback_level Default level.
|
||||
* @return int Safe level from 2 through 6.
|
||||
*/
|
||||
function chrnx_get_heading_level( $value, $fallback_level = 2 ) {
|
||||
$level = absint( $value );
|
||||
return in_array( $level, array( 2, 3, 4, 5, 6 ), true ) ? $level : $fallback_level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a post's meaningful featured image or decorative topic fallback.
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @param string $class_name CSS class prefix.
|
||||
* @return string Escaped image/fallback markup.
|
||||
*/
|
||||
function chrnx_get_card_media_markup( $post_id, $class_name ) {
|
||||
$thumbnail_id = get_post_thumbnail_id( $post_id );
|
||||
if ( $thumbnail_id ) {
|
||||
return wp_get_attachment_image(
|
||||
$thumbnail_id,
|
||||
'chrnx-card',
|
||||
false,
|
||||
array( 'class' => $class_name . '__image' )
|
||||
);
|
||||
}
|
||||
$topic = chrnx_get_post_topic_slug( $post_id );
|
||||
$class = $class_name . '__fallback' . ( '' !== $topic ? ' is-topic-' . sanitize_html_class( $topic ) : '' );
|
||||
return '<span class="' . esc_attr( $class ) . '" aria-hidden="true"></span>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Render one semantic editorial card.
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @param int $heading_level Heading level.
|
||||
* @param string $class_name Card class.
|
||||
* @return string Escaped card markup or empty string.
|
||||
*/
|
||||
function chrnx_get_editorial_card_markup( $post_id, $heading_level = 3, $class_name = 'chrnx-story-card' ) {
|
||||
if ( ! chrnx_is_eligible_editorial_post( $post_id ) ) {
|
||||
return '';
|
||||
}
|
||||
$post = get_post( $post_id );
|
||||
$heading_level = chrnx_get_heading_level( $heading_level, 3 );
|
||||
$topic_terms = get_the_terms( $post_id, 'chrnx_topic' );
|
||||
$topic_label = ! is_wp_error( $topic_terms ) && ! empty( $topic_terms ) ? $topic_terms[0]->name : '';
|
||||
$excerpt = wp_trim_words( wp_strip_all_tags( get_the_excerpt( $post ) ), 24 );
|
||||
|
||||
$markup = '<article class="' . esc_attr( $class_name ) . '">';
|
||||
$markup .= '<a class="' . esc_attr( $class_name . '__link' ) . '" href="' . esc_url( get_permalink( $post ) ) . '">';
|
||||
$markup .= chrnx_get_card_media_markup( $post_id, $class_name );
|
||||
$markup .= '<span class="' . esc_attr( $class_name . '__content' ) . '">';
|
||||
if ( '' !== $topic_label ) {
|
||||
$markup .= '<span class="' . esc_attr( $class_name . '__badge' ) . '">' . esc_html( $topic_label ) . '</span>';
|
||||
}
|
||||
$markup .= '<h' . $heading_level . ' class="' . esc_attr( $class_name . '__title' ) . '">' . esc_html( get_the_title( $post ) ) . '</h' . $heading_level . '>';
|
||||
if ( '' !== $excerpt ) {
|
||||
$markup .= '<span class="' . esc_attr( $class_name . '__excerpt' ) . '">' . esc_html( $excerpt ) . '</span>';
|
||||
}
|
||||
$markup .= '</span></a></article>';
|
||||
return $markup;
|
||||
}
|
||||
|
||||
/** Register all block metadata directories that exist. */
|
||||
function chrnx_register_editorial_blocks() {
|
||||
$blocks = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta' );
|
||||
foreach ( $blocks as $block ) {
|
||||
$name = 'chrnx/' . $block;
|
||||
if ( WP_Block_Type_Registry::get_instance()->is_registered( $name ) ) {
|
||||
continue;
|
||||
}
|
||||
$directory = get_theme_file_path( 'blocks/' . $block );
|
||||
if ( is_readable( $directory . '/block.json' ) ) {
|
||||
register_block_type( $directory );
|
||||
}
|
||||
}
|
||||
}
|
||||
add_action( 'init', 'chrnx_register_editorial_blocks' );
|
||||
+301
@@ -0,0 +1,301 @@
|
||||
<?php
|
||||
/**
|
||||
* Editorial query services.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
/**
|
||||
* Whether a post is eligible for public editorial surfaces.
|
||||
*
|
||||
* @param int|WP_Post $post Post object or ID.
|
||||
* @return bool Whether the post is eligible.
|
||||
*/
|
||||
function chrnx_is_eligible_editorial_post( $post ) {
|
||||
$post = get_post( $post );
|
||||
|
||||
return $post instanceof WP_Post
|
||||
&& 'post' === $post->post_type
|
||||
&& 'publish' === $post->post_status
|
||||
&& '' !== trim( wp_strip_all_tags( $post->post_title ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all eligible post IDs in deterministic publication order.
|
||||
*
|
||||
* @param array<string,mixed> $extra_args Additional WP_Query arguments.
|
||||
* @return int[] Eligible post IDs.
|
||||
*/
|
||||
function chrnx_get_eligible_editorial_post_ids( $extra_args = array() ) {
|
||||
$args = array_merge(
|
||||
array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => -1,
|
||||
'orderby' => array(
|
||||
'date' => 'DESC',
|
||||
'ID' => 'DESC',
|
||||
),
|
||||
'ignore_sticky_posts' => true,
|
||||
'no_found_rows' => true,
|
||||
'fields' => 'ids',
|
||||
),
|
||||
$extra_args
|
||||
);
|
||||
$ids = get_posts( $args );
|
||||
|
||||
return array_values( array_map( 'absint', array_filter( $ids, 'chrnx_is_eligible_editorial_post' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first assigned topic slug for a post.
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @return string Topic slug or empty string.
|
||||
*/
|
||||
function chrnx_get_post_topic_slug( $post_id ) {
|
||||
$terms = wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'slugs' ) );
|
||||
return ! is_wp_error( $terms ) && ! empty( $terms ) ? (string) reset( $terms ) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Select featured story IDs using sticky primary and topic-diverse secondaries.
|
||||
*
|
||||
* @param int $limit Maximum cards, bounded to 12.
|
||||
* @return int[] Selected post IDs.
|
||||
*/
|
||||
function chrnx_get_featured_story_ids( int $limit = 3 ): array {
|
||||
$limit = min( 12, max( 0, $limit ) );
|
||||
if ( 0 === $limit ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$eligible = chrnx_get_eligible_editorial_post_ids();
|
||||
if ( empty( $eligible ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$sticky_ids = array_map( 'absint', (array) get_option( 'sticky_posts', array() ) );
|
||||
$primary = 0;
|
||||
foreach ( $eligible as $post_id ) {
|
||||
if ( in_array( $post_id, $sticky_ids, true ) ) {
|
||||
$primary = $post_id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( 0 === $primary ) {
|
||||
$primary = $eligible[0];
|
||||
}
|
||||
|
||||
$selected = array( $primary );
|
||||
$pool = array_values( array_diff( $eligible, array( $primary ) ) );
|
||||
$used = array();
|
||||
$topic = chrnx_get_post_topic_slug( $primary );
|
||||
if ( '' !== $topic ) {
|
||||
$used[] = $topic;
|
||||
}
|
||||
|
||||
foreach ( $pool as $post_id ) {
|
||||
if ( count( $selected ) >= $limit ) {
|
||||
break;
|
||||
}
|
||||
$topic = chrnx_get_post_topic_slug( $post_id );
|
||||
if ( '' !== $topic && ! in_array( $topic, $used, true ) ) {
|
||||
$selected[] = $post_id;
|
||||
$used[] = $topic;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $pool as $post_id ) {
|
||||
if ( count( $selected ) >= $limit ) {
|
||||
break;
|
||||
}
|
||||
if ( ! in_array( $post_id, $selected, true ) ) {
|
||||
$selected[] = $post_id;
|
||||
}
|
||||
}
|
||||
|
||||
return $selected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a request taxonomy slug against an existing editorial term.
|
||||
*
|
||||
* @param mixed $value Requested value.
|
||||
* @param string $taxonomy Editorial taxonomy.
|
||||
* @return string Valid slug or empty string.
|
||||
*/
|
||||
function chrnx_validate_editorial_term_slug( $value, $taxonomy ) {
|
||||
if ( ! is_string( $value ) || ! taxonomy_exists( $taxonomy ) ) {
|
||||
return '';
|
||||
}
|
||||
$value = trim( wp_unslash( $value ) );
|
||||
if ( '' === $value || sanitize_title( $value ) !== $value ) {
|
||||
return '';
|
||||
}
|
||||
$term = get_term_by( 'slug', $value, $taxonomy );
|
||||
return $term instanceof WP_Term ? $term->slug : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Build safe Journal WP_Query arguments from request-shaped data.
|
||||
*
|
||||
* @param array $request Request values.
|
||||
* @return array<string,mixed> Safe WP_Query arguments.
|
||||
*/
|
||||
function chrnx_get_journal_query_args( array $request ): array {
|
||||
$eligible_ids = chrnx_get_eligible_editorial_post_ids();
|
||||
$args = array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'post__in' => ! empty( $eligible_ids ) ? $eligible_ids : array( 0 ),
|
||||
'posts_per_page' => 10,
|
||||
'paged' => isset( $request['paged'] ) && is_scalar( $request['paged'] ) ? max( 1, intval( $request['paged'] ) ) : 1,
|
||||
'orderby' => array(
|
||||
'date' => 'DESC',
|
||||
'ID' => 'DESC',
|
||||
),
|
||||
'ignore_sticky_posts' => true,
|
||||
);
|
||||
|
||||
$tax_query = array();
|
||||
foreach ( array( 'chrnx_topic', 'chrnx_language' ) as $taxonomy ) {
|
||||
$value = $request[ $taxonomy ] ?? '';
|
||||
$slug = chrnx_validate_editorial_term_slug( $value, $taxonomy );
|
||||
if ( '' !== $slug ) {
|
||||
$tax_query[] = array(
|
||||
'taxonomy' => $taxonomy,
|
||||
'field' => 'slug',
|
||||
'terms' => $slug,
|
||||
);
|
||||
}
|
||||
}
|
||||
if ( ! empty( $tax_query ) ) {
|
||||
$args['tax_query'] = $tax_query;
|
||||
}
|
||||
|
||||
if ( isset( $request['s'] ) && is_scalar( $request['s'] ) ) {
|
||||
$search = sanitize_text_field( wp_unslash( (string) $request['s'] ) );
|
||||
$search = mb_substr( trim( $search ), 0, 100, 'UTF-8' );
|
||||
if ( '' !== $search ) {
|
||||
$args['s'] = $search;
|
||||
}
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the latest eligible podcast assigned to any show.
|
||||
*
|
||||
* @return int Podcast post ID or zero.
|
||||
*/
|
||||
function chrnx_get_latest_podcast_id(): int {
|
||||
$ids = chrnx_get_eligible_editorial_post_ids(
|
||||
array(
|
||||
'posts_per_page' => -1,
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'chrnx_topic',
|
||||
'field' => 'slug',
|
||||
'terms' => 'podcasts',
|
||||
),
|
||||
array(
|
||||
'taxonomy' => 'chrnx_show',
|
||||
'operator' => 'EXISTS',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
return ! empty( $ids ) ? $ids[0] : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build query arguments for one validated show.
|
||||
*
|
||||
* @param string $show_slug Existing show slug.
|
||||
* @param int $page Positive page number.
|
||||
* @return array<string,mixed> Safe WP_Query arguments.
|
||||
*/
|
||||
function chrnx_get_show_query_args( string $show_slug, int $page = 1 ): array {
|
||||
$args = array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => 10,
|
||||
'paged' => max( 1, $page ),
|
||||
'orderby' => array(
|
||||
'date' => 'DESC',
|
||||
'ID' => 'DESC',
|
||||
),
|
||||
'ignore_sticky_posts' => true,
|
||||
);
|
||||
$slug = chrnx_validate_editorial_term_slug( $show_slug, 'chrnx_show' );
|
||||
if ( '' === $slug ) {
|
||||
$args['post__in'] = array( 0 );
|
||||
return $args;
|
||||
}
|
||||
$args['tax_query'] = array(
|
||||
array(
|
||||
'taxonomy' => 'chrnx_show',
|
||||
'field' => 'slug',
|
||||
'terms' => $slug,
|
||||
),
|
||||
);
|
||||
$eligible_ids = chrnx_get_eligible_editorial_post_ids( array( 'tax_query' => $args['tax_query'] ) );
|
||||
$args['post__in'] = ! empty( $eligible_ids ) ? $eligible_ids : array( 0 );
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return related posts tiered by topic, shared tags, then recency.
|
||||
*
|
||||
* @param int $post_id Current post ID.
|
||||
* @param int $limit Maximum related stories, bounded to 12.
|
||||
* @return int[] Related post IDs.
|
||||
*/
|
||||
function chrnx_get_related_post_ids( int $post_id, int $limit = 3 ): array {
|
||||
$limit = min( 12, max( 0, $limit ) );
|
||||
if ( 0 === $limit || ! chrnx_is_eligible_editorial_post( $post_id ) ) {
|
||||
return array();
|
||||
}
|
||||
$candidates = chrnx_get_eligible_editorial_post_ids( array( 'post__not_in' => array( $post_id ) ) );
|
||||
$topic = chrnx_get_post_topic_slug( $post_id );
|
||||
$tag_ids = wp_get_post_tags( $post_id, array( 'fields' => 'ids' ) );
|
||||
$tag_ids = is_wp_error( $tag_ids ) ? array() : array_map( 'absint', $tag_ids );
|
||||
$tiers = array( array(), array(), array() );
|
||||
|
||||
foreach ( $candidates as $candidate_id ) {
|
||||
if ( '' !== $topic && chrnx_get_post_topic_slug( $candidate_id ) === $topic ) {
|
||||
$tiers[0][] = $candidate_id;
|
||||
continue;
|
||||
}
|
||||
$candidate_tags = wp_get_post_tags( $candidate_id, array( 'fields' => 'ids' ) );
|
||||
$candidate_tags = is_wp_error( $candidate_tags ) ? array() : array_map( 'absint', $candidate_tags );
|
||||
if ( ! empty( array_intersect( $tag_ids, $candidate_tags ) ) ) {
|
||||
$tiers[1][] = $candidate_id;
|
||||
} else {
|
||||
$tiers[2][] = $candidate_id;
|
||||
}
|
||||
}
|
||||
|
||||
return array_slice( array_merge( $tiers[0], $tiers[1], $tiers[2] ), 0, $limit );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the fixed theme-relative topic art asset path.
|
||||
*
|
||||
* @param string $topic_slug Supported topic slug.
|
||||
* @return string Theme-relative asset path or empty string.
|
||||
*/
|
||||
function chrnx_get_topic_art_asset( string $topic_slug ): string {
|
||||
$mapping = array(
|
||||
'research' => 'alter-ego-research.png',
|
||||
'code' => 'alter-ego-code.png',
|
||||
'literature' => 'alter-ego-literature.png',
|
||||
'music' => 'the-no-names-musician-cutout.png',
|
||||
'podcasts' => 'alter-ego-podcasts.png',
|
||||
'life' => 'alter-ego-life.png',
|
||||
'gaming' => 'alter-ego-gaming.png',
|
||||
);
|
||||
return isset( $mapping[ $topic_slug ] ) ? 'assets/images/' . $mapping[ $topic_slug ] : '';
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* Theme settings on Settings > Reading.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sanitize the configured subscribe Page ID.
|
||||
*
|
||||
* @param mixed $value Submitted option value.
|
||||
* @return int Editable Page ID or zero.
|
||||
*/
|
||||
function chrnx_sanitize_subscribe_page_id( $value ) {
|
||||
$page_id = absint( $value );
|
||||
$page = get_post( $page_id );
|
||||
if ( ! $page instanceof WP_Post || 'page' !== $page->post_type || ! current_user_can( 'edit_post', $page_id ) ) {
|
||||
return 0;
|
||||
}
|
||||
return $page_id;
|
||||
}
|
||||
|
||||
/** Render the subscribe Page dropdown. */
|
||||
function chrnx_render_subscribe_page_field() {
|
||||
wp_dropdown_pages(
|
||||
array(
|
||||
'name' => 'chrnx_subscribe_page_id',
|
||||
'selected' => absint( get_option( 'chrnx_subscribe_page_id', 0 ) ),
|
||||
'show_option_none' => esc_html__( '— Select a page —', 'chrnx' ),
|
||||
'option_none_value' => '0',
|
||||
)
|
||||
);
|
||||
echo '<p class="description">' . esc_html__( 'Choose the published page used by subscription calls to action.', 'chrnx' ) . '</p>';
|
||||
}
|
||||
|
||||
/** Register the subscribe Page setting and Reading field. */
|
||||
function chrnx_register_subscribe_setting() {
|
||||
register_setting(
|
||||
'reading',
|
||||
'chrnx_subscribe_page_id',
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'default' => 0,
|
||||
'sanitize_callback' => 'chrnx_sanitize_subscribe_page_id',
|
||||
)
|
||||
);
|
||||
add_settings_field(
|
||||
'chrnx_subscribe_page_id',
|
||||
esc_html__( 'Subscribe page', 'chrnx' ),
|
||||
'chrnx_render_subscribe_page_field',
|
||||
'reading'
|
||||
);
|
||||
}
|
||||
add_action( 'admin_init', 'chrnx_register_subscribe_setting' );
|
||||
|
||||
/**
|
||||
* Return the configured public subscription URL.
|
||||
*
|
||||
* @return string Published Page permalink or empty string.
|
||||
*/
|
||||
function chrnx_get_subscribe_url(): string {
|
||||
$page_id = absint( get_option( 'chrnx_subscribe_page_id', 0 ) );
|
||||
$page = get_post( $page_id );
|
||||
if ( ! $page instanceof WP_Post || 'page' !== $page->post_type || 'publish' !== $page->post_status ) {
|
||||
return '';
|
||||
}
|
||||
$url = get_permalink( $page );
|
||||
return is_string( $url ) ? $url : '';
|
||||
}
|
||||
Reference in new issue
Block a user