Make WordPress Core

Ticket #38099: 38099.6.patch

File 38099.6.patch, 4.3 KB (added by dlh, 7 years ago)
  • src/wp-includes/class-wp-query.php

     
    722722         *     @type string       $s                       Search keyword(s). Prepending a term with a hyphen will
    723723         *                                                 exclude posts matching that term. Eg, 'pillow -sofa' will
    724724         *                                                 return posts containing 'pillow' but not 'sofa'. This feature
    725          *                                                 can be disabled using the
    726          *                                                 'wp_query_use_hyphen_for_exclusion' filter.
     725         *                                                 can be altered with the 'wp_query_search_exclusion_prefix' filter.
    727726         *     @type int          $second                  Second of the minute. Default empty. Accepts numbers 0-60.
    728727         *     @type bool         $sentence                Whether to search by phrase. Default false.
    729728         *     @type bool         $suppress_filters        Whether to suppress filters. Default false.
     
    13221321                $q['search_orderby_title'] = array();
    13231322
    13241323                /**
    1325                  * Filters whether search terms preceded by hyphens should excluded from results.
     1324                 * Filters the prefix denoting that a search term should be excluded from results.
    13261325                 *
    13271326                 * @since 4.7.0
    13281327                 *
    1329                  * @param bool Whether the query should exclude terms preceded with a hyphen.
     1328                 * @param string $exclusion_prefix The prefix. Default hyphen. Returning
     1329                 *                                 an empty value disables exclusions.
    13301330                 */
    1331                 $hyphen_exclusion = apply_filters( 'wp_query_use_hyphen_for_exclusion', true );
     1331                $exclusion_prefix = apply_filters( 'wp_query_search_exclusion_prefix', '-' );
    13321332
    13331333                foreach ( $q['search_terms'] as $term ) {
    1334                         // Terms prefixed with '-' should be excluded.
    1335                         $include = '-' !== substr( $term, 0, 1 );
    1336                         if ( $include || ! $hyphen_exclusion ) {
     1334                        // If there is an $exclusion_prefix, terms prefixed with it should be excluded.
     1335                        $include = ( ! $exclusion_prefix || ( substr( $term, 0, 1 ) !== $exclusion_prefix ) );
     1336                        if ( $include ) {
    13371337                                $like_op  = 'LIKE';
    13381338                                $andor_op = 'OR';
    13391339                        } else {
  • tests/phpunit/tests/query/search.php

     
    6262        /**
    6363         * @ticket 38099
    6464         */
    65         function test_filter_wp_query_use_hyphen_for_exclusion() {
     65        function test_disable_search_exclusion_prefix() {
    6666                $title = '-HYPHENATION_TEST';
    6767
    6868                // Create a post with a title which starts with a hyphen
     
    7474                $this->assertEquals( array(), $this->get_search_results( $title ) );
    7575
    7676                // 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' );
    7878                $result = $this->get_search_results( $title );
    7979                $post = array_pop( $result );
    8080                $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' );
    8282        }
    8383
    8484        /**
     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                $this->assertEquals( array(), $this->get_search_results( $title ) );
     103                remove_filter( 'wp_query_search_exclusion_prefix', array( $this, 'filter_search_exclusion_prefix_octothorpe' ) );
     104        }
     105
     106        function filter_search_exclusion_prefix_octothorpe() {
     107                return '#';
     108        }
     109
     110        /**
    85111         * @ticket 33988
    86112         */
    87113        public function test_s_should_exclude_term_prefixed_with_dash() {