fix: bound CHRNX editorial queries

This commit is contained in:
chrnx committed 2026-08-11 15:49:58 -05:00
1 parent 52f08a1efd
commit a181f405bf
5 files changed
+429 -87

No files matched your search

+229 -80
View File
@@ -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;
}
/**