Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate on Legacy Widget block #29960

Merged
merged 10 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bin/build-plugin-zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ build_files=$(
build/block-library/blocks/*.php \
build/block-library/blocks/*/block.json \
build/block-library/blocks/*/*.css \
build/edit-widgets/blocks/*.php \
build/edit-widgets/blocks/*/block.json \
)

Expand Down
5 changes: 2 additions & 3 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function gutenberg_reregister_core_block_types() {
'cover.php' => 'core/cover',
'latest-comments.php' => 'core/latest-comments',
'latest-posts.php' => 'core/latest-posts',
'legacy-widget.php' => 'core/legacy-widget',
'loginout.php' => 'core/loginout',
'navigation.php' => 'core/navigation',
'navigation-link.php' => 'core/navigation-link',
Expand Down Expand Up @@ -97,12 +98,10 @@ function gutenberg_reregister_core_block_types() {
),
__DIR__ . '/../build/edit-widgets/blocks/' => array(
'block_folders' => array(
'legacy-widget',
'widget-area',
),
'block_names' => array(
'legacy-widget.php' => 'core/legacy-widget',
'widget-area.php' => 'core/widget-area',
'widget-area.php' => 'core/widget-area',
),
),
);
Expand Down
50 changes: 21 additions & 29 deletions lib/class-wp-rest-widgets-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,14 @@ public function get_item_permissions_check( $request ) { // phpcs:ignore Variabl
*/
public function get_item( $request ) {
$widget_id = $request['id'];
$sidebar_id = $this->find_widgets_sidebar( $widget_id );
$sidebar_id = gutenberg_find_widgets_sidebar( $widget_id );

if ( is_wp_error( $sidebar_id ) ) {
return $sidebar_id;
if ( is_null( $sidebar_id ) ) {
return new WP_Error(
'rest_widget_not_found',
__( 'No widget was found with that id.', 'gutenberg' ),
array( 'status' => 404 )
);
}

return $this->prepare_item_for_response( compact( 'widget_id', 'sidebar_id' ), $request );
Expand Down Expand Up @@ -225,13 +229,17 @@ public function update_item_permissions_check( $request ) { // phpcs:ignore Vari
*/
public function update_item( $request ) {
$widget_id = $request['id'];
$sidebar_id = $this->find_widgets_sidebar( $widget_id );
$sidebar_id = gutenberg_find_widgets_sidebar( $widget_id );

// Allow sidebar to be unset or missing when widget is not a WP_Widget.
$parsed_id = gutenberg_parse_widget_id( $widget_id );
$widget_object = gutenberg_get_widget_object( $parsed_id['id_base'] );
if ( is_wp_error( $sidebar_id ) && $widget_object ) {
return $sidebar_id;
if ( is_null( $sidebar_id ) && $widget_object ) {
return new WP_Error(
'rest_widget_not_found',
__( 'No widget was found with that id.', 'gutenberg' ),
array( 'status' => 404 )
);
}

if (
Expand Down Expand Up @@ -279,10 +287,14 @@ public function delete_item_permissions_check( $request ) { // phpcs:ignore Vari
*/
public function delete_item( $request ) {
$widget_id = $request['id'];
$sidebar_id = $this->find_widgets_sidebar( $widget_id );
$sidebar_id = gutenberg_find_widgets_sidebar( $widget_id );

if ( is_wp_error( $sidebar_id ) ) {
return $sidebar_id;
if ( is_null( $sidebar_id ) ) {
return new WP_Error(
'rest_widget_not_found',
__( 'No widget was found with that id.', 'gutenberg' ),
array( 'status' => 404 )
);
}

$request['context'] = 'edit';
Expand Down Expand Up @@ -331,26 +343,6 @@ protected function permissions_check() {
return true;
}

/**
* Finds the sidebar a widget belongs to.
*
* @since 5.6.0
*
* @param string $widget_id The widget id to search for.
* @return string|WP_Error The found sidebar id, or a WP_Error instance if it does not exist.
*/
protected function find_widgets_sidebar( $widget_id ) {
foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
foreach ( $widget_ids as $maybe_widget_id ) {
if ( $maybe_widget_id === $widget_id ) {
return (string) $sidebar_id;
}
}
}

return new WP_Error( 'rest_widget_not_found', __( 'No widget was found with that id.', 'gutenberg' ), array( 'status' => 404 ) );
}

/**
* Saves the widget in the request object.
*
Expand Down
44 changes: 0 additions & 44 deletions lib/widget-preview-template.php

This file was deleted.

22 changes: 22 additions & 0 deletions lib/widgets-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ function gutenberg_assign_widget_to_sidebar( $widget_id, $sidebar_id ) {
wp_set_sidebars_widgets( $sidebars );
}

/**
* Finds the sidebar that a given widget belongs to.
*
* Belongs in wp-includes/widgets.php when merged to Core.
*
* @since 10.3.0
*
* @param string $widget_id The widget id to look for.
* @return string|null The found sidebar's id, or null if it was not found.
*/
function gutenberg_find_widgets_sidebar( $widget_id ) {
foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
foreach ( $widget_ids as $maybe_widget_id ) {
if ( $maybe_widget_id === $widget_id ) {
return (string) $sidebar_id;
}
}
}

return null;
}

/**
* Converts a widget ID into its id_base and number components.
*
Expand Down
147 changes: 67 additions & 80 deletions lib/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,82 +123,86 @@ function gutenberg_print_save_widgets_nonce() {
* @return array Legacy widget settings.
*/
function gutenberg_get_legacy_widget_settings() {
global $wp_widget_factory;

$settings = array();

/**
* Filters the list of widget classes that should **not** be offered by the legacy widget block.
*
* Returning an empty array will make all the widgets available.
*
* @param array $widgets An array of excluded widgets classnames.
*
* @since 5.6.0
*/
$widgets_to_exclude_from_legacy_widget_block = apply_filters(
'widgets_to_exclude_from_legacy_widget_block',
$widget_types_to_hide_from_legacy_widget_block = apply_filters(
'widget_types_to_hide_from_legacy_widget_block',
array(
'WP_Widget_Block',
'WP_Widget_Pages',
'WP_Widget_Calendar',
'WP_Widget_Archives',
'WP_Widget_Media_Audio',
'WP_Widget_Media_Image',
'WP_Widget_Media_Gallery',
'WP_Widget_Media_Video',
'WP_Widget_Meta',
'WP_Widget_Search',
'WP_Widget_Text',
'WP_Widget_Categories',
'WP_Widget_Recent_Posts',
'WP_Widget_Recent_Comments',
'WP_Widget_RSS',
'WP_Widget_Tag_Cloud',
'WP_Nav_Menu_Widget',
'WP_Widget_Custom_HTML',
'pages',
'calendar',
'archives',
'media_audio',
'media_image',
'media_gallery',
'media_video',
'meta',
'search',
'text',
'categories',
'recent-posts',
'recent-comments',
'rss',
'tag_cloud',
'nav_menu',
'custom_html',
'block',
)
);

$available_legacy_widgets = array();
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 ),
'id_base' => $widget_obj->id_base,
// 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,
'isReferenceWidget' => false,
'isHidden' => in_array( $class, $widgets_to_exclude_from_legacy_widget_block, true ),
);
}
}
global $wp_registered_widgets;
if ( ! empty( $wp_registered_widgets ) ) {
foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) {
// Backwards compatibility. Remove this in or after Gutenberg 10.5.
if ( has_filter( 'widgets_to_exclude_from_legacy_widget_block' ) ) {
/**
* Filters the list of widget classes that should **not** be offered by the legacy widget block.
*
* Returning an empty array will make all the widgets available.
*
* @param array $widgets An array of excluded widgets classnames.
*
* @since 5.6.0
*/
$widgets_to_exclude_from_legacy_widget_block = apply_filters(
'widgets_to_exclude_from_legacy_widget_block',
array(
'WP_Widget_Block',
'WP_Widget_Pages',
'WP_Widget_Calendar',
'WP_Widget_Archives',
'WP_Widget_Media_Audio',
'WP_Widget_Media_Image',
'WP_Widget_Media_Gallery',
'WP_Widget_Media_Video',
'WP_Widget_Meta',
'WP_Widget_Search',
'WP_Widget_Text',
'WP_Widget_Categories',
'WP_Widget_Recent_Posts',
'WP_Widget_Recent_Comments',
'WP_Widget_RSS',
'WP_Widget_Tag_Cloud',
'WP_Nav_Menu_Widget',
'WP_Widget_Custom_HTML',
)
);

$block_widget_start = 'blocks-widget-';
_deprecated_hook(
'widgets_to_exclude_from_legacy_widget_block',
'10.3',
"wp.hooks.addFilter( 'legacyWidget.isWidgetTypeHidden', ... )"
);

foreach ( $wp_widget_factory->widgets as $widget ) {
if (
( is_array( $widget_obj['callback'] ) &&
isset( $widget_obj['callback'][0] ) &&
( $widget_obj['callback'][0] instanceof WP_Widget ) ) ||
// $widget_id starts with $block_widget_start.
strncmp( $widget_id, $block_widget_start, strlen( $block_widget_start ) ) === 0
in_array( get_class( $widget ), $widgets_to_exclude_from_legacy_widget_block, true ) &&
! in_array( $widget->id_base, $widget_types_to_hide_from_legacy_widget_block, true )
) {
continue;
$widget_types_to_hide_from_legacy_widget_block[] = $widget->id_base;
}
$available_legacy_widgets[ $widget_id ] = array(
'name' => html_entity_decode( $widget_obj['name'] ),
'description' => html_entity_decode( wp_widget_description( $widget_id ) ),
'isReferenceWidget' => true,
);
}
}

$settings['availableLegacyWidgets'] = $available_legacy_widgets;
$settings['widgetTypesToHideFromLegacyWidgetBlock'] = $widget_types_to_hide_from_legacy_widget_block;

return $settings;
}
Expand Down Expand Up @@ -267,23 +271,6 @@ function gutenberg_register_widgets() {

add_action( 'widgets_init', 'gutenberg_register_widgets' );

/**
* Hook into before the widgets editor screen is loaded and, if widget-preview
* is set, render the requested preview of a legacy widget instead. This powers
* the Preview option in the Legacy Widget block.
*/
function gutenberg_load_widget_preview_if_requested() {
if (
isset( $_GET['widget-preview'] ) &&
current_user_can( 'edit_theme_options' )
) {
define( 'IFRAME_REQUEST', true );
require_once __DIR__ . '/widget-preview-template.php';
exit;
}
}
add_filter( 'load-appearance_page_gutenberg-widgets', 'gutenberg_load_widget_preview_if_requested' );

/**
* Sets show_instance_in_rest to true on all of the core WP_Widget subclasses.
* When merge dto Core, this property should be added to WP_Widget and set to
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ _Properties_

- _alignWide_ `boolean`: Enable/Disable Wide/Full Alignments
- _supportsLayout_ `boolean`: Enable/disable layouts support in container blocks.
- _availableLegacyWidgets_ `Array`: Array of objects representing the legacy widgets available.
- _imageEditing_ `boolean`: Image Editing settings set to false to disable.
- _imageSizes_ `Array`: Available image sizes
- _maxWidth_ `number`: Max width to constraint resizing
Expand Down
2 changes: 0 additions & 2 deletions packages/block-editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const PREFERENCES_DEFAULTS = {
* @typedef {Object} SETTINGS_DEFAULT
* @property {boolean} alignWide Enable/Disable Wide/Full Alignments
* @property {boolean} supportsLayout Enable/disable layouts support in container blocks.
* @property {Array} availableLegacyWidgets Array of objects representing the legacy widgets available.
* @property {boolean} imageEditing Image Editing settings set to false to disable.
* @property {Array} imageSizes Available image sizes
* @property {number} maxWidth Max width to constraint resizing
Expand Down Expand Up @@ -150,7 +149,6 @@ export const SETTINGS_DEFAULTS = {
// List of allowed mime types and file extensions.
allowedMimeTypes: null,

availableLegacyWidgets: {},
__experimentalCanUserUseUnfilteredHTML: false,
__experimentalBlockDirectory: false,
__mobileEnablePageTemplates: false,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
@import "./image/editor.scss";
@import "./latest-comments/editor.scss";
@import "./latest-posts/editor.scss";
@import "./legacy-widget/editor.scss";
@import "./media-text/editor.scss";
@import "./more/editor.scss";
@import "./navigation/editor.scss";
Expand Down
Loading