Skip to content

Commit

Permalink
Networks and Sites: Use metadata api in `*_network_options functions.
Browse files Browse the repository at this point in the history
Replace logic found in `get_network_option`, `update_network_option` and `delete_network_option` to use the metadata api. Using the metadata api has a number of benefits, such as consistency, default values and useful filters. This change also improves performance by priming the caches of all network options in a single database request. 

Props spacedmonkey, swissspidy, sc0ttkclark, johnjamesjacoby, flixos90, jeremyfelt, pento, peterwilsoncc, mukesh27, desrosj.
Fixes #37181
Built from https://develop.svn.wordpress.org/trunk@54080


git-svn-id: http://core.svn.wordpress.org/trunk@53639 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
spacedmonkey committed Sep 6, 2022
1 parent 4a5f783 commit 8d1156f
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 198 deletions.
87 changes: 45 additions & 42 deletions wp-includes/class-wp-network-query.php
Expand Up @@ -89,49 +89,52 @@ class WP_Network_Query {
* @param string|array $query {
* Optional. Array or query string of network query parameters. Default empty.
*
* @type int[] $network__in Array of network IDs to include. Default empty.
* @type int[] $network__not_in Array of network IDs to exclude. Default empty.
* @type bool $count Whether to return a network count (true) or array of network objects.
* Default false.
* @type string $fields Network fields to return. Accepts 'ids' (returns an array of network IDs)
* or empty (returns an array of complete network objects). Default empty.
* @type int $number Maximum number of networks to retrieve. Default empty (no limit).
* @type int $offset Number of networks to offset the query. Used to build LIMIT clause.
* Default 0.
* @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
* @type string|array $orderby Network status or array of statuses. Accepts 'id', 'domain', 'path',
* 'domain_length', 'path_length' and 'network__in'. Also accepts false,
* an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
* @type string $order How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
* @type string $domain Limit results to those affiliated with a given domain. Default empty.
* @type string[] $domain__in Array of domains to include affiliated networks for. Default empty.
* @type string[] $domain__not_in Array of domains to exclude affiliated networks for. Default empty.
* @type string $path Limit results to those affiliated with a given path. Default empty.
* @type string[] $path__in Array of paths to include affiliated networks for. Default empty.
* @type string[] $path__not_in Array of paths to exclude affiliated networks for. Default empty.
* @type string $search Search term(s) to retrieve matching networks for. Default empty.
* @type bool $update_network_cache Whether to prime the cache for found networks. Default true.
* @type int[] $network__in Array of network IDs to include. Default empty.
* @type int[] $network__not_in Array of network IDs to exclude. Default empty.
* @type bool $count Whether to return a network count (true) or array of network objects.
* Default false.
* @type string $fields Network fields to return. Accepts 'ids' (returns an array of network IDs)
* or empty (returns an array of complete network objects). Default empty.
* @type int $number Maximum number of networks to retrieve. Default empty (no limit).
* @type int $offset Number of networks to offset the query. Used to build LIMIT clause.
* Default 0.
* @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
* @type string|array $orderby Network status or array of statuses. Accepts 'id', 'domain', 'path',
* 'domain_length', 'path_length' and 'network__in'. Also accepts false,
* an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
* @type string $order How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
* @type string $domain Limit results to those affiliated with a given domain. Default empty.
* @type string[] $domain__in Array of domains to include affiliated networks for. Default empty.
* @type string[] $domain__not_in Array of domains to exclude affiliated networks for. Default empty.
* @type string $path Limit results to those affiliated with a given path. Default empty.
* @type string[] $path__in Array of paths to include affiliated networks for. Default empty.
* @type string[] $path__not_in Array of paths to exclude affiliated networks for. Default empty.
* @type string $search Search term(s) to retrieve matching networks for. Default empty.
* @type bool $update_network_cache Whether to prime the cache for found networks. Default true.
* @type bool $update_network_meta_cache Whether to prime the metadata (option) cache for found networks.
* Default true.
* }
*/
public function __construct( $query = '' ) {
$this->query_var_defaults = array(
'network__in' => '',
'network__not_in' => '',
'count' => false,
'fields' => '',
'number' => '',
'offset' => '',
'no_found_rows' => true,
'orderby' => 'id',
'order' => 'ASC',
'domain' => '',
'domain__in' => '',
'domain__not_in' => '',
'path' => '',
'path__in' => '',
'path__not_in' => '',
'search' => '',
'update_network_cache' => true,
'network__in' => '',
'network__not_in' => '',
'count' => false,
'fields' => '',
'number' => '',
'offset' => '',
'no_found_rows' => true,
'orderby' => 'id',
'order' => 'ASC',
'domain' => '',
'domain__in' => '',
'domain__not_in' => '',
'path' => '',
'path__in' => '',
'path__not_in' => '',
'search' => '',
'update_network_cache' => true,
'update_network_meta_cache' => true,
);

if ( ! empty( $query ) ) {
Expand Down Expand Up @@ -242,8 +245,8 @@ public function get_networks() {
// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );

// Ignore the $fields, $update_network_cache arguments as the queried result will be the same regardless.
unset( $_args['fields'], $_args['update_network_cache'] );
// Ignore the $fields, $update_network_cache, and $update_network_meta_cache arguments as the queried result will be the same regardless.
unset( $_args['fields'], $_args['update_network_cache'], $_args['update_network_meta_cache'] );

$key = md5( serialize( $_args ) );
$last_changed = wp_cache_get_last_changed( 'networks' );
Expand Down Expand Up @@ -285,7 +288,7 @@ public function get_networks() {
}

if ( $this->query_vars['update_network_cache'] ) {
_prime_network_caches( $network_ids );
_prime_network_caches( $network_ids, $this->query_vars['update_network_meta_cache'] );
}

// Fetch full network objects from the primed cache.
Expand Down
1 change: 1 addition & 0 deletions wp-includes/load.php
Expand Up @@ -743,6 +743,7 @@ function wp_start_object_cache() {
'site-details',
'site-options',
'site-transient',
'site_meta',
'rss',
'users',
'useremail',
Expand Down
2 changes: 2 additions & 0 deletions wp-includes/ms-blogs.php
Expand Up @@ -564,6 +564,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) {
'site-details',
'site-options',
'site-transient',
'site_meta',
'rss',
'users',
'useremail',
Expand Down Expand Up @@ -655,6 +656,7 @@ function restore_current_blog() {
'site-details',
'site-options',
'site-transient',
'site_meta',
'rss',
'users',
'useremail',
Expand Down
4 changes: 2 additions & 2 deletions wp-includes/ms-functions.php
Expand Up @@ -113,7 +113,7 @@ function get_active_blog_for_user( $user_id ) {
* @return int Number of active sites on the network.
*/
function get_blog_count( $network_id = null ) {
return get_network_option( $network_id, 'blog_count' );
return (int) get_network_option( $network_id, 'blog_count' );
}

/**
Expand Down Expand Up @@ -2654,7 +2654,7 @@ function get_space_allowed() {
*
* @param int $space_allowed Upload quota in megabytes for the current blog.
*/
return apply_filters( 'get_space_allowed', $space_allowed );
return (int) apply_filters( 'get_space_allowed', $space_allowed );
}

/**
Expand Down
20 changes: 15 additions & 5 deletions wp-includes/ms-network.php
Expand Up @@ -84,6 +84,7 @@ function clean_network_cache( $ids ) {

$network_ids = (array) $ids;
wp_cache_delete_multiple( $network_ids, 'networks' );
wp_cache_delete_multiple( $network_ids, 'site_meta' );

foreach ( $network_ids as $id ) {
/**
Expand All @@ -107,35 +108,44 @@ function clean_network_cache( $ids ) {
* cache using the network group with the key using the ID of the networks.
*
* @since 4.6.0
* @since 6.1.0 Introduced the `$update_meta_cache` parameter.
*
* @param array $networks Array of network row objects.
* @param array $networks Array of network row objects.
* @param bool $update_meta_cache Whether to update site meta cache. Default true.
*/
function update_network_cache( $networks ) {
function update_network_cache( $networks, $update_meta_cache = true ) {
$data = array();
foreach ( (array) $networks as $network ) {
$data[ $network->id ] = $network;
}

wp_cache_add_multiple( $data, 'networks' );
if ( $update_meta_cache ) {
$network_ids = array_keys( $data );
update_meta_cache( 'site', $network_ids );
}
}

/**
* Adds any networks from the given IDs to the cache that do not already exist in cache.
*
* @since 4.6.0
* @since 6.1.0 Introduced the `$update_meta_cache` parameter.
* @since 6.1.0 This function is no longer marked as "private".
*
* @see update_network_cache()
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $network_ids Array of network IDs.
* @param array $network_ids Array of network IDs.
* @param bool $update_meta_cache Whether to update site meta cache. Default true.
*/
function _prime_network_caches( $network_ids ) {
function _prime_network_caches( $network_ids, $update_meta_cache = true ) {
global $wpdb;

$non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

update_network_cache( $fresh_networks );
update_network_cache( $fresh_networks, $update_meta_cache );
}
}

0 comments on commit 8d1156f

Please sign in to comment.