diff --git a/docs/how-to-guides/widgets/legacy-widget-block.md b/docs/how-to-guides/widgets/legacy-widget-block.md index c32136a10e4fef..21d22561d1509c 100644 --- a/docs/how-to-guides/widgets/legacy-widget-block.md +++ b/docs/how-to-guides/widgets/legacy-widget-block.md @@ -123,3 +123,49 @@ function hide_example_widget( $widget_types ) { } add_filter( 'widget_types_to_hide_from_legacy_widget_block', 'hide_example_widget' ); ``` + +## Using the Legacy Widget block in other block editors (Advanced) + +You may optionally allow the Legacy Widget block in other block editors such as +the WordPress post editor. This is not enabled by default. + +First, ensure that any styles and scripts required by the legacy widgets are +loaded onto the page. A convenient way of doing this is to manually perform all +of the hooks that ordinarily run when a user browses to the widgets WP Admin +screen. + +```php +add_action( 'admin_print_styles', function() { + if ( get_current_screen()->is_block_editor() ) { + do_action( 'admin_print_styles-widgets.php' ); + } +} ); +add_action( 'admin_print_scripts', function() { + if ( get_current_screen()->is_block_editor() ) { + do_action( 'load-widgets.php' ); + do_action( 'widgets.php' ); + do_action( 'sidebar_admin_setup' ); + do_action( 'admin_print_scripts-widgets.php' ); + } +} ); +add_action( 'admin_print_footer_scripts', function() { + if ( get_current_screen()->is_block_editor() ) { + do_action( 'admin_print_footer_scripts-widgets.php' ); + } +} ); +add_action( 'admin_footer', function() { + if ( get_current_screen()->is_block_editor() ) { + do_action( 'admin_footer-widgets.php' ); + } +} ); +``` + +Then, register the Legacy Widget block using `registerLegacyWidgetBlock` which +is defined in the `@wordpress/widgets` package. + +```php +add_action( 'enqueue_block_editor_assets', function() { + wp_enqueue_script( 'wp-widgets' ); + wp_add_inline_script( 'wp-widgets', 'wp.widgets.registerLegacyWidgetBlock()' ); +} ); +``` diff --git a/lib/blocks.php b/lib/blocks.php index e04842e8a9445f..5140c33732bacf 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -5,24 +5,6 @@ * @package gutenberg */ -/* - * Fixes the priority of register_block_core_legacy_widget(). - * - * This hook was incorrectly added to Core with priority 20. #32300 fixes this - * but causes block registration warnings in the Gutenberg plugin until the - * changes are made in Core. - * - * This temporary fix can be removed after the changes to - * @wordpress/block-library in #32300 have been published to npm and updated in - * Core. - * - * See https://github.com/WordPress/gutenberg/pull/32300. - */ -if ( 20 === has_action( 'init', 'register_block_core_legacy_widget' ) ) { - remove_action( 'init', 'register_block_core_legacy_widget', 20 ); - add_action( 'init', 'register_block_core_legacy_widget', 10 ); -} - /** * Substitutes the implementation of a core-registered block type, if exists, * with the built result from the plugin. @@ -74,7 +56,6 @@ function gutenberg_reregister_core_block_types() { 'file.php' => 'core/file', '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', @@ -120,8 +101,14 @@ function gutenberg_reregister_core_block_types() { 'block_folders' => array( 'widget-area', ), + 'block_names' => array(), + ), + __DIR__ . '/../build/widgets/blocks/' => array( + 'block_folders' => array( + 'legacy-widget', + ), 'block_names' => array( - 'widget-area.php' => 'core/widget-area', + 'legacy-widget.php' => 'core/legacy-widget', ), ), ); diff --git a/lib/client-assets.php b/lib/client-assets.php index c52c5ae04f2b04..3a366796d6218c 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -447,7 +447,7 @@ function gutenberg_register_packages_styles( $styles ) { $styles, 'wp-edit-widgets', gutenberg_url( 'build/edit-widgets/style.css' ), - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks' ), + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks', 'wp-widgets' ), $version ); $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' ); @@ -465,7 +465,7 @@ function gutenberg_register_packages_styles( $styles ) { $styles, 'wp-customize-widgets', gutenberg_url( 'build/customize-widgets/style.css' ), - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ), + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-widgets' ), $version ); $styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' ); @@ -478,6 +478,14 @@ function gutenberg_register_packages_styles( $styles ) { $version ); $styles->add_data( 'wp-reusable-block', 'rtl', 'replace' ); + + gutenberg_override_style( + $styles, + 'wp-widgets', + gutenberg_url( 'build/widgets/style.css' ), + array( 'wp-components' ) + ); + $styles->add_data( 'wp-widgets', 'rtl', 'replace' ); } add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' ); diff --git a/lib/widgets-page.php b/lib/widgets-page.php index 06376c2c71e396..1a4089eea66fd7 100644 --- a/lib/widgets-page.php +++ b/lib/widgets-page.php @@ -103,3 +103,59 @@ function gutenberg_widgets_editor_load_block_editor_scripts_and_styles( $is_bloc function gutenberg_widgets_editor_add_admin_body_classes( $classes ) { return "$classes block-editor-page wp-embed-responsive"; } + +/** + * Emulates the Widgets screen `admin_print_styles` when at the block editor + * screen. + */ +function gutenberg_block_editor_admin_print_styles() { + if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { + /** This action is documented in wp-admin/admin-footer.php */ + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + do_action( 'admin_print_styles-widgets.php' ); + } +} +add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' ); + +/** + * Emulates the Widgets screen `admin_print_scripts` when at the block editor + * screen. + */ +function gutenberg_block_editor_admin_print_scripts() { + if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { + /** This action is documented in wp-admin/includes/ajax-actions.php */ + do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + /** This action is documented in wp-admin/includes/ajax-actions.php */ + do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + /** This action is documented in wp-admin/widgets.php */ + do_action( 'sidebar_admin_setup' ); + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + do_action( 'admin_print_scripts-widgets.php' ); + } +} +add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' ); + +/** + * Emulates the Widgets screen `admin_print_footer_scripts` when at the block + * editor screen. + */ +function gutenberg_block_editor_admin_print_footer_scripts() { + if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { + /** This action is documented in wp-admin/admin-footer.php */ + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + do_action( 'admin_print_footer_scripts-widgets.php' ); + } +} +add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' ); + +/** + * Emulates the Widgets screen `admin_footer` when at the block editor screen. + */ +function gutenberg_block_editor_admin_footer() { + if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { + /** This action is documented in wp-admin/admin-footer.php */ + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + do_action( 'admin_footer-widgets.php' ); + } +} +add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' ); diff --git a/lib/widgets.php b/lib/widgets.php index 3d6143337003be..53621323f9c182 100644 --- a/lib/widgets.php +++ b/lib/widgets.php @@ -11,6 +11,12 @@ * @return boolean True if a screen containing the block editor is being loaded. */ function gutenberg_is_block_editor() { + _deprecated_function( + 'gutenberg_is_block_editor', + '10.8', + 'WP_Screen::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' ) ) { @@ -44,79 +50,6 @@ function gutenberg_use_widgets_block_editor() { ); } -/** - * Emulates the Widgets screen `admin_print_styles` when at the block editor - * screen. - */ -function gutenberg_block_editor_admin_print_styles() { - if ( gutenberg_is_block_editor() ) { - /** This action is documented in wp-admin/admin-footer.php */ - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - do_action( 'admin_print_styles-widgets.php' ); - } -} -add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' ); - -/** - * Emulates the Widgets screen `admin_print_scripts` when at the block editor - * screen. - */ -function gutenberg_block_editor_admin_print_scripts() { - if ( gutenberg_is_block_editor() ) { - /** This action is documented in wp-admin/includes/ajax-actions.php */ - do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - /** This action is documented in wp-admin/includes/ajax-actions.php */ - do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - /** This action is documented in wp-admin/widgets.php */ - do_action( 'sidebar_admin_setup' ); - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - do_action( 'admin_print_scripts-widgets.php' ); - } -} -add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' ); - -/** - * Emulates the Widgets screen `admin_print_footer_scripts` when at the block - * editor screen. - */ -function gutenberg_block_editor_admin_print_footer_scripts() { - if ( gutenberg_is_block_editor() ) { - /** This action is documented in wp-admin/admin-footer.php */ - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - do_action( 'admin_print_footer_scripts-widgets.php' ); - } -} -add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' ); - -/** - * Emulates the Widgets screen `admin_footer` when at the block editor screen. - */ -function gutenberg_block_editor_admin_footer() { - if ( gutenberg_is_block_editor() ) { - /** This action is documented in wp-admin/admin-footer.php */ - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - do_action( 'admin_footer-widgets.php' ); - } -} -add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' ); - -/** - * Adds a save widgets nonce required by the legacy widgets block. - */ -function gutenberg_print_save_widgets_nonce() { - // The function wpWidgets.save needs this nonce to work as expected. - echo implode( - "\n", - array( - '
', - ) - ); -} -add_action( 'admin_footer-widgets.php', 'gutenberg_print_save_widgets_nonce' ); - - /** * Returns the settings required by legacy widgets blocks. * @@ -137,7 +70,6 @@ function gutenberg_get_legacy_widget_settings() { 'media_image', 'media_gallery', 'media_video', - 'meta', 'search', 'text', 'categories', @@ -150,89 +82,11 @@ function gutenberg_get_legacy_widget_settings() { ) ); - // 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_Widget_Custom_HTML', - ) - ); - - _deprecated_hook( - 'widgets_to_exclude_from_legacy_widget_block', - '10.3', - "wp.hooks.addFilter( 'legacyWidget.isWidgetTypeHidden', ... )" - ); - - foreach ( $wp_widget_factory->widgets as $widget ) { - if ( - 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 ) - ) { - $widget_types_to_hide_from_legacy_widget_block[] = $widget->id_base; - } - } - } - $settings['widgetTypesToHideFromLegacyWidgetBlock'] = $widget_types_to_hide_from_legacy_widget_block; return $settings; } -/** - * Extends default editor settings with values supporting legacy widgets. - * - * This can be removed when plugin support requires WordPress 5.8.0+. - * - * @param array $settings Default editor settings. - * - * @return array Filtered editor settings. - */ -function gutenberg_legacy_widget_settings( $settings ) { - return array_merge( $settings, gutenberg_get_legacy_widget_settings() ); -} -// This can be removed when plugin support requires WordPress 5.8.0+. -if ( function_exists( 'get_block_editor_settings' ) ) { - add_filter( 'block_editor_settings_all', 'gutenberg_legacy_widget_settings' ); -} else { - add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' ); -} - -/** - * Function to enqueue admin-widgets as part of the block editor assets. - */ -function gutenberg_enqueue_widget_scripts() { - wp_enqueue_script( 'admin-widgets' ); -} - -add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_widget_scripts' ); - /** * Overrides dynamic_sidebar_params to make sure Blocks are not wrapped in