Changeset 38844
- Timestamp:
- 10/20/2016 06:41:22 PM (7 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-query.php
r38792 r38844 722 722 * @type string $s Search keyword(s). Prepending a term with a hyphen will 723 723 * exclude posts matching that term. Eg, 'pillow -sofa' will 724 * return posts containing 'pillow' but not 'sofa'. Th is feature725 * c an be disabled using the726 * 'wp_query_use_hyphen_for_exclusion' filter.724 * return posts containing 'pillow' but not 'sofa'. The 725 * character used for exclusion can be modified using the 726 * the 'wp_query_search_exclusion_prefix' filter. 727 727 * @type int $second Second of the minute. Default empty. Accepts numbers 0-60. 728 728 * @type bool $sentence Whether to search by phrase. Default false. … … 1323 1323 1324 1324 /** 1325 * Filters whether search terms preceded by hyphens shouldexcluded from results.1325 * Filters the prefix that indicates that a search term should be excluded from results. 1326 1326 * 1327 1327 * @since 4.7.0 1328 1328 * 1329 * @param bool Whether the query should exclude terms preceded with a hyphen. 1329 * @param string $exclusion_prefix The prefix. Default '-'. Returning 1330 * an empty value disables exclusions. 1330 1331 */ 1331 $ hyphen_exclusion = apply_filters( 'wp_query_use_hyphen_for_exclusion', true);1332 $exclusion_prefix = apply_filters( 'wp_query_search_exclusion_prefix', '-' ); 1332 1333 1333 1334 foreach ( $q['search_terms'] as $term ) { 1334 // Terms prefixed with '-' should be excluded. 1335 $include = '-' !== substr( $term, 0, 1 ); 1336 if ( $include || ! $hyphen_exclusion ) { 1337 $like_op = 'LIKE'; 1338 $andor_op = 'OR'; 1339 } else { 1335 // If there is an $exclusion_prefix, terms prefixed with it should be excluded. 1336 $exclude = $exclusion_prefix && ( $exclusion_prefix === substr( $term, 0, 1 ) ); 1337 if ( $exclude ) { 1340 1338 $like_op = 'NOT LIKE'; 1341 1339 $andor_op = 'AND'; 1342 1340 $term = substr( $term, 1 ); 1343 } 1344 1345 if ( $n && $include ) { 1341 } else { 1342 $like_op = 'LIKE'; 1343 $andor_op = 'OR'; 1344 } 1345 1346 if ( $n && ! $exclude ) { 1346 1347 $like = '%' . $wpdb->esc_like( $term ) . '%'; 1347 1348 $q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like ); -
trunk/tests/phpunit/tests/query/search.php
r38792 r38844 63 63 * @ticket 38099 64 64 */ 65 function test_ filter_wp_query_use_hyphen_for_exclusion() {65 function test_disable_search_exclusion_prefix() { 66 66 $title = '-HYPHENATION_TEST'; 67 67 … … 75 75 76 76 // After we disable the feature using the filter, we should get the result 77 add_filter( 'wp_query_ use_hyphen_for_exclusion', '__return_false' );77 add_filter( 'wp_query_search_exclusion_prefix', '__return_false' ); 78 78 $result = $this->get_search_results( $title ); 79 79 $post = array_pop( $result ); 80 80 $this->assertEquals( $post->ID, $post_id ); 81 remove_filter( 'wp_query_use_hyphen_for_exclusion', '__return_false' ); 81 remove_filter( 'wp_query_search_exclusion_prefix', '__return_false' ); 82 } 83 84 /** 85 * @ticket 38099 86 */ 87 function test_change_search_exclusion_prefix() { 88 $title = '#OCTOTHORPE_TEST'; 89 90 // Create a post with a title that starts with a non-hyphen prefix. 91 $post_id = self::factory()->post->create( array( 92 'post_content' => $title, 'post_type' => $this->post_type 93 ) ); 94 95 // By default, we should get the result. 96 $result = $this->get_search_results( $title ); 97 $post = array_pop( $result ); 98 $this->assertEquals( $post->ID, $post_id ); 99 100 // After we change the prefix, the result should be excluded. 101 add_filter( 'wp_query_search_exclusion_prefix', array( $this, 'filter_search_exclusion_prefix_octothorpe' ) ); 102 $found = $this->get_search_results( $title ); 103 remove_filter( 'wp_query_search_exclusion_prefix', array( $this, 'filter_search_exclusion_prefix_octothorpe' ) ); 104 $this->assertEquals( array(), $found ); 105 } 106 107 function filter_search_exclusion_prefix_octothorpe() { 108 return '#'; 82 109 } 83 110
Note: See TracChangeset
for help on using the changeset viewer.