From 4857c6996cf5350351ed9f2fd473c935349f5575 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Sun, 25 Jan 2015 07:50:31 +0000 Subject: [PATCH] Allow $autoload in add_option() to receive false. props dllh. fixes #31119. git-svn-id: https://develop.svn.wordpress.org/trunk@31278 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/option.php | 5 +++-- tests/phpunit/tests/option/option.php | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php index 6134606471..9b3e884388 100644 --- a/src/wp-includes/option.php +++ b/src/wp-includes/option.php @@ -346,7 +346,8 @@ function update_option( $option, $value ) { * @param string $option Name of option to add. Expected to not be SQL-escaped. * @param mixed $value Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. * @param string $deprecated Optional. Description. Not used anymore. - * @param string|bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up. + * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. + * Default is enabled. Accepts 'no' to disable for legacy reasons. * @return bool False if option was not added and true if option was added. */ function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) { @@ -373,7 +374,7 @@ function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) return false; $serialized_value = maybe_serialize( $value ); - $autoload = ( 'no' === $autoload ) ? 'no' : 'yes'; + $autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes'; /** * Fires before an option is added. diff --git a/tests/phpunit/tests/option/option.php b/tests/phpunit/tests/option/option.php index a866cea8ab..f31cdf45f1 100644 --- a/tests/phpunit/tests/option/option.php +++ b/tests/phpunit/tests/option/option.php @@ -99,4 +99,30 @@ class Tests_Option_Option extends WP_UnitTestCase { function test_special_option_name_notoptions() { delete_option( 'notoptions' ); } + + function data_option_autoloading() { + return array( + array( 'autoload_yes', 'yes', 'yes' ), + array( 'autoload_true', true, 'yes' ), + array( 'autoload_string', 'foo', 'yes' ), + array( 'autoload_int', 123456, 'yes' ), + array( 'autoload_array', array(), 'yes' ), + array( 'autoload_no', 'no', 'no' ), + array( 'autoload_false', false, 'no' ), + ); + } + /** + * Options should be autoloaded unless they were added with "no" or `false`. + * + * @ticket 31119 + * @dataProvider data_option_autoloading + */ + function test_option_autoloading( $name, $autoload_value, $expected ) { + global $wpdb; + $added = add_option( $name, 'Autoload test', '', $autoload_value ); + $this->assertTrue( $added ); + + $actual = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", $name ) ); + $this->assertEquals( $expected, $actual->autoload ); + } }