Make WordPress Core


Ignore:
Timestamp:
04/14/2014 10:45:40 PM (10 years ago)
Author:
nacin
Message:

Customizer: Properly handle widget settings when activating a previewed theme.

props westonruter, ocean90, gcorne.
fixes #27767.

File:
1 edited

Legend:

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

    r28117 r28124  
    6262
    6363    /**
     64     * @since 3.9.0
     65     * @access protected
     66     * @var array
     67     */
     68    protected $old_sidebars_widgets = array();
     69
     70    /**
    6471     * Initial loader.
    6572     *
     
    7380
    7481        add_action( 'after_setup_theme',                       array( $this, 'setup_widget_addition_previews' ) );
     82        add_action( 'wp_loaded',                               array( $this, 'override_sidebars_widgets_for_theme_switch' ) );
    7583        add_action( 'customize_controls_init',                 array( $this, 'customize_controls_init' ) );
    7684        add_action( 'customize_register',                      array( $this, 'schedule_customize_register' ), 1 );
     
    116124     *
    117125     * @access public
    118      * @global WP_Customize_Manager $wp_customize Customizer instance.
    119126     */
    120127    public function setup_widget_addition_previews() {
     
    126133
    127134        $is_ajax_widget_update = false;
    128         if ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'update-widget' === $this->get_post_value( 'action' ) ) {
     135        if ( $this->manager->doing_ajax() && 'update-widget' === $this->get_post_value( 'action' ) ) {
    129136            $is_ajax_widget_update = check_ajax_referer( 'update-widget', 'nonce', false );
    130137        }
    131138
    132139        $is_ajax_customize_save = false;
    133         if ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'customize_save' === $this->get_post_value( 'action' ) ) {
     140        if ( $this->manager->doing_ajax() && 'customize_save' === $this->get_post_value( 'action' ) ) {
    134141            $is_ajax_customize_save = check_ajax_referer( 'save-customize_' . $this->manager->get_stylesheet(), 'nonce', false );
    135142        }
     
    280287
    281288    /**
     289     * Override sidebars_widgets for theme switch.
     290     *
     291     * When switching a theme via the customizer, supply any previously-configured
     292     * sidebars_widgets from the target theme as the initial sidebars_widgets
     293     * setting. Also store the old theme's existing settings so that they can
     294     * be passed along for storing in the sidebars_widgets theme_mod when the
     295     * theme gets switched.
     296     *
     297     * @since 3.9.0
     298     * @access public
     299     */
     300    public function override_sidebars_widgets_for_theme_switch() {
     301        global $sidebars_widgets;
     302
     303        if ( $this->manager->doing_ajax() || $this->manager->is_theme_active() ) {
     304            return;
     305        }
     306
     307        $this->old_sidebars_widgets = wp_get_sidebars_widgets();
     308        add_filter( 'customize_value_old_sidebars_widgets_data', array( $this, 'filter_customize_value_old_sidebars_widgets_data' ) );
     309
     310        // retrieve_widgets() looks at the global $sidebars_widgets
     311        $sidebars_widgets = $this->old_sidebars_widgets;
     312        $sidebars_widgets = retrieve_widgets( 'customize' );
     313        add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 );
     314    }
     315
     316    /**
     317     * Filter old_sidebars_widgets_data customizer setting.
     318     *
     319     * When switching themes, filter the Customizer setting
     320     * old_sidebars_widgets_data to supply initial $sidebars_widgets before they
     321     * were overridden by retrieve_widgets(). The value for
     322     * old_sidebars_widgets_data gets set in the old theme's sidebars_widgets
     323     * theme_mod.
     324     *
     325     * @see WP_Customize_Widgets::handle_theme_switch()
     326     * @since 3.9.0
     327     * @access public
     328     *
     329     * @param array $sidebars_widgets
     330     */
     331    public function filter_customize_value_old_sidebars_widgets_data( $old_sidebars_widgets ) {
     332        return $this->old_sidebars_widgets;
     333    }
     334
     335    /**
     336     * Filter sidebars_widgets option for theme switch.
     337     *
     338     * When switching themes, the retrieve_widgets() function is run when the
     339     * Customizer initializes, and then the new sidebars_widgets here get
     340     * supplied as the default value for the sidebars_widgets option.
     341     *
     342     * @see WP_Customize_Widgets::handle_theme_switch()
     343     * @since 3.9.0
     344     * @access public
     345     *
     346     * @param array $sidebars_widgets
     347     */
     348    public function filter_option_sidebars_widgets_for_theme_switch( $sidebars_widgets ) {
     349        $sidebars_widgets = $GLOBALS['sidebars_widgets'];
     350        $sidebars_widgets['array_version'] = 3;
     351        return $sidebars_widgets;
     352    }
     353
     354    /**
    282355     * Make sure all widgets get loaded into the Customizer.
    283356     *
     
    348421
    349422            $new_setting_ids[] = $setting_id;
     423        }
     424
     425        /*
     426         * Add a setting which will be supplied for the theme's sidebars_widgets
     427         * theme_mod when the the theme is switched.
     428         */
     429        if ( ! $this->manager->is_theme_active() ) {
     430            $setting_id = 'old_sidebars_widgets_data';
     431            $setting_args = $this->get_setting_args( $setting_id, array(
     432                'type' => 'global_variable',
     433            ) );
     434            $this->manager->add_setting( $setting_id, $setting_args );
    350435        }
    351436
Note: See TracChangeset for help on using the changeset viewer.