<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[37736] trunk: Multisite: Use `WP_Site_Query` to power `WP_MS_Sites_List_Table`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/37736">37736</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/37736","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jeremyfelt</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-06-17 00:02:05 +0000 (Fri, 17 Jun 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/33185">#33185</a>, <a href="https://core.trac.wordpress.org/ticket/24833">#24833</a>, <a href="https://core.trac.wordpress.org/ticket/21837">#21837</a>, <a href="https://core.trac.wordpress.org/ticket/36675">#36675</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasswpmssiteslisttablephp">trunk/src/wp-admin/includes/class-wp-ms-sites-list-table.php</a></li>
<li><a href="#trunktestsphpunittestsmultisitewpMSSitesListTablephp">trunk/tests/phpunit/tests/multisite/wpMSSitesListTable.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasswpmssiteslisttablephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-wp-ms-sites-list-table.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-ms-sites-list-table.php 2016-06-16 23:08:30 UTC (rev 37735)
+++ trunk/src/wp-admin/includes/class-wp-ms-sites-list-table.php 2016-06-17 00:02:05 UTC (rev 37736)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -67,8 +67,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function prepare_items() {
</span><span class="cx" style="display: block; padding: 0 10px"> global $s, $mode, $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $current_site = get_current_site();
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $_REQUEST['mode'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
</span><span class="cx" style="display: block; padding: 0 10px"> set_user_setting( 'sites_list_mode', $mode );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -83,7 +81,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
</span><span class="cx" style="display: block; padding: 0 10px"> $wild = '';
</span><span class="cx" style="display: block; padding: 0 10px"> if ( false !== strpos($s, '*') ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wild = '%';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wild = '*';
</ins><span class="cx" style="display: block; padding: 0 10px"> $s = trim($s, '*');
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -98,7 +96,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $_GET['order'] = $_REQUEST['order'] = 'DESC';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $args = array(
+ 'number' => intval( $per_page ),
+ 'offset' => intval( ( $pagenum - 1 ) * $per_page ),
+ 'network_id' => get_current_network_id(),
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty($s) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // Nothing to do.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -107,67 +109,66 @@
</span><span class="cx" style="display: block; padding: 0 10px"> preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
</span><span class="cx" style="display: block; padding: 0 10px"> preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // IPv4 address
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . $wild );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $reg_blog_ids = $wpdb->get_col( $sql );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( !$reg_blog_ids )
- $reg_blog_ids = array( 0 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $reg_blog_ids ) {
+ $args['site__in'] = $reg_blog_ids;
+ }
+ } elseif ( is_numeric( $s ) && empty( $wild ) ) {
+ $args['ID'] = $s;
+ } else {
+ $args['search'] = $s;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query = "SELECT *
- FROM {$wpdb->blogs}
- WHERE site_id = '{$wpdb->siteid}'
- AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")";
- } 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 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! is_subdomain_install() ) {
+ $args['search_columns'] = array( 'path' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $order_by === 'registered' ) {
- $query .= ' ORDER BY registered ';
- } elseif ( $order_by === 'lastupdated' ) {
- $query .= ' ORDER BY last_updated ';
- } elseif ( $order_by === 'blogname' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'registered' === $order_by ) {
+ // registered is a valid field name.
+ } elseif ( 'lastupdated' === $order_by ) {
+ $order_by = 'last_updated';
+ } elseif ( 'blogname' === $order_by ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_subdomain_install() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query .= ' ORDER BY domain ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $order_by = 'domain';
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query .= ' ORDER BY path ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $order_by = 'path';
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } elseif ( $order_by === 'blog_id' ) {
- $query .= ' ORDER BY blog_id ';
- } else {
- $order_by = null;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( 'blog_id' === $order_by ) {
+ $order_by = 'id';
+ } elseif ( ! $order_by ) {
+ $order_by = false;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $order_by ) ) {
- $order = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
- $query .= $order;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $args['orderby'] = $order_by;
+
+ if ( $order_by ) {
+ $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // 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 ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( wp_is_large_network() ) {
+ $args['no_found_rows'] = true;
+ } else {
+ $args['no_found_rows'] = false;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query .= " LIMIT " . intval( ( $pagenum - 1 ) * $per_page ) . ", " . intval( $per_page );
- $this->items = $wpdb->get_results( $query, ARRAY_A );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $_sites = get_sites( $args );
+ if ( is_array( $_sites ) ) {
+ update_site_cache( $_sites );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( wp_is_large_network() )
- $total = count($this->items);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->items = array_slice( $_sites, 0, $per_page );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $total_sites = get_sites( array_merge( $args, array(
+ 'count' => true,
+ 'offset' => 0,
+ 'number' => 0,
+ ) ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->set_pagination_args( array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'total_items' => $total,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'total_items' => $total_sites,
</ins><span class="cx" style="display: block; padding: 0 10px"> 'per_page' => $per_page,
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -445,6 +446,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function display_rows() {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $this->items as $blog ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $blog = $blog->to_array();
</ins><span class="cx" style="display: block; padding: 0 10px"> $class = '';
</span><span class="cx" style="display: block; padding: 0 10px"> reset( $this->status_list );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsmultisitewpMSSitesListTablephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/multisite/wpMSSitesListTable.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/multisite/wpMSSitesListTable.php 2016-06-16 23:08:30 UTC (rev 37735)
+++ trunk/tests/phpunit/tests/multisite/wpMSSitesListTable.php 2016-06-17 00:02:05 UTC (rev 37736)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -73,8 +73,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = array(
</span><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['wordpress.org/foo/'],
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$site_ids['wordpress.org/foo/bar/'],
+ self::$site_ids['wordpress.org/afoo/'],
</ins><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['make.wordpress.org/foo/'],
</span><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['www.w.org/foo/'],
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$site_ids['www.w.org/foo/bar/'],
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEqualSets( $expected, $items );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -131,6 +134,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = array(
</span><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['test.example.org/'],
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$site_ids['test2.example.org/'],
+ self::$site_ids['test3.example.org/zig/'],
+ self::$site_ids['atest.example.org/'],
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEqualSets( $expected, $items );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -154,6 +160,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['test.example.org/'],
</span><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['test2.example.org/'],
</span><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['test3.example.org/zig/'],
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$site_ids['atest.example.org/'],
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEqualSets( $expected, $items );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -176,6 +183,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = array(
</span><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['wordpress.org/foo/'],
</span><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['wordpress.org/foo/bar/'],
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$site_ids['wordpress.org/afoo/'],
</ins><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['make.wordpress.org/foo/'],
</span><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['www.w.org/foo/'],
</span><span class="cx" style="display: block; padding: 0 10px"> self::$site_ids['www.w.org/foo/bar/'],
</span></span></pre>
</div>
</div>
</body>
</html>