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

+1 -1
View File
@@ -9,7 +9,7 @@
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$chrnx_request = is_array( $_GET ) ? $_GET : array();
$chrnx_args = chrnx_get_journal_query_args( $chrnx_request );
$chrnx_query = new WP_Query( $chrnx_args );
$chrnx_query = chrnx_run_eligible_query( $chrnx_args );
$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' );
+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 );
}
+104 -5
View File
@@ -46,6 +46,15 @@ class EditorialQueriesTest extends WP_UnitTestCase {
return $post_id;
}
/** Execute an eligible query and return IDs, using the scoped wrapper when available. */
private function get_eligible_query_ids( $args ) {
$args['fields'] = 'ids';
if ( function_exists( 'chrnx_run_eligible_query' ) ) {
return array_map( 'absint', chrnx_run_eligible_query( $args )->posts );
}
return array_map( 'absint', get_posts( $args ) );
}
/** Newest eligible sticky is primary and older stickies rejoin chronology. */
public function test_featured_primary_is_newest_eligible_sticky_and_other_stickies_rejoin_chronology() {
$old_sticky = $this->create_post( 'Old sticky', '2026-01-01 10:00:00', 'research' );
@@ -104,9 +113,8 @@ class EditorialQueriesTest extends WP_UnitTestCase {
wp_set_object_terms( $empty_title, 'silicon-ranch', 'chrnx_show' );
$this->assertSame( $with_show, chrnx_get_latest_podcast_id() );
$journal_ids = get_posts(
$journal_ids = $this->get_eligible_query_ids(
chrnx_get_journal_query_args( array() ) + array(
'fields' => 'ids',
'posts_per_page' => 20,
)
);
@@ -122,13 +130,13 @@ class EditorialQueriesTest extends WP_UnitTestCase {
wp_set_object_terms( $member, 'silicon-ranch', 'chrnx_show' );
wp_set_object_terms( $untitled, 'silicon-ranch', 'chrnx_show' );
$this->assertSame( array( $member ), get_posts( chrnx_get_show_query_args( 'silicon-ranch' ) + array( 'fields' => 'ids' ) ) );
$this->assertSame( array( $member ), $this->get_eligible_query_ids( chrnx_get_show_query_args( 'silicon-ranch' ) ) );
foreach ( array( 'unknown', '../silicon-ranch', '', "silicon-ranch' OR 1=1" ) as $slug ) {
$args = chrnx_get_show_query_args( $slug );
$this->assertSame( array( 0 ), $args['post__in'] );
$this->assertSame( array(), get_posts( $args + array( 'fields' => 'ids' ) ) );
$this->assertSame( array(), $this->get_eligible_query_ids( $args ) );
}
$this->assertNotContains( $outsider, get_posts( chrnx_get_show_query_args( 'silicon-ranch' ) + array( 'fields' => 'ids' ) ) );
$this->assertNotContains( $outsider, $this->get_eligible_query_ids( chrnx_get_show_query_args( 'silicon-ranch' ) ) );
}
/** Show pagination reuses canonical bounded public page parsing. */
@@ -266,4 +274,95 @@ class EditorialQueriesTest extends WP_UnitTestCase {
$newer = $this->create_post( 'Cached newer', '2026-05-02 10:00:00', 'code' );
$this->assertSame( array( $newer ), chrnx_get_featured_story_ids( 1 ) );
}
/** Tag-only titles are ineligible while marked-up titles with visible text remain eligible. */
public function test_tag_only_titles_are_excluded_from_featured_and_related_surfaces() {
$tag_only = $this->create_post( '<b></b>', '2026-06-05 10:00:00', 'research' );
$nested = $this->create_post( '<span><b> </b></span>', '2026-06-04 10:00:00', 'code' );
$visible = $this->create_post( '<b>Visible story</b>', '2026-06-03 10:00:00', 'life' );
update_option( 'sticky_posts', array( $tag_only, $nested ) );
$this->assertSame( array( $visible ), chrnx_get_featured_story_ids( 1 ) );
$current = $this->create_post( 'Current tags', '2026-06-06 10:00:00', 'research' );
$this->assertSame( array( $visible ), chrnx_get_related_post_ids( $current, 1 ) );
}
/** Journal and Show counts exclude tag-only titles without hiding visible marked-up titles. */
public function test_tag_only_titles_are_excluded_from_journal_and_show_queries() {
$tag_only = $this->create_post( '<b></b>', '2026-07-03 10:00:00', 'podcasts' );
$visible = $this->create_post( '<b>Visible episode</b>', '2026-07-02 10:00:00', 'podcasts' );
wp_set_object_terms( $tag_only, 'silicon-ranch', 'chrnx_show' );
wp_set_object_terms( $visible, 'silicon-ranch', 'chrnx_show' );
$journal = function_exists( 'chrnx_run_eligible_query' )
? chrnx_run_eligible_query( chrnx_get_journal_query_args( array() ) )
: new WP_Query( chrnx_get_journal_query_args( array() ) );
$this->assertSame( array( $visible ), array_map( 'absint', wp_list_pluck( $journal->posts, 'ID' ) ) );
$this->assertSame( 1, (int) $journal->found_posts );
$this->assertSame( array( $visible ), $this->get_eligible_query_ids( chrnx_get_show_query_args( 'silicon-ranch' ) ) );
}
/** Latest Podcast excludes tag-only titles and retains marked-up visible titles. */
public function test_tag_only_titles_are_excluded_from_latest_podcast() {
$visible = $this->create_post( '<b>Visible podcast</b>', '2026-08-01 10:00:00', 'podcasts' );
$tag_only = $this->create_post( '<b><span> </span></b>', '2026-08-02 10:00:00', 'podcasts' );
wp_set_object_terms( $visible, 'silicon-ranch', 'chrnx_show' );
wp_set_object_terms( $tag_only, 'silicon-ranch', 'chrnx_show' );
$this->assertSame( $visible, chrnx_get_latest_podcast_id() );
}
/** Eligible SQL filtering is absent outside the focused wrapper, including exceptions. */
public function test_eligible_title_filter_is_scoped_and_removed_on_success_or_exception() {
$this->assertFalse( has_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles' ) );
$this->assertTrue( function_exists( 'chrnx_run_eligible_query' ) );
if ( ! function_exists( 'chrnx_run_eligible_query' ) ) {
return;
}
chrnx_run_eligible_query( chrnx_get_journal_query_args( array() ) );
$this->assertFalse( has_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles' ) );
$throw = static function () {
throw new RuntimeException( 'Synthetic query failure.' );
};
add_filter( 'posts_pre_query', $throw );
try {
chrnx_run_eligible_query( chrnx_get_journal_query_args( array() ) );
$this->fail( 'The synthetic query failure should escape.' );
} catch ( RuntimeException $exception ) {
$this->assertSame( 'Synthetic query failure.', $exception->getMessage() );
} finally {
remove_filter( 'posts_pre_query', $throw );
}
$this->assertFalse( has_filter( 'posts_where', 'chrnx_filter_nonempty_editorial_titles' ) );
$unmarked = new WP_Query();
$this->assertSame( 'WHERE 1=1', chrnx_filter_nonempty_editorial_titles( 'WHERE 1=1', $unmarked ) );
}
/** Deleted show, topic, and tag relationships invalidate cached editorial selections. */
public function test_deleted_relationships_invalidate_podcast_topic_and_tag_caches() {
$podcast = $this->create_post( 'Cached podcast', '2026-07-01 10:00:00', 'podcasts' );
wp_set_object_terms( $podcast, 'silicon-ranch', 'chrnx_show' );
$this->assertSame( $podcast, chrnx_get_latest_podcast_id() );
wp_delete_object_term_relationships( $podcast, 'chrnx_show' );
$this->assertSame( 0, chrnx_get_latest_podcast_id() );
$current = $this->create_post( 'Relationship current', '2026-07-05 10:00:00', 'research' );
$topic_match = $this->create_post( 'Topic match', '2026-07-01 10:00:00', 'research' );
$recent_other = $this->create_post( 'Recent other', '2026-07-04 10:00:00', 'code' );
$this->assertSame( array( $topic_match ), chrnx_get_related_post_ids( $current, 1 ) );
wp_delete_object_term_relationships( $topic_match, 'chrnx_topic' );
$this->assertSame( array( $recent_other ), chrnx_get_related_post_ids( $current, 1 ) );
$tag_match = $this->create_post( 'Tag match', '2026-07-02 10:00:00', 'life' );
$tag_id = self::factory()->tag->create();
wp_set_post_tags( $current, array( $tag_id ) );
wp_set_post_tags( $tag_match, array( $tag_id ) );
$this->assertSame( array( $tag_match ), chrnx_get_related_post_ids( $current, 1 ) );
wp_delete_object_term_relationships( $tag_match, 'post_tag' );
$this->assertSame( array( $recent_other ), chrnx_get_related_post_ids( $current, 1 ) );
}
}