Make WordPress Core

Changeset 55256


Ignore:
Timestamp:
02/07/2023 12:47:30 PM (4 years ago)
Author:
spacedmonkey
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/option.php

    r55033 r55256  
    301301function wp_load_alloptions( $force_cache = false ) {
    302302        global $wpdb;
     303
     304        /**
     305         * Filters the array of alloptions before it is populated.
     306         *
     307         * Returning an array from the filter will effectively short circuit
     308         * wp_load_alloptions(), returning that value instead.
     309         *
     310         * @since 6.2.0
     311         *
     312         * @param array|null $alloptions  An array of alloptions. Default null.
     313         * @param bool       $force_cache Whether to force an update of the local cache from the persistent cache. Default false.
     314         */
     315        $alloptions = apply_filters( 'pre_wp_load_alloptions', null, $force_cache );
     316        if ( is_array( $alloptions ) ) {
     317                return $alloptions;
     318        }
    303319
    304320        if ( ! wp_installing() || ! is_multisite() ) {
  • trunk/tests/phpunit/tests/option/wpLoadAlloptions.php

    r53865 r55256  
    121121                return $this->alloptions;
    122122        }
     123
     124        /**
     125         * Tests that `$alloptions` can be filtered with a custom value, short circuiting `wp_load_alloptions()`.
     126         *
     127         * @ticket 56045
     128         *
     129         * @covers ::wp_load_alloptions
     130         */
     131        public function test_filter_pre_wp_load_alloptions_filter_is_called() {
     132                $filter = new MockAction();
     133
     134                add_filter( 'pre_wp_load_alloptions', array( &$filter, 'filter' ) );
     135
     136                wp_load_alloptions();
     137
     138                $this->assertSame(
     139                        1,
     140                        $filter->get_call_count(),
     141                        'The filter was not called 1 time.'
     142                );
     143
     144                $this->assertSame(
     145                        array( 'pre_wp_load_alloptions' ),
     146                        $filter->get_hook_names(),
     147                        'The hook name was incorrect.'
     148                );
     149        }
    123150}
Note: See TracChangeset for help on using the changeset viewer.