Make WordPress Core

Changeset 54145


Ignore:
Timestamp:
09/13/2022 07:27:19 PM (22 months ago)
Author:
davidbaumwald
Message:

Options, Meta APIs: Add a new pre-option filter.

Although a pre_option_{$option} filter already exists, this change adds a more general pre_option filter that will run on every get_option call. This brings the control flow into similar flow as update_option.

Props flixos90, NathanAtmoz, desrosj, spacedmonkey, pbearne.
Fixes #37930.

Location:
trunk
Files:
2 edited

Legend:

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

    r54080 r54145  
    131131     */
    132132    $pre = apply_filters( "pre_option_{$option}", false, $option, $default );
     133
     134    /**
     135     * Filters the value of all existing options before it is retrieved.
     136     *
     137     * Returning a truthy value from the filter will effectively short-circuit retrieval
     138     * and return the passed value instead.
     139     *
     140     * @since 6.1.0
     141     *
     142     * @param mixed  $pre_option  The value to return instead of the option value. This differs
     143     *                            from `$default`, which is used as the fallback value in the event
     144     *                            the option doesn't exist elsewhere in get_option().
     145     *                            Default false (to skip past the short-circuit).
     146     * @param string $option      Name of the option.
     147     * @param mixed  $default     The fallback value to return if the option does not exist.
     148     *                            Default false.
     149     */
     150    $pre = apply_filters( 'pre_option', $pre, $option, $default );
    133151
    134152    if ( false !== $pre ) {
  • trunk/tests/phpunit/tests/option/option.php

    r53865 r54145  
    298298        );
    299299    }
     300
     301    /**
     302     * @ticket 37930
     303     *
     304     * @covers ::get_option
     305     */
     306    public function test_filter_pre_option_all_filter_is_called() {
     307        $filter = new MockAction();
     308
     309        add_filter( 'pre_option', array( $filter, 'filter' ) );
     310
     311        get_option( 'ignored' );
     312
     313        $this->assertSame( 1, $filter->get_call_count() );
     314    }
    300315}
Note: See TracChangeset for help on using the changeset viewer.