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.
Built from https://develop.svn.wordpress.org/trunk@31640


git-svn-id: http://core.svn.wordpress.org/trunk@31621 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-03-06 13:57:26 +00:00
parent 3acf36fe48
commit 305cf8b95e
2 changed files with 20 additions and 5 deletions

View File

@ -225,11 +225,13 @@ function wp_load_core_site_options( $site_id = null ) {
*
* @param string $option Option name. Expected to not be SQL-escaped.
* @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
* @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. Accepts 'yes' or true to
* enable, 'no' or false to disable. Default is enabled.
* @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
* `$autoload` can only be updated using `update_option()` if `$value` is also changed.
* Accepts 'yes' or true to enable, 'no' or false to disable. For non-existent options,
* the default value is 'yes'.
* @return bool False if value was not updated and true if value was updated.
*/
function update_option( $option, $value, $autoload = 'yes' ) {
function update_option( $option, $value, $autoload = null ) {
global $wpdb;
$option = trim($option);
@ -273,6 +275,11 @@ function update_option( $option, $value, $autoload = 'yes' ) {
/** This filter is documented in wp-includes/option.php */
if ( apply_filters( 'default_option_' . $option, false ) === $old_value ) {
// Default setting for new options is 'yes'.
if ( null === $autoload ) {
$autoload = 'yes';
}
return add_option( $option, $value, '', $autoload );
}
@ -289,7 +296,15 @@ function update_option( $option, $value, $autoload = 'yes' ) {
*/
do_action( 'update_option', $option, $old_value, $value );
$result = $wpdb->update( $wpdb->options, array( 'option_value' => $serialized_value ), array( 'option_name' => $option ) );
$update_args = array(
'option_value' => $serialized_value,
);
if ( null !== $autoload ) {
$update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
}
$result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
if ( ! $result )
return false;

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.2-alpha-31639';
$wp_version = '4.2-alpha-31640';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.