Skip to content

Commit 58677a9

Browse files
committed
Taxonomy: Include encoded form of decoded search terms in WP_Term_Query.
Addresses review feedback on PR WordPress#14187 by decoding search queries before entity-encoding them. This ensures queries containing decimal entities (e.g. 'Tour &WordPress#38; Travel') and hexadecimal entities (e.g. 'Tour & Travel') match term names stored in the database as HTML entities ('Tour & Travel'). Adds dedicated unit tests for decimal and hex numeric entities. See #66279.
1 parent 8bea9a5 commit 58677a9

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

‎src/wp-includes/class-wp-term-query.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,16 +1124,16 @@ protected function parse_order( $order ) {
11241124
protected function get_search_sql( $search ) {
11251125
$searches = array( $search );
11261126

1127-
$encoded = _wp_specialchars( $search, ENT_COMPAT, 'UTF-8', true );
1128-
if ( $encoded !== $search ) {
1129-
$searches[] = $encoded;
1130-
}
1131-
11321127
$decoded = wp_specialchars_decode( $search, ENT_COMPAT );
1133-
if ( $decoded !== $search && ! in_array( $decoded, $searches, true ) ) {
1128+
if ( $decoded !== $search ) {
11341129
$searches[] = $decoded;
11351130
}
11361131

1132+
$encoded = _wp_specialchars( $decoded, ENT_COMPAT, 'UTF-8', true );
1133+
if ( $encoded !== $search && ! in_array( $encoded, $searches, true ) ) {
1134+
$searches[] = $encoded;
1135+
}
1136+
11371137
global $wpdb;
11381138

11391139
$clauses = array();

‎tests/phpunit/tests/term/getSearchSql.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ public static function data_term_search_with_ampersand_queries() {
9999
'encoded entity followed by space' => array( 'Tour & ' ),
100100
'full name with encoded entity' => array( 'Tour & Travel' ),
101101
'suffix with encoded entity' => array( '& Travel' ),
102+
'decimal entity followed by space' => array( 'Tour & ' ),
103+
'full name with decimal entity' => array( 'Tour & Travel' ),
104+
'suffix with decimal entity' => array( '& Travel' ),
105+
'decimal entity only' => array( '&' ),
106+
'hex entity followed by space' => array( 'Tour & ' ),
107+
'full name with hex entity' => array( 'Tour & Travel' ),
108+
'suffix with hex entity' => array( '& Travel' ),
109+
'hex entity only' => array( '&' ),
102110
);
103111
}
104112

@@ -147,5 +155,11 @@ public function test_get_search_sql_structure() {
147155
$this->assertStringContainsString( ') OR ((', $ampersand_sql, 'Expanded search must join groups with OR.' );
148156
$this->assertStringContainsString( 'Tour & ', $ampersand_sql, 'SQL must match raw search term.' );
149157
$this->assertStringContainsString( 'Tour & ', $ampersand_sql, 'SQL must match entity-encoded search term.' );
158+
159+
// Case 3: Search term with numeric entity returns expanded OR clauses covering input, decoded, and encoded forms.
160+
$numeric_entity_sql = $reflection_method->invoke( $query, 'Tour & Travel' );
161+
$this->assertStringContainsString( 'Tour & Travel', $numeric_entity_sql, 'SQL must match numeric entity search term.' );
162+
$this->assertStringContainsString( 'Tour & Travel', $numeric_entity_sql, 'SQL must match decoded search term.' );
163+
$this->assertStringContainsString( 'Tour & Travel', $numeric_entity_sql, 'SQL must match entity-encoded search term.' );
150164
}
151165
}

0 commit comments

Comments
 (0)