fix: bound CHRNX editorial queries
This commit is contained in:
5 files changed
+429
-87
No files matched your search
@@ -14,10 +14,11 @@ $chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 )
|
||||
$chrnx_topic = chrnx_validate_editorial_term_slug( $chrnx_request['chrnx_topic'] ?? '', 'chrnx_topic' );
|
||||
$chrnx_language = chrnx_validate_editorial_term_slug( $chrnx_request['chrnx_language'] ?? '', 'chrnx_language' );
|
||||
$chrnx_search = $chrnx_args['s'] ?? '';
|
||||
$chrnx_form_url = strtok( get_pagenum_link( 1, false ), '?' );
|
||||
$chrnx_form_url = chrnx_get_journal_base_url();
|
||||
$chrnx_title_id = wp_unique_id( 'chrnx-journal-title-' );
|
||||
?>
|
||||
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-journal-stream' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="chrnx-journal-title">
|
||||
<h<?php echo esc_attr( $chrnx_heading ); ?> id="chrnx-journal-title"><?php echo esc_html__( 'Journal', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
|
||||
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-journal-stream' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="<?php echo esc_attr( $chrnx_title_id ); ?>">
|
||||
<h<?php echo esc_attr( $chrnx_heading ); ?> id="<?php echo esc_attr( $chrnx_title_id ); ?>"><?php echo esc_html__( 'Journal', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
|
||||
<form class="chrnx-journal-stream__filters" method="get" action="<?php echo esc_url( $chrnx_form_url ); ?>">
|
||||
<label>
|
||||
<span><?php echo esc_html__( 'Topic', 'chrnx' ); ?></span>
|
||||
@@ -103,4 +104,3 @@ $chrnx_form_url = strtok( get_pagenum_link( 1, false ), '?' );
|
||||
<p class="chrnx-journal-stream__empty"><?php echo esc_html__( 'No journal entries match these filters yet.', 'chrnx' ); ?></p>
|
||||
<?php endif; ?>
|
||||
</section>
|
||||
<?php wp_reset_postdata(); ?>
|
||||
@@ -11,10 +11,11 @@ $chrnx_ids = chrnx_get_related_post_ids( $chrnx_post_id, $chrnx_limit );
|
||||
if ( empty( $chrnx_ids ) ) {
|
||||
return;
|
||||
}
|
||||
$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 );
|
||||
$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 );
|
||||
$chrnx_title_id = wp_unique_id( 'chrnx-related-posts-title-' );
|
||||
?>
|
||||
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-related-posts' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="chrnx-related-posts-title-<?php echo esc_attr( $chrnx_post_id ); ?>">
|
||||
<h<?php echo esc_attr( $chrnx_heading ); ?> id="chrnx-related-posts-title-<?php echo esc_attr( $chrnx_post_id ); ?>"><?php echo esc_html__( 'Related stories', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
|
||||
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-related-posts' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="<?php echo esc_attr( $chrnx_title_id ); ?>">
|
||||
<h<?php echo esc_attr( $chrnx_heading ); ?> id="<?php echo esc_attr( $chrnx_title_id ); ?>"><?php echo esc_html__( 'Related stories', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
|
||||
<div class="chrnx-related-posts__grid">
|
||||
<?php
|
||||
foreach ( $chrnx_ids as $chrnx_related_id ) {
|
||||
|
||||
+229
-80
@@ -20,31 +20,96 @@ function chrnx_is_eligible_editorial_post( $post ) {
|
||||
&& '' !== trim( wp_strip_all_tags( $post->post_title ) );
|
||||
}
|
||||
|
||||
/** Clear request-local editorial query results after relevant mutations. */
|
||||
function chrnx_clear_editorial_query_cache() {
|
||||
$GLOBALS['chrnx_editorial_query_cache'] = array();
|
||||
}
|
||||
add_action( 'save_post_post', 'chrnx_clear_editorial_query_cache', 10, 0 );
|
||||
add_action( 'deleted_post', 'chrnx_clear_editorial_query_cache', 10, 0 );
|
||||
add_action( 'set_object_terms', 'chrnx_clear_editorial_query_cache', 10, 0 );
|
||||
add_action( 'update_option_sticky_posts', 'chrnx_clear_editorial_query_cache', 10, 0 );
|
||||
add_action( 'add_option_sticky_posts', 'chrnx_clear_editorial_query_cache', 10, 0 );
|
||||
add_action( 'delete_option_sticky_posts', 'chrnx_clear_editorial_query_cache', 10, 0 );
|
||||
|
||||
/**
|
||||
* Return all eligible post IDs in deterministic publication order.
|
||||
* Recursively normalize query arguments for a stable request-local cache key.
|
||||
*
|
||||
* @param mixed $value Query argument value.
|
||||
* @return mixed Normalized value.
|
||||
*/
|
||||
function chrnx_normalize_editorial_query_cache_value( $value ) {
|
||||
if ( ! is_array( $value ) ) {
|
||||
return $value;
|
||||
}
|
||||
if ( ! array_is_list( $value ) ) {
|
||||
ksort( $value );
|
||||
}
|
||||
foreach ( $value as $key => $item ) {
|
||||
$value[ $key ] = chrnx_normalize_editorial_query_cache_value( $item );
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restrict explicitly marked editorial queries to nonblank post titles.
|
||||
*
|
||||
* @param string $where Existing WHERE clause.
|
||||
* @param WP_Query $query Query instance.
|
||||
* @return string Filtered WHERE clause.
|
||||
*/
|
||||
function chrnx_filter_nonempty_editorial_titles( $where, $query ) {
|
||||
if ( ! $query->get( 'chrnx_require_nonempty_title' ) ) {
|
||||
return $where;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
return $where . $wpdb->prepare( ' AND ' . $wpdb->posts . '.post_title REGEXP %s', '[^[:space:]]' );
|
||||
}
|
||||
add_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles', 10, 2 );
|
||||
|
||||
/**
|
||||
* Return a bounded eligible candidate set 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(
|
||||
$requested_limit = isset( $extra_args['posts_per_page'] ) ? (int) $extra_args['posts_per_page'] : 12;
|
||||
$limit = min( 12, max( 1, $requested_limit ) );
|
||||
$args = array_merge(
|
||||
array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => -1,
|
||||
'orderby' => array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $limit,
|
||||
'orderby' => array(
|
||||
'date' => 'DESC',
|
||||
'ID' => 'DESC',
|
||||
),
|
||||
'ignore_sticky_posts' => true,
|
||||
'no_found_rows' => true,
|
||||
'fields' => 'ids',
|
||||
'ignore_sticky_posts' => true,
|
||||
'no_found_rows' => true,
|
||||
'fields' => 'ids',
|
||||
'suppress_filters' => false,
|
||||
'chrnx_require_nonempty_title' => true,
|
||||
),
|
||||
$extra_args
|
||||
$extra_args,
|
||||
array( 'posts_per_page' => $limit )
|
||||
);
|
||||
$ids = get_posts( $args );
|
||||
$key = md5( (string) wp_json_encode( chrnx_normalize_editorial_query_cache_value( $args ) ) );
|
||||
$cache = isset( $GLOBALS['chrnx_editorial_query_cache'] ) && is_array( $GLOBALS['chrnx_editorial_query_cache'] )
|
||||
? $GLOBALS['chrnx_editorial_query_cache']
|
||||
: array();
|
||||
if ( isset( $cache[ $key ] ) ) {
|
||||
return $cache[ $key ];
|
||||
}
|
||||
|
||||
return array_values( array_map( 'absint', array_filter( $ids, 'chrnx_is_eligible_editorial_post' ) ) );
|
||||
$query = new WP_Query( $args );
|
||||
$ids = array_values( array_map( 'absint', $query->posts ) );
|
||||
if ( ! empty( $ids ) ) {
|
||||
_prime_post_caches( $ids, true, true );
|
||||
}
|
||||
$cache[ $key ] = $ids;
|
||||
$GLOBALS['chrnx_editorial_query_cache'] = $cache;
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,49 +135,83 @@ function chrnx_get_featured_story_ids( int $limit = 3 ): array {
|
||||
return array();
|
||||
}
|
||||
|
||||
$eligible = chrnx_get_eligible_editorial_post_ids();
|
||||
if ( empty( $eligible ) ) {
|
||||
$sticky_ids = array_map( 'absint', (array) get_option( 'sticky_posts', array() ) );
|
||||
$primary_ids = ! empty( $sticky_ids )
|
||||
? chrnx_get_eligible_editorial_post_ids(
|
||||
array(
|
||||
'post__in' => $sticky_ids,
|
||||
'posts_per_page' => 1,
|
||||
)
|
||||
)
|
||||
: array();
|
||||
if ( empty( $primary_ids ) ) {
|
||||
$primary_ids = chrnx_get_eligible_editorial_post_ids( array( 'posts_per_page' => 1 ) );
|
||||
}
|
||||
if ( empty( $primary_ids ) ) {
|
||||
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];
|
||||
}
|
||||
|
||||
$primary = $primary_ids[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;
|
||||
}
|
||||
if ( count( $selected ) >= $limit ) {
|
||||
return $selected;
|
||||
}
|
||||
|
||||
foreach ( $pool as $post_id ) {
|
||||
$definitions = chrnx_get_editorial_taxonomies();
|
||||
$candidates = array();
|
||||
foreach ( array_keys( $definitions['chrnx_topic']['terms'] ) as $topic_slug ) {
|
||||
if ( in_array( $topic_slug, $used, true ) ) {
|
||||
continue;
|
||||
}
|
||||
$topic_ids = chrnx_get_eligible_editorial_post_ids(
|
||||
array(
|
||||
'posts_per_page' => 1,
|
||||
'post__not_in' => array( $primary ),
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'chrnx_topic',
|
||||
'field' => 'slug',
|
||||
'terms' => $topic_slug,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
if ( ! empty( $topic_ids ) ) {
|
||||
$candidates[] = $topic_ids[0];
|
||||
}
|
||||
}
|
||||
usort(
|
||||
$candidates,
|
||||
static function ( $first_id, $second_id ) {
|
||||
$first = get_post( $first_id );
|
||||
$second = get_post( $second_id );
|
||||
$date_compare = strcmp( $second->post_date, $first->post_date );
|
||||
return 0 !== $date_compare ? $date_compare : $second_id <=> $first_id;
|
||||
}
|
||||
);
|
||||
foreach ( $candidates as $candidate_id ) {
|
||||
if ( count( $selected ) >= $limit ) {
|
||||
break;
|
||||
}
|
||||
if ( ! in_array( $post_id, $selected, true ) ) {
|
||||
$selected[] = $post_id;
|
||||
}
|
||||
$selected[] = $candidate_id;
|
||||
}
|
||||
|
||||
$remaining = $limit - count( $selected );
|
||||
if ( $remaining > 0 ) {
|
||||
$selected = array_merge(
|
||||
$selected,
|
||||
chrnx_get_eligible_editorial_post_ids(
|
||||
array(
|
||||
'posts_per_page' => $remaining,
|
||||
'post__not_in' => $selected,
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $selected;
|
||||
@@ -169,6 +268,32 @@ function chrnx_sanitize_journal_search( $value ) {
|
||||
return mb_substr( trim( sanitize_text_field( wp_unslash( (string) $value ) ) ), 0, 100, 'UTF-8' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current Journal base URL while retaining required WP routing.
|
||||
*
|
||||
* @return string Safe current Page or archive URL.
|
||||
*/
|
||||
function chrnx_get_journal_base_url() {
|
||||
$queried = get_queried_object();
|
||||
if ( $queried instanceof WP_Post && 'page' === $queried->post_type ) {
|
||||
$url = get_permalink( $queried );
|
||||
return is_string( $url ) ? $url : '';
|
||||
}
|
||||
|
||||
$url = get_pagenum_link( 1, false );
|
||||
$base_url = strtok( $url, '?' );
|
||||
$query_string = wp_parse_url( $url, PHP_URL_QUERY );
|
||||
$routing_values = array();
|
||||
if ( is_string( $query_string ) ) {
|
||||
parse_str( html_entity_decode( $query_string, ENT_QUOTES | ENT_HTML5, 'UTF-8' ), $routing_values );
|
||||
}
|
||||
$routing_values = array_intersect_key(
|
||||
$routing_values,
|
||||
array_flip( array( 'page_id', 'p', 'pagename', 'name', 'post_type', 'taxonomy', 'term', 'cat', 'category_name', 'tag' ) )
|
||||
);
|
||||
return add_query_arg( $routing_values, $base_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a Journal page URL from only validated public filters.
|
||||
*
|
||||
@@ -177,7 +302,7 @@ function chrnx_sanitize_journal_search( $value ) {
|
||||
* @return string Safe Journal URL.
|
||||
*/
|
||||
function chrnx_get_journal_page_url( int $page, array $filters ) {
|
||||
$base_url = strtok( get_pagenum_link( 1, false ), '?' );
|
||||
$base_url = chrnx_get_journal_base_url();
|
||||
$args = array();
|
||||
foreach ( array( 'chrnx_topic', 'chrnx_language' ) as $taxonomy ) {
|
||||
$slug = chrnx_validate_editorial_term_slug( $filters[ $taxonomy ] ?? '', $taxonomy );
|
||||
@@ -204,18 +329,18 @@ function chrnx_get_journal_page_url( int $page, array $filters ) {
|
||||
* @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' => chrnx_parse_journal_page( $request['paged'] ?? null ),
|
||||
'orderby' => array(
|
||||
$args = array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => 10,
|
||||
'paged' => chrnx_parse_journal_page( $request['paged'] ?? null ),
|
||||
'orderby' => array(
|
||||
'date' => 'DESC',
|
||||
'ID' => 'DESC',
|
||||
),
|
||||
'ignore_sticky_posts' => true,
|
||||
'ignore_sticky_posts' => true,
|
||||
'suppress_filters' => false,
|
||||
'chrnx_require_nonempty_title' => true,
|
||||
);
|
||||
|
||||
$tax_query = array();
|
||||
@@ -250,7 +375,7 @@ function chrnx_get_journal_query_args( array $request ): array {
|
||||
function chrnx_get_latest_podcast_id(): int {
|
||||
$ids = chrnx_get_eligible_editorial_post_ids(
|
||||
array(
|
||||
'posts_per_page' => -1,
|
||||
'posts_per_page' => 1,
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'chrnx_topic',
|
||||
@@ -271,20 +396,22 @@ function chrnx_get_latest_podcast_id(): int {
|
||||
* Build query arguments for one validated show.
|
||||
*
|
||||
* @param string $show_slug Existing show slug.
|
||||
* @param int $page Positive page number.
|
||||
* @param mixed $page Request-shaped page number.
|
||||
* @return array<string,mixed> Safe WP_Query arguments.
|
||||
*/
|
||||
function chrnx_get_show_query_args( string $show_slug, int $page = 1 ): array {
|
||||
function chrnx_get_show_query_args( string $show_slug, $page = 1 ): array {
|
||||
$args = array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => 10,
|
||||
'paged' => max( 1, $page ),
|
||||
'orderby' => array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => 10,
|
||||
'paged' => chrnx_parse_journal_page( $page ),
|
||||
'orderby' => array(
|
||||
'date' => 'DESC',
|
||||
'ID' => 'DESC',
|
||||
),
|
||||
'ignore_sticky_posts' => true,
|
||||
'ignore_sticky_posts' => true,
|
||||
'suppress_filters' => false,
|
||||
'chrnx_require_nonempty_title' => true,
|
||||
);
|
||||
$slug = chrnx_validate_editorial_term_slug( $show_slug, 'chrnx_show' );
|
||||
if ( '' === $slug ) {
|
||||
@@ -298,8 +425,6 @@ function chrnx_get_show_query_args( string $show_slug, int $page = 1 ): array {
|
||||
'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;
|
||||
}
|
||||
|
||||
@@ -315,27 +440,51 @@ function chrnx_get_related_post_ids( int $post_id, int $limit = 3 ): array {
|
||||
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() );
|
||||
$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 );
|
||||
$selected = 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;
|
||||
}
|
||||
if ( '' !== $topic ) {
|
||||
$selected = chrnx_get_eligible_editorial_post_ids(
|
||||
array(
|
||||
'posts_per_page' => $limit,
|
||||
'post__not_in' => array( $post_id ),
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'chrnx_topic',
|
||||
'field' => 'slug',
|
||||
'terms' => $topic,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return array_slice( array_merge( $tiers[0], $tiers[1], $tiers[2] ), 0, $limit );
|
||||
$remaining = $limit - count( $selected );
|
||||
if ( $remaining > 0 && ! empty( $tag_ids ) ) {
|
||||
$tag_matches = chrnx_get_eligible_editorial_post_ids(
|
||||
array(
|
||||
'posts_per_page' => $remaining,
|
||||
'post__not_in' => array_merge( array( $post_id ), $selected ),
|
||||
'tag__in' => $tag_ids,
|
||||
)
|
||||
);
|
||||
$selected = array_merge( $selected, $tag_matches );
|
||||
}
|
||||
|
||||
$remaining = $limit - count( $selected );
|
||||
if ( $remaining > 0 ) {
|
||||
$recent = chrnx_get_eligible_editorial_post_ids(
|
||||
array(
|
||||
'posts_per_page' => $remaining,
|
||||
'post__not_in' => array_merge( array( $post_id ), $selected ),
|
||||
)
|
||||
);
|
||||
$selected = array_merge( $selected, $recent );
|
||||
}
|
||||
|
||||
return $selected;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -275,6 +275,136 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/** Plain-permalink Journal links preserve Page routing and only validated public filters. */
|
||||
public function test_journal_pagination_preserves_plain_page_routing() {
|
||||
global $wp_rewrite;
|
||||
$previous_structure = get_option( 'permalink_structure' );
|
||||
$page_id = self::factory()->post->create(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => 'Journal Page',
|
||||
)
|
||||
);
|
||||
for ( $index = 1; $index <= 12; ++$index ) {
|
||||
$post_id = self::factory()->post->create( array( 'post_title' => 'Plain result ' . $index ) );
|
||||
wp_set_object_terms( $post_id, 'code', 'chrnx_topic' );
|
||||
}
|
||||
|
||||
$wp_rewrite->set_permalink_structure( '' );
|
||||
$this->go_to( get_permalink( $page_id ) );
|
||||
$previous_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
|
||||
$_GET = array( // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
|
||||
'chrnx_topic' => 'code',
|
||||
'evil' => 'drop-me',
|
||||
);
|
||||
|
||||
try {
|
||||
$output = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/journal-stream',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
} finally {
|
||||
$_GET = $previous_get;
|
||||
$wp_rewrite->set_permalink_structure( $previous_structure );
|
||||
}
|
||||
|
||||
preg_match( '/<nav class="chrnx-journal-stream__pagination".*?href="([^"]+)".*?<\/nav>/s', $output, $match );
|
||||
$this->assertNotEmpty( $match[1] ?? '' );
|
||||
$href = html_entity_decode( $match[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' );
|
||||
parse_str( (string) wp_parse_url( $href, PHP_URL_QUERY ), $query );
|
||||
$this->assertSame( (string) $page_id, $query['page_id'] ?? '' );
|
||||
$this->assertSame( '2', (string) ( $query['paged'] ?? '' ) );
|
||||
$this->assertSame( 'code', $query['chrnx_topic'] ?? '' );
|
||||
$this->assertArrayNotHasKey( 'evil', $query );
|
||||
}
|
||||
|
||||
/** Multiple block instances produce unique matching aria-labelledby targets. */
|
||||
public function test_journal_and_related_instances_have_unique_heading_targets() {
|
||||
$current = self::factory()->post->create( array( 'post_title' => 'Unique current' ) );
|
||||
$related = self::factory()->post->create( array( 'post_title' => 'Unique related' ) );
|
||||
wp_set_object_terms( $current, 'life', 'chrnx_topic' );
|
||||
wp_set_object_terms( $related, 'life', 'chrnx_topic' );
|
||||
|
||||
$journal_one = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/journal-stream',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
$journal_two = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/journal-stream',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
$related_one = $this->render_for_post( 'chrnx/related-posts', $current );
|
||||
$related_two = $this->render_for_post( 'chrnx/related-posts', $current );
|
||||
|
||||
$this->assert_unique_label_targets( $journal_one, $journal_two );
|
||||
$this->assert_unique_label_targets( $related_one, $related_two );
|
||||
}
|
||||
|
||||
/** Journal rendering leaves a nested outer global post context untouched. */
|
||||
public function test_journal_render_preserves_outer_global_post_context() {
|
||||
global $post;
|
||||
$main_page = self::factory()->post->create(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'publish',
|
||||
)
|
||||
);
|
||||
$outer_id = self::factory()->post->create( array( 'post_title' => 'Outer context' ) );
|
||||
$this->go_to( get_permalink( $main_page ) );
|
||||
$post = get_post( $outer_id );
|
||||
setup_postdata( $post );
|
||||
$context_resets = 0;
|
||||
$count_reset = static function () use ( &$context_resets ) {
|
||||
++$context_resets;
|
||||
};
|
||||
add_action( 'the_post', $count_reset );
|
||||
|
||||
try {
|
||||
render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/journal-stream',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
$this->assertSame( $outer_id, $post->ID );
|
||||
$this->assertSame( $outer_id, get_the_ID() );
|
||||
$this->assertSame( 0, $context_resets, 'Journal must not reset or set up global post data.' );
|
||||
} finally {
|
||||
remove_action( 'the_post', $count_reset );
|
||||
wp_reset_postdata();
|
||||
}
|
||||
}
|
||||
|
||||
/** Assert two rendered sections have matching but distinct label targets. */
|
||||
private function assert_unique_label_targets( $first, $second ) {
|
||||
preg_match( '/aria-labelledby="([^"]+)"/', $first, $first_label );
|
||||
preg_match( '/id="([^"]+)"/', $first, $first_id );
|
||||
preg_match( '/aria-labelledby="([^"]+)"/', $second, $second_label );
|
||||
preg_match( '/id="([^"]+)"/', $second, $second_id );
|
||||
$this->assertSame( $first_label[1] ?? '', $first_id[1] ?? '' );
|
||||
$this->assertSame( $second_label[1] ?? '', $second_id[1] ?? '' );
|
||||
$this->assertNotSame( $first_label[1] ?? '', $second_label[1] ?? '' );
|
||||
}
|
||||
|
||||
/** Render one dynamic post-context block for a specific post. */
|
||||
private function render_for_post( $block_name, $post_id ) {
|
||||
global $post;
|
||||
|
||||
@@ -131,6 +131,21 @@ class EditorialQueriesTest extends WP_UnitTestCase {
|
||||
$this->assertNotContains( $outsider, get_posts( chrnx_get_show_query_args( 'silicon-ranch' ) + array( 'fields' => 'ids' ) ) );
|
||||
}
|
||||
|
||||
/** Show pagination reuses canonical bounded public page parsing. */
|
||||
public function test_show_query_normalizes_invalid_and_extreme_pages() {
|
||||
foreach ( array( '3junk', '0', -2, '3.0', 3.5, array( '3' ), 10001 ) as $invalid ) {
|
||||
try {
|
||||
$args = chrnx_get_show_query_args( 'silicon-ranch', $invalid );
|
||||
} catch ( TypeError $error ) {
|
||||
$this->fail( 'Malformed show pages must normalize to page 1 instead of throwing.' );
|
||||
}
|
||||
$this->assertSame( 1, $args['paged'] );
|
||||
}
|
||||
|
||||
$this->assertSame( 4, chrnx_get_show_query_args( 'silicon-ranch', '4' )['paged'] );
|
||||
$this->assertSame( 10000, chrnx_get_show_query_args( 'silicon-ranch', 10000 )['paged'] );
|
||||
}
|
||||
|
||||
/** Journal filters are term-whitelisted, normalized, and safe WP_Query arguments. */
|
||||
public function test_journal_query_sanitizes_and_preserves_valid_filters() {
|
||||
$args = chrnx_get_journal_query_args(
|
||||
@@ -204,4 +219,51 @@ class EditorialQueriesTest extends WP_UnitTestCase {
|
||||
$this->assertSame( array(), chrnx_get_related_post_ids( $current, 0 ) );
|
||||
$this->assertSame( array(), chrnx_get_related_post_ids( 999999, 3 ) );
|
||||
}
|
||||
|
||||
/** Editorial selectors issue only bounded candidate queries and Journal avoids archive-sized post__in. */
|
||||
public function test_editorial_services_never_request_unbounded_candidate_sets() {
|
||||
$current = $this->create_post( 'Current bounded', '2026-04-30 10:00:00', 'research' );
|
||||
for ( $index = 1; $index <= 30; ++$index ) {
|
||||
$this->create_post( 'Candidate ' . $index, sprintf( '2026-04-%02d 10:00:00', $index ), 0 === $index % 2 ? 'research' : 'code' );
|
||||
}
|
||||
|
||||
$observed = array();
|
||||
$capture = static function ( $query ) use ( &$observed ) {
|
||||
if ( 'post' === $query->get( 'post_type' ) ) {
|
||||
$observed[] = (int) $query->get( 'posts_per_page' );
|
||||
}
|
||||
};
|
||||
add_action( 'pre_get_posts', $capture );
|
||||
try {
|
||||
chrnx_get_featured_story_ids( 4 );
|
||||
chrnx_get_related_post_ids( $current, 4 );
|
||||
} finally {
|
||||
remove_action( 'pre_get_posts', $capture );
|
||||
}
|
||||
|
||||
$this->assertNotEmpty( $observed );
|
||||
$this->assertNotContains( -1, $observed );
|
||||
foreach ( $observed as $candidate_limit ) {
|
||||
$this->assertGreaterThanOrEqual( 1, $candidate_limit );
|
||||
$this->assertLessThanOrEqual( 12, $candidate_limit );
|
||||
}
|
||||
|
||||
$journal_args = chrnx_get_journal_query_args( array() );
|
||||
$this->assertArrayNotHasKey( 'post__in', $journal_args );
|
||||
$this->assertTrue( $journal_args['chrnx_require_nonempty_title'] );
|
||||
}
|
||||
|
||||
/** Identical request-local selections are cached without becoming stale after a post mutation. */
|
||||
public function test_featured_request_cache_reuses_results_and_invalidates_after_post_creation() {
|
||||
$first = $this->create_post( 'Cached first', '2026-05-01 10:00:00', 'research' );
|
||||
$this->assertSame( array( $first ), chrnx_get_featured_story_ids( 1 ) );
|
||||
|
||||
global $wpdb;
|
||||
$before = $wpdb->num_queries;
|
||||
$this->assertSame( array( $first ), chrnx_get_featured_story_ids( 1 ) );
|
||||
$this->assertSame( 0, $wpdb->num_queries - $before, 'A normalized identical request should reuse its request-local selection.' );
|
||||
|
||||
$newer = $this->create_post( 'Cached newer', '2026-05-02 10:00:00', 'code' );
|
||||
$this->assertSame( array( $newer ), chrnx_get_featured_story_ids( 1 ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user