WordPress.org

Make WordPress Core


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

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

props jeremyfelt.
see #14511.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.