Make WordPress Core

Ticket #31047: 31047.2.patch

File 31047.2.patch, 2.4 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/option.php

    diff --git src/wp-includes/option.php src/wp-includes/option.php
    index 6134606..da3e3a9 100644
    function update_option( $option, $value ) { 
    268268        if ( $value === $old_value )
    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
    274275        $serialized_value = maybe_serialize( $value );
    function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) 
    369370        // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
    370371        $notoptions = wp_cache_get( 'notoptions', 'options' );
    371372        if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
    372                 if ( false !== get_option( $option ) )
     373                /** This filter is documented in wp-includes/option.php */
     374                if ( apply_filters( 'default_option_' . $option, false ) !== get_option( $option ) )
    373375                        return false;
    374376
    375377        $serialized_value = maybe_serialize( $value );
  • tests/phpunit/tests/option/option.php

    diff --git tests/phpunit/tests/option/option.php tests/phpunit/tests/option/option.php
    index a866cea..847591c 100644
    class Tests_Option_Option extends WP_UnitTestCase { 
    5959                $this->assertFalse( get_option( 'doesnotexist' ) );
    6060        }
    6161
     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' ) );
     84        }
     85
    6286        function test_serialized_data() {
    6387                $key = rand_str();
    6488                $value = array( 'foo' => true, 'bar' => true );