diff --git a/inc/queries.php b/inc/queries.php index d4d3d62..dc6379c 100644 --- a/inc/queries.php +++ b/inc/queries.php @@ -17,7 +17,7 @@ function chrnx_is_eligible_editorial_post( $post ) { return $post instanceof WP_Post && 'post' === $post->post_type && 'publish' === $post->post_status - && '' !== trim( wp_strip_all_tags( $post->post_title ) ); + && 1 === preg_match( '/[^[:space:]]/', $post->post_title ); } /** Clear request-local editorial query results after relevant mutations. */ @@ -76,8 +76,7 @@ function chrnx_filter_nonempty_editorial_titles( $where, $query ) { } global $wpdb; - $visible_text_pattern = '(^|>)[[:space:]]*[^<[:space:]][^<]*(<|$)'; - return $where . $wpdb->prepare( ' AND ' . $wpdb->posts . '.post_title REGEXP %s', $visible_text_pattern ); + return $where . $wpdb->prepare( ' AND ' . $wpdb->posts . '.post_title REGEXP %s', '[^[:space:]]' ); } /** @@ -138,7 +137,7 @@ function chrnx_get_eligible_editorial_post_ids( $extra_args = array() ) { } $query = chrnx_run_eligible_query( $args ); - $ids = array_values( array_map( 'absint', array_filter( $query->posts, 'chrnx_is_eligible_editorial_post' ) ) ); + $ids = array_values( array_map( 'absint', $query->posts ) ); if ( ! empty( $ids ) ) { _prime_post_caches( $ids, true, true ); } diff --git a/tests/php/BlockRenderingTest.php b/tests/php/BlockRenderingTest.php index 1f16ad7..2eeb971 100644 --- a/tests/php/BlockRenderingTest.php +++ b/tests/php/BlockRenderingTest.php @@ -172,6 +172,26 @@ class BlockRenderingTest extends WP_UnitTestCase { $this->assertStringNotContainsString( 'placeholder', strtolower( $output ) ); } + /** Stored title markup is displayed literally and cannot inject executable elements. */ + public function test_editorial_cards_escape_stored_title_markup() { + $title = ''; + $post_id = self::factory()->post->create( array( 'post_title' => $title ) ); + $output = render_block( + array( + 'blockName' => 'chrnx/featured-stories', + 'attrs' => array( 'limit' => 1 ), + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ) + ); + + $this->assertSame( $post_id, chrnx_get_featured_story_ids( 1 )[0] ); + $this->assertStringContainsString( esc_html( get_the_title( $post_id ) ), $output ); + $this->assertStringNotContainsString( 'assertStringNotContainsString( '', '2026-06-05 10:00:00', 'research' ); - $nested = $this->create_post( ' ', '2026-06-04 10:00:00', 'code' ); - $visible = $this->create_post( 'Visible story', '2026-06-03 10:00:00', 'life' ); - update_option( 'sticky_posts', array( $tag_only, $nested ) ); + /** Literal stored markup is nonempty title text and cannot make bounded selectors underfill. */ + public function test_stored_markup_titles_are_eligible_for_featured_and_related_surfaces() { + $comment = $this->create_post( '', '2026-06-05 10:00:00', 'research' ); + $attribute = $this->create_post( '', '2026-06-04 10:00:00', 'code' ); + $nested = $this->create_post( ' ', '2026-06-03 10:00:00', 'life' ); + update_option( 'sticky_posts', array( $comment ) ); - $this->assertSame( array( $visible ), chrnx_get_featured_story_ids( 1 ) ); + foreach ( array( $comment, $attribute, $nested ) as $post_id ) { + $this->assertTrue( chrnx_is_eligible_editorial_post( $post_id ) ); + } + $this->assertSame( array( $comment, $attribute, $nested ), chrnx_get_featured_story_ids( 3 ) ); $current = $this->create_post( 'Current tags', '2026-06-06 10:00:00', 'research' ); - $this->assertSame( array( $visible ), chrnx_get_related_post_ids( $current, 1 ) ); + $this->assertSame( array( $comment, $attribute, $nested ), chrnx_get_related_post_ids( $current, 3 ) ); } - /** 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( '', '2026-07-03 10:00:00', 'podcasts' ); - $visible = $this->create_post( 'Visible episode', '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' ); + /** PHP uses the same POSIX whitespace definition as the prepared SQL predicate. */ + public function test_php_title_eligibility_matches_sql_whitespace_semantics() { + $post = get_post( $this->create_post( 'Temporary title', '2026-06-07 10:00:00' ) ); + $post->post_title = "\f"; + $this->assertFalse( chrnx_is_eligible_editorial_post( $post ) ); + $post->post_title = "\fX"; + $this->assertTrue( chrnx_is_eligible_editorial_post( $post ) ); + } - $journal = function_exists( 'chrnx_run_eligible_query' ) + /** Journal and Show SQL use the same literal stored-title contract as PHP. */ + public function test_stored_markup_titles_are_eligible_in_journal_and_show_queries() { + $comment = $this->create_post( '', '2026-07-03 10:00:00', 'podcasts' ); + $attribute = $this->create_post( '', '2026-07-02 10:00:00', 'podcasts' ); + $nested = $this->create_post( ' ', '2026-07-01 10:00:00', 'podcasts' ); + $whitespace = $this->create_post( "\f", '2026-07-04 10:00:00', 'podcasts' ); + foreach ( array( $comment, $attribute, $nested ) as $post_id ) { + wp_set_object_terms( $post_id, 'silicon-ranch', 'chrnx_show' ); + } + wp_set_object_terms( $whitespace, 'silicon-ranch', 'chrnx_show' ); + $this->assertFalse( chrnx_is_eligible_editorial_post( $whitespace ) ); + + $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' ) ) ); + $expected = array( $comment, $attribute, $nested ); + $this->assertSame( $expected, array_map( 'absint', wp_list_pluck( $journal->posts, 'ID' ) ) ); + $this->assertSame( 3, (int) $journal->found_posts ); + $this->assertSame( $expected, $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( 'Visible podcast', '2026-08-01 10:00:00', 'podcasts' ); - $tag_only = $this->create_post( ' ', '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' ); + /** Latest Podcast treats nested stored markup as a nonempty literal title. */ + public function test_stored_nested_markup_title_is_eligible_for_latest_podcast() { + $older = $this->create_post( 'Older podcast', '2026-08-01 10:00:00', 'podcasts' ); + $nested = $this->create_post( ' ', '2026-08-02 10:00:00', 'podcasts' ); + wp_set_object_terms( $older, 'silicon-ranch', 'chrnx_show' ); + wp_set_object_terms( $nested, 'silicon-ranch', 'chrnx_show' ); - $this->assertSame( $visible, chrnx_get_latest_podcast_id() ); + $this->assertSame( $nested, chrnx_get_latest_podcast_id() ); } /** Eligible SQL filtering is absent outside the focused wrapper, including exceptions. */