Ticket #38099: 38099.3.patch
File 38099.3.patch, 3.2 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-query.php
721 721 * @type array $post_name__in An array of post slugs that results must match. 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'. 724 * return posts containing 'pillow' but not 'sofa'. This feature 725 * can be disabled using the filter wp_query_use_hyphen_for_negation. 725 726 * @type int $second Second of the minute. Default empty. Accepts numbers 0-60. 726 727 * @type bool $sentence Whether to search by phrase. Default false. 727 728 * @type bool $suppress_filters Whether to suppress filters. Default false. … … 1318 1319 $n = ! empty( $q['exact'] ) ? '' : '%'; 1319 1320 $searchand = ''; 1320 1321 $q['search_orderby_title'] = array(); 1322 1323 /** 1324 * Filters whether to exclude search terms preceded with a hyphen 1325 * 1326 * @since 4.7.0 1327 * 1328 * @param bool Whether the search query exclude terms preceded with a hyphen 1329 */ 1330 $hyphen_exclusion = apply_filters( 'wp_query_use_hyphen_for_negation', '__return_true' ); 1331 1321 1332 foreach ( $q['search_terms'] as $term ) { 1322 1333 // Terms prefixed with '-' should be excluded. 1323 1334 $include = '-' !== substr( $term, 0, 1 ); 1324 if ( $include ) {1335 if ( $include || ! $hyphen_exclusion ) { 1325 1336 $like_op = 'LIKE'; 1326 1337 $andor_op = 'OR'; 1327 1338 } else { -
tests/phpunit/tests/query/search.php
60 60 } 61 61 62 62 /** 63 * @ticket 38099 64 */ 65 function test_filter_wp_query_use_hyphen_for_negation() { 66 $title = '-HYPHENATION_TEST'; 67 68 // Create a post with a title which starts with a hyphen 69 $post_id = self::factory()->post->create( array( 70 'post_content' => $title, 'post_type' => $this->post_type 71 ) ); 72 73 // By default, we can use the hyphen prefix to exclude results 74 $this->assertEquals( array(), $this->get_search_results( $title ) ); 75 76 // After we disable the feature using the filter, we should get the result 77 add_filter( 'wp_query_use_hyphen_for_negation', array( $this, 'filter_wp_query_use_hyphen_for_negation' ) ); 78 $result = $this->get_search_results( $title ); 79 $post = array_pop( $result ); 80 $this->assertEquals( $post->ID, $post_id ); 81 remove_filter( 'wp_query_use_hyphen_for_negation', array( $this, 'filter_wp_query_use_hyphen_for_negation' ) ); 82 } 83 84 /** 85 * @ticket 38099 86 */ 87 function filter_wp_query_use_hyphen_for_negation() { 88 return false; 89 } 90 91 /** 63 92 * @ticket 33988 64 93 */ 65 94 public function test_s_should_exclude_term_prefixed_with_dash() {