Make WordPress Core

Changeset 31640


Ignore:
Timestamp:
03/06/2015 01:56:44 PM (12 years ago)
Author:
boonebgorges
Message:

Allow $autoload setting to be changed for existing options using update_option().

[31628] made it possible to pass an $autoload param to update_option() that
applies when the option does not yet exist in the database. The current
changeset introduces parity for existing options: the $autoload setting
for existing options can be changed via the $autoload parameter. For internal
simplicity, $autoload is ignored for existing options when $value is not
also changed.

This changeset also moves update_option() tests into their own class.

Props dd32.
Fixes #26394.

Location:
trunk
Files:
1 added
2 edited

Legend:

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

    r31628 r31640  
    226226 * @param string      $option   Option name. Expected to not be SQL-escaped.
    227227 * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
    228  * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. Accepts 'yes' or true to
    229  *                              enable, 'no' or false to disable. Default is enabled.
     228 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
     229 *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
     230 *                              Accepts 'yes' or true to enable, 'no' or false to disable. For non-existent options,
     231 *                              the default value is 'yes'.
    230232 * @return bool False if value was not updated and true if value was updated.
    231233 */
    232 function update_option( $option, $value, $autoload = 'yes' ) {
     234function update_option( $option, $value, $autoload = null ) {
    233235        global $wpdb;
    234236
    … …  
    274276        /** This filter is documented in wp-includes/option.php */
    275277        if ( apply_filters( 'default_option_' . $option, false ) === $old_value ) {
     278                // Default setting for new options is 'yes'.
     279                if ( null === $autoload ) {
     280                        $autoload = 'yes';
     281                }
     282
    276283                return add_option( $option, $value, '', $autoload );
    277284        }
    … …  
    290297        do_action( 'update_option', $option, $old_value, $value );
    291298
    292         $result = $wpdb->update( $wpdb->options, array( 'option_value' => $serialized_value ), array( 'option_name' => $option ) );
     299        $update_args = array(
     300                'option_value' => $serialized_value,
     301        );
     302
     303        if ( null !== $autoload ) {
     304                $update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
     305        }
     306
     307        $result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
    293308        if ( ! $result )
    294309                return false;
  • trunk/tests/phpunit/tests/option/option.php

    r31628 r31640  
    7272        }
    7373
    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 
    8674        function test_serialized_data() {
    8775                $key = rand_str();
    … …  
    150138                $this->assertEquals( $expected, $actual->autoload );
    151139        }
    152 
    153         /**
    154          * @ticket 26394
    155          */
    156         public function test_update_option_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_missing() {
    157                 if ( is_multisite() ) {
    158                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    159                 }
    160 
    161                 global $wpdb;
    162                 wp_cache_flush();
    163                 update_option( 'test_update_option_default', 'value' );
    164                 wp_cache_flush();
    165 
    166                 // Populate the alloptions cache, which includes autoload=yes options.
    167                 wp_load_alloptions();
    168 
    169                 $before = $wpdb->num_queries;
    170                 $value = get_option( 'test_update_option_default' );
    171                 $after = $wpdb->num_queries;
    172 
    173                 $this->assertEquals( $before, $after );
    174                 $this->assertEquals( $value, 'value' );
    175         }
    176 
    177         /**
    178          * @ticket 26394
    179          */
    180         public function test_update_option_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_yes() {
    181                 if ( is_multisite() ) {
    182                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    183                 }
    184 
    185                 global $wpdb;
    186                 wp_cache_flush();
    187                 update_option( 'test_update_option_default', 'value', 'yes' );
    188                 wp_cache_flush();
    189 
    190                 // Populate the alloptions cache, which includes autoload=yes options.
    191                 wp_load_alloptions();
    192 
    193                 $before = $wpdb->num_queries;
    194                 $value = get_option( 'test_update_option_default' );
    195                 $after = $wpdb->num_queries;
    196 
    197                 $this->assertEquals( $before, $after );
    198                 $this->assertEquals( $value, 'value' );
    199         }
    200 
    201         /**
    202          * @ticket 26394
    203          */
    204         public function test_update_option_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_no() {
    205                 if ( is_multisite() ) {
    206                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    207                 }
    208 
    209                 global $wpdb;
    210                 wp_cache_flush();
    211                 update_option( 'test_update_option_default', 'value', 'no' );
    212                 wp_cache_flush();
    213 
    214                 // Populate the alloptions cache, which does not include autoload=no options.
    215                 wp_load_alloptions();
    216 
    217                 $before = $wpdb->num_queries;
    218                 $value = get_option( 'test_update_option_default' );
    219                 $after = $wpdb->num_queries;
    220 
    221                 // Database has been hit.
    222                 $this->assertEquals( $before + 1, $after );
    223                 $this->assertEquals( $value, 'value' );
    224         }
    225 
    226         /**
    227          * @ticket 26394
    228          */
    229         public function test_update_option_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_false() {
    230                 if ( is_multisite() ) {
    231                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    232                 }
    233 
    234                 global $wpdb;
    235                 wp_cache_flush();
    236                 update_option( 'test_update_option_default', 'value', false );
    237                 wp_cache_flush();
    238 
    239                 // Populate the alloptions cache, which does not include autoload=no options.
    240                 wp_load_alloptions();
    241 
    242                 $before = $wpdb->num_queries;
    243                 $value = get_option( 'test_update_option_default' );
    244                 $after = $wpdb->num_queries;
    245 
    246                 // Database has been hit.
    247                 $this->assertEquals( $before + 1, $after );
    248                 $this->assertEquals( $value, 'value' );
    249         }
    250140}
Note: See TracChangeset for help on using the changeset viewer.