Make WordPress Core


Ignore:
Timestamp:
09/06/2022 11:26:45 AM (4 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/class-wp-network-query.php

    r53376 r54080  
    9090         *     Optional. Array or query string of network query parameters. Default empty.
    9191         *
    92          *     @type int[]        $network__in          Array of network IDs to include. Default empty.
    93          *     @type int[]        $network__not_in      Array of network IDs to exclude. Default empty.
    94          *     @type bool         $count                Whether to return a network count (true) or array of network objects.
    95          *                                              Default false.
    96          *     @type string       $fields               Network fields to return. Accepts 'ids' (returns an array of network IDs)
    97          *                                              or empty (returns an array of complete network objects). Default empty.
    98          *     @type int          $number               Maximum number of networks to retrieve. Default empty (no limit).
    99          *     @type int          $offset               Number of networks to offset the query. Used to build LIMIT clause.
    100          *                                              Default 0.
    101          *     @type bool         $no_found_rows        Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
    102          *     @type string|array $orderby              Network status or array of statuses. Accepts 'id', 'domain', 'path',
    103          *                                              'domain_length', 'path_length' and 'network__in'. Also accepts false,
    104          *                                              an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
    105          *     @type string       $order                How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
    106          *     @type string       $domain               Limit results to those affiliated with a given domain. Default empty.
    107          *     @type string[]     $domain__in           Array of domains to include affiliated networks for. Default empty.
    108          *     @type string[]     $domain__not_in       Array of domains to exclude affiliated networks for. Default empty.
    109          *     @type string       $path                 Limit results to those affiliated with a given path. Default empty.
    110          *     @type string[]     $path__in             Array of paths to include affiliated networks for. Default empty.
    111          *     @type string[]     $path__not_in         Array of paths to exclude affiliated networks for. Default empty.
    112          *     @type string       $search               Search term(s) to retrieve matching networks for. Default empty.
    113          *     @type bool         $update_network_cache Whether to prime the cache for found networks. Default true.
     92         *     @type int[]        $network__in                Array of network IDs to include. Default empty.
     93         *     @type int[]        $network__not_in            Array of network IDs to exclude. Default empty.
     94         *     @type bool         $count                      Whether to return a network count (true) or array of network objects.
     95         *                                                    Default false.
     96         *     @type string       $fields                     Network fields to return. Accepts 'ids' (returns an array of network IDs)
     97         *                                                    or empty (returns an array of complete network objects). Default empty.
     98         *     @type int          $number                     Maximum number of networks to retrieve. Default empty (no limit).
     99         *     @type int          $offset                     Number of networks to offset the query. Used to build LIMIT clause.
     100         *                                                    Default 0.
     101         *     @type bool         $no_found_rows              Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
     102         *     @type string|array $orderby                    Network status or array of statuses. Accepts 'id', 'domain', 'path',
     103         *                                                    'domain_length', 'path_length' and 'network__in'. Also accepts false,
     104         *                                                    an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
     105         *     @type string       $order                      How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
     106         *     @type string       $domain                     Limit results to those affiliated with a given domain. Default empty.
     107         *     @type string[]     $domain__in                 Array of domains to include affiliated networks for. Default empty.
     108         *     @type string[]     $domain__not_in             Array of domains to exclude affiliated networks for. Default empty.
     109         *     @type string       $path                       Limit results to those affiliated with a given path. Default empty.
     110         *     @type string[]     $path__in                   Array of paths to include affiliated networks for. Default empty.
     111         *     @type string[]     $path__not_in               Array of paths to exclude affiliated networks for. Default empty.
     112         *     @type string       $search                     Search term(s) to retrieve matching networks for. Default empty.
     113         *     @type bool         $update_network_cache       Whether to prime the cache for found networks. Default true.
     114         *     @type bool         $update_network_meta_cache  Whether to prime the metadata (option) cache for found networks.
     115         *                                                    Default true.
    114116         * }
    115117         */
    116118        public function __construct( $query = '' ) {
    117119                $this->query_var_defaults = array(
    118                         'network__in'          => '',
    119                         'network__not_in'      => '',
    120                         'count'                => false,
    121                         'fields'               => '',
    122                         'number'               => '',
    123                         'offset'               => '',
    124                         'no_found_rows'        => true,
    125                         'orderby'              => 'id',
    126                         'order'                => 'ASC',
    127                         'domain'               => '',
    128                         'domain__in'           => '',
    129                         'domain__not_in'       => '',
    130                         'path'                 => '',
    131                         'path__in'             => '',
    132                         'path__not_in'         => '',
    133                         'search'               => '',
    134                         'update_network_cache' => true,
     120                        'network__in'               => '',
     121                        'network__not_in'           => '',
     122                        'count'                     => false,
     123                        'fields'                    => '',
     124                        'number'                    => '',
     125                        'offset'                    => '',
     126                        'no_found_rows'             => true,
     127                        'orderby'                   => 'id',
     128                        'order'                     => 'ASC',
     129                        'domain'                    => '',
     130                        'domain__in'                => '',
     131                        'domain__not_in'            => '',
     132                        'path'                      => '',
     133                        'path__in'                  => '',
     134                        'path__not_in'              => '',
     135                        'search'                    => '',
     136                        'update_network_cache'      => true,
     137                        'update_network_meta_cache' => true,
    135138                );
    136139
    … …  
    243246                $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    244247
    245                 // Ignore the $fields, $update_network_cache arguments as the queried result will be the same regardless.
    246                 unset( $_args['fields'], $_args['update_network_cache'] );
     248                // Ignore the $fields, $update_network_cache, and $update_network_meta_cache arguments as the queried result will be the same regardless.
     249                unset( $_args['fields'], $_args['update_network_cache'], $_args['update_network_meta_cache'] );
    247250
    248251                $key          = md5( serialize( $_args ) );
    … …  
    286289
    287290                if ( $this->query_vars['update_network_cache'] ) {
    288                         _prime_network_caches( $network_ids );
     291                        _prime_network_caches( $network_ids, $this->query_vars['update_network_meta_cache'] );
    289292                }
    290293
Note: See TracChangeset for help on using the changeset viewer.