From cbac39d35e0dc2bd2a6906ffaa9b621bb8fd0b9b Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Sat, 8 Jun 2019 00:09:56 +0100 Subject: [PATCH] Add availability checks to widget related code (#15983) This commit adds availability checks for the function get_current_screen and for the globals wp_widget_factory, wp_registered_widgets. The function get_current_screen should only be called in admin pages, but according to the docs, we may have admin pages without the function so I think a check should be added. For example, I guess it may be possible to have an admin page with admin_footer but without the function defined. Regarding the globals wp_widget_factory, wp_registered_widgets used in gutenberg_get_legacy_widget_settings function, the function is called as part of the filter block_editor_settings, I guess the filter may be executed in a situation where the widget globals are not initialized e.g: in a custom plugin page with a custom block editor. I think it is better to check the availability of the globals. If the globals are not available we will not pass available legacy widgets to the front end. --- lib/widgets.php | 60 ++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/lib/widgets.php b/lib/widgets.php index 7ea5d3a5e770d..3c528b1446927 100644 --- a/lib/widgets.php +++ b/lib/widgets.php @@ -11,6 +11,11 @@ * @return boolean True if a screen containing the block editor is being loaded. */ function gutenberg_is_block_editor() { + // If get_current_screen does not exist, we are neither in the standard block editor for posts, or the widget block editor. + // We can safely return false. + if ( ! function_exists( 'get_current_screen' ) ) { + return false; + } $screen = get_current_screen(); return $screen->is_block_editor() || 'gutenberg_page_gutenberg-widgets' === $screen->id; } @@ -100,33 +105,38 @@ function gutenberg_get_legacy_widget_settings() { $has_permissions_to_manage_widgets = current_user_can( 'edit_theme_options' ); $available_legacy_widgets = array(); - global $wp_widget_factory, $wp_registered_widgets; - foreach ( $wp_widget_factory->widgets as $class => $widget_obj ) { - $available_legacy_widgets[ $class ] = array( - 'name' => html_entity_decode( $widget_obj->name ), - // wp_widget_description is not being used because its input parameter is a Widget Id. - // Widgets id's reference to a specific widget instance. - // Here we are iterating on all the available widget classes even if no widget instance exists for them. - 'description' => isset( $widget_obj->widget_options['description'] ) ? - html_entity_decode( $widget_obj->widget_options['description'] ) : - null, - 'isCallbackWidget' => false, - 'isHidden' => in_array( $class, $core_widgets, true ), - ); + global $wp_widget_factory; + if ( ! empty( $wp_widget_factory ) ) { + foreach ( $wp_widget_factory->widgets as $class => $widget_obj ) { + $available_legacy_widgets[ $class ] = array( + 'name' => html_entity_decode( $widget_obj->name ), + // wp_widget_description is not being used because its input parameter is a Widget Id. + // Widgets id's reference to a specific widget instance. + // Here we are iterating on all the available widget classes even if no widget instance exists for them. + 'description' => isset( $widget_obj->widget_options['description'] ) ? + html_entity_decode( $widget_obj->widget_options['description'] ) : + null, + 'isCallbackWidget' => false, + 'isHidden' => in_array( $class, $core_widgets, true ), + ); + } } - foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) { - if ( - is_array( $widget_obj['callback'] ) && - isset( $widget_obj['callback'][0] ) && - ( $widget_obj['callback'][0] instanceof WP_Widget ) - ) { - continue; + global $wp_registered_widgets; + if ( ! empty( $wp_registered_widgets ) ) { + foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) { + if ( + is_array( $widget_obj['callback'] ) && + isset( $widget_obj['callback'][0] ) && + ( $widget_obj['callback'][0] instanceof WP_Widget ) + ) { + continue; + } + $available_legacy_widgets[ $widget_id ] = array( + 'name' => html_entity_decode( $widget_obj['name'] ), + 'description' => html_entity_decode( wp_widget_description( $widget_id ) ), + 'isCallbackWidget' => true, + ); } - $available_legacy_widgets[ $widget_id ] = array( - 'name' => html_entity_decode( $widget_obj['name'] ), - 'description' => html_entity_decode( wp_widget_description( $widget_id ) ), - 'isCallbackWidget' => true, - ); } $settings['hasPermissionsToManageWidgets'] = $has_permissions_to_manage_widgets;