mirror of
git://develop.git.wordpress.org/
synced 2025-04-11 15:42:03 +02:00
Options, Meta APIs: Rename option cache priming functions.
Rename the option cache priming functions to more closely follow the naming convention used by other cache priming functions. * `wp_load_options()` becomes `wp_prime_option_caches()` * `wp_load_options_by_group()` becomes `wp_prime_option_caches_by_group()` The unit test files and classes are renamed accordingly. Unlike the existing cache priming functions, these functions were introduced with the intention of being public so use the `wp_` prefix rather than the `_` prefix used by the functions initially introduced as private functions but since made public. Follow up to [56445],[56990]. Props flixos90, peterwilsoncc, joemcgill, SergeyBiryukov, desrosj. Reviewed by flixos90. Merges [57013] to the 6.4 branch. Fixes #58962. git-svn-id: https://develop.svn.wordpress.org/branches/6.4@57016 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
1df8ffbe1b
commit
cd04d7552e
@ -248,7 +248,7 @@ function get_option( $option, $default_value = false ) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads specific options into the cache with a single database query.
|
||||
* Primes specific options into the cache with a single database query.
|
||||
*
|
||||
* Only options that do not already exist in cache will be loaded.
|
||||
*
|
||||
@ -258,7 +258,7 @@ function get_option( $option, $default_value = false ) {
|
||||
*
|
||||
* @param array $options An array of option names to be loaded.
|
||||
*/
|
||||
function wp_load_options( $options ) {
|
||||
function wp_prime_option_caches( $options ) {
|
||||
$alloptions = wp_load_alloptions();
|
||||
$cached_options = wp_cache_get_multiple( $options, 'options' );
|
||||
|
||||
@ -321,7 +321,7 @@ function wp_load_options( $options ) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all options registered with a specific option group.
|
||||
* Primes the cache of all options registered with a specific option group.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
@ -329,11 +329,11 @@ function wp_load_options( $options ) {
|
||||
*
|
||||
* @param string $option_group The option group to load options for.
|
||||
*/
|
||||
function wp_load_options_by_group( $option_group ) {
|
||||
function wp_prime_option_caches_by_group( $option_group ) {
|
||||
global $new_allowed_options;
|
||||
|
||||
if ( isset( $new_allowed_options[ $option_group ] ) ) {
|
||||
wp_load_options( $new_allowed_options[ $option_group ] );
|
||||
wp_prime_option_caches( $new_allowed_options[ $option_group ] );
|
||||
}
|
||||
}
|
||||
|
||||
@ -348,7 +348,7 @@ function wp_load_options_by_group( $option_group ) {
|
||||
* @return array An array of key-value pairs for the requested options.
|
||||
*/
|
||||
function get_options( $options ) {
|
||||
wp_load_options( $options );
|
||||
wp_prime_option_caches( $options );
|
||||
|
||||
$result = array();
|
||||
foreach ( $options as $option ) {
|
||||
|
@ -1,21 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* Test wp_load_options().
|
||||
* Test wp_prime_option_caches().
|
||||
*
|
||||
* @group option
|
||||
*
|
||||
* @covers ::wp_load_options
|
||||
* @covers ::wp_prime_option_caches
|
||||
*/
|
||||
class Tests_Option_PrimeOptions extends WP_UnitTestCase {
|
||||
class Tests_Option_WpPrimeOptionCaches extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Tests that wp_load_options() loads multiple options.
|
||||
* Tests that wp_prime_option_caches() primes multiple options.
|
||||
*
|
||||
* @ticket 58962
|
||||
*/
|
||||
public function test_wp_load_options() {
|
||||
// Create some options to load.
|
||||
$options_to_load = array(
|
||||
public function test_wp_prime_option_caches() {
|
||||
// Create some options to prime.
|
||||
$options_to_prime = array(
|
||||
'option1',
|
||||
'option2',
|
||||
'option3',
|
||||
@ -26,30 +26,30 @@ class Tests_Option_PrimeOptions extends WP_UnitTestCase {
|
||||
* clear the cache for the options,
|
||||
* check options are not in cache initially.
|
||||
*/
|
||||
foreach ( $options_to_load as $option ) {
|
||||
foreach ( $options_to_prime as $option ) {
|
||||
update_option( $option, "value_$option", false );
|
||||
wp_cache_delete( $option, 'options' );
|
||||
$this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
|
||||
}
|
||||
|
||||
// Call the wp_load_options function to load the options.
|
||||
wp_load_options( $options_to_load );
|
||||
// Call the wp_prime_option_caches function to prime the options.
|
||||
wp_prime_option_caches( $options_to_prime );
|
||||
|
||||
// Store the initial database query count.
|
||||
$initial_query_count = get_num_queries();
|
||||
|
||||
// Check that options are only in the 'options' cache group.
|
||||
foreach ( $options_to_load as $option ) {
|
||||
foreach ( $options_to_prime as $option ) {
|
||||
$this->assertSame(
|
||||
wp_cache_get( $option, 'options' ),
|
||||
get_option( $option ),
|
||||
"$option was not loaded to the 'options' cache group."
|
||||
"$option was not primed in the 'options' cache group."
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
wp_cache_get( $option, 'notoptions' ),
|
||||
get_option( $option ),
|
||||
"$option was loaded to the 'notoptions' cache group."
|
||||
"$option was primed in the 'notoptions' cache group."
|
||||
);
|
||||
}
|
||||
|
||||
@ -62,13 +62,13 @@ class Tests_Option_PrimeOptions extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests wp_load_options() with options that do not exist in the database.
|
||||
* Tests wp_prime_option_caches() with options that do not exist in the database.
|
||||
*
|
||||
* @ticket 58962
|
||||
*/
|
||||
public function test_wp_load_options_with_nonexistent_options() {
|
||||
// Create some options to load.
|
||||
$options_to_load = array(
|
||||
public function test_wp_prime_option_caches_with_nonexistent_options() {
|
||||
// Create some options to prime.
|
||||
$options_to_prime = array(
|
||||
'option1',
|
||||
'option2',
|
||||
);
|
||||
@ -78,50 +78,50 @@ class Tests_Option_PrimeOptions extends WP_UnitTestCase {
|
||||
* clear the cache for the options,
|
||||
* check options are not in cache initially.
|
||||
*/
|
||||
foreach ( $options_to_load as $option ) {
|
||||
foreach ( $options_to_prime as $option ) {
|
||||
$this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
|
||||
}
|
||||
|
||||
// Call the wp_load_options function to load the options.
|
||||
wp_load_options( $options_to_load );
|
||||
// Call the wp_prime_option_caches function to prime the options.
|
||||
wp_prime_option_caches( $options_to_prime );
|
||||
|
||||
// Check that options are not in the cache or database.
|
||||
foreach ( $options_to_load as $option ) {
|
||||
foreach ( $options_to_prime as $option ) {
|
||||
$this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
|
||||
}
|
||||
|
||||
// Check that options are present in the notoptions cache.
|
||||
$new_notoptions = wp_cache_get( 'notoptions', 'options' );
|
||||
$this->assertIsArray( $new_notoptions, 'The notoptions cache should be an array.' );
|
||||
foreach ( $options_to_load as $option ) {
|
||||
foreach ( $options_to_prime as $option ) {
|
||||
$this->assertArrayHasKey( $option, $new_notoptions, "$option was not added to the notoptions cache." );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests wp_load_options() with an empty array.
|
||||
* Tests wp_prime_option_caches() with an empty array.
|
||||
*
|
||||
* @ticket 58962
|
||||
*/
|
||||
public function test_wp_load_options_with_empty_array() {
|
||||
public function test_wp_prime_option_caches_with_empty_array() {
|
||||
$alloptions = wp_load_alloptions();
|
||||
$notoptions = wp_cache_get( 'notoptions', 'options' );
|
||||
|
||||
wp_load_options( array() );
|
||||
wp_prime_option_caches( array() );
|
||||
|
||||
$this->assertSame( $alloptions, wp_cache_get( 'alloptions', 'options' ), 'The alloptions cache was modified.' );
|
||||
$this->assertSame( $notoptions, wp_cache_get( 'notoptions', 'options' ), 'The notoptions cache was modified.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that wp_load_options handles an empty "notoptions" cache.
|
||||
* Tests that wp_prime_option_caches() handles an empty "notoptions" cache.
|
||||
*
|
||||
* @ticket 58962
|
||||
*/
|
||||
public function test_wp_load_options_handles_empty_notoptions_cache() {
|
||||
public function test_wp_prime_option_caches_handles_empty_notoptions_cache() {
|
||||
wp_cache_delete( 'notoptions', 'options' );
|
||||
|
||||
wp_load_options( array( 'nonexistent_option' ) );
|
||||
wp_prime_option_caches( array( 'nonexistent_option' ) );
|
||||
|
||||
$notoptions = wp_cache_get( 'notoptions', 'options' );
|
||||
$this->assertIsArray( $notoptions, 'The notoptions cache should be an array.' );
|
@ -1,22 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* Test wp_load_options_by_group().
|
||||
* Test wp_prime_option_caches_by_group().
|
||||
*
|
||||
* @group option
|
||||
*
|
||||
* @covers ::wp_load_options_by_group
|
||||
* @covers ::wp_prime_option_caches_by_group
|
||||
*/
|
||||
class Tests_Option_PrimeOptionsByGroup extends WP_UnitTestCase {
|
||||
class Tests_Option_WpPrimeOptionCachesByGroup extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Tests that wp_load_options_by_group() only loads options in the specified group.
|
||||
* Tests that wp_prime_option_caches_by_group() only primes options in the specified group.
|
||||
*
|
||||
* @ticket 58962
|
||||
*/
|
||||
public function test_wp_load_options_by_group() {
|
||||
public function test_wp_prime_option_caches_by_group() {
|
||||
global $new_allowed_options;
|
||||
|
||||
// Create some options to load.
|
||||
// Create some options to prime.
|
||||
$new_allowed_options = array(
|
||||
'group1' => array(
|
||||
'option1',
|
||||
@ -27,7 +27,7 @@ class Tests_Option_PrimeOptionsByGroup extends WP_UnitTestCase {
|
||||
),
|
||||
);
|
||||
|
||||
$options_to_load = array(
|
||||
$options_to_prime = array(
|
||||
'option1',
|
||||
'option2',
|
||||
'option3',
|
||||
@ -38,35 +38,35 @@ class Tests_Option_PrimeOptionsByGroup extends WP_UnitTestCase {
|
||||
* clear the cache for the options,
|
||||
* check options are not in cache initially.
|
||||
*/
|
||||
foreach ( $options_to_load as $option ) {
|
||||
foreach ( $options_to_prime as $option ) {
|
||||
update_option( $option, "value_$option", false );
|
||||
wp_cache_delete( $option, 'options' );
|
||||
$this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
|
||||
}
|
||||
|
||||
// Call the wp_load_options_by_group function to load the options.
|
||||
wp_load_options_by_group( 'group1' );
|
||||
// Call the wp_prime_option_caches_by_group function to prime the options.
|
||||
wp_prime_option_caches_by_group( 'group1' );
|
||||
|
||||
// Check that options are now in the cache.
|
||||
$this->assertSame( get_option( 'option1' ), wp_cache_get( 'option1', 'options' ), 'option1 was not loaded.' );
|
||||
$this->assertSame( get_option( 'option2' ), wp_cache_get( 'option2', 'options' ), 'option2 was not loaded.' );
|
||||
$this->assertSame( get_option( 'option1' ), wp_cache_get( 'option1', 'options' ), 'option1\'s cache was not primed.' );
|
||||
$this->assertSame( get_option( 'option2' ), wp_cache_get( 'option2', 'options' ), 'option2\'s cache was not primed.' );
|
||||
|
||||
// Make sure option3 is still not in cache.
|
||||
$this->assertFalse( wp_cache_get( 'option3', 'options' ), 'option3 was not deleted from the cache.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests wp_load_options_by_group() with a nonexistent option group.
|
||||
* Tests wp_prime_option_caches_by_group() with a nonexistent option group.
|
||||
*
|
||||
* @ticket 58962
|
||||
*/
|
||||
public function test_wp_load_options_by_group_with_nonexistent_group() {
|
||||
public function test_wp_prime_option_caches_by_group_with_nonexistent_group() {
|
||||
// Make sure options are not in cache or database initially.
|
||||
$this->assertFalse( wp_cache_get( 'option1', 'options' ), 'option1 was not deleted from the cache.' );
|
||||
$this->assertFalse( wp_cache_get( 'option2', 'options' ), 'option2 was not deleted from the cache.' );
|
||||
|
||||
// Call the wp_load_options_by_group function with a nonexistent group.
|
||||
wp_load_options_by_group( 'nonexistent_group' );
|
||||
// Call the wp_prime_option_caches_by_group function with a nonexistent group.
|
||||
wp_prime_option_caches_by_group( 'nonexistent_group' );
|
||||
|
||||
// Check that options are still not in the cache or database.
|
||||
$this->assertFalse( wp_cache_get( 'option1', 'options' ), 'option1 was not deleted from the cache.' );
|
Loading…
x
Reference in New Issue
Block a user