WordPress.org

Make WordPress Core

Ticket #27767: 27767-08.patch

File 27767-08.patch, 9.3 KB (added by gcorne, 7 years ago)
  • 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..3b4db65 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 { 
    7178        public function __construct( $manager ) {
    7279                $this->manager = $manager;
    7380
     81                add_action( 'widgets_init',                            array( $this, 'handle_theme_switch' ), 10000 );
    7482                add_action( 'after_setup_theme',                       array( $this, 'setup_widget_addition_previews' ) );
    7583                add_action( 'customize_controls_init',                 array( $this, 'customize_controls_init' ) );
    7684                add_action( 'customize_register',                      array( $this, 'schedule_customize_register' ), 1 );
    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() {
     128
     129
     130
    121131                $is_customize_preview = false;
    122132
    123133                if ( ! empty( $this->manager ) && ! is_admin() && 'on' === $this->get_post_value( 'wp_customize' ) ) {
    final class WP_Customize_Widgets { 
    279289        }
    280290
    281291        /**
     292         * Handle theme switch
     293         *
     294         * When switching a theme via the customizer, supply any previously-configured
     295         * sidebars_widgets from the target theme as the initial sidebars_widgets
     296         * setting. Also store the old theme's existing settings so that they can
     297         * be passed along for storing in the sidebars_widgets theme_mod when the
     298         * theme gets switched.
     299         *
     300         * @since 3.9.0
     301         * @access public
     302         */
     303        public function handle_theme_switch() {
     304                if ( 'customize.php' === $GLOBALS['pagenow'] && ! $this->manager->is_theme_active() ) {
     305                        $this->old_sidebars_widgets = wp_get_sidebars_widgets();
     306                        add_filter( 'customize_value_old_sidebars_widgets_data', array( $this, 'filter_customize_value_old_sidebars_widgets_data' ) );
     307
     308                        $GLOBALS['sidebars_widgets'] = $this->old_sidebars_widgets; // retrieve_widgets() looks at the global $sidebars_widgets
     309                        $GLOBALS['sidebars_widgets'] = retrieve_widgets( true, false );
     310                        add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 );
     311                }
     312        }
     313
     314        /**
     315         *
     316         * @see WP_Customize_Widgets::handle_theme_switch()
     317         * @since 3.9.0
     318         * @access public
     319         *
     320         * @param array $sidebars_widgets
     321         */
     322        public function filter_customize_value_old_sidebars_widgets_data( $old_sidebars_widgets ) {
     323                $old_sidebars_widgets = $this->old_sidebars_widgets;
     324                return $old_sidebars_widgets;
     325        }
     326
     327        /**
     328         *
     329         * @see WP_Customize_Widgets::handle_theme_switch()
     330         * @since 3.9.0
     331         * @access public
     332         *
     333         * @param array $sidebars_widgets
     334         */
     335        public function filter_option_sidebars_widgets_for_theme_switch( $sidebars_widgets ) {
     336                $sidebars_widgets = $GLOBALS['sidebars_widgets'];
     337                $sidebars_widgets['array_version'] = 3;
     338                return $sidebars_widgets;
     339        }
     340
     341        /**
    282342         * Make sure all widgets get loaded into the Customizer.
    283343         *
    284344         * Note: these actions are also fired in wp_ajax_update_widget().
    final class WP_Customize_Widgets { 
    349409                        $new_setting_ids[] = $setting_id;
    350410                }
    351411
     412                /**
     413                 * Add a setting which will be supplied for the theme's sidebars_widgets
     414                 * theme_mod when the the theme is switched.
     415                 */
     416                if ( ! $this->manager->is_theme_active() ) {
     417                        $setting_id = 'old_sidebars_widgets_data';
     418                        $setting_args = $this->get_setting_args( $setting_id, array(
     419                                'type' => 'global_variable',
     420                        ) );
     421                        $this->manager->add_setting( $setting_id, $setting_args );
     422                }
     423
    352424                foreach ( $sidebars_widgets as $sidebar_id => $sidebar_widget_ids ) {
    353425                        if ( empty( $sidebar_widget_ids ) ) {
    354426                                $sidebar_widget_ids = array();
  • src/wp-includes/theme.php

    diff --git src/wp-includes/theme.php src/wp-includes/theme.php
    index f66fd95..5b81f1f 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                /**
     1807                 * Prevent retrieve_widgets() from running since Customizer already called it up front
     1808                 */
     1809                if ( get_option( 'theme_switched_via_customizer' ) ) {
     1810                        remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
     1811                        update_option( 'theme_switched_via_customizer', false );
     1812                }
     1813
    17941814                if ( $old_theme->exists() ) {
    17951815                        /**
    17961816                         * Fires on the first WP load after a theme switch if the old theme still exists.
    function wp_customize_support_script() { 
    19181938                }());
    19191939        </script>
    19201940        <?php
    1921 }
    1922  No newline at end of file
     1941}
  • src/wp-includes/widgets.php

    diff --git src/wp-includes/widgets.php src/wp-includes/widgets.php
    index b911fde..aad8870 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 * @param bool $theme_changed
     1408 * @param bool $update
     1409 *
     1410 * @return array
     1411 */
     1412function retrieve_widgets( $theme_changed = false, $update = true ) {
    14061413        global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
    14071414
    14081415        $registered_sidebar_keys = array_keys( $wp_registered_sidebars );
    function retrieve_widgets($theme_changed = false) { 
    14121419        if ( is_array( $old_sidebars_widgets ) ) {
    14131420                // time() that sidebars were stored is in $old_sidebars_widgets['time']
    14141421                $_sidebars_widgets = $old_sidebars_widgets['data'];
    1415                 remove_theme_mod( 'sidebars_widgets' );
     1422
     1423                if ( $update ) {
     1424                        remove_theme_mod( 'sidebars_widgets' );
     1425                }
    14161426
    14171427                foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
    14181428                        if ( 'wp_inactive_widgets' == $sidebar || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) )
    function retrieve_widgets($theme_changed = false) { 
    14951505        }
    14961506
    14971507        $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
    1498         wp_set_sidebars_widgets($sidebars_widgets);
     1508        if ( $update ) {
     1509                wp_set_sidebars_widgets( $sidebars_widgets );
     1510        }
    14991511
    15001512        return $sidebars_widgets;
    15011513}