Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/wp-includes/class-wp-term-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/tests/term/getTerms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading