Skip to content

Commit

Permalink
Move Legacy Widget block to @wordpress/widgets (#32501)
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks authored and youknowriad committed Jun 14, 2021
1 parent 0f1bec8 commit 9921f4e
Show file tree
Hide file tree
Showing 34 changed files with 192 additions and 224 deletions.
46 changes: 46 additions & 0 deletions docs/how-to-guides/widgets/legacy-widget-block.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()' );
} );
```
27 changes: 7 additions & 20 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
),
),
);
Expand Down
12 changes: 10 additions & 2 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -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' );
Expand All @@ -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' );

Expand Down
56 changes: 56 additions & 0 deletions lib/widgets-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
164 changes: 10 additions & 154 deletions lib/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ) {
Expand Down Expand Up @@ -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(
'<form method="post">',
wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ),
'</form>',
)
);
}
add_action( 'admin_footer-widgets.php', 'gutenberg_print_save_widgets_nonce' );


/**
* Returns the settings required by legacy widgets blocks.
*
Expand All @@ -137,7 +70,6 @@ function gutenberg_get_legacy_widget_settings() {
'media_image',
'media_gallery',
'media_video',
'meta',
'search',
'text',
'categories',
Expand All @@ -151,90 +83,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_Nav_Menu_Widget',
'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 <form> tag.
*
Expand Down Expand Up @@ -308,4 +161,7 @@ function gutenberg_set_show_instance_in_rest_on_core_widgets() {
}
}
}
add_action( 'widgets_init', 'gutenberg_set_show_instance_in_rest_on_core_widgets' );

if ( ! function_exists( 'wp_use_widgets_block_editor' ) ) {
add_action( 'widgets_init', 'gutenberg_set_show_instance_in_rest_on_core_widgets' );
}
Loading

0 comments on commit 9921f4e

Please sign in to comment.