Make WordPress Core

Changeset 35305


Ignore:
Timestamp:
10/20/2015 09:18:04 PM (8 years ago)
Author:
westonruter
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-setting.php

    r35302 r35305  
    144144            // Other setting types can opt-in to aggregate multidimensional explicitly.
    145145            $this->aggregate_multidimensional();
     146
     147            // Allow option settings to indicate whether they should be autoloaded.
     148            if ( 'option' === $this->type && isset( $args['autoload'] ) ) {
     149                self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] = $args['autoload'];
     150            }
    146151        }
    147152    }
     
    174179     */
    175180    protected function aggregate_multidimensional() {
    176         if ( empty( $this->id_data['keys'] ) ) {
    177             return;
    178         }
    179 
    180181        $id_base = $this->id_data['base'];
    181182        if ( ! isset( self::$aggregated_multidimensionals[ $this->type ] ) ) {
     
    189190            );
    190191        }
    191         $this->is_multidimensional_aggregated = true;
     192
     193        if ( ! empty( $this->id_data['keys'] ) ) {
     194            $this->is_multidimensional_aggregated = true;
     195        }
    192196    }
    193197
     
    503507        $id_base = $this->id_data['base'];
    504508        if ( 'option' === $this->type ) {
    505             return update_option( $id_base, $value );
     509            $autoload = true;
     510            if ( isset( self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] ) ) {
     511                $autoload = self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'];
     512            }
     513            return update_option( $id_base, $value, $autoload );
    506514        } else if ( 'theme_mod' ) {
    507515            set_theme_mod( $id_base, $value );
  • trunk/tests/phpunit/tests/customize/setting.php

    r35242 r35305  
    473473        restore_current_blog();
    474474    }
     475
     476    /**
     477     * @ticket 33499
     478     */
     479    function test_option_autoloading() {
     480        global $wpdb;
     481        wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     482
     483        $name = 'autoloaded1';
     484        $setting = new WP_Customize_Setting( $this->manager, $name, array(
     485            'type' => 'option',
     486        ) );
     487        $value = 'value1';
     488        $this->manager->set_post_value( $setting->id, $value );
     489        $setting->save();
     490        $autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $setting->id ) );
     491        $this->assertEquals( 'yes', $autoload );
     492        $this->assertEquals( $value, get_option( $name ) );
     493
     494        $name = 'autoloaded2';
     495        $setting = new WP_Customize_Setting( $this->manager, $name, array(
     496            'type' => 'option',
     497            'autoload' => true,
     498        ) );
     499        $value = 'value2';
     500        $this->manager->set_post_value( $setting->id, $value );
     501        $setting->save();
     502        $autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $setting->id ) );
     503        $this->assertEquals( 'yes', $autoload );
     504        $this->assertEquals( $value, get_option( $name ) );
     505
     506        $name = 'not-autoloaded1';
     507        $setting = new WP_Customize_Setting( $this->manager, $name, array(
     508            'type' => 'option',
     509            'autoload' => false,
     510        ) );
     511        $value = 'value3';
     512        $this->manager->set_post_value( $setting->id, $value );
     513        $setting->save();
     514        $autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $setting->id ) );
     515        $this->assertEquals( 'no', $autoload );
     516        $this->assertEquals( $value, get_option( $name ) );
     517
     518        $id_base = 'multi-not-autoloaded';
     519        $setting1 = new WP_Customize_Setting( $this->manager, $id_base . '[foo]', array(
     520            'type' => 'option',
     521        ) );
     522        $setting2 = new WP_Customize_Setting( $this->manager, $id_base . '[bar]', array(
     523            'type' => 'option',
     524            'autoload' => false,
     525        ) );
     526        $this->manager->set_post_value( $setting1->id, 'value1' );
     527        $this->manager->set_post_value( $setting2->id, 'value2' );
     528        $setting1->save();
     529        $autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $id_base ) );
     530        $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' );
     531    }
    475532}
    476533
Note: See TracChangeset for help on using the changeset viewer.