Make WordPress Core

Ticket #27805: 27805-02.patch

File 27805-02.patch, 6.0 KB (added by westonruter, 11 years ago)

Simplify logic for prepreview_added_widget_instance. (And re-use JS variable instead of running another query.) https://github.com/x-team/wordpress-develop/commit/c43440262a5f16dd0518296b57c1c054689013ae

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

    diff --git src/wp-admin/js/customize-widgets.js src/wp-admin/js/customize-widgets.js
    index f2782b9..47505dd 100644
     
    722722                        }, 250 );
    723723
    724724                        // Trigger widget form update when hitting Enter within an input
    725                         this.container.find( '.widget-content' ).on( 'keydown', 'input', function( e ) {
     725                        $widgetContent.on( 'keydown', 'input', function( e ) {
    726726                                if ( 13 === e.which ) { // Enter
    727727                                        e.preventDefault();
    728728                                        self.updateWidget( { ignoreActiveElement: true } );
  • 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..42f88d0 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|bool|mixed $value Widget instance(s), false if open was empty.
     236         * @return array|mixed 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 = false ) {
     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
    254                         // Single widget.
    255251                        if ( is_null( $widget_number ) ) {
    256                                 if ( false === $instance && empty( $value ) ) {
    257                                         $instance = array();
     252                                // Single widget.
     253                                if ( false === $value ) {
     254                                        $value = array();
    258255                                }
    259                         } else if ( false === $instance || ! isset( $instance[$widget_number] ) ) { // Multi widget
    260                                 if ( empty( $instance ) ) {
    261                                         $instance = array( '_multiwidget' => 1 );
     256                        } else {
     257                                // Multi widget.
     258                                if ( empty( $value ) ) {
     259                                        $value = array( '_multiwidget' => 1 );
    262260                                }
    263                                 if ( ! isset( $instance[$widget_number] ) ) {
    264                                         $instance[$widget_number] = array();
     261                                if ( ! isset( $value[ $widget_number ] ) ) {
     262                                        $value[ $widget_number ] = array();
    265263                                }
    266264                        }
    267265                }
    268                 return $instance;
     266
     267                return $value;
    269268        }
    270269
    271270        /**