<!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>[25445] trunk: Introduce wp_get_sites(), a long-awaited replacement for get_blog_list().</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">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/25445">25445</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2013-09-14 21:12:26 +0000 (Sat, 14 Sep 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Introduce wp_get_sites(), a long-awaited replacement for get_blog_list().
props jeremyfelt.
see <a href="http://core.trac.wordpress.org/ticket/14511">#14511</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmsdeprecatedphp">trunk/src/wp-includes/ms-deprecated.php</a></li>
<li><a href="#trunksrcwpincludesmsfunctionsphp">trunk/src/wp-includes/ms-functions.php</a></li>
<li><a href="#trunktestsphpunittestsmsphp">trunk/tests/phpunit/tests/ms.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesmsdeprecatedphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/ms-deprecated.php (25444 => 25445)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/ms-deprecated.php 2013-09-14 20:44:26 UTC (rev 25444)
+++ trunk/src/wp-includes/ms-deprecated.php 2013-09-14 21:12:26 UTC (rev 25445)
</span><span class="lines">@@ -161,7 +161,7 @@
</span><span class="cx"> * @deprecated No alternative available. For performance reasons this function is not recommended.
</span><span class="cx"> */
</span><span class="cx"> function get_blog_list( $start = 0, $num = 10, $deprecated = '' ) {
</span><del>- _deprecated_function( __FUNCTION__, '3.0' );
</del><ins>+ _deprecated_function( __FUNCTION__, '3.0', 'wp_get_sites()' );
</ins><span class="cx">
</span><span class="cx"> global $wpdb;
</span><span class="cx"> $blogs = $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $wpdb->siteid), ARRAY_A );
</span></span></pre></div>
<a id="trunksrcwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/ms-functions.php (25444 => 25445)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/ms-functions.php 2013-09-14 20:44:26 UTC (rev 25444)
+++ trunk/src/wp-includes/ms-functions.php 2013-09-14 21:12:26 UTC (rev 25445)
</span><span class="lines">@@ -1987,3 +1987,75 @@
</span><span class="cx"> $count = get_blog_count();
</span><span class="cx"> return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count );
</span><span class="cx"> }
</span><ins>+
+
+/**
+ * Return an array of sites for a network.
+ *
+ * @see wp_is_large_network() Returns an empty array if the install is considered large.
+ *
+ * @since 3.7.0
+ *
+ * @param array $args {
+ * Array of arguments. Optional.
+ *
+ * @type int|array 'network_id' A network ID or array of network IDs. Set to null to retrieve sites
+ * from all networks. Defaults to current network ID.
+ * @type int 'public' Retrieve public or non-public sites. Default null, for any.
+ * @type int 'archived' Retrieve archived or non-archived sites. Default null, for any.
+ * @type int 'mature' Retrieve mature or non-mature sites. Default null, for any.
+ * @type int 'spam' Retrieve spam or non-spam sites. Default null, for any.
+ * @type int 'deleted' Retrieve deleted or non-deleted sites. Default null, for any.
+ * @type int 'limit' Number of sites to limit the query to. Default 100.
+ * }
+ *
+ * @return array An array of site data
+ */
+function wp_get_sites( $args = array() ) {
+ global $wpdb;
+
+ if ( wp_is_large_network() )
+ return array();
+
+ $defaults = array(
+ 'network_id' => $wpdb->siteid,
+ 'public' => null,
+ 'archived' => null,
+ 'mature' => null,
+ 'spam' => null,
+ 'deleted' => null,
+ 'limit' => 100,
+ );
+
+ $args = wp_parse_args( $args, $defaults );
+
+ $query = "SELECT * FROM $wpdb->blogs WHERE 1=1 ";
+
+ if ( isset( $args['network_id'] ) && ( is_array( $args['network_id'] ) || is_numeric( $args['network_id'] ) ) ) {
+ $network_ids = array_map('intval', (array) $args['network_id'] );
+ $network_ids = implode( ',', $network_ids );
+ $query .= "AND site_id IN ($network_ids) ";
+ }
+
+ if ( isset( $args['public'] ) )
+ $query .= $wpdb->prepare( "AND public = %d ", $args['public'] );
+
+ if ( isset( $args['archived'] ) )
+ $query .= $wpdb->prepare( "AND archived = %d ", $args['archived'] );
+
+ if ( isset( $args['mature'] ) )
+ $query .= $wpdb->prepare( "AND mature = %d ", $args['mature'] );
+
+ if ( isset( $args['spam'] ) )
+ $query .= $wpdb->prepare( "AND spam = %d ", $args['spam'] );
+
+ if ( isset( $args['deleted'] ) )
+ $query .= $wpdb->prepare( "AND deleted = %d ", $args['deleted'] );
+
+ if ( isset( $args['limit'] ) )
+ $query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] );
+
+ $site_results = $wpdb->get_results( $query, ARRAY_A );
+
+ return $site_results;
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsmsphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/ms.php (25444 => 25445)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/ms.php 2013-09-14 20:44:26 UTC (rev 25444)
+++ trunk/tests/phpunit/tests/ms.php 2013-09-14 21:12:26 UTC (rev 25445)
</span><span class="lines">@@ -1029,6 +1029,45 @@
</span><span class="cx"> $this->assertFalse( is_user_spammy( 'testuser1' ) );
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ /**
+ * @ticket 14511
+ */
+ function test_wp_get_sites() {
+ global $wpdb;
+ $this->factory->blog->create_many( 2, array( 'site_id' => 2, 'meta' => array( 'public' => 1 ) ) );
+ $this->factory->blog->create_many( 3, array( 'site_id' => 3, 'meta' => array( 'public' => 0 ) ) );
+
+ // Expect no sites when passed an invalid network_id
+ $this->assertCount( 0, wp_get_sites( array( 'network_id' => 0 ) ) );
+ $this->assertCount( 0, wp_get_sites( array( 'network_id' => 4 ) ) );
+
+ // Expect 1 site when no network_id is specified - defaults to current network.
+ $this->assertCount( 1, wp_get_sites() );
+ // Expect 6 sites when network_id = null.
+ $this->assertCount( 6, wp_get_sites( array( 'network_id' => null ) ) );
+
+ // Expect 1 site with a network_id of 1, 2 for network_id 2, 3 for 3
+ $this->assertCount( 1, wp_get_sites( array( 'network_id' => 1 ) ) );
+ $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2 ) ) );
+ $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3 ) ) );
+
+ // Expect 6 sites when public is null (across all networks)
+ $this->assertCount( 6, wp_get_sites( array( 'public' => null, 'network_id' => null ) ) );
+
+ // Expect 3 sites when public is 1
+ $this->assertCount( 3, wp_get_sites( array( 'public' => 1, 'network_id' => null ) ) );
+
+ // Expect 2 sites when public is 1 and network_id is 2
+ $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2, 'public' => 1 ) ) );
+
+ // Expect no sites when public is set to 0 and network_id is not 3
+ $this->assertCount( 0, wp_get_sites( array( 'network_id' => 1, 'public' => 0 ) ) );
+
+ // Test public + network_id = 3
+ $this->assertCount( 0, wp_get_sites( array( 'network_id' => 3, 'public' => 1 ) ) );
+ $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3, 'public' => 0 ) ) );
+ }
+
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> endif;
</span></span></pre>
</div>
</div>
</body>
</html>