Skip to content

Commit

Permalink
Add availability checks to widget related code (#15983)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jorgefilipecosta authored Jun 7, 2019
1 parent 42b2748 commit cbac39d
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions lib/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit cbac39d

Please sign in to comment.