mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Query: Allow the prefix used for search term exclusion to be filtered.
[38792] allowed `WP_Query`'s hyphen-as-exclusion-prefix feature to be disabled via filter. A more general solution is to allow the prefix to be filtered; returning an empty value from a filter callback works to disable the feature. Props dlh. Fixes #38099. git-svn-id: https://develop.svn.wordpress.org/trunk@38844 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -62,7 +62,7 @@ class Tests_Query_Search extends WP_UnitTestCase {
|
||||
/**
|
||||
* @ticket 38099
|
||||
*/
|
||||
function test_filter_wp_query_use_hyphen_for_exclusion() {
|
||||
function test_disable_search_exclusion_prefix() {
|
||||
$title = '-HYPHENATION_TEST';
|
||||
|
||||
// Create a post with a title which starts with a hyphen
|
||||
@@ -74,11 +74,38 @@ class Tests_Query_Search extends WP_UnitTestCase {
|
||||
$this->assertEquals( array(), $this->get_search_results( $title ) );
|
||||
|
||||
// After we disable the feature using the filter, we should get the result
|
||||
add_filter( 'wp_query_use_hyphen_for_exclusion', '__return_false' );
|
||||
add_filter( 'wp_query_search_exclusion_prefix', '__return_false' );
|
||||
$result = $this->get_search_results( $title );
|
||||
$post = array_pop( $result );
|
||||
$this->assertEquals( $post->ID, $post_id );
|
||||
remove_filter( 'wp_query_use_hyphen_for_exclusion', '__return_false' );
|
||||
remove_filter( 'wp_query_search_exclusion_prefix', '__return_false' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 38099
|
||||
*/
|
||||
function test_change_search_exclusion_prefix() {
|
||||
$title = '#OCTOTHORPE_TEST';
|
||||
|
||||
// Create a post with a title that starts with a non-hyphen prefix.
|
||||
$post_id = self::factory()->post->create( array(
|
||||
'post_content' => $title, 'post_type' => $this->post_type
|
||||
) );
|
||||
|
||||
// By default, we should get the result.
|
||||
$result = $this->get_search_results( $title );
|
||||
$post = array_pop( $result );
|
||||
$this->assertEquals( $post->ID, $post_id );
|
||||
|
||||
// After we change the prefix, the result should be excluded.
|
||||
add_filter( 'wp_query_search_exclusion_prefix', array( $this, 'filter_search_exclusion_prefix_octothorpe' ) );
|
||||
$found = $this->get_search_results( $title );
|
||||
remove_filter( 'wp_query_search_exclusion_prefix', array( $this, 'filter_search_exclusion_prefix_octothorpe' ) );
|
||||
$this->assertEquals( array(), $found );
|
||||
}
|
||||
|
||||
function filter_search_exclusion_prefix_octothorpe() {
|
||||
return '#';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user