Make WordPress Core

Ticket #35791: 35791.diff

File 35791.diff, 5.1 KB (added by jeremyfelt, 9 years ago)
  • src/wp-includes/class-wp-site-query.php

     
    141141         *     @type int          $spam              Limit results to spam sites. Accepts '1' or '0'. Default empty.
    142142         *     @type int          $deleted           Limit results to deleted sites. Accepts '1' or '0'. Default empty.
    143143         *     @type string       $search            Search term(s) to retrieve matching sites for. Default empty.
     144         *     @type array        $search_columns    Array of column names to be searched. Accepts 'domain' and 'path'.
     145         *                                           Default empty array.
    144146         *     @type bool         $update_site_cache Whether to prime the cache for found sites. Default false.
    145147         * }
    146148         */
     
    170172                        'spam'              => null,
    171173                        'deleted'           => null,
    172174                        'search'            => '',
     175                        'search_columns'    => array(),
    173176                        'count'             => false,
    174177                        'date_query'        => null, // See WP_Date_Query
    175178                        'update_site_cache' => true,
     
    481484
    482485                // Falsey search strings are ignored.
    483486                if ( strlen( $this->query_vars['search'] ) ) {
    484                         $this->sql_clauses['where']['search'] = $this->get_search_sql(
    485                                 $this->query_vars['search'],
    486                                 array( 'domain', 'path' )
    487                         );
     487                        $search_columns = array();
     488
     489                        if ( $this->query_vars['search_columns'] ) {
     490                                $search_columns = array_intersect( $this->query_vars['search_columns'], array( 'domain', 'path' ) );
     491                        }
     492
     493                        if ( ! $search_columns ) {
     494                                $search_columns = array( 'domain', 'path' );
     495                        }
     496
     497                        /**
     498                         * Filters the columns to search in a WP_Site_Query search.
     499                         *
     500                         * The default columns include 'domain' and 'path.
     501                         *
     502                         * @since 4.6.0
     503                         *
     504                         * @param array         $search_columns Array of column names to be searched.
     505                         * @param string        $search         Text being searched.
     506                         * @param WP_Site_Query $this           The current WP_Site_Query instance.
     507                         */
     508                        $search_columns = apply_filters( 'site_search_columns', $search_columns, $this->query_vars['search'], $this );
     509
     510                        $this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search'], $search_columns );
    488511                }
    489512
    490513                $date_query = $this->query_vars['date_query'];
     
    563586        protected function get_search_sql( $string, $columns ) {
    564587                global $wpdb;
    565588
    566                 $like = '%' . $wpdb->esc_like( $string ) . '%';
     589                if ( false !== strpos( $string, '*' ) ) {
     590                        $like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%';
     591                } else {
     592                        $like = '%' . $wpdb->esc_like( $string ) . '%';
     593                }
    567594
    568595                $searches = array();
    569596                foreach ( $columns as $column ) {
  • tests/phpunit/tests/multisite/siteQuery.php

     
    470470
    471471                $this->assertEquals( $expected, $found );
    472472        }
     473
     474        public function test_wp_site_query_by_search_with_text_in_path_exclude_domain_from_search() {
     475                $q = new WP_Site_Query();
     476                $found = $q->query( array(
     477                        'fields' => 'ids',
     478                        'search' => 'make',
     479                        'search_columns' => array( 'path' ),
     480                ) );
     481
     482                $expected = array(
     483                        self::$site_ids['www.w.org/make/'],
     484                );
     485
     486                $this->assertEquals( $expected, $found );
     487        }
     488
     489        public function test_wp_site_query_by_search_with_text_in_domain_exclude_path_from_search() {
     490                $q = new WP_Site_Query();
     491                $found = $q->query( array(
     492                        'fields' => 'ids',
     493                        'search' => 'make',
     494                        'search_columns' => array( 'domain' ),
     495                ) );
     496
     497                $expected = array(
     498                        self::$site_ids['make.wordpress.org/'],
     499                        self::$site_ids['make.wordpress.org/foo/'],
     500                );
     501
     502                $this->assertEquals( $expected, $found );
     503        }
     504
     505        public function test_wp_site_query_by_search_with_wildcard_in_text() {
     506                $q = new WP_Site_Query();
     507                $found = $q->query( array(
     508                        'fields'       => 'ids',
     509                        'search'       => 'm*ke',
     510                ) );
     511
     512                $expected = array(
     513                        self::$site_ids['www.w.org/make/'],
     514                        self::$site_ids['make.wordpress.org/'],
     515                        self::$site_ids['make.wordpress.org/foo/'],
     516                );
     517
     518                $this->assertEqualSets( $expected, $found );
     519        }
     520
     521        public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_path_from_search() {
     522                $q = new WP_Site_Query();
     523                $found = $q->query( array(
     524                        'fields' => 'ids',
     525                        'search' => 'm*ke',
     526                        'search_columns' => array( 'domain' ),
     527                ) );
     528
     529                $expected = array(
     530                        self::$site_ids['make.wordpress.org/'],
     531                        self::$site_ids['make.wordpress.org/foo/'],
     532                );
     533
     534                $this->assertEqualSets( $expected, $found );
     535        }
     536
     537        public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_domain_from_search() {
     538                $q = new WP_Site_Query();
     539                $found = $q->query( array(
     540                        'fields' => 'ids',
     541                        'search' => 'm*ke',
     542                        'search_columns' => array( 'path' ),
     543                ) );
     544
     545                $expected = array(
     546                        self::$site_ids['www.w.org/make/'],
     547                );
     548
     549                $this->assertEqualSets( $expected, $found );
     550        }
    473551}
    474552
    475553endif;