Make WordPress Core

Ticket #21803: 21803.diff

File 21803.diff, 2.8 KB (added by wonderboymusic, 12 years ago)
  • tests/phpunit/tests/query/search.php

     
     1<?php
     2
     3/**
     4 * Test search query vars, without any initial posts in the database
     5 *
     6 * @group query
     7 * @group search
     8 */
     9class Tests_Query_Search extends WP_UnitTestCase {
     10        protected $q;
     11
     12        function setUp() {
     13                parent::setUp();
     14
     15                unset( $this->q );
     16                $this->q = new WP_Query();
     17        }
     18
     19        function test_search_fields_filter() {
     20                $p1 = $this->factory->post->create( array( 'post_title' => 'Burrito' ) );
     21                $p2 = $this->factory->post->create( array( 'post_content' => 'Burrito' ) );
     22                $p3 = $this->factory->post->create( array( 'post_excerpt' => 'Burrito' ) );
     23
     24                add_filter( 'search_fields', array( $this, 'filter_search_fields' ) );
     25                $posts = $this->q->query( array(
     26                        'fields' => 'ids',
     27                        's' => 'Burrito'
     28                ) );
     29                $this->assertEquals( array( $p3 ), $posts );
     30                remove_filter( 'search_fields', array( $this, 'filter_search_fields' ) );
     31
     32                add_filter( 'search_fields', array( $this, 'empty_search_fields' ) );
     33                $posts2 = $this->q->query( array(
     34                        'fields' => 'ids',
     35                        's' => 'Burrito'
     36                ) );
     37                $this->assertEqualSets( array( $p3, $p2, $p1 ), $posts2 );
     38                remove_filter( 'search_fields', array( $this, 'empty_search_fields' ) );
     39        }
     40
     41        function filter_search_fields() {
     42                global $wpdb;
     43                return array( "$wpdb->posts.post_excerpt" );
     44        }
     45
     46        function empty_search_fields() {
     47                return array();
     48        }
     49}
     50 No newline at end of file
  • src/wp-includes/query.php

     
    22332233                                preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
    22342234                                $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
    22352235                        }
    2236                         $n = !empty($q['exact']) ? '' : '%';
     2236                        $n = ! empty( $q['exact'] ) ? '' : '%';
    22372237                        $searchand = '';
    2238                         foreach( (array) $q['search_terms'] as $term ) {
    2239                                 $term = esc_sql( like_escape( $term ) );
    2240                                 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
    2241                                 $searchand = ' AND ';
     2238                        $search_fields = apply_filters( 'search_fields', array( "$wpdb->posts.post_title", "$wpdb->posts.post_content" ) );
     2239
     2240                        if ( ! empty( $search_fields ) && is_array( $search_fields ) ) {
     2241                                foreach( (array) $q['search_terms'] as $term ) {
     2242                                        $term = esc_sql( like_escape( $term ) );
     2243                                        foreach ( $search_fields as &$s )
     2244                                                $s = "({$s} LIKE '{$n}{$term}{$n}')";
     2245                                        $search .= sprintf( '%s(%s)', $searchand, implode( ' OR ', $search_fields  ) );
     2246                                        $searchand = ' AND ';
     2247                                }
    22422248                        }
    22432249
    22442250                        if ( !empty($search) ) {