Make WordPress Core


Ignore:
Timestamp:
09/06/2022 11:26:45 AM (2 years ago)
Author:
spacedmonkey
Message:

Networks and Sites: Use metadata api in *_network_options` functions.

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

File:
1 edited

Legend:

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

    r53944 r54080  
    8585    $network_ids = (array) $ids;
    8686    wp_cache_delete_multiple( $network_ids, 'networks' );
     87    wp_cache_delete_multiple( $network_ids, 'site_meta' );
    8788
    8889    foreach ( $network_ids as $id ) {
     
    108109 *
    109110 * @since 4.6.0
     111 * @since 6.1.0 Introduced the `$update_meta_cache` parameter.
    110112 *
    111  * @param array $networks Array of network row objects.
     113 * @param array $networks          Array of network row objects.
     114 * @param bool  $update_meta_cache Whether to update site meta cache. Default true.
    112115 */
    113 function update_network_cache( $networks ) {
     116function update_network_cache( $networks, $update_meta_cache = true ) {
    114117    $data = array();
    115118    foreach ( (array) $networks as $network ) {
    116119        $data[ $network->id ] = $network;
    117120    }
     121
    118122    wp_cache_add_multiple( $data, 'networks' );
     123    if ( $update_meta_cache ) {
     124        $network_ids = array_keys( $data );
     125        update_meta_cache( 'site', $network_ids );
     126    }
    119127}
    120128
     
    123131 *
    124132 * @since 4.6.0
     133 * @since 6.1.0 Introduced the `$update_meta_cache` parameter.
    125134 * @since 6.1.0 This function is no longer marked as "private".
    126135 *
     
    128137 * @global wpdb $wpdb WordPress database abstraction object.
    129138 *
    130  * @param array $network_ids Array of network IDs.
     139 * @param array $network_ids       Array of network IDs.
     140 * @param bool  $update_meta_cache Whether to update site meta cache. Default true.
    131141 */
    132 function _prime_network_caches( $network_ids ) {
     142function _prime_network_caches( $network_ids, $update_meta_cache = true ) {
    133143    global $wpdb;
    134144
     
    137147        $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
    138148
    139         update_network_cache( $fresh_networks );
     149        update_network_cache( $fresh_networks, $update_meta_cache );
    140150    }
    141151}
Note: See TracChangeset for help on using the changeset viewer.