Make WordPress Core

Ticket #27805: 27805-01.patch

File 27805-01.patch, 5.5 KB (added by westonruter, 11 years ago)

https://github.com/x-team/wordpress-develop/commit/75b430546e368745a54dfebe51377c6724dd86ac https://github.com/x-team/wordpress-develop/pull/11

  • 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 049cedd..4821dd9 100644
    final class WP_Customize_Widgets { 
    148148
    149149                // Input from customizer preview.
    150150                if ( isset( $_POST['customized'] ) ) {
    151                         $customized = json_decode( $this->get_post_value( 'customized' ), true );
     151                        $this->_customized = json_decode( $this->get_post_value( 'customized' ), true );
    152152                } else { // Input from ajax widget update request.
    153                         $customized    = array();
    154                         $id_base       = $this->get_post_value( 'id_base' );
    155                         $widget_number = (int) $this->get_post_value( 'widget_number' );
    156                         $option_name   = 'widget_' . $id_base;
    157                         $customized[$option_name] = array();
    158                         if ( false !== $widget_number ) {
     153                        $this->_customized = array();
     154                        $id_base = $this->get_post_value( 'id_base' );
     155                        $widget_number = $this->get_post_value( 'widget_number', false );
     156                        $option_name = 'widget_' . $id_base;
     157                        $this->_customized[ $option_name ] = array();
     158                        if ( preg_match( '/^[0-9]+$/', $widget_number ) ) {
    159159                                $option_name .= '[' . $widget_number . ']';
    160                                 $customized[$option_name][$widget_number] = array();
     160                                $this->_customized[ $option_name ][ $widget_number ] = array();
    161161                        }
    162162                }
    163163
    final class WP_Customize_Widgets { 
    171171                add_filter( $hook, $function );
    172172                $this->_prepreview_added_filters[] = compact( 'hook', 'function' );
    173173
    174                 foreach ( $customized as $setting_id => $value ) {
    175                         if ( preg_match( '/^(widget_.+?)(\[(\d+)\])?$/', $setting_id, $matches ) ) {
    176 
    177                                 /*
    178                                  * @todo Replace the next two lines with the following once WordPress supports PHP 5.3.
    179                                  *
    180                                  * $self = $this; // not needed in PHP 5.4
    181                                  *
    182                                  * $function = function ( $value ) use ( $self, $setting_id ) {
    183                                  *              return $self->manager->widgets->prepreview_added_widget_instance( $value, $setting_id );
    184                                  * };
    185                                  */
    186                                 $body     = sprintf( 'global $wp_customize; return $wp_customize->widgets->prepreview_added_widget_instance( $value, %s );', var_export( $setting_id, true ) );
    187                                 $function = create_function( '$value', $body );
    188 
     174                $function = array( $this, 'prepreview_added_widget_instance' );
     175                foreach ( $this->_customized as $setting_id => $value ) {
     176                        if ( preg_match( '/^(widget_.+?)(?:\[(\d+)\])?$/', $setting_id, $matches ) ) {
    189177                                $option = $matches[1];
    190178
    191179                                $hook = sprintf( 'option_%s', $option );
    192                                 add_filter( $hook, $function );
    193                                 $this->_prepreview_added_filters[] = compact( 'hook', 'function' );
     180                                if ( ! has_filter( $hook, $function ) ) {
     181                                        add_filter( $hook, $function );
     182                                        $this->_prepreview_added_filters[] = compact( 'hook', 'function' );
     183                                }
    194184
    195185                                $hook = sprintf( 'default_option_%s', $option );
    196                                 add_filter( $hook, $function );
    197                                 $this->_prepreview_added_filters[] = compact( 'hook', 'function' );
     186                                if ( ! has_filter( $hook, $function ) ) {
     187                                        add_filter( $hook, $function );
     188                                        $this->_prepreview_added_filters[] = compact( 'hook', 'function' );
     189                                }
    198190
    199191                                /*
    200192                                 * Make sure the option is registered so that the update_option()
    final class WP_Customize_Widgets { 
    204196                                add_option( $option, array() );
    205197                        }
    206198                }
    207 
    208                 $this->_customized = $customized;
    209199        }
    210200
    211201        /**
    final class WP_Customize_Widgets { 
    225215                foreach ( $this->_customized as $setting_id => $value ) {
    226216                        if ( preg_match( '/^sidebars_widgets\[(.+?)\]$/', $setting_id, $matches ) ) {
    227217                                $sidebar_id = $matches[1];
    228                                 $sidebars_widgets[$sidebar_id] = $value;
     218                                $sidebars_widgets[ $sidebar_id ] = $value;
    229219                        }
    230220                }
    231221                return $sidebars_widgets;
    final class WP_Customize_Widgets { 
    242232         * @since 3.9.0
    243233         * @access public
    244234         *
    245          * @param array $instance    Widget instance.
    246          * @param string $setting_id Widget setting ID.
    247          * @return array Parsed widget instance.
     235         * @param array $value Widget instance(s).
     236         * @return array Widget instance(s) with additions.
    248237         */
    249         public function prepreview_added_widget_instance( $instance, $setting_id ) {
    250                 if ( isset( $this->_customized[$setting_id] ) ) {
     238        public function prepreview_added_widget_instance( $value ) {
     239                if ( ! preg_match( '/^(?:default_)?option_(widget_(.+))/', current_filter(), $matches ) ) {
     240                        return $value;
     241                }
     242                $id_base = $matches[2];
     243
     244                foreach ( $this->_customized as $setting_id => $setting ) {
    251245                        $parsed_setting_id = $this->parse_widget_setting_id( $setting_id );
    252                         $widget_number     = $parsed_setting_id['number'];
     246                        if ( is_wp_error( $parsed_setting_id ) || $id_base !== $parsed_setting_id['id_base'] ) {
     247                                continue;
     248                        }
     249                        $widget_number = $parsed_setting_id['number'];
    253250
    254251                        // Single widget.
    255                         if ( is_null( $widget_number ) ) {
    256                                 if ( false === $instance && empty( $value ) ) {
    257                                         $instance = array();
    258                                 }
    259                         } else if ( false === $instance || ! isset( $instance[$widget_number] ) ) { // Multi widget
    260                                 if ( empty( $instance ) ) {
    261                                         $instance = array( '_multiwidget' => 1 );
     252                        if ( ! $widget_number ) {
     253                                $value = array();
     254                        } else if ( false === $value || ! isset( $value[ $widget_number ] ) ) { // Multi widget
     255                                if ( empty( $value ) ) {
     256                                        $value = array( '_multiwidget' => 1 );
    262257                                }
    263                                 if ( ! isset( $instance[$widget_number] ) ) {
    264                                         $instance[$widget_number] = array();
     258                                if ( ! isset( $value[ $widget_number ] ) ) {
     259                                        $value[ $widget_number ] = array();
    265260                                }
    266261                        }
    267262                }
    268                 return $instance;
     263
     264                return $value;
    269265        }
    270266
    271267        /**