fix: scope CHRNX eligible queries

This commit is contained in:
chrnx committed 2026-08-11 15:59:29 -05:00
1 parent a181f405bf
commit dab82cfa06
3 files changed
+144 -10

No files matched your search

+39 -4
View File
@@ -31,6 +31,19 @@ add_action( 'update_option_sticky_posts', 'chrnx_clear_editorial_query_cache', 1
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 );
/**
* Clear request-local results after direct relationship deletion.
*
* @param int $object_id Object whose relationships were deleted.
* @param int[] $term_taxonomy_ids Deleted term-taxonomy IDs.
* @param string $taxonomy Taxonomy name.
*/
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- The hook contract requires all three arguments.
function chrnx_clear_editorial_query_cache_after_relationship_delete( $object_id, $term_taxonomy_ids, $taxonomy ) {
chrnx_clear_editorial_query_cache();
}
add_action( 'deleted_term_relationships', 'chrnx_clear_editorial_query_cache_after_relationship_delete', 10, 3 );
/**
* Recursively normalize query arguments for a stable request-local cache key.
*
@@ -63,9 +76,30 @@ function chrnx_filter_nonempty_editorial_titles( $where, $query ) {
}
global $wpdb;
return $where . $wpdb->prepare( ' AND ' . $wpdb->posts . '.post_title REGEXP %s', '[^[:space:]]' );
$visible_text_pattern = '(^|>)[[:space:]]*[^<[:space:]][^<]*(<|$)';
return $where . $wpdb->prepare( ' AND ' . $wpdb->posts . '.post_title REGEXP %s', $visible_text_pattern );
}
/**
* Run one eligible editorial query with a tightly scoped title predicate.
*
* @param array<string,mixed> $args WP_Query arguments carrying the internal eligibility flag.
* @return WP_Query Completed query.
*/
function chrnx_run_eligible_query( array $args ) {
$already_registered = false !== has_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles' );
if ( ! $already_registered ) {
add_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles', 10, 2 );
}
try {
return new WP_Query( $args );
} finally {
if ( ! $already_registered ) {
remove_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles', 10 );
}
}
}
add_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles', 10, 2 );
/**
* Return a bounded eligible candidate set in deterministic publication order.
@@ -88,6 +122,7 @@ function chrnx_get_eligible_editorial_post_ids( $extra_args = array() ) {
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'fields' => 'ids',
'cache_results' => false,
'suppress_filters' => false,
'chrnx_require_nonempty_title' => true,
),
@@ -102,8 +137,8 @@ function chrnx_get_eligible_editorial_post_ids( $extra_args = array() ) {
return $cache[ $key ];
}
$query = new WP_Query( $args );
$ids = array_values( array_map( 'absint', $query->posts ) );
$query = chrnx_run_eligible_query( $args );
$ids = array_values( array_map( 'absint', array_filter( $query->posts, 'chrnx_is_eligible_editorial_post' ) ) );
if ( ! empty( $ids ) ) {
_prime_post_caches( $ids, true, true );
}