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. git-svn-id: https://develop.svn.wordpress.org/trunk@37736 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
3d56328372
commit
96d7eadb0c
@ -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 );
|
||||
|
||||
|
@ -73,8 +73,11 @@ class Tests_WP_MS_Sites_List_Table extends WP_UnitTestCase {
|
||||
|
||||
$expected = array(
|
||||
self::$site_ids['wordpress.org/foo/'],
|
||||
self::$site_ids['wordpress.org/foo/bar/'],
|
||||
self::$site_ids['wordpress.org/afoo/'],
|
||||
self::$site_ids['make.wordpress.org/foo/'],
|
||||
self::$site_ids['www.w.org/foo/'],
|
||||
self::$site_ids['www.w.org/foo/bar/'],
|
||||
);
|
||||
|
||||
$this->assertEqualSets( $expected, $items );
|
||||
@ -131,6 +134,9 @@ class Tests_WP_MS_Sites_List_Table extends WP_UnitTestCase {
|
||||
|
||||
$expected = array(
|
||||
self::$site_ids['test.example.org/'],
|
||||
self::$site_ids['test2.example.org/'],
|
||||
self::$site_ids['test3.example.org/zig/'],
|
||||
self::$site_ids['atest.example.org/'],
|
||||
);
|
||||
|
||||
$this->assertEqualSets( $expected, $items );
|
||||
@ -154,6 +160,7 @@ class Tests_WP_MS_Sites_List_Table extends WP_UnitTestCase {
|
||||
self::$site_ids['test.example.org/'],
|
||||
self::$site_ids['test2.example.org/'],
|
||||
self::$site_ids['test3.example.org/zig/'],
|
||||
self::$site_ids['atest.example.org/'],
|
||||
);
|
||||
|
||||
$this->assertEqualSets( $expected, $items );
|
||||
@ -176,6 +183,7 @@ class Tests_WP_MS_Sites_List_Table extends WP_UnitTestCase {
|
||||
$expected = array(
|
||||
self::$site_ids['wordpress.org/foo/'],
|
||||
self::$site_ids['wordpress.org/foo/bar/'],
|
||||
self::$site_ids['wordpress.org/afoo/'],
|
||||
self::$site_ids['make.wordpress.org/foo/'],
|
||||
self::$site_ids['www.w.org/foo/'],
|
||||
self::$site_ids['www.w.org/foo/bar/'],
|
||||
|
Loading…
Reference in New Issue
Block a user