Make WordPress Core

Ticket #56845: 56845.diff

File 56845.diff, 35.3 KB (added by davidbaumwald, 4 years ago)

Revert patch

  • src/wp-includes/class-wp-network-query.php

     
    8686         * Sets up the network query, based on the query vars passed.
    8787         *
    8888         * @since 4.6.0
    89          * @since 6.1.0 Introduced the 'update_network_meta_cache' parameter.
    9089         *
    9190         * @param string|array $query {
    9291         *     Optional. Array or query string of network query parameters. Default empty.
    9392         *
    94          *     @type int[]        $network__in                Array of network IDs to include. Default empty.
    95          *     @type int[]        $network__not_in            Array of network IDs to exclude. Default empty.
    96          *     @type bool         $count                      Whether to return a network count (true) or array of network objects.
    97          *                                                    Default false.
    98          *     @type string       $fields                     Network fields to return. Accepts 'ids' (returns an array of network IDs)
    99          *                                                    or empty (returns an array of complete network objects). Default empty.
    100          *     @type int          $number                     Maximum number of networks to retrieve. Default empty (no limit).
    101          *     @type int          $offset                     Number of networks to offset the query. Used to build LIMIT clause.
    102          *                                                    Default 0.
    103          *     @type bool         $no_found_rows              Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
    104          *     @type string|array $orderby                    Network status or array of statuses. Accepts 'id', 'domain', 'path',
    105          *                                                    'domain_length', 'path_length' and 'network__in'. Also accepts false,
    106          *                                                    an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
    107          *     @type string       $order                      How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
    108          *     @type string       $domain                     Limit results to those affiliated with a given domain. Default empty.
    109          *     @type string[]     $domain__in                 Array of domains to include affiliated networks for. Default empty.
    110          *     @type string[]     $domain__not_in             Array of domains to exclude affiliated networks for. Default empty.
    111          *     @type string       $path                       Limit results to those affiliated with a given path. Default empty.
    112          *     @type string[]     $path__in                   Array of paths to include affiliated networks for. Default empty.
    113          *     @type string[]     $path__not_in               Array of paths to exclude affiliated networks for. Default empty.
    114          *     @type string       $search                     Search term(s) to retrieve matching networks for. Default empty.
    115          *     @type bool         $update_network_cache       Whether to prime the cache for found networks. Default true.
    116          *     @type bool         $update_network_meta_cache  Whether to prime the metadata (option) cache for found networks.
    117          *                                                    Default true.
     93         *     @type int[]        $network__in          Array of network IDs to include. Default empty.
     94         *     @type int[]        $network__not_in      Array of network IDs to exclude. Default empty.
     95         *     @type bool         $count                Whether to return a network count (true) or array of network objects.
     96         *                                              Default false.
     97         *     @type string       $fields               Network fields to return. Accepts 'ids' (returns an array of network IDs)
     98         *                                              or empty (returns an array of complete network objects). Default empty.
     99         *     @type int          $number               Maximum number of networks to retrieve. Default empty (no limit).
     100         *     @type int          $offset               Number of networks to offset the query. Used to build LIMIT clause.
     101         *                                              Default 0.
     102         *     @type bool         $no_found_rows        Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
     103         *     @type string|array $orderby              Network status or array of statuses. Accepts 'id', 'domain', 'path',
     104         *                                              'domain_length', 'path_length' and 'network__in'. Also accepts false,
     105         *                                              an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
     106         *     @type string       $order                How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
     107         *     @type string       $domain               Limit results to those affiliated with a given domain. Default empty.
     108         *     @type string[]     $domain__in           Array of domains to include affiliated networks for. Default empty.
     109         *     @type string[]     $domain__not_in       Array of domains to exclude affiliated networks for. Default empty.
     110         *     @type string       $path                 Limit results to those affiliated with a given path. Default empty.
     111         *     @type string[]     $path__in             Array of paths to include affiliated networks for. Default empty.
     112         *     @type string[]     $path__not_in         Array of paths to exclude affiliated networks for. Default empty.
     113         *     @type string       $search               Search term(s) to retrieve matching networks for. Default empty.
     114         *     @type bool         $update_network_cache Whether to prime the cache for found networks. Default true.
    118115         * }
    119116         */
    120117        public function __construct( $query = '' ) {
    121118                $this->query_var_defaults = array(
    122                         'network__in'               => '',
    123                         'network__not_in'           => '',
    124                         'count'                     => false,
    125                         'fields'                    => '',
    126                         'number'                    => '',
    127                         'offset'                    => '',
    128                         'no_found_rows'             => true,
    129                         'orderby'                   => 'id',
    130                         'order'                     => 'ASC',
    131                         'domain'                    => '',
    132                         'domain__in'                => '',
    133                         'domain__not_in'            => '',
    134                         'path'                      => '',
    135                         'path__in'                  => '',
    136                         'path__not_in'              => '',
    137                         'search'                    => '',
    138                         'update_network_cache'      => true,
    139                         'update_network_meta_cache' => true,
     119                        'network__in'          => '',
     120                        'network__not_in'      => '',
     121                        'count'                => false,
     122                        'fields'               => '',
     123                        'number'               => '',
     124                        'offset'               => '',
     125                        'no_found_rows'        => true,
     126                        'orderby'              => 'id',
     127                        'order'                => 'ASC',
     128                        'domain'               => '',
     129                        'domain__in'           => '',
     130                        'domain__not_in'       => '',
     131                        'path'                 => '',
     132                        'path__in'             => '',
     133                        'path__not_in'         => '',
     134                        'search'               => '',
     135                        'update_network_cache' => true,
    140136                );
    141137
    142138                if ( ! empty( $query ) ) {
     
    247243                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    248244                $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    249245
    250                 // Ignore these arguments, as the queried result will be the same regardless.
    251                 unset( $_args['fields'], $_args['update_network_cache'], $_args['update_network_meta_cache'] );
     246                // Ignore the $fields, $update_network_cache arguments as the queried result will be the same regardless.
     247                unset( $_args['fields'], $_args['update_network_cache'] );
    252248
    253249                $key          = md5( serialize( $_args ) );
    254250                $last_changed = wp_cache_get_last_changed( 'networks' );
     
    290286                }
    291287
    292288                if ( $this->query_vars['update_network_cache'] ) {
    293                         _prime_network_caches( $network_ids, $this->query_vars['update_network_meta_cache'] );
     289                        _prime_network_caches( $network_ids );
    294290                }
    295291
    296292                // Fetch full network objects from the primed cache.
  • src/wp-includes/class-wp-site-query.php

     
    351351                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    352352                $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    353353
    354                 // Ignore these arguments, as the queried result will be the same regardless.
     354                // Ignore the $fields, $update_site_cache, $update_site_meta_cache argument as the queried result will be the same regardless.
    355355                unset( $_args['fields'], $_args['update_site_cache'], $_args['update_site_meta_cache'] );
    356356
    357357                $key          = md5( serialize( $_args ) );
  • src/wp-includes/load.php

     
    743743                                'site-details',
    744744                                'site-options',
    745745                                'site-transient',
    746                                 'site_meta',
    747746                                'rss',
    748747                                'users',
    749748                                'useremail',
  • src/wp-includes/ms-blogs.php

     
    564564                                                'site-details',
    565565                                                'site-options',
    566566                                                'site-transient',
    567                                                 'site_meta',
    568567                                                'rss',
    569568                                                'users',
    570569                                                'useremail',
     
    656655                                                'site-details',
    657656                                                'site-options',
    658657                                                'site-transient',
    659                                                 'site_meta',
    660658                                                'rss',
    661659                                                'users',
    662660                                                'useremail',
  • src/wp-includes/ms-functions.php

     
    113113 * @return int Number of active sites on the network.
    114114 */
    115115function get_blog_count( $network_id = null ) {
    116         return (int) get_network_option( $network_id, 'blog_count' );
     116        return get_network_option( $network_id, 'blog_count' );
    117117}
    118118
    119119/**
     
    25632563         *
    25642564         * @param int $space_allowed Upload quota in megabytes for the current blog.
    25652565         */
    2566         return (int) apply_filters( 'get_space_allowed', $space_allowed );
     2566        return apply_filters( 'get_space_allowed', $space_allowed );
    25672567}
    25682568
    25692569/**
  • src/wp-includes/ms-network.php

     
    8484
    8585        $network_ids = (array) $ids;
    8686        wp_cache_delete_multiple( $network_ids, 'networks' );
    87         wp_cache_delete_multiple( $network_ids, 'site_meta' );
    8887
    8988        foreach ( $network_ids as $id ) {
    9089                /**
     
    108107 * cache using the network group with the key using the ID of the networks.
    109108 *
    110109 * @since 4.6.0
    111  * @since 6.1.0 Introduced the `$update_meta_cache` parameter.
    112110 *
    113  * @param array $networks          Array of network row objects.
    114  * @param bool  $update_meta_cache Whether to update site meta cache. Default true.
     111 * @param array $networks Array of network row objects.
    115112 */
    116 function update_network_cache( $networks, $update_meta_cache = true ) {
     113function update_network_cache( $networks ) {
    117114        $data = array();
    118115        foreach ( (array) $networks as $network ) {
    119116                $data[ $network->id ] = $network;
    120117        }
    121 
    122118        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         }
    127119}
    128120
    129121/**
     
    130122 * Adds any networks from the given IDs to the cache that do not already exist in cache.
    131123 *
    132124 * @since 4.6.0
    133  * @since 6.1.0 Introduced the `$update_meta_cache` parameter.
    134125 * @since 6.1.0 This function is no longer marked as "private".
    135126 *
    136127 * @see update_network_cache()
    137128 * @global wpdb $wpdb WordPress database abstraction object.
    138129 *
    139  * @param array $network_ids       Array of network IDs.
    140  * @param bool  $update_meta_cache Whether to update site meta cache. Default true.
     130 * @param array $network_ids Array of network IDs.
    141131 */
    142 function _prime_network_caches( $network_ids, $update_meta_cache = true ) {
     132function _prime_network_caches( $network_ids ) {
    143133        global $wpdb;
    144134
    145135        $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
     
    146136        if ( ! empty( $non_cached_ids ) ) {
    147137                $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
    148138
    149                 update_network_cache( $fresh_networks, $update_meta_cache );
     139                update_network_cache( $fresh_networks );
    150140        }
    151141}
  • src/wp-includes/option.php

     
    348348 * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
    349349 *
    350350 * @since 3.0.0
    351  * @since 6.1.0 Now uses update_meta_cache().
    352351 *
     352 * @global wpdb $wpdb WordPress database abstraction object.
     353 *
    353354 * @param int $network_id Optional site ID for which to query the options. Defaults to the current site.
    354355 */
    355356function wp_load_core_site_options( $network_id = null ) {
    356         if ( ! is_multisite() || wp_installing() ) {
     357        global $wpdb;
     358
     359        if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() ) {
    357360                return;
    358361        }
    359362
     
    361364                $network_id = get_current_network_id();
    362365        }
    363366
    364         update_meta_cache( 'site', $network_id );
     367        $core_options = array( 'site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled', 'ms_files_rewriting' );
     368
     369        $core_options_in = "'" . implode( "', '", $core_options ) . "'";
     370        $options         = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $network_id ) );
     371
     372        $data = array();
     373        foreach ( $options as $option ) {
     374                $key                = $option->meta_key;
     375                $cache_key          = "{$network_id}:$key";
     376                $option->meta_value = maybe_unserialize( $option->meta_value );
     377
     378                $data[ $cache_key ] = $option->meta_value;
     379        }
     380        wp_cache_set_multiple( $data, 'site-options' );
    365381}
    366382
    367383/**
     
    13701386 * Retrieves a network's option value based on the option name.
    13711387 *
    13721388 * @since 4.4.0
    1373  * @since 6.1.0 Now uses get_metadata().
    13741389 *
    13751390 * @see get_option()
    1376  * @see get_metadata()
    13771391 *
     1392 * @global wpdb $wpdb WordPress database abstraction object.
     1393 *
    13781394 * @param int    $network_id ID of the network. Can be null to default to the current network ID.
    13791395 * @param string $option     Name of the option to retrieve. Expected to not be SQL-escaped.
    13801396 * @param mixed  $default    Optional. Value to return if the option doesn't exist. Default false.
     
    13811397 * @return mixed Value set for the option.
    13821398 */
    13831399function get_network_option( $network_id, $option, $default = false ) {
     1400        global $wpdb;
     1401
    13841402        if ( $network_id && ! is_numeric( $network_id ) ) {
    13851403                return false;
    13861404        }
     
    14211439                return $pre;
    14221440        }
    14231441
    1424         if ( ! is_multisite() ) {
     1442        // Prevent non-existent options from triggering multiple queries.
     1443        $notoptions_key = "$network_id:notoptions";
     1444        $notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
     1445
     1446        if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
     1447
    14251448                /**
    14261449                 * Filters the value of a specific default network option.
    14271450                 *
     
    14361459                 * @param string $option     Option name.
    14371460                 * @param int    $network_id ID of the network.
    14381461                 */
    1439                 $default = apply_filters( "default_site_option_{$option}", $default, $option, $network_id );
     1462                return apply_filters( "default_site_option_{$option}", $default, $option, $network_id );
     1463        }
     1464
     1465        if ( ! is_multisite() ) {
     1466                /** This filter is documented in wp-includes/option.php */
     1467                $default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
    14401468                $value   = get_option( $option, $default );
    14411469        } else {
    1442                 $meta = get_metadata_raw( 'site', $network_id, $option );
    1443                 if ( is_array( $meta ) && ! empty( $meta ) ) {
    1444                         $value = array_shift( $meta );
    1445                 } else {
    1446                         /** This filter is documented in wp-includes/option.php */
    1447                         $value = apply_filters( "default_site_option_{$option}", $default, $option, $network_id );
     1470                $cache_key = "$network_id:$option";
     1471                $value     = wp_cache_get( $cache_key, 'site-options' );
    14481472
    1449                         /** This action is documented in wp-includes/meta.php */
    1450                         $value = apply_filters( 'default_site_metadata', $value, $network_id, $option, true, 'site' );
     1473                if ( ! isset( $value ) || false === $value ) {
     1474                        $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
     1475
     1476                        // Has to be get_row() instead of get_var() because of funkiness with 0, false, null values.
     1477                        if ( is_object( $row ) ) {
     1478                                $value = $row->meta_value;
     1479                                $value = maybe_unserialize( $value );
     1480                                wp_cache_set( $cache_key, $value, 'site-options' );
     1481                        } else {
     1482                                if ( ! is_array( $notoptions ) ) {
     1483                                        $notoptions = array();
     1484                                }
     1485
     1486                                $notoptions[ $option ] = true;
     1487                                wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
     1488
     1489                                /** This filter is documented in wp-includes/option.php */
     1490                                $value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
     1491                        }
    14511492                }
    14521493        }
    14531494
     1495        if ( ! is_array( $notoptions ) ) {
     1496                $notoptions = array();
     1497                wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
     1498        }
     1499
    14541500        /**
    14551501         * Filters the value of an existing network option.
    14561502         *
     
    14741520 * Existing options will not be updated.
    14751521 *
    14761522 * @since 4.4.0
    1477  * @since 6.1.0 Now uses add_metadata().
    14781523 *
    14791524 * @see add_option()
    1480  * @see add_metadata()
    14811525 *
     1526 * @global wpdb $wpdb WordPress database abstraction object.
     1527 *
    14821528 * @param int    $network_id ID of the network. Can be null to default to the current network ID.
    14831529 * @param string $option     Name of the option to add. Expected to not be SQL-escaped.
    14841530 * @param mixed  $value      Option value, can be anything. Expected to not be SQL-escaped.
     
    14851531 * @return bool True if the option was added, false otherwise.
    14861532 */
    14871533function add_network_option( $network_id, $option, $value ) {
     1534        global $wpdb;
     1535
    14881536        if ( $network_id && ! is_numeric( $network_id ) ) {
    14891537                return false;
    14901538        }
     
    15141562         */
    15151563        $value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id );
    15161564
     1565        $notoptions_key = "$network_id:notoptions";
     1566
    15171567        if ( ! is_multisite() ) {
    15181568                $result = add_option( $option, $value, '', 'no' );
    15191569        } else {
    1520                 $value  = sanitize_option( $option, $value );
    1521                 $result = add_metadata( 'site', $network_id, wp_slash( $option ), wp_slash( $value ), true );
     1570                $cache_key = "$network_id:$option";
     1571
     1572                // Make sure the option doesn't already exist.
     1573                // We can check the 'notoptions' cache before we ask for a DB query.
     1574                $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
     1575
     1576                if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
     1577                        if ( false !== get_network_option( $network_id, $option, false ) ) {
     1578                                return false;
     1579                        }
     1580                }
     1581
     1582                $value = sanitize_option( $option, $value );
     1583
     1584                $serialized_value = maybe_serialize( $value );
     1585                $result           = $wpdb->insert(
     1586                        $wpdb->sitemeta,
     1587                        array(
     1588                                'site_id'    => $network_id,
     1589                                'meta_key'   => $option,
     1590                                'meta_value' => $serialized_value,
     1591                        )
     1592                );
     1593
     1594                if ( ! $result ) {
     1595                        return false;
     1596                }
     1597
     1598                wp_cache_set( $cache_key, $value, 'site-options' );
     1599
     1600                // This option exists now.
     1601                $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // Yes, again... we need it to be fresh.
     1602
     1603                if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
     1604                        unset( $notoptions[ $option ] );
     1605                        wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
     1606                }
    15221607        }
    15231608
    15241609        if ( $result ) {
     
    15601645 * Removes a network option by name.
    15611646 *
    15621647 * @since 4.4.0
    1563  * @since 6.1.0 Now uses delete_metadata().
    15641648 *
    15651649 * @see delete_option()
    1566  * @see delete_metadata()
    15671650 *
    15681651 * @global wpdb $wpdb WordPress database abstraction object.
    15691652 *
     
    15721655 * @return bool True if the option was deleted, false otherwise.
    15731656 */
    15741657function delete_network_option( $network_id, $option ) {
     1658        global $wpdb;
     1659
    15751660        if ( $network_id && ! is_numeric( $network_id ) ) {
    15761661                return false;
    15771662        }
     
    16001685        if ( ! is_multisite() ) {
    16011686                $result = delete_option( $option );
    16021687        } else {
    1603                 $result = delete_metadata( 'site', $network_id, wp_slash( $option ), '' );
     1688                $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
     1689                if ( is_null( $row ) || ! $row->meta_id ) {
     1690                        return false;
     1691                }
     1692                $cache_key = "$network_id:$option";
     1693                wp_cache_delete( $cache_key, 'site-options' );
     1694
     1695                $result = $wpdb->delete(
     1696                        $wpdb->sitemeta,
     1697                        array(
     1698                                'meta_key' => $option,
     1699                                'site_id'  => $network_id,
     1700                        )
     1701                );
    16041702        }
    16051703
    16061704        if ( $result ) {
     
    16401738 * Updates the value of a network option that was already added.
    16411739 *
    16421740 * @since 4.4.0
    1643  * @since 6.1.0 Now uses update_metadata().
    16441741 *
    16451742 * @see update_option()
    1646  * @see update_metadata()
    16471743 *
     1744 * @global wpdb $wpdb WordPress database abstraction object.
     1745 *
    16481746 * @param int    $network_id ID of the network. Can be null to default to the current network ID.
    16491747 * @param string $option     Name of the option. Expected to not be SQL-escaped.
    16501748 * @param mixed  $value      Option value. Expected to not be SQL-escaped.
     
    16511749 * @return bool True if the value was updated, false otherwise.
    16521750 */
    16531751function update_network_option( $network_id, $option, $value ) {
     1752        global $wpdb;
     1753
    16541754        if ( $network_id && ! is_numeric( $network_id ) ) {
    16551755                return false;
    16561756        }
     
    17001800                return add_network_option( $network_id, $option, $value );
    17011801        }
    17021802
     1803        $notoptions_key = "$network_id:notoptions";
     1804        $notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
     1805
     1806        if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
     1807                unset( $notoptions[ $option ] );
     1808                wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
     1809        }
     1810
    17031811        if ( ! is_multisite() ) {
    17041812                $result = update_option( $option, $value, 'no' );
    17051813        } else {
    1706                 $value  = sanitize_option( $option, $value );
    1707                 $result = update_metadata( 'site', $network_id, wp_slash( $option ), wp_slash( $value ) );
     1814                $value = sanitize_option( $option, $value );
     1815
     1816                $serialized_value = maybe_serialize( $value );
     1817                $result           = $wpdb->update(
     1818                        $wpdb->sitemeta,
     1819                        array( 'meta_value' => $serialized_value ),
     1820                        array(
     1821                                'site_id'  => $network_id,
     1822                                'meta_key' => $option,
     1823                        )
     1824                );
     1825
     1826                if ( $result ) {
     1827                        $cache_key = "$network_id:$option";
     1828                        wp_cache_set( $cache_key, $value, 'site-options' );
     1829                }
    17081830        }
    17091831
    17101832        if ( $result ) {
  • tests/phpunit/tests/ajax/Compression.php

     
    154154                }
    155155
    156156                // Check the site option is not changed due to lack of nonce.
    157                 $this->assertSame( 0, (int) get_site_option( 'can_compress_scripts' ) );
     157                $this->assertSame( 0, get_site_option( 'can_compress_scripts' ) );
    158158
    159159                // Add a nonce.
    160160                $_GET['_ajax_nonce'] = wp_create_nonce( 'update_can_compress_scripts' );
     
    167167                }
    168168
    169169                // Check the site option is changed.
    170                 $this->assertSame( 1, (int) get_site_option( 'can_compress_scripts' ) );
     170                $this->assertSame( 1, get_site_option( 'can_compress_scripts' ) );
    171171        }
    172172
    173173        /**
     
    194194                }
    195195
    196196                // Check the site option is not changed due to lack of nonce.
    197                 $this->assertSame( 1, (int) get_site_option( 'can_compress_scripts' ) );
     197                $this->assertSame( 1, get_site_option( 'can_compress_scripts' ) );
    198198
    199199                // Add a nonce.
    200200                $_GET['_ajax_nonce'] = wp_create_nonce( 'update_can_compress_scripts' );
     
    207207                }
    208208
    209209                // Check the site option is changed.
    210                 $this->assertSame( 0, (int) get_site_option( 'can_compress_scripts' ) );
     210                $this->assertSame( 0, get_site_option( 'can_compress_scripts' ) );
    211211        }
    212212
    213213        /**
  • tests/phpunit/tests/option/multisite.php

     
    153153                }
    154154
    155155                /**
     156                 * @group multisite
     157                 *
     158                 * @covers ::get_site_option
     159                 */
     160                public function test_site_notoptions() {
     161                        $network_id     = get_current_network_id();
     162                        $notoptions_key = "{$network_id}:notoptions";
     163
     164                        $_notoptions = wp_cache_get( 'notoptions', 'site-options' );
     165                        $this->assertEmpty( $_notoptions );
     166                        $_notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );
     167                        $this->assertEmpty( $_notoptions1 );
     168
     169                        get_site_option( 'burrito' );
     170
     171                        $notoptions = wp_cache_get( 'notoptions', 'site-options' );
     172                        $this->assertEmpty( $notoptions );
     173                        $notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );
     174                        $this->assertNotEmpty( $notoptions1 );
     175                }
     176
     177                /**
    156178                 * @covers ::users_can_register_signup_filter
    157179                 * @covers ::get_site_option
    158180                 */
  • tests/phpunit/tests/option/networkOption.php

     
    137137        }
    138138
    139139        /**
    140          * @ticket 37181
    141          *
     140         * @ticket 43506
    142141         * @group ms-required
    143142         *
    144143         * @covers ::get_network_option
     
    145144         * @covers ::wp_cache_get
    146145         * @covers ::wp_cache_delete
    147146         */
    148         public function test_meta_api_use_values_in_network_option() {
    149                 $network_id = self::factory()->network->create();
    150                 $option     = __FUNCTION__;
    151                 $value      = __FUNCTION__;
     147        public function test_get_network_option_sets_notoptions_if_option_found() {
     148                $network_id     = get_current_network_id();
     149                $notoptions_key = "$network_id:notoptions";
    152150
    153                 add_metadata( 'site', $network_id, $option, $value, true );
    154                 $this->assertEqualSets( get_metadata( 'site', $network_id, $option ), array( get_network_option( $network_id, $option, true ) ) );
    155         }
     151                $original_cache = wp_cache_get( $notoptions_key, 'site-options' );
     152                if ( false !== $original_cache ) {
     153                        wp_cache_delete( $notoptions_key, 'site-options' );
     154                }
    156155
    157         /**
    158          * @ticket 37181
    159          *
    160          * @group ms-required
    161          */
    162         function test_funky_network_meta() {
    163                 $network_id      = self::factory()->network->create();
    164                 $option          = __FUNCTION__;
    165                 $classy          = new StdClass();
    166                 $classy->ID      = 1;
    167                 $classy->stringy = 'I love slashes\\\\';
    168                 $funky_meta[]    = $classy;
     156                // Retrieve any existing option.
     157                get_network_option( $network_id, 'site_name' );
    169158
    170                 $classy          = new StdClass();
    171                 $classy->ID      = 2;
    172                 $classy->stringy = 'I love slashes\\\\ more';
    173                 $funky_meta[]    = $classy;
     159                $cache = wp_cache_get( $notoptions_key, 'site-options' );
     160                if ( false !== $original_cache ) {
     161                        wp_cache_set( $notoptions_key, $original_cache, 'site-options' );
     162                }
    174163
    175                 // Add a network meta item.
    176                 $this->assertIsInt( add_metadata( 'site', $network_id, $option, $funky_meta, true ) );
    177 
    178                 // Check they exists.
    179                 $this->assertEqualSets( $funky_meta, get_network_option( $network_id, $option ) );
     164                $this->assertSame( array(), $cache );
    180165        }
    181166
    182167        /**
    183          * @ticket 37181
    184          *
     168         * @ticket 43506
    185169         * @group ms-required
    186          */
    187         public function test_meta_api_multiple_values_in_network_option() {
    188                 $network_id = self::factory()->network->create();
    189                 $option     = __FUNCTION__;
    190                 add_metadata( 'site', $network_id, $option, 'monday', true );
    191                 add_metadata( 'site', $network_id, $option, 'tuesday', true );
    192                 add_metadata( 'site', $network_id, $option, 'wednesday', true );
    193                 $this->assertSame( 'monday', get_network_option( $network_id, $option, true ) );
    194         }
    195 
    196         /**
    197          * @ticket 37181
    198170         *
    199          * @group ms-required
    200          *
    201171         * @covers ::get_network_option
    202172         * @covers ::wp_cache_get
    203173         */
    204         public function test_network_option_count_queries_on_non_existing() {
    205                 $network_id = self::factory()->network->create();
    206                 $option     = __FUNCTION__;
    207                 add_network_option( $network_id, $option, 'monday' );
    208                 get_network_option( $network_id, $option );
    209                 $num_queries_pre_get = get_num_queries();
    210                 get_network_option( $network_id, 'do_not_exist' );
    211                 $num_queries_after_get = get_num_queries();
     174        public function test_get_network_option_sets_notoptions_if_option_not_found() {
     175                $network_id     = get_current_network_id();
     176                $notoptions_key = "$network_id:notoptions";
    212177
    213                 $this->assertSame( $num_queries_pre_get, $num_queries_after_get );
    214         }
     178                $original_cache = wp_cache_get( $notoptions_key, 'site-options' );
     179                if ( false !== $original_cache ) {
     180                        wp_cache_delete( $notoptions_key, 'site-options' );
     181                }
    215182
    216         /**
    217          * @ticket 37181
    218          *
    219          * @group ms-required
    220          */
    221         public function test_register_meta_network_option_single_false() {
    222                 $network_id = self::factory()->network->create();
    223                 $option     = __FUNCTION__;
    224                 $value      = __FUNCTION__;
    225                 register_meta(
    226                         'site',
    227                         $option,
    228                         array(
    229                                 'type'    => 'string',
    230                                 'default' => $value,
    231                                 'single'  => false,
    232                         )
    233                 );
     183                // Retrieve any non-existing option.
     184                get_network_option( $network_id, 'this_does_not_exist' );
    234185
    235                 $this->assertSame( $value, get_network_option( $network_id, $option ) );
    236         }
     186                $cache = wp_cache_get( $notoptions_key, 'site-options' );
     187                if ( false !== $original_cache ) {
     188                        wp_cache_set( $notoptions_key, $original_cache, 'site-options' );
     189                }
    237190
    238         /**
    239          * @ticket 37181
    240          *
    241          * @group ms-required
    242          */
    243         public function test_register_meta_network_option_single_true() {
    244                 $network_id = self::factory()->network->create();
    245                 $option     = __FUNCTION__;
    246                 $value      = __FUNCTION__;
    247                 register_meta(
    248                         'site',
    249                         $option,
    250                         array(
    251                                 'type'    => 'string',
    252                                 'default' => $value,
    253                                 'single'  => true,
    254                         )
    255                 );
    256 
    257                 $this->assertSame( $value, get_network_option( $network_id, $option ) );
     191                $this->assertSame( array( 'this_does_not_exist' => true ), $cache );
    258192        }
    259193
    260194        /**
     
    262196         *
    263197         * @ticket 44956
    264198         *
    265          * @group ms-required
    266          *
    267199         * @covers ::update_network_option
    268200         */
    269201        public function test_update_network_option_array_with_object() {
    270                 $network_id     = self::factory()->network->create();
    271                 $option         = __FUNCTION__;
    272202                $array_w_object = array(
    273203                        'url'       => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
    274204                        'meta_data' => (object) array(
     
    278208                        ),
    279209                );
    280210
    281                 add_metadata( 'site', $network_id, $option, $array_w_object, true );
    282                 $this->assertEqualSets( $array_w_object, get_network_option( $network_id, $option ) );
    283         }
     211                $array_w_object_2 = array(
     212                        'url'       => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
     213                        'meta_data' => (object) array(
     214                                'attachment_id' => 292,
     215                                'height'        => 708,
     216                                'width'         => 1260,
     217                        ),
     218                );
    284219
    285         /**
    286          * @ticket 37181
    287          *
    288          * @group ms-required
    289          *
    290          * @covers ::add_network_option
    291          *
    292          * @dataProvider data_types_options
    293          */
    294         public function test_type_add_network_option( $name, $value, $expected ) {
    295                 $result = add_network_option( null, $name, $value );
    296                 $this->assertTrue( $result, 'Network option was not added' );
     220                // Add the option, it did not exist before this.
     221                add_network_option( null, 'array_w_object', $array_w_object );
    297222
    298                 $test_value = get_network_option( null, $name );
    299                 $this->assertSame( $expected, $test_value, 'Values do not match' );
    300         }
     223                $num_queries_pre_update = get_num_queries();
    301224
    302         /**
    303          * @ticket 37181
    304          *
    305          * @covers ::add_network_option
    306          *
    307          * @dataProvider data_slashed_options
    308          */
    309         public function test_slash_add_network_option( $name, $value ) {
    310                 $result = add_network_option( null, $name, $value );
    311                 $this->assertTrue( $result, 'Network option was not added' );
    312                 $this->assertSame( $value, get_network_option( null, $name ), 'Values do not match' );
    313         }
     225                // Update the option using the same array with an object for the value.
     226                $this->assertFalse( update_network_option( null, 'array_w_object', $array_w_object_2 ) );
    314227
    315         /**
    316          * @ticket 37181
    317          *
    318          * @covers ::update_network_option
    319          *
    320          * @dataProvider data_slashed_options
    321          */
    322         public function test_slash_update_network_option( $name, $value ) {
    323                 $result = update_network_option( null, $name, $value );
    324                 $this->assertTrue( $result, 'Network option was not updated' );
    325                 $this->assertSame( $value, get_network_option( null, $name ), 'Values do not match' );
     228                // Check that no new database queries were performed.
     229                $this->assertSame( $num_queries_pre_update, get_num_queries() );
    326230        }
    327 
    328         /**
    329          * @ticket 37181
    330          *
    331          * @covers ::delete_network_option()
    332          *
    333          * @dataProvider data_slashed_options
    334          */
    335         public function test_slash_delete_network_option( $name, $value ) {
    336                 $result = add_network_option( null, $name, $value );
    337                 $this->assertTrue( $result, 'Network option was not added' );
    338                 $this->assertSame( $value, get_network_option( null, $name ) );
    339                 $result = delete_network_option( null, $name );
    340                 $this->assertTrue( $result, 'Network option was not deleted' );
    341                 $this->assertFalse( get_network_option( null, $name ), 'Network option was not deleted' );
    342         }
    343 
    344         public function data_slashed_options() {
    345                 return array(
    346                         'slashed option name'                   => array(
    347                                 'option' => 'String with 1 slash \\',
    348                                 'value'  => 'foo',
    349                         ),
    350                         'slashed in middle option name'         => array(
    351                                 'option' => 'String\\thing',
    352                                 'value'  => 'foo',
    353                         ),
    354                         'slashed option value'                  => array(
    355                                 'option' => 'bar',
    356                                 'value'  => 'String with 1 slash \\',
    357                         ),
    358                         'slashed option name and value'         => array(
    359                                 'option' => 'String with 1 slash \\',
    360                                 'value'  => 'String with 1 slash \\',
    361                         ),
    362                         'slashed 4 times option name and value' => array(
    363                                 'option' => 'String with 4 slashes \\\\\\\\',
    364                                 'value'  => 'String with 4 slashes \\\\\\\\',
    365                         ),
    366                         'slashed 7 times option name and value' => array(
    367                                 'option' => 'String with 7 slashes \\\\\\\\\\\\\\',
    368                                 'value'  => 'String with 7 slashes \\\\\\\\\\\\\\',
    369                         ),
    370                 );
    371         }
    372 
    373         public function data_types_options() {
    374                 return array(
    375                         'array'       => array(
    376                                 'option'   => 'array',
    377                                 'value'    => array(),
    378                                 'expected' => array(),
    379                         ),
    380                         'array_keys'  => array(
    381                                 'option'   => 'array',
    382                                 'value'    => array( 'key' => 'value' ),
    383                                 'expected' => array( 'key' => 'value' ),
    384                         ),
    385                         'int'         => array(
    386                                 'option'   => 'int',
    387                                 'value'    => 33,
    388                                 'expected' => '33',
    389                         ),
    390                         'string'      => array(
    391                                 'option'   => 'string',
    392                                 'value'    => 'foo',
    393                                 'expected' => 'foo',
    394                         ),
    395                         'string_bool' => array(
    396                                 'option'   => 'string',
    397                                 'value'    => 'true',
    398                                 'expected' => 'true',
    399                         ),
    400                         'float'       => array(
    401                                 'option'   => 'float',
    402                                 'value'    => 33.5555,
    403                                 'expected' => '33.5555',
    404                         ),
    405                         'bool'        => array(
    406                                 'option'   => 'bool',
    407                                 'value'    => true,
    408                                 'expected' => '1',
    409                         ),
    410                         'null'        => array(
    411                                 'option'   => 'null',
    412                                 'value'    => null,
    413                                 'expected' => null,
    414                         ),
    415                 );
    416         }
    417231}