Skip to content

Commit 42adb15

Browse files
committed
Strip widgets from sidebars_widgets[] settings if not registered
A widget added to a sidebar leaves its widget_id in the sidebar even when the widget goes away, for example due to a plugin deactivation. Now any such widget remants will get filtered out when sent to the client. Without this fix, a JavaScript error would be thrown: > Uncaught TypeError: Cannot read property 'transport' of undefined
1 parent 55146e0 commit 42adb15

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

widget-customizer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ static function customize_register( $wp_customize = null ) {
430430
} else {
431431
self::$sidebars_eligible_for_post_message[$sidebar_id] = ( 'postMessage' === self::get_sidebar_widgets_setting_transport( $sidebar_id ) );
432432
}
433-
$setting_args['sanitize_callback'] = array( __CLASS__, 'sanitize_sidebar_widgets' );
433+
$setting_args['sanitize_callback'] = array( __CLASS__, 'sanitize_sidebar_widgets' );
434+
$setting_args['sanitize_js_callback'] = array( __CLASS__, 'sanitize_sidebar_widgets_js_instance' );
434435
$wp_customize->add_setting( $setting_id, $setting_args );
435436
$new_setting_ids[] = $setting_id;
436437

@@ -1314,6 +1315,19 @@ static function sanitize_widget_js_instance( $value ) {
13141315
return $value;
13151316
}
13161317

1318+
/**
1319+
* Strip out widget IDs for widgets which are no longer registered, such
1320+
* as the case when a plugin orphans a widget in a sidebar when it is deactivated.
1321+
*
1322+
* @param array $widget_ids
1323+
* @return array
1324+
*/
1325+
static function sanitize_sidebar_widgets_js_instance( $widget_ids ) {
1326+
global $wp_registered_widgets;
1327+
$widget_ids = array_values( array_intersect( $widget_ids, array_keys( $wp_registered_widgets ) ) );
1328+
return $widget_ids;
1329+
}
1330+
13171331
/**
13181332
* Find and invoke the widget update and control callbacks. Requires that
13191333
* $_POST be populated with the instance data.

0 commit comments

Comments
 (0)