WordPress.org

Make WordPress Core

Changeset 37468


Ignore:
Timestamp:
05/20/2016 04:40:39 AM (4 years ago)
Author:
jeremyfelt
Message:

Multisite: Introduce get_site()

Given a site ID or site object, get_site() retrieves site data in the same vein as get_post() or get_comment(). This will allow for clean retrieval of sites from a primed cache when WP_Site_Query is implemented.

Adds a WP_Site::to_array() method to support multiple return types within get_site().

Props spacedmonkey.
See #35791.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-site.php

    r36495 r37468  
    199199        }
    200200    }
     201
     202    /**
     203     * Converts an object to array.
     204     *
     205     * @since 4.6.0
     206     * @access public
     207     *
     208     * @return array Object as array.
     209     */
     210    public function to_array() {
     211        return get_object_vars( $this );
     212    }
    201213}
  • trunk/src/wp-includes/ms-blogs.php

    r37204 r37468  
    469469
    470470/**
     471 * Retrieves site data given a site ID or site object.
     472 *
     473 * Site data will be cached and returned after being passed through a filter.
     474 * If the provided site is empty, the current site global will be used.
     475 *
     476 * @since 4.6.0
     477 *
     478 * @global WP_Site $current_blog The current site.
     479 *
     480 * @param WP_Site|int $site   Site to retrieve.
     481 * @param string      $output Optional. Type of output to return. OBJECT or ARRAY_A or ARRAY_N constants.
     482 * @return WP_Site|array|null Depends on $output value.
     483 */
     484function get_site( &$site = null, $output = OBJECT ) {
     485    global $current_blog;
     486    if ( empty( $site ) && isset( $current_blog ) ) {
     487        $site = $current_blog;
     488    }
     489
     490    if ( $site instanceof WP_Site ) {
     491        $_site = $site;
     492    } elseif ( is_object( $site ) ) {
     493        $_site = new WP_Site( $site );
     494    } else {
     495        $_site = WP_Site::get_instance( $site );
     496    }
     497
     498    if ( ! $_site ) {
     499        return null;
     500    }
     501
     502    /**
     503     * Fires after a site is retrieved.
     504     *
     505     * @since 4.6.0
     506     *
     507     * @param mixed $_site Site data.
     508     */
     509    $_site = apply_filters( 'get_site', $_site );
     510
     511    if ( $output == OBJECT ) {
     512        return $_site;
     513    } elseif ( $output == ARRAY_A ) {
     514        return $_site->to_array();
     515    } elseif ( $output == ARRAY_N ) {
     516        return array_values( $_site->to_array() );
     517    }
     518
     519    return $_site;
     520}
     521
     522/**
    471523 * Retrieve option value for a given blog id based on name of option.
    472524 *
Note: See TracChangeset for help on using the changeset viewer.