WordPress.org

Make WordPress Core

Ticket #27767: 27767-10.patch

File 27767-10.patch, 10.0 KB (added by westonruter, 7 years ago)

https://github.com/x-team/wordpress-develop/commit/af03194179956fdaa054b01db555baa4caf34d98

  • src/wp-admin/js/customize-widgets.js

    diff --git src/wp-admin/js/customize-widgets.js src/wp-admin/js/customize-widgets.js
    index 48ba335..f2782b9 100644
     
    942942                        params.action = 'update-widget';
    943943                        params.wp_customize = 'on';
    944944                        params.nonce = api.Widgets.data.nonce;
     945                        params.theme = api.settings.theme.stylesheet;
    945946
    946947                        data = $.param( params );
    947948                        $inputs = this._getInputs( $widgetContent );
     
    16141615                                widgetNumber = widget.get( 'multi_number' );
    16151616                        }
    16161617
    1617                         controlHtml = $( '#widget-tpl-' + widget.get( 'id' ) ).html();
     1618                        controlHtml = $.trim( $( '#widget-tpl-' + widget.get( 'id' ) ).html() );
    16181619                        if ( widget.get( 'is_multi' ) ) {
    16191620                                controlHtml = controlHtml.replace( /<[^<>]+>/g, function( m ) {
    16201621                                        return m.replace( /__i__|%i%/g, widgetNumber );
  • src/wp-includes/class-wp-customize-manager.php

    diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php
    index 18a1d76..8ed4e69 100644
    final class WP_Customize_Manager { 
    575575                        // to operate properly.
    576576                        $this->stop_previewing_theme();
    577577                        switch_theme( $this->get_stylesheet() );
     578                        update_option( 'theme_switched_via_customizer', true );
    578579                        $this->start_previewing_theme();
    579580                }
    580581
  • src/wp-includes/class-wp-customize-widgets.php

    diff --git src/wp-includes/class-wp-customize-widgets.php src/wp-includes/class-wp-customize-widgets.php
    index 72ff397..269e217 100644
    final class WP_Customize_Widgets { 
    6161        protected $rendered_widgets = array();
    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         *
    6673         * @since 3.9.0
    final class WP_Customize_Widgets { 
    7279                $this->manager = $manager;
    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 );
    7785                add_action( 'customize_controls_enqueue_scripts',      array( $this, 'enqueue_scripts' ) );
    final class WP_Customize_Widgets { 
    115123         * @since 3.9.0
    116124         *
    117125         * @access public
    118          * @global WP_Customize_Manager $wp_customize Customizer instance.
    119126         */
    120127        public function setup_widget_addition_previews() {
    121128                $is_customize_preview = false;
    final class WP_Customize_Widgets { 
    279286        }
    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                if ( 'customize.php' === $GLOBALS['pagenow'] && ! $this->manager->is_theme_active() ) {
     302                        $this->old_sidebars_widgets = wp_get_sidebars_widgets();
     303                        add_filter( 'customize_value_old_sidebars_widgets_data', array( $this, 'filter_customize_value_old_sidebars_widgets_data' ) );
     304
     305                        $GLOBALS['sidebars_widgets'] = $this->old_sidebars_widgets; // retrieve_widgets() looks at the global $sidebars_widgets
     306                        $GLOBALS['sidebars_widgets'] = retrieve_widgets( true, false );
     307                        add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 );
     308                }
     309        }
     310
     311        /**
     312         * Filter old_sidebars_widgets_data customizer setting.
     313         *
     314         * When switching themes, filter the Customizer setting
     315         * old_sidebars_widgets_data to supply initial $sidebars_widgets before they
     316         * were overridden by retrieve_widgets(). The value for
     317         * old_sidebars_widgets_data gets set in the old theme's sidebars_widgets
     318         * theme_mod.
     319         *
     320         * @see WP_Customize_Widgets::handle_theme_switch()
     321         * @since 3.9.0
     322         * @access public
     323         *
     324         * @param array $sidebars_widgets
     325         */
     326        public function filter_customize_value_old_sidebars_widgets_data( $old_sidebars_widgets ) {
     327                $old_sidebars_widgets = $this->old_sidebars_widgets;
     328                return $old_sidebars_widgets;
     329        }
     330
     331        /**
     332         * Filter sidebars_widgets option for theme switch.
     333         *
     334         * When switching themes, the retrieve_widgets() function is run when the
     335         * Customizer initializes, and then the new sidebars_widgets here get
     336         * supplied as the default value for the sidebars_widgets option.
     337         *
     338         * @see WP_Customize_Widgets::handle_theme_switch()
     339         * @since 3.9.0
     340         * @access public
     341         *
     342         * @param array $sidebars_widgets
     343         */
     344        public function filter_option_sidebars_widgets_for_theme_switch( $sidebars_widgets ) {
     345                $sidebars_widgets = $GLOBALS['sidebars_widgets'];
     346                $sidebars_widgets['array_version'] = 3;
     347                return $sidebars_widgets;
     348        }
     349
     350        /**
    282351         * Make sure all widgets get loaded into the Customizer.
    283352         *
    284353         * Note: these actions are also fired in wp_ajax_update_widget().
    final class WP_Customize_Widgets { 
    349418                        $new_setting_ids[] = $setting_id;
    350419                }
    351420
     421                /*
     422                 * Add a setting which will be supplied for the theme's sidebars_widgets
     423                 * theme_mod when the the theme is switched.
     424                 */
     425                if ( ! $this->manager->is_theme_active() ) {
     426                        $setting_id = 'old_sidebars_widgets_data';
     427                        $setting_args = $this->get_setting_args( $setting_id, array(
     428                                'type' => 'global_variable',
     429                        ) );
     430                        $this->manager->add_setting( $setting_id, $setting_args );
     431                }
     432
    352433                foreach ( $sidebars_widgets as $sidebar_id => $sidebar_widget_ids ) {
    353434                        if ( empty( $sidebar_widget_ids ) ) {
    354435                                $sidebar_widget_ids = array();
  • src/wp-includes/theme.php

    diff --git src/wp-includes/theme.php src/wp-includes/theme.php
    index f66fd95..39ffc6d 100644
    function preview_theme_ob_filter_callback( $matches ) { 
    752752 * @param string $stylesheet Stylesheet name
    753753 */
    754754function switch_theme( $stylesheet ) {
    755         global $wp_theme_directories, $sidebars_widgets;
     755        global $wp_theme_directories, $wp_customize, $sidebars_widgets;
    756756
    757         if ( is_array( $sidebars_widgets ) )
    758                 set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
     757        $_sidebars_widgets = null;
     758        if ( current_action() === 'wp_ajax_customize_save' ) {
     759                $_sidebars_widgets = $wp_customize->post_value( $wp_customize->get_setting( 'old_sidebars_widgets_data' ) );
     760        } else if ( is_array( $sidebars_widgets ) ) {
     761                $_sidebars_widgets = $sidebars_widgets;
     762        }
     763        if ( is_array( $_sidebars_widgets ) ) {
     764                set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) );
     765        }
    759766
    760767        $old_theme  = wp_get_theme();
    761768        $new_theme = wp_get_theme( $stylesheet );
    function switch_theme( $stylesheet ) { 
    785792        if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
    786793                $default_theme_mods = (array) get_option( 'mods_' . $new_name );
    787794                add_option( "theme_mods_$stylesheet", $default_theme_mods );
     795        } else {
     796                // Since retrieve_widgets() is called when initializing the customizer theme, we need to remove the theme mods to avoid overwriting changes made via the widget customizer when accessing /wp-admin/widgets.php
     797                if ( current_action() === 'wp_ajax_customize_save' ) {
     798                        remove_theme_mod( 'sidebars_widgets' );
     799                }
    788800        }
    789801
    790802        update_option( 'theme_switched', $old_theme->get_stylesheet() );
    function check_theme_switched() { 
    17911803        if ( $stylesheet = get_option( 'theme_switched' ) ) {
    17921804                $old_theme = wp_get_theme( $stylesheet );
    17931805
     1806                // Prevent retrieve_widgets() from running since Customizer already called it up front
     1807                if ( get_option( 'theme_switched_via_customizer' ) ) {
     1808                        remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
     1809                        update_option( 'theme_switched_via_customizer', false );
     1810                }
     1811
    17941812                if ( $old_theme->exists() ) {
    17951813                        /**
    17961814                         * Fires on the first WP load after a theme switch if the old theme still exists.
    function wp_customize_support_script() { 
    19181936                }());
    19191937        </script>
    19201938        <?php
    1921 }
    1922  No newline at end of file
     1939}
  • src/wp-includes/widgets.php

    diff --git src/wp-includes/widgets.php src/wp-includes/widgets.php
    index b911fde..b042199 100644
    function _wp_sidebars_changed() { 
    14011401        retrieve_widgets(true);
    14021402}
    14031403
    1404 // look for "lost" widgets, this has to run at least on each theme change
    1405 function retrieve_widgets($theme_changed = false) {
     1404/**
     1405 * Look for "lost" widgets, this has to run at least on each theme change.
     1406 *
     1407 * @since 2.8.0
     1408 *
     1409 * @param bool $theme_changed  Whether the theme was changed.
     1410 * @param bool $update  Whether to update the theme_mods. False is used for deferred update in Customizer.
     1411 *
     1412 * @return array
     1413 */
     1414function retrieve_widgets( $theme_changed = false, $update = true ) {
    14061415        global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
    14071416
    14081417        $registered_sidebar_keys = array_keys( $wp_registered_sidebars );
    function retrieve_widgets($theme_changed = false) { 
    14121421        if ( is_array( $old_sidebars_widgets ) ) {
    14131422                // time() that sidebars were stored is in $old_sidebars_widgets['time']
    14141423                $_sidebars_widgets = $old_sidebars_widgets['data'];
    1415                 remove_theme_mod( 'sidebars_widgets' );
     1424
     1425                if ( $update ) {
     1426                        remove_theme_mod( 'sidebars_widgets' );
     1427                }
    14161428
    14171429                foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
    14181430                        if ( 'wp_inactive_widgets' == $sidebar || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) )
    function retrieve_widgets($theme_changed = false) { 
    14951507        }
    14961508
    14971509        $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
    1498         wp_set_sidebars_widgets($sidebars_widgets);
     1510        if ( $update ) {
     1511                wp_set_sidebars_widgets( $sidebars_widgets );
     1512        }
    14991513
    15001514        return $sidebars_widgets;
    15011515}