Introduce wp_get_sites(), a long-awaited replacement for get_blog_list().

props jeremyfelt.
see #14511.


git-svn-id: https://develop.svn.wordpress.org/trunk@25445 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2013-09-14 21:12:26 +00:00
parent a2892990f1
commit eb2e3536a9
3 changed files with 112 additions and 1 deletions

View File

@ -161,7 +161,7 @@ function validate_email( $email, $check_domain = true) {
* @deprecated No alternative available. For performance reasons this function is not recommended.
*/
function get_blog_list( $start = 0, $num = 10, $deprecated = '' ) {
_deprecated_function( __FUNCTION__, '3.0' );
_deprecated_function( __FUNCTION__, '3.0', 'wp_get_sites()' );
global $wpdb;
$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 );

View File

@ -1987,3 +1987,75 @@ function wp_is_large_network( $using = 'sites' ) {
$count = get_blog_count();
return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count );
}
/**
* 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;
}

View File

@ -1029,6 +1029,45 @@ class Tests_MS extends WP_UnitTestCase {
$this->assertFalse( is_user_spammy( 'testuser1' ) );
}
/**
* @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 ) ) );
}
}
endif;