From dcd1ba9330c23d3745c154eb11069ac6288c0665 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 7 Feb 2023 12:47:30 +0000 Subject: [PATCH] Options, Meta APIs: Add a filter to allow the shortcut return to `wp_load_alloptions` function. Add a new filter `pre_wp_load_alloptions` in the `wp_load_alloptions` function to short circuit the return value. Props pbearne, spacedmonkey, joyously, SergeyBiryukov, mukesh27, costdev. Fixes #56045. git-svn-id: https://develop.svn.wordpress.org/trunk@55256 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/option.php | 16 +++++++++++ .../phpunit/tests/option/wpLoadAlloptions.php | 27 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php index d87a2f9626..c5f9708a50 100644 --- a/src/wp-includes/option.php +++ b/src/wp-includes/option.php @@ -301,6 +301,22 @@ function form_option( $option ) { function wp_load_alloptions( $force_cache = false ) { global $wpdb; + /** + * Filters the array of alloptions before it is populated. + * + * Returning an array from the filter will effectively short circuit + * wp_load_alloptions(), returning that value instead. + * + * @since 6.2.0 + * + * @param array|null $alloptions An array of alloptions. Default null. + * @param bool $force_cache Whether to force an update of the local cache from the persistent cache. Default false. + */ + $alloptions = apply_filters( 'pre_wp_load_alloptions', null, $force_cache ); + if ( is_array( $alloptions ) ) { + return $alloptions; + } + if ( ! wp_installing() || ! is_multisite() ) { $alloptions = wp_cache_get( 'alloptions', 'options', $force_cache ); } else { diff --git a/tests/phpunit/tests/option/wpLoadAlloptions.php b/tests/phpunit/tests/option/wpLoadAlloptions.php index b012e72435..56533255fb 100644 --- a/tests/phpunit/tests/option/wpLoadAlloptions.php +++ b/tests/phpunit/tests/option/wpLoadAlloptions.php @@ -120,4 +120,31 @@ class Tests_Option_wpLoadAlloptions extends WP_UnitTestCase { $this->alloptions = $alloptions; return $this->alloptions; } + + /** + * Tests that `$alloptions` can be filtered with a custom value, short circuiting `wp_load_alloptions()`. + * + * @ticket 56045 + * + * @covers ::wp_load_alloptions + */ + public function test_filter_pre_wp_load_alloptions_filter_is_called() { + $filter = new MockAction(); + + add_filter( 'pre_wp_load_alloptions', array( &$filter, 'filter' ) ); + + wp_load_alloptions(); + + $this->assertSame( + 1, + $filter->get_call_count(), + 'The filter was not called 1 time.' + ); + + $this->assertSame( + array( 'pre_wp_load_alloptions' ), + $filter->get_hook_names(), + 'The hook name was incorrect.' + ); + } }