From 305cf8b95e8f8dc08e06ca15f653c801b02d8747 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 6 Mar 2015 13:57:26 +0000 Subject: [PATCH] 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 --- wp-includes/option.php | 23 +++++++++++++++++++---- wp-includes/version.php | 2 +- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/wp-includes/option.php b/wp-includes/option.php index 2f548e6151..b7725fc472 100644 --- a/wp-includes/option.php +++ b/wp-includes/option.php @@ -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; diff --git a/wp-includes/version.php b/wp-includes/version.php index 74a648f3c8..358451fae4 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.