diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index 27c58b57dd671..3dc215ea9ee01 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -810,13 +810,19 @@ function wp_set_wpdb_vars() { function wp_using_ext_object_cache( $using = null ) { global $_wp_using_ext_object_cache; + // Save the current state to return later. $current_using = $_wp_using_ext_object_cache; if ( null !== $using ) { - $_wp_using_ext_object_cache = $using; + $_wp_using_ext_object_cache = (bool) $using; } - return $current_using; + if ( null === $_wp_using_ext_object_cache ) { + // If the global is uninitialized, the value would be null, which violates the type signature. + return false; + } + + return (bool) $current_using; } /** diff --git a/tests/phpunit/tests/load/wpUsingExtObjectCache.php b/tests/phpunit/tests/load/wpUsingExtObjectCache.php new file mode 100644 index 0000000000000..8f99893b02ea0 --- /dev/null +++ b/tests/phpunit/tests/load/wpUsingExtObjectCache.php @@ -0,0 +1,32 @@ +orig_using_ext_cache = $_wp_using_ext_object_cache; + } + + public function tear_down() { + global $_wp_using_ext_object_cache; + + $_wp_using_ext_object_cache = $this->orig_using_ext_cache; + parent::tear_down(); + } + + public function test_should_always_return_boolean() { + wp_using_ext_object_cache( 1 ); + $this->assertIsBool( wp_using_ext_object_cache() ); + } +}