Make WordPress Core

Ticket #27451: 27451.2.patch

File 27451.2.patch, 4.3 KB (added by westonruter, 11 years ago)

Update a search_matched property on the WidgetModel instead of filtering out widgets from availableWidgets; otherwise, the incremented multi_number gets lost, and only one instance of a multi-widget can be added. This also fixes a bug with a widget area losing its sortability after adding a widget via a search. Also, don't equate an empty array with an error when the instance is passed to the update_widget Ajax request. https://github.com/x-team/wordpress-develop/pull/9/files

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

    diff --git src/wp-admin/js/customize-widgets.js src/wp-admin/js/customize-widgets.js
    index 0f42db3..48ba335 100644
     
    3636                transport: 'refresh',
    3737                params: [],
    3838                width: null,
    39                 height: null
     39                height: null,
     40                search_matched: true
    4041        });
    4142
    4243        /**
     
    7172                        // If search is blank, show all themes
    7273                        // Useful for resetting the views when you clean the input
    7374                        if ( this.terms === '' ) {
    74                                 this.reset( api.Widgets.data.availableWidgets );
     75                                this.each( function ( widget ) {
     76                                        widget.set( 'search_matched', true );
     77                                } );
    7578                        }
    76 
    77                         // Trigger an 'update' event
    78                         this.trigger( 'update' );
    7979                },
    8080
    8181                // Performs a search within the collection
    8282                // @uses RegExp
    8383                search: function( term ) {
    84                         var match, results, haystack;
    85 
    86                         // Start with a full collection
    87                         this.reset( api.Widgets.data.availableWidgets, { silent: true } );
     84                        var match, haystack;
    8885
    8986                        // Escape the term string for RegExp meta characters
    9087                        term = term.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' );
     
    9491                        term = term.replace( / /g, ')(?=.*' );
    9592                        match = new RegExp( '^(?=.*' + term + ').+', 'i' );
    9693
    97                         results = this.filter( function( data ) {
    98                                 haystack = _.union( data.get( 'name' ), data.get( 'id' ), data.get( 'description' ) );
    99 
    100                                 return match.test( haystack );
    101                         });
    102 
    103                         this.reset( results );
     94                        this.each( function ( data ) {
     95                                haystack = [ data.get( 'name' ), data.get( 'id' ), data.get( 'description' ) ].join( ' ' );
     96                                data.set( 'search_matched', match.test( haystack ) );
     97                        } );
    10498                }
    10599        });
    106100        api.Widgets.availableWidgets = new api.Widgets.WidgetCollection( api.Widgets.data.availableWidgets );
     
    176170
    177171                        _.bindAll( this, 'close' );
    178172
    179                         this.listenTo( this.collection, 'update', this.updateList );
    180173                        this.listenTo( this.collection, 'change', this.updateList );
    181174
    182175                        this.updateList();
     
    222215                        }
    223216                },
    224217
    225                 // Changes visibilty of available widgets
     218                // Changes visibility of available widgets
    226219                updateList: function() {
    227                         // First hide all widgets...
    228                         this.$el.find( '.widget-tpl' ).hide();
    229 
    230                         // ..and then show only available widgets which could be filtered
    231220                        this.collection.each( function( widget ) {
    232221                                var widgetTpl = $( '#widget-tpl-' + widget.id );
    233                                 widgetTpl.toggle( ! widget.get( 'is_disabled' ) );
     222                                widgetTpl.toggle( widget.get( 'search_matched' ) && ! widget.get( 'is_disabled' ) );
    234223                                if ( widget.get( 'is_disabled' ) && widgetTpl.is( this.selected ) ) {
    235224                                        this.selected = null;
    236225                                }
    237226                        } );
    238227                },
    239228
    240                 // Hightlights a widget
     229                // Highlights a widget
    241230                select: function( widgetTpl ) {
    242231                        this.selected = $( widgetTpl );
    243232                        this.selected.siblings( '.widget-tpl' ).removeClass( 'selected' );
    244233                        this.selected.addClass( 'selected' );
    245234                },
    246235
    247                 // Hightlights a widget on focus
     236                // Highlights a widget on focus
    248237                focus: function( event ) {
    249238                        this.select( $( event.currentTarget ) );
    250239                },
     
    16011590
    16021591                /**
    16031592                 * @param {string} widgetId or an id_base for adding a previously non-existing widget
    1604                  * @returns {object} widget_form control instance
     1593                 * @returns {object|false} widget_form control instance, or false on error
    16051594                 */
    16061595                addWidget: function( widgetId ) {
    16071596                        var self = this, controlHtml, $widget, controlType = 'widget_form', $control, controlConstructor,
     
    16121601                                settingId, isExistingWidget, widgetFormControl, sidebarWidgets, settingArgs;
    16131602
    16141603                        if ( ! widget ) {
    1615                                 return;
     1604                                return false;
    16161605                        }
    16171606
    16181607                        if ( widgetNumber && ! widget.get( 'is_multi' ) ) {
    1619                                 return;
     1608                                return false;
    16201609                        }
    16211610
    16221611                        // Set up new multi widget
  • 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 adeb3f0..72ff397 100644
    final class WP_Customize_Widgets { 
    11501150                $added_input_vars = array();
    11511151                if ( ! empty( $_POST['sanitized_widget_setting'] ) ) {
    11521152                        $sanitized_widget_setting = json_decode( $this->get_post_value( 'sanitized_widget_setting' ), true );
    1153                         if ( empty( $sanitized_widget_setting ) ) {
     1153                        if ( false === $sanitized_widget_setting ) {
    11541154                                $this->stop_capturing_option_updates();
    11551155                                return new WP_Error( 'widget_setting_malformed' );
    11561156                        }