Make WordPress Core


Ignore:
Timestamp:
06/17/2016 12:02:05 AM (10 years ago)
Author:
jeremyfelt
Message:

Multisite: Use WP_Site_Query to power WP_MS_Sites_List_Table.

WP_Site_Query provides for a cleaner prepare_items() method. It significantly improves the search experience in the sites list table:

  • In a subdomain configuration, domain and path are searched for a provided terms.
  • In a subdirectory configuration, path is searched for a provided term.
  • The full domain is searched in a subdomain configuration rather than the portion not matching the network's domain.
  • Terms are searched as %term% by default. Adding * in the middle of a term will search %te%rm%.

Props flixos90, Fab1en.
Fixes #33185, #24833, #21837, #36675.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-ms-sites-list-table.php

    r37488 r37736  
    6868                global $s, $mode, $wpdb;
    6969
    70                 $current_site = get_current_site();
    71 
    7270                if ( ! empty( $_REQUEST['mode'] ) ) {
    7371                        $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
     
    8482                $wild = '';
    8583                if ( false !== strpos($s, '*') ) {
    86                         $wild = '%';
     84                        $wild = '*';
    8785                        $s = trim($s, '*');
    8886                }
     
    9997                }
    10098
    101                 $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
     99                $args = array(
     100                        'number'     => intval( $per_page ),
     101                        'offset'     => intval( ( $pagenum - 1 ) * $per_page ),
     102                        'network_id' => get_current_network_id(),
     103                );
    102104
    103105                if ( empty($s) ) {
     
    108110                                        preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
    109111                        // 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 );
     112                        $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' ) );
    111113                        $reg_blog_ids = $wpdb->get_col( $sql );
    112114
    113                         if ( !$reg_blog_ids )
    114                                 $reg_blog_ids = array( 0 );
    115 
    116                         $query = "SELECT *
    117                                 FROM {$wpdb->blogs}
    118                                 WHERE site_id = '{$wpdb->siteid}'
    119                                 AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")";
     115                        if ( $reg_blog_ids ) {
     116                                $args['site__in'] = $reg_blog_ids;
     117                        }
     118                } elseif ( is_numeric( $s ) && empty( $wild ) ) {
     119                        $args['ID'] = $s;
    120120                } 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 );
     121                        $args['search'] = $s;
     122
     123                        if ( ! is_subdomain_install() ) {
     124                                $args['search_columns'] = array( 'path' );
     125                        }
     126                }
     127
     128                $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
     129                if ( 'registered' === $order_by ) {
     130                        // registered is a valid field name.
     131                } elseif ( 'lastupdated' === $order_by ) {
     132                        $order_by = 'last_updated';
     133                } elseif ( 'blogname' === $order_by ) {
     134                        if ( is_subdomain_install() ) {
     135                                $order_by = 'domain';
    127136                        } 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 );
    134                         }
    135                 }
    136 
    137                 $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' ) {
    143                         if ( is_subdomain_install() ) {
    144                                 $query .= ' ORDER BY domain ';
    145                         } else {
    146                                 $query .= ' ORDER BY path ';
    147                         }
    148                 } elseif ( $order_by === 'blog_id' ) {
    149                         $query .= ' ORDER BY blog_id ';
     137                                $order_by = 'path';
     138                        }
     139                } elseif ( 'blog_id' === $order_by ) {
     140                        $order_by = 'id';
     141                } elseif ( ! $order_by ) {
     142                        $order_by = false;
     143                }
     144
     145                $args['orderby'] = $order_by;
     146
     147                if ( $order_by ) {
     148                        $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
     149                }
     150
     151                if ( wp_is_large_network() ) {
     152                        $args['no_found_rows'] = true;
    150153                } else {
    151                         $order_by = null;
    152                 }
    153 
    154                 if ( isset( $order_by ) ) {
    155                         $order = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
    156                         $query .= $order;
    157                 }
    158 
    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 ) );
    162 
    163                 $query .= " LIMIT " . intval( ( $pagenum - 1 ) * $per_page ) . ", " . intval( $per_page );
    164                 $this->items = $wpdb->get_results( $query, ARRAY_A );
    165 
    166                 if ( wp_is_large_network() )
    167                         $total = count($this->items);
     154                        $args['no_found_rows'] = false;
     155                }
     156
     157                $_sites = get_sites( $args );
     158                if ( is_array( $_sites ) ) {
     159                        update_site_cache( $_sites );
     160
     161                        $this->items = array_slice( $_sites, 0, $per_page );
     162                }
     163
     164                $total_sites = get_sites( array_merge( $args, array(
     165                        'count' => true,
     166                        'offset' => 0,
     167                        'number' => 0,
     168                ) ) );
    168169
    169170                $this->set_pagination_args( array(
    170                         'total_items' => $total,
     171                        'total_items' => $total_sites,
    171172                        'per_page' => $per_page,
    172173                ) );
     
    446447        public function display_rows() {
    447448                foreach ( $this->items as $blog ) {
     449                        $blog = $blog->to_array();
    448450                        $class = '';
    449451                        reset( $this->status_list );
Note: See TracChangeset for help on using the changeset viewer.