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.

Built from https://develop.svn.wordpress.org/trunk@37736


git-svn-id: http://core.svn.wordpress.org/trunk@37701 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Jeremy Felt 2016-06-17 00:03:29 +00:00
parent b4df848b6d
commit d242446e59
2 changed files with 51 additions and 49 deletions

View File

@ -67,8 +67,6 @@ class WP_MS_Sites_List_Table extends WP_List_Table {
public function prepare_items() {
global $s, $mode, $wpdb;
$current_site = get_current_site();
if ( ! empty( $_REQUEST['mode'] ) ) {
$mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
set_user_setting( 'sites_list_mode', $mode );
@ -83,7 +81,7 @@ class WP_MS_Sites_List_Table extends WP_List_Table {
$s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
$wild = '';
if ( false !== strpos($s, '*') ) {
$wild = '%';
$wild = '*';
$s = trim($s, '*');
}
@ -98,7 +96,11 @@ class WP_MS_Sites_List_Table extends WP_List_Table {
$_GET['order'] = $_REQUEST['order'] = 'DESC';
}
$query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
$args = array(
'number' => intval( $per_page ),
'offset' => intval( ( $pagenum - 1 ) * $per_page ),
'network_id' => get_current_network_id(),
);
if ( empty($s) ) {
// Nothing to do.
@ -107,67 +109,66 @@ class WP_MS_Sites_List_Table extends WP_List_Table {
preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
// IPv4 address
$sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . $wild );
$sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' ) );
$reg_blog_ids = $wpdb->get_col( $sql );
if ( !$reg_blog_ids )
$reg_blog_ids = array( 0 );
$query = "SELECT *
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")";
if ( $reg_blog_ids ) {
$args['site__in'] = $reg_blog_ids;
}
} elseif ( is_numeric( $s ) && empty( $wild ) ) {
$args['ID'] = $s;
} else {
if ( is_numeric($s) && empty( $wild ) ) {
$query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.blog_id = %s )", $s );
} elseif ( is_subdomain_install() ) {
$blog_s = str_replace( '.' . $current_site->domain, '', $s );
$blog_s = $wpdb->esc_like( $blog_s ) . $wild . $wpdb->esc_like( '.' . $current_site->domain );
$query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.domain LIKE %s ) ", $blog_s );
} else {
if ( $s != trim('/', $current_site->path) ) {
$blog_s = $wpdb->esc_like( $current_site->path . $s ) . $wild . $wpdb->esc_like( '/' );
} else {
$blog_s = $wpdb->esc_like( $s );
}
$query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.path LIKE %s )", $blog_s );
$args['search'] = $s;
if ( ! is_subdomain_install() ) {
$args['search_columns'] = array( 'path' );
}
}
$order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
if ( $order_by === 'registered' ) {
$query .= ' ORDER BY registered ';
} elseif ( $order_by === 'lastupdated' ) {
$query .= ' ORDER BY last_updated ';
} elseif ( $order_by === 'blogname' ) {
if ( 'registered' === $order_by ) {
// registered is a valid field name.
} elseif ( 'lastupdated' === $order_by ) {
$order_by = 'last_updated';
} elseif ( 'blogname' === $order_by ) {
if ( is_subdomain_install() ) {
$query .= ' ORDER BY domain ';
$order_by = 'domain';
} else {
$query .= ' ORDER BY path ';
$order_by = 'path';
}
} elseif ( $order_by === 'blog_id' ) {
$query .= ' ORDER BY blog_id ';
} elseif ( 'blog_id' === $order_by ) {
$order_by = 'id';
} elseif ( ! $order_by ) {
$order_by = false;
}
$args['orderby'] = $order_by;
if ( $order_by ) {
$args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
}
if ( wp_is_large_network() ) {
$args['no_found_rows'] = true;
} else {
$order_by = null;
$args['no_found_rows'] = false;
}
if ( isset( $order_by ) ) {
$order = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
$query .= $order;
$_sites = get_sites( $args );
if ( is_array( $_sites ) ) {
update_site_cache( $_sites );
$this->items = array_slice( $_sites, 0, $per_page );
}
// Don't do an unbounded count on large networks
if ( ! wp_is_large_network() )
$total = $wpdb->get_var( str_replace( 'SELECT *', 'SELECT COUNT( blog_id )', $query ) );
$query .= " LIMIT " . intval( ( $pagenum - 1 ) * $per_page ) . ", " . intval( $per_page );
$this->items = $wpdb->get_results( $query, ARRAY_A );
if ( wp_is_large_network() )
$total = count($this->items);
$total_sites = get_sites( array_merge( $args, array(
'count' => true,
'offset' => 0,
'number' => 0,
) ) );
$this->set_pagination_args( array(
'total_items' => $total,
'total_items' => $total_sites,
'per_page' => $per_page,
) );
}
@ -445,6 +446,7 @@ class WP_MS_Sites_List_Table extends WP_List_Table {
*/
public function display_rows() {
foreach ( $this->items as $blog ) {
$blog = $blog->to_array();
$class = '';
reset( $this->status_list );

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-alpha-37735';
$wp_version = '4.6-alpha-37736';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.