Customizer: Allow new `option` settings to not be saved as autoloaded by passing an `autoload` arg value of `false`.

The `autoload` argument value is passed along to `update_option()` which has accepted an `$autoload` parameter since [31628].

Props westonruter, dlh.
See #26394.
Fixes #33499.


git-svn-id: https://develop.svn.wordpress.org/trunk@35305 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2015-10-20 21:18:04 +00:00
parent 8774b40dc8
commit 261aa51182
2 changed files with 71 additions and 6 deletions

View File

@ -143,6 +143,11 @@ class WP_Customize_Setting {
if ( 'option' === $this->type || 'theme_mod' === $this->type ) { if ( 'option' === $this->type || 'theme_mod' === $this->type ) {
// Other setting types can opt-in to aggregate multidimensional explicitly. // Other setting types can opt-in to aggregate multidimensional explicitly.
$this->aggregate_multidimensional(); $this->aggregate_multidimensional();
// Allow option settings to indicate whether they should be autoloaded.
if ( 'option' === $this->type && isset( $args['autoload'] ) ) {
self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] = $args['autoload'];
}
} }
} }
@ -173,10 +178,6 @@ class WP_Customize_Setting {
* @access protected * @access protected
*/ */
protected function aggregate_multidimensional() { protected function aggregate_multidimensional() {
if ( empty( $this->id_data['keys'] ) ) {
return;
}
$id_base = $this->id_data['base']; $id_base = $this->id_data['base'];
if ( ! isset( self::$aggregated_multidimensionals[ $this->type ] ) ) { if ( ! isset( self::$aggregated_multidimensionals[ $this->type ] ) ) {
self::$aggregated_multidimensionals[ $this->type ] = array(); self::$aggregated_multidimensionals[ $this->type ] = array();
@ -188,8 +189,11 @@ class WP_Customize_Setting {
'root_value' => $this->get_root_value( array() ), // Root value for initial state, manipulated by preview and update calls. 'root_value' => $this->get_root_value( array() ), // Root value for initial state, manipulated by preview and update calls.
); );
} }
if ( ! empty( $this->id_data['keys'] ) ) {
$this->is_multidimensional_aggregated = true; $this->is_multidimensional_aggregated = true;
} }
}
/** /**
* The ID for the current blog when the preview() method was called. * The ID for the current blog when the preview() method was called.
@ -502,7 +506,11 @@ class WP_Customize_Setting {
protected function set_root_value( $value ) { protected function set_root_value( $value ) {
$id_base = $this->id_data['base']; $id_base = $this->id_data['base'];
if ( 'option' === $this->type ) { if ( 'option' === $this->type ) {
return update_option( $id_base, $value ); $autoload = true;
if ( isset( self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] ) ) {
$autoload = self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'];
}
return update_option( $id_base, $value, $autoload );
} else if ( 'theme_mod' ) { } else if ( 'theme_mod' ) {
set_theme_mod( $id_base, $value ); set_theme_mod( $id_base, $value );
return true; return true;

View File

@ -472,5 +472,62 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
$this->assertNotEquals( $post_value, get_option( $name ) ); $this->assertNotEquals( $post_value, get_option( $name ) );
restore_current_blog(); restore_current_blog();
} }
/**
* @ticket 33499
*/
function test_option_autoloading() {
global $wpdb;
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$name = 'autoloaded1';
$setting = new WP_Customize_Setting( $this->manager, $name, array(
'type' => 'option',
) );
$value = 'value1';
$this->manager->set_post_value( $setting->id, $value );
$setting->save();
$autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $setting->id ) );
$this->assertEquals( 'yes', $autoload );
$this->assertEquals( $value, get_option( $name ) );
$name = 'autoloaded2';
$setting = new WP_Customize_Setting( $this->manager, $name, array(
'type' => 'option',
'autoload' => true,
) );
$value = 'value2';
$this->manager->set_post_value( $setting->id, $value );
$setting->save();
$autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $setting->id ) );
$this->assertEquals( 'yes', $autoload );
$this->assertEquals( $value, get_option( $name ) );
$name = 'not-autoloaded1';
$setting = new WP_Customize_Setting( $this->manager, $name, array(
'type' => 'option',
'autoload' => false,
) );
$value = 'value3';
$this->manager->set_post_value( $setting->id, $value );
$setting->save();
$autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $setting->id ) );
$this->assertEquals( 'no', $autoload );
$this->assertEquals( $value, get_option( $name ) );
$id_base = 'multi-not-autoloaded';
$setting1 = new WP_Customize_Setting( $this->manager, $id_base . '[foo]', array(
'type' => 'option',
) );
$setting2 = new WP_Customize_Setting( $this->manager, $id_base . '[bar]', array(
'type' => 'option',
'autoload' => false,
) );
$this->manager->set_post_value( $setting1->id, 'value1' );
$this->manager->set_post_value( $setting2->id, 'value2' );
$setting1->save();
$autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $id_base ) );
$this->assertEquals( 'no', $autoload, 'Even though setting1 did not indicate autoload (thus normally true), since another multidimensional option setting of the base did say autoload=false, it should be autoload=no' );
}
} }