From 52a77e5254d7ca78e3cff631fa25e01d0e285447 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 13 Mar 2018 15:36:14 +0000 Subject: [PATCH] Multisite: Ensure the `{$network_id}:notoptions` array is set in cache in `get_network_option()`. Prior to this change, the `{$network_id}:notoptions` cache would only be fetched, but not set, unless the actual database lookup would be unsuccessful. This enhancement slightly improves performance by preventing unnecessary external object cache lookups if one is used. Fixes #43506. git-svn-id: https://develop.svn.wordpress.org/trunk@42833 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/option.php | 7 ++- tests/phpunit/tests/option/networkOption.php | 46 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php index f52b7efee7..a80de70784 100644 --- a/src/wp-includes/option.php +++ b/src/wp-includes/option.php @@ -1247,7 +1247,7 @@ function get_network_option( $network_id, $option, $default = false ) { $notoptions_key = "$network_id:notoptions"; $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); - if ( isset( $notoptions[ $option ] ) ) { + if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { /** * Filters a specific default network option. @@ -1295,6 +1295,11 @@ function get_network_option( $network_id, $option, $default = false ) { } } + if ( ! is_array( $notoptions ) ) { + $notoptions = array(); + wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); + } + /** * Filters the value of an existing network option. * diff --git a/tests/phpunit/tests/option/networkOption.php b/tests/phpunit/tests/option/networkOption.php index 3f57910e03..4dbb3eb782 100644 --- a/tests/phpunit/tests/option/networkOption.php +++ b/tests/phpunit/tests/option/networkOption.php @@ -121,4 +121,50 @@ class Tests_Option_NetworkOption extends WP_UnitTestCase { array( 'string', false ), ); } + + /** + * @ticket 43506 + */ + public function test_get_network_option_sets_notoptions_if_option_found() { + $network_id = get_current_network_id(); + $notoptions_key = "$network_id:notoptions"; + + $original_cache = wp_cache_get( $notoptions_key, 'site-options' ); + if ( false !== $original_cache ) { + wp_cache_delete( $notoptions_key, 'site-options' ); + } + + // Retrieve any existing option. + get_network_option( $network_id, 'site_name' ); + + $cache = wp_cache_get( $notoptions_key, 'site-options' ); + if ( false !== $original_cache ) { + wp_cache_set( $notoptions_key, $original_cache, 'site-options' ); + } + + $this->assertSame( array(), $cache ); + } + + /** + * @ticket 43506 + */ + public function test_get_network_option_sets_notoptions_if_option_not_found() { + $network_id = get_current_network_id(); + $notoptions_key = "$network_id:notoptions"; + + $original_cache = wp_cache_get( $notoptions_key, 'site-options' ); + if ( false !== $original_cache ) { + wp_cache_delete( $notoptions_key, 'site-options' ); + } + + // Retrieve any non-existing option. + get_network_option( $network_id, 'this_does_not_exist' ); + + $cache = wp_cache_get( $notoptions_key, 'site-options' ); + if ( false !== $original_cache ) { + wp_cache_set( $notoptions_key, $original_cache, 'site-options' ); + } + + $this->assertSame( array( 'this_does_not_exist' => true ), $cache ); + } }