@@ -332,8 +332,6 @@ wp.customize.widgetsPreview = wp.customize.WidgetCustomizerPreview = (function(
332332 *
333333 * @since 4.5.0
334334 *
335- * @todo This is an expensive operation. Optimize.
336- *
337335 * @param {string } widgetId
338336 * @returns {wp.customize.Partial } Widget instance partial.
339337 */
@@ -349,48 +347,37 @@ wp.customize.widgetsPreview = wp.customize.WidgetCustomizerPreview = (function(
349347
350348// Make sure that there is a container element for the widget in the sidebar, if at least a placeholder.
351349_ . each ( sidebarPartial . placements ( ) , function ( widgetAreaPlacement ) {
352- var widgetContainerElement ;
350+ var foundWidgetPlacement , widgetContainerElement ;
353351
354- // @todo Why are we doing this? We can just look for the placement that has a matching instanceNumber and sidebarId, right?
355- _ . each ( widgetPartial . placements ( ) , function ( widgetInstancePlacement ) {
356- var elementNode , isBounded ;
357- if ( ! widgetInstancePlacement . container ) {
358- return ;
359- }
360- elementNode = widgetInstancePlacement . container [ 0 ] ;
361- isBounded = (
362- ( widgetAreaPlacement . startNode . compareDocumentPosition ( elementNode ) & Node . DOCUMENT_POSITION_FOLLOWING ) &&
363- ( widgetAreaPlacement . endNode . compareDocumentPosition ( elementNode ) & Node . DOCUMENT_POSITION_PRECEDING )
364- ) ;
365- if ( isBounded ) {
366- widgetContainerElement = widgetInstancePlacement . container ;
367- }
352+ foundWidgetPlacement = _ . find ( widgetPartial . placements ( ) , function ( widgetInstancePlacement ) {
353+ return ( widgetInstancePlacement . context . sidebar_instance_number === widgetAreaPlacement . context . instanceNumber ) ;
368354} ) ;
355+ if ( foundWidgetPlacement ) {
356+ return ;
357+ }
369358
370- if ( ! widgetContainerElement ) {
371- widgetContainerElement = $ (
372- sidebarPartial . params . sidebarArgs . before_widget . replace ( '%1$s' , widgetId ) . replace ( '%2$s' , 'widget' ) +
373- sidebarPartial . params . sidebarArgs . after_widget
374- ) ;
375-
376- widgetContainerElement . attr ( 'data-customize-partial-id' , widgetPartial . id ) ;
377- widgetContainerElement . attr ( 'data-customize-partial-type' , 'widget_instance' ) ;
378- widgetContainerElement . attr ( 'data-customize-widget-id' , widgetId ) ;
379-
380- /*
381- * Make sure the widget container element has the customize-container context data.
382- * The sidebar_instance_number is used to disambiguate multiple instances of the
383- * same sidebar are rendered onto the template, and so the same widget is embedded
384- * multiple times.
385- */
386- widgetContainerElement . data ( 'customize-partial-placement-context' , {
387- 'sidebar_id' : sidebarPartial . sidebarId ,
388- 'sidebar_instance_number' : widgetAreaPlacement . context . instanceNumber
389- } ) ;
359+ widgetContainerElement = $ (
360+ sidebarPartial . params . sidebarArgs . before_widget . replace ( '%1$s' , widgetId ) . replace ( '%2$s' , 'widget' ) +
361+ sidebarPartial . params . sidebarArgs . after_widget
362+ ) ;
390363
391- widgetAreaPlacement . endNode . parentNode . insertBefore ( widgetContainerElement [ 0 ] , widgetAreaPlacement . endNode ) ;
392- wasInserted = true ;
393- }
364+ widgetContainerElement . attr ( 'data-customize-partial-id' , widgetPartial . id ) ;
365+ widgetContainerElement . attr ( 'data-customize-partial-type' , 'widget_instance' ) ;
366+ widgetContainerElement . attr ( 'data-customize-widget-id' , widgetId ) ;
367+
368+ /*
369+ * Make sure the widget container element has the customize-container context data.
370+ * The sidebar_instance_number is used to disambiguate multiple instances of the
371+ * same sidebar are rendered onto the template, and so the same widget is embedded
372+ * multiple times.
373+ */
374+ widgetContainerElement . data ( 'customize-partial-placement-context' , {
375+ 'sidebar_id' : sidebarPartial . sidebarId ,
376+ 'sidebar_instance_number' : widgetAreaPlacement . context . instanceNumber
377+ } ) ;
378+
379+ widgetAreaPlacement . endNode . parentNode . insertBefore ( widgetContainerElement [ 0 ] , widgetAreaPlacement . endNode ) ;
380+ wasInserted = true ;
394381} ) ;
395382
396383if ( wasInserted ) {
0 commit comments