Make WordPress Core

Changeset 25445


Ignore:
Timestamp:
09/14/2013 09:12:26 PM (11 years ago)
Author:
nacin
Message:

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

props jeremyfelt.
see #14511.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-deprecated.php

    r25276 r25445  
    162162 */
    163163function get_blog_list( $start = 0, $num = 10, $deprecated = '' ) {
    164     _deprecated_function( __FUNCTION__, '3.0' );
     164    _deprecated_function( __FUNCTION__, '3.0', 'wp_get_sites()' );
    165165
    166166    global $wpdb;
  • trunk/src/wp-includes/ms-functions.php

    r25397 r25445  
    19881988    return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count );
    19891989}
     1990
     1991
     1992/**
     1993 * Return an array of sites for a network.
     1994 *
     1995 * @see wp_is_large_network() Returns an empty array if the install is considered large.
     1996 *
     1997 * @since 3.7.0
     1998 *
     1999 * @param array $args {
     2000 *     Array of arguments. Optional.
     2001 *
     2002 *     @type int|array 'network_id' A network ID or array of network IDs. Set to null to retrieve sites
     2003 *                                  from all networks. Defaults to current network ID.
     2004 *     @type int       'public'     Retrieve public or non-public sites. Default null, for any.
     2005 *     @type int       'archived'   Retrieve archived or non-archived sites. Default null, for any.
     2006 *     @type int       'mature'     Retrieve mature or non-mature sites. Default null, for any.
     2007 *     @type int       'spam'       Retrieve spam or non-spam sites. Default null, for any.
     2008 *     @type int       'deleted'    Retrieve deleted or non-deleted sites. Default null, for any.
     2009 *     @type int       'limit'      Number of sites to limit the query to. Default 100.
     2010 * }
     2011 *
     2012 * @return array An array of site data
     2013 */
     2014function wp_get_sites( $args = array() ) {
     2015    global $wpdb;
     2016
     2017    if ( wp_is_large_network() )
     2018        return array();
     2019
     2020    $defaults = array(
     2021        'network_id' => $wpdb->siteid,
     2022        'public'     => null,
     2023        'archived'   => null,
     2024        'mature'     => null,
     2025        'spam'       => null,
     2026        'deleted'    => null,
     2027        'limit'      => 100,
     2028    );
     2029
     2030    $args = wp_parse_args( $args, $defaults );
     2031
     2032    $query = "SELECT * FROM $wpdb->blogs WHERE 1=1 ";
     2033
     2034    if ( isset( $args['network_id'] ) && ( is_array( $args['network_id'] ) || is_numeric( $args['network_id'] ) ) ) {
     2035        $network_ids = array_map('intval', (array) $args['network_id'] );
     2036        $network_ids = implode( ',', $network_ids );
     2037        $query .= "AND site_id IN ($network_ids) ";
     2038    }
     2039
     2040    if ( isset( $args['public'] ) )
     2041        $query .= $wpdb->prepare( "AND public = %d ", $args['public'] );
     2042
     2043    if ( isset( $args['archived'] ) )
     2044        $query .= $wpdb->prepare( "AND archived = %d ", $args['archived'] );
     2045
     2046    if ( isset( $args['mature'] ) )
     2047        $query .= $wpdb->prepare( "AND mature = %d ", $args['mature'] );
     2048
     2049    if ( isset( $args['spam'] ) )
     2050        $query .= $wpdb->prepare( "AND spam = %d ", $args['spam'] );
     2051
     2052    if ( isset( $args['deleted'] ) )
     2053        $query .= $wpdb->prepare( "AND deleted = %d ", $args['deleted'] );
     2054
     2055    if ( isset( $args['limit'] ) )
     2056        $query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] );
     2057
     2058    $site_results = $wpdb->get_results( $query, ARRAY_A );
     2059
     2060    return $site_results;
     2061}
  • trunk/tests/phpunit/tests/ms.php

    r25409 r25445  
    10301030    }
    10311031
     1032    /**
     1033     * @ticket 14511
     1034     */
     1035    function test_wp_get_sites() {
     1036        global $wpdb;
     1037        $this->factory->blog->create_many( 2, array( 'site_id' => 2, 'meta' => array( 'public' => 1 ) ) );
     1038        $this->factory->blog->create_many( 3, array( 'site_id' => 3, 'meta' => array( 'public' => 0 ) ) );
     1039
     1040        // Expect no sites when passed an invalid network_id
     1041        $this->assertCount( 0, wp_get_sites( array( 'network_id' => 0 ) ) );
     1042        $this->assertCount( 0, wp_get_sites( array( 'network_id' => 4 ) ) );
     1043
     1044        // Expect 1 site when no network_id is specified - defaults to current network.
     1045        $this->assertCount( 1, wp_get_sites() );
     1046        // Expect 6 sites when network_id = null.
     1047        $this->assertCount( 6, wp_get_sites( array( 'network_id' => null ) ) );
     1048
     1049        // Expect 1 site with a network_id of 1, 2 for network_id 2, 3 for 3
     1050        $this->assertCount( 1, wp_get_sites( array( 'network_id' => 1 ) ) );
     1051        $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2 ) ) );
     1052        $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3 ) ) );
     1053
     1054        // Expect 6 sites when public is null (across all networks)
     1055        $this->assertCount( 6, wp_get_sites( array( 'public' => null, 'network_id' => null ) ) );
     1056
     1057        // Expect 3 sites when public is 1
     1058        $this->assertCount( 3, wp_get_sites( array( 'public' => 1, 'network_id' => null ) ) );
     1059
     1060        // Expect 2 sites when public is 1 and network_id is 2
     1061        $this->assertCount( 2, wp_get_sites( array( 'network_id' => 2, 'public' => 1 ) ) );
     1062
     1063        // Expect no sites when public is set to 0 and network_id is not 3
     1064        $this->assertCount( 0, wp_get_sites( array( 'network_id' => 1, 'public' => 0 ) ) );
     1065
     1066        // Test public + network_id = 3
     1067        $this->assertCount( 0, wp_get_sites( array( 'network_id' => 3, 'public' => 1 ) ) );
     1068        $this->assertCount( 3, wp_get_sites( array( 'network_id' => 3, 'public' => 0 ) ) );
     1069    }
     1070
    10321071}
    10331072
Note: See TracChangeset for help on using the changeset viewer.