diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index 13e768c52de87..c2ab012b44414 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -1128,6 +1128,17 @@ protected function get_search_sql( $search ) { $like = '%' . $wpdb->esc_like( $search ) . '%'; + /* + * Slugs containing non-ASCII characters are stored percent-encoded, so also + * match the encoded form of the search string against the slug. + */ + $slug_search = strtolower( utf8_uri_encode( $search ) ); + if ( $slug_search !== $search ) { + $slug_like = '%' . $wpdb->esc_like( $slug_search ) . '%'; + + return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s) OR (t.slug LIKE %s))', $like, $like, $slug_like ); + } + return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like ); } diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 066e127867b5c..aedd402ff8af4 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -446,6 +446,41 @@ public function test_get_terms_search() { $this->assertSameSets( array( $term_id1, $term_id2 ), $terms ); } + /** + * @ticket 43626 + */ + public function test_get_terms_search_matches_non_ascii_slug() { + $term_id1 = self::factory()->tag->create( + array( + 'name' => 'tag1', + 'slug' => 'slug1', + ) + ); + $term_id2 = self::factory()->tag->create( + array( + 'name' => 'تاج1', + 'slug' => 'سلج1', + ) + ); + $term_id3 = self::factory()->tag->create( + array( + 'name' => 'Foo', + 'slug' => 'foo', + ) + ); + + $args = array( + 'hide_empty' => false, + 'fields' => 'ids', + ); + + $this->assertSame( array( $term_id1 ), get_terms( 'post_tag', $args + array( 'search' => 'slug1' ) ), 'An ASCII slug should still match.' ); + $this->assertSame( array( $term_id2 ), get_terms( 'post_tag', $args + array( 'search' => 'سلج1' ) ), 'A non-ASCII slug should match.' ); + $this->assertSame( array( $term_id2 ), get_terms( 'post_tag', $args + array( 'search' => 'تاج1' ) ), 'A non-ASCII name should still match.' ); + $this->assertSame( array(), get_terms( 'post_tag', $args + array( 'search' => 'سلج2' ) ), 'A non-matching non-ASCII search should not match.' ); + $this->assertNotContains( $term_id3, get_terms( 'post_tag', $args + array( 'search' => 'سلج1' ) ) ); + } + /** * @ticket 8214 */