Make WordPress Core

Ticket #36675: 36675.4.diff

File 36675.4.diff, 11.4 KB (added by jeremyfelt, 9 years ago)
  • src/wp-admin/includes/class-wp-ms-sites-list-table.php

     
    8383                $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
    8484                $wild = '';
    8585                if ( false !== strpos($s, '*') ) {
    86                         $wild = '%';
     86                        $wild = '*';
    8787                        $s = trim($s, '*');
    8888                }
    8989
     
    9898                                $_GET['order'] = $_REQUEST['order'] = 'DESC';
    9999                }
    100100
    101                 $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
     101                $args = array(
     102                        'number'     => intval( $per_page ),
     103                        'offset'     => intval( ( $pagenum - 1 ) * $per_page ),
     104                        'network_id' => $current_site->id,
     105                );
    102106
    103107                if ( empty($s) ) {
    104108                        // Nothing to do.
     
    107111                                        preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
    108112                                        preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
    109113                        // IPv4 address
    110                         $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . $wild );
     114                        $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' ) );
    111115                        $reg_blog_ids = $wpdb->get_col( $sql );
    112116
    113                         if ( !$reg_blog_ids )
    114                                 $reg_blog_ids = array( 0 );
     117                        if ( $reg_blog_ids ) {
     118                                $args['site__in'] = $reg_blog_ids;
     119                        }
     120                } elseif ( is_numeric( $s ) && empty( $wild ) ) {
     121                        $args['ID'] = $s;
     122                } else {
     123                        $args['search'] = $s;
    115124
    116                         $query = "SELECT *
    117                                 FROM {$wpdb->blogs}
    118                                 WHERE site_id = '{$wpdb->siteid}'
    119                                 AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")";
    120                 } else {
    121                         if ( is_numeric($s) && empty( $wild ) ) {
    122                                 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.blog_id = %s )", $s );
    123                         } elseif ( is_subdomain_install() ) {
    124                                 $blog_s = str_replace( '.' . $current_site->domain, '', $s );
    125                                 $blog_s = $wpdb->esc_like( $blog_s ) . $wild . $wpdb->esc_like( '.' . $current_site->domain );
    126                                 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.domain LIKE %s ) ", $blog_s );
    127                         } else {
    128                                 if ( $s != trim('/', $current_site->path) ) {
    129                                         $blog_s = $wpdb->esc_like( $current_site->path . $s ) . $wild . $wpdb->esc_like( '/' );
    130                                 } else {
    131                                         $blog_s = $wpdb->esc_like( $s );
    132                                 }
    133                                 $query .= $wpdb->prepare( " AND  ( {$wpdb->blogs}.path LIKE %s )", $blog_s );
     125                        if ( ! is_subdomain_install() ) {
     126                                $args['search_columns'] = array( 'path' );
    134127                        }
    135128                }
    136129
    137130                $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
    138                 if ( $order_by === 'registered' ) {
    139                         $query .= ' ORDER BY registered ';
    140                 } elseif ( $order_by === 'lastupdated' ) {
    141                         $query .= ' ORDER BY last_updated ';
    142                 } elseif ( $order_by === 'blogname' ) {
     131                if ( 'registered' === $order_by ) {
     132                        // registered is named properly.
     133                } elseif ( 'lastupdated' === $order_by ) {
     134                        $order_by = 'last_updated';
     135                } elseif ( 'blogname' === $order_by ) {
    143136                        if ( is_subdomain_install() ) {
    144                                 $query .= ' ORDER BY domain ';
     137                                $order_by = 'domain';
    145138                        } else {
    146                                 $query .= ' ORDER BY path ';
     139                                $order_by = 'path';
    147140                        }
    148                 } elseif ( $order_by === 'blog_id' ) {
    149                         $query .= ' ORDER BY blog_id ';
    150                 } else {
    151                         $order_by = null;
     141                } elseif ( 'blog_id' === $order_by ) {
     142                        $order_by = 'id';
     143                } elseif ( ! $order_by ) {
     144                        $order_by = false;
    152145                }
    153146
    154                 if ( isset( $order_by ) ) {
    155                         $order = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
    156                         $query .= $order;
     147                $args['orderby'] = $order_by;
     148
     149                if ( $order_by ) {
     150                        $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
    157151                }
    158152
    159                 // Don't do an unbounded count on large networks
    160                 if ( ! wp_is_large_network() )
    161                         $total = $wpdb->get_var( str_replace( 'SELECT *', 'SELECT COUNT( blog_id )', $query ) );
     153                if ( wp_is_large_network() ) {
     154                        $args['no_found_rows'] = true;
     155                } else {
     156                        $args['no_found_rows'] = false;
     157                }
    162158
    163                 $query .= " LIMIT " . intval( ( $pagenum - 1 ) * $per_page ) . ", " . intval( $per_page );
    164                 $this->items = $wpdb->get_results( $query, ARRAY_A );
     159                $_sites = get_sites( $args );
     160                if ( is_array( $_sites ) ) {
     161                        update_site_cache( $_sites );
    165162
    166                 if ( wp_is_large_network() )
    167                         $total = count($this->items);
     163                        $this->items = array_slice( $_sites, 0, $per_page );
     164                }
    168165
     166                $total_sites = get_sites( array_merge( $args, array(
     167                        'count' => true,
     168                        'offset' => 0,
     169                        'number' => 0,
     170                ) ) );
     171
    169172                $this->set_pagination_args( array(
    170                         'total_items' => $total,
     173                        'total_items' => $total_sites,
    171174                        'per_page' => $per_page,
    172175                ) );
    173176        }
     
    445448         */
    446449        public function display_rows() {
    447450                foreach ( $this->items as $blog ) {
     451                        $blog = $blog->to_array();
    448452                        $class = '';
    449453                        reset( $this->status_list );
    450454
  • 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;
  • tests/phpunit/tests/multisite/wpMSSitesListTable.php

     
    7373
    7474                $expected = array(
    7575                        self::$site_ids['wordpress.org/foo/'],
     76                        self::$site_ids['wordpress.org/foo/bar/'],
     77                        self::$site_ids['wordpress.org/afoo/'],
    7678                        self::$site_ids['make.wordpress.org/foo/'],
    7779                        self::$site_ids['www.w.org/foo/'],
     80                        self::$site_ids['www.w.org/foo/bar/'],
    7881                );
    7982
    8083                $this->assertEqualSets( $expected, $items );
     
    131134
    132135                $expected = array(
    133136                        self::$site_ids['test.example.org/'],
     137                        self::$site_ids['test2.example.org/'],
     138                        self::$site_ids['test3.example.org/zig/'],
     139                        self::$site_ids['atest.example.org/'],
    134140                );
    135141
    136142                $this->assertEqualSets( $expected, $items );
     
    154160                        self::$site_ids['test.example.org/'],
    155161                        self::$site_ids['test2.example.org/'],
    156162                        self::$site_ids['test3.example.org/zig/'],
     163                        self::$site_ids['atest.example.org/'],
    157164                );
    158165
    159166                $this->assertEqualSets( $expected, $items );
     
    176183                $expected = array(
    177184                        self::$site_ids['wordpress.org/foo/'],
    178185                        self::$site_ids['wordpress.org/foo/bar/'],
     186                        self::$site_ids['wordpress.org/afoo/'],
    179187                        self::$site_ids['make.wordpress.org/foo/'],
    180188                        self::$site_ids['www.w.org/foo/'],
    181189                        self::$site_ids['www.w.org/foo/bar/'],