Make WordPress Core


Ignore:
Timestamp:
03/06/2015 01:56:44 PM (10 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.

File:
1 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;
Note: See TracChangeset for help on using the changeset viewer.