Make WordPress Core

Changeset 31473


Ignore:
Timestamp:
02/18/2015 07:13:43 PM (10 years ago)
Author:
boonebgorges
Message:

Respect 'default_option_' filters during early sanity checks in add_option() and update_option().

add_option() and update_option() both call get_option() to compare the
value passed to the function with any existing value for the given option name.
When a 'default_option_' filter is in place to change the default value of
an option, add_option() and update_option() ought to check against the
filtered value, rather than a hardcoded false, in order to determine whether
a prior value exists.

Props GregLone, tyxla.
Fixes #31047.

Location:
trunk
Files:
2 edited

Legend:

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

    r31414 r31473  
    269269        return false;
    270270
    271     if ( false === $old_value )
     271    /** This filter is documented in wp-includes/option.php */
     272    if ( apply_filters( 'default_option_' . $option, false ) === $old_value )
    272273        return add_option( $option, $value );
    273274
     
    371372    $notoptions = wp_cache_get( 'notoptions', 'options' );
    372373    if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
    373         if ( false !== get_option( $option ) )
     374        /** This filter is documented in wp-includes/option.php */
     375        if ( apply_filters( 'default_option_' . $option, false ) !== get_option( $option ) )
    374376            return false;
    375377
  • trunk/tests/phpunit/tests/option/option.php

    r31278 r31473  
    5858        $this->assertTrue( delete_option( 'doesnotexist' ) );
    5959        $this->assertFalse( get_option( 'doesnotexist' ) );
     60    }
     61
     62    /**
     63     * @ticket 31047
     64     */
     65    public function test_add_option_should_respect_default_option_filter() {
     66        add_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
     67        $added = add_option( 'doesnotexist', 'bar' );
     68        remove_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
     69
     70        $this->assertTrue( $added );
     71        $this->assertSame( 'bar', get_option( 'doesnotexist' ) );
     72    }
     73
     74    /**
     75     * @ticket 31047
     76     */
     77    public function test_update_option_should_respect_default_option_filter_when_option_does_not_yet_exist_in_database() {
     78        add_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
     79        $added = update_option( 'doesnotexist', 'bar' );
     80        remove_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
     81
     82        $this->assertTrue( $added );
     83        $this->assertSame( 'bar', get_option( 'doesnotexist' ) );
    6084    }
    6185
Note: See TracChangeset for help on using the changeset viewer.