Introduce 'offset' parameter for wp_get_sites().

props jamescollins.
see #14511.

git-svn-id: https://develop.svn.wordpress.org/trunk@25488 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2013-09-19 01:46:03 +00:00
parent ec7af133a5
commit a30670afc6
2 changed files with 31 additions and 3 deletions

View File

@ -2007,6 +2007,7 @@ function wp_is_large_network( $using = 'sites' ) {
* @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.
* @type int 'offset' Exclude the first x sites. Used in combination with the limit parameter. Default 0.
* }
*
* @return array An array of site data
@ -2025,6 +2026,7 @@ function wp_get_sites( $args = array() ) {
'spam' => null,
'deleted' => null,
'limit' => 100,
'offset' => 0,
);
$args = wp_parse_args( $args, $defaults );
@ -2051,8 +2053,12 @@ function wp_get_sites( $args = array() ) {
if ( isset( $args['deleted'] ) )
$query .= $wpdb->prepare( "AND deleted = %d ", $args['deleted'] );
if ( isset( $args['limit'] ) )
$query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] );
if ( isset( $args['limit'] ) && $args['limit'] ) {
if ( isset( $args['offset'] ) && $args['offset'] )
$query .= $wpdb->prepare( "LIMIT %d , %d ", $args['offset'], $args['limit'] );
else
$query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] );
}
$site_results = $wpdb->get_results( $query, ARRAY_A );

View File

@ -1033,7 +1033,6 @@ class Tests_MS extends WP_UnitTestCase {
* @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 ) ) );
@ -1068,6 +1067,29 @@ class Tests_MS extends WP_UnitTestCase {
$this->assertCount( 3, wp_get_sites( array( 'network_id' => 3, 'public' => 0 ) ) );
}
/**
* @ticket 14511
*/
function test_wp_get_sites_limit_offset() {
// Create 4 more sites (in addition to the default one)
$this->factory->blog->create_many( 4, array( 'meta' => array( 'public' => 1 ) ) );
// Expect all 5 sites when no limit/offset is specified
$this->assertCount( 5, wp_get_sites() );
// Expect first 2 sites when using limit
$this->assertCount( 2, wp_get_sites( array( 'limit' => 2 ) ) );
// Expect only the last 3 sites when using offset of 2 (limit will default to 100)
$this->assertCount( 3, wp_get_sites( array( 'offset' => 2 ) ) );
// Expect only the last 1 site when using offset of 4 and limit of 2
$this->assertCount( 1, wp_get_sites( array( 'limit' => 2, 'offset' => 4 ) ) );
// Expect 0 sites when using an offset larger than the number of sites
$this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) );
}
/**
* Test the 'archived' argument for wp_get_sites().
*