Make WordPress Core

Ticket #38099: 38099.3.patch

File 38099.3.patch, 3.2 KB (added by choongsavvii, 8 years ago)
  • src/wp-includes/class-wp-query.php

     
    721721         *     @type array        $post_name__in           An array of post slugs that results must match.
    722722         *     @type string       $s                       Search keyword(s). Prepending a term with a hyphen will
    723723         *                                                 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.
    725726         *     @type int          $second                  Second of the minute. Default empty. Accepts numbers 0-60.
    726727         *     @type bool         $sentence                Whether to search by phrase. Default false.
    727728         *     @type bool         $suppress_filters        Whether to suppress filters. Default false.
     
    13181319                $n = ! empty( $q['exact'] ) ? '' : '%';
    13191320                $searchand = '';
    13201321                $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
    13211332                foreach ( $q['search_terms'] as $term ) {
    13221333                        // Terms prefixed with '-' should be excluded.
    13231334                        $include = '-' !== substr( $term, 0, 1 );
    1324                         if ( $include ) {
     1335                        if ( $include || ! $hyphen_exclusion ) {
    13251336                                $like_op  = 'LIKE';
    13261337                                $andor_op = 'OR';
    13271338                        } else {
  • tests/phpunit/tests/query/search.php

     
    6060        }
    6161
    6262        /**
     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        /**
    6392         * @ticket 33988
    6493         */
    6594        public function test_s_should_exclude_term_prefixed_with_dash() {