| @@ -19,14 +19,33 @@ | ||
| 19 | 19 | $screen = get_current_screen(); |
| 20 | 20 | return ! empty( $screen ) && |
| 21 | 21 | ( |
| 22 | 22 | $screen->is_block_editor() || |
| 23 | - 'gutenberg_page_gutenberg-widgets' === $screen->id || | |
| 24 | - 'gutenberg_page_gutenberg-edit-site' === $screen->id | |
| 23 | + 'appearance_page_gutenberg-widgets' === $screen->id || | |
| 24 | + ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $screen->id ) ) | |
| 25 | 25 | ); |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | + * Whether or not to use the block editor to manage widgets. Defaults to true | |
| 30 | + * unless a theme has removed support for widgets-block-editor or a plugin has | |
| 31 | + * filtered the return value of this function. | |
| 32 | + * | |
| 33 | + * @return boolean Whether or not to use the block editor to manage widgets. | |
| 34 | + */ | |
| 35 | +function gutenberg_use_widgets_block_editor() { | |
| 36 | + /** | |
| 37 | + * Filters whether or not to use the block editor to manage widgets. | |
| 38 | + * | |
| 39 | + * @param boolean $use_widgets_block_editor Whether or not to use the block editor to manage widgets. | |
| 40 | + */ | |
| 41 | + return apply_filters( | |
| 42 | + 'gutenberg_use_widgets_block_editor', | |
| 43 | + get_theme_support( 'widgets-block-editor' ) | |
| 44 | + ); | |
| 45 | +} | |
| 46 | + | |
| 47 | +/** | |
| 29 | 48 | * Emulates the Widgets screen `admin_print_styles` when at the block editor |
| 30 | 49 | * screen. |
| 31 | 50 | */ |
| 32 | 51 | function gutenberg_block_editor_admin_print_styles() { |
| @@ -43,8 +62,14 @@ | ||
| 43 | 62 | * screen. |
| 44 | 63 | */ |
| 45 | 64 | function gutenberg_block_editor_admin_print_scripts() { |
| 46 | 65 | if ( gutenberg_is_block_editor() ) { |
| 66 | + /** This action is documented in wp-admin/includes/ajax-actions.php */ | |
| 67 | + do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores | |
| 68 | + /** This action is documented in wp-admin/includes/ajax-actions.php */ | |
| 69 | + do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores | |
| 70 | + /** This action is documented in wp-admin/widgets.php */ | |
| 71 | + do_action( 'sidebar_admin_setup' ); | |
| 47 | 72 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 48 | 73 | do_action( 'admin_print_scripts-widgets.php' ); |
| 49 | 74 | } |
| 50 | 75 | } |
| @@ -67,17 +92,8 @@ | ||
| 67 | 92 | * Emulates the Widgets screen `admin_footer` when at the block editor screen. |
| 68 | 93 | */ |
| 69 | 94 | function gutenberg_block_editor_admin_footer() { |
| 70 | 95 | if ( gutenberg_is_block_editor() ) { |
| 71 | - // The function wpWidgets.save needs this nonce to work as expected. | |
| 72 | - echo implode( | |
| 73 | - "\n", | |
| 74 | - array( | |
| 75 | - '<form method="post">', | |
| 76 | - wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ), | |
| 77 | - '</form>', | |
| 78 | - ) | |
| 79 | - ); | |
| 80 | 96 | /** This action is documented in wp-admin/admin-footer.php */ |
| 81 | 97 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 82 | 98 | do_action( 'admin_footer-widgets.php' ); |
| 83 | 99 | } |
| @@ -83,9 +99,25 @@ | ||
| 83 | 99 | } |
| 84 | 100 | } |
| 85 | 101 | add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' ); |
| 86 | 102 | |
| 103 | +/** | |
| 104 | + * Adds a save widgets nonce required by the legacy widgets block. | |
| 105 | + */ | |
| 106 | +function gutenberg_print_save_widgets_nonce() { | |
| 107 | + // The function wpWidgets.save needs this nonce to work as expected. | |
| 108 | + echo implode( | |
| 109 | + "\n", | |
| 110 | + array( | |
| 111 | + '<form method="post">', | |
| 112 | + wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ), | |
| 113 | + '</form>', | |
| 114 | + ) | |
| 115 | + ); | |
| 116 | +} | |
| 117 | +add_action( 'admin_footer-widgets.php', 'gutenberg_print_save_widgets_nonce' ); | |
| 87 | 118 | |
| 119 | + | |
| 88 | 120 | /** |
| 89 | 121 | * Returns the settings required by legacy widgets blocks. |
| 90 | 122 | * |
| 91 | 123 | * @return array Legacy widget settings. |
| @@ -91,31 +123,40 @@ | ||
| 91 | 123 | * @return array Legacy widget settings. |
| 92 | 124 | */ |
| 93 | 125 | function gutenberg_get_legacy_widget_settings() { |
| 94 | 126 | $settings = array(); |
| 127 | + | |
| 95 | 128 | /** |
| 96 | - * TODO: The hardcoded array should be replaced with a mechanism to allow | |
| 97 | - * core and third party blocks to specify they already have equivalent | |
| 98 | - * blocks, and maybe even allow them to have a migration function. | |
| 129 | + * Filters the list of widget classes that should **not** be offered by the legacy widget block. | |
| 130 | + * | |
| 131 | + * Returning an empty array will make all the widgets available. | |
| 132 | + * | |
| 133 | + * @param array $widgets An array of excluded widgets classnames. | |
| 134 | + * | |
| 135 | + * @since 5.6.0 | |
| 99 | 136 | */ |
| 100 | - $core_widgets = array( | |
| 101 | - 'WP_Widget_Pages', | |
| 102 | - 'WP_Widget_Calendar', | |
| 103 | - 'WP_Widget_Archives', | |
| 104 | - 'WP_Widget_Media_Audio', | |
| 105 | - 'WP_Widget_Media_Image', | |
| 106 | - 'WP_Widget_Media_Gallery', | |
| 107 | - 'WP_Widget_Media_Video', | |
| 108 | - 'WP_Widget_Meta', | |
| 109 | - 'WP_Widget_Search', | |
| 110 | - 'WP_Widget_Text', | |
| 111 | - 'WP_Widget_Categories', | |
| 112 | - 'WP_Widget_Recent_Posts', | |
| 113 | - 'WP_Widget_Recent_Comments', | |
| 114 | - 'WP_Widget_RSS', | |
| 115 | - 'WP_Widget_Tag_Cloud', | |
| 116 | - 'WP_Nav_Menu_Widget', | |
| 117 | - 'WP_Widget_Custom_HTML', | |
| 137 | + $widgets_to_exclude_from_legacy_widget_block = apply_filters( | |
| 138 | + 'widgets_to_exclude_from_legacy_widget_block', | |
| 139 | + array( | |
| 140 | + 'WP_Widget_Block', | |
| 141 | + 'WP_Widget_Pages', | |
| 142 | + 'WP_Widget_Calendar', | |
| 143 | + 'WP_Widget_Archives', | |
| 144 | + 'WP_Widget_Media_Audio', | |
| 145 | + 'WP_Widget_Media_Image', | |
| 146 | + 'WP_Widget_Media_Gallery', | |
| 147 | + 'WP_Widget_Media_Video', | |
| 148 | + 'WP_Widget_Meta', | |
| 149 | + 'WP_Widget_Search', | |
| 150 | + 'WP_Widget_Text', | |
| 151 | + 'WP_Widget_Categories', | |
| 152 | + 'WP_Widget_Recent_Posts', | |
| 153 | + 'WP_Widget_Recent_Comments', | |
| 154 | + 'WP_Widget_RSS', | |
| 155 | + 'WP_Widget_Tag_Cloud', | |
| 156 | + 'WP_Nav_Menu_Widget', | |
| 157 | + 'WP_Widget_Custom_HTML', | |
| 158 | + ) | |
| 118 | 159 | ); |
| 119 | 160 | |
| 120 | 161 | $has_permissions_to_manage_widgets = current_user_can( 'edit_theme_options' ); |
| 121 | 162 | $available_legacy_widgets = array(); |
| @@ -123,8 +164,9 @@ | ||
| 123 | 164 | if ( ! empty( $wp_widget_factory ) ) { |
| 124 | 165 | foreach ( $wp_widget_factory->widgets as $class => $widget_obj ) { |
| 125 | 166 | $available_legacy_widgets[ $class ] = array( |
| 126 | 167 | 'name' => html_entity_decode( $widget_obj->name ), |
| 168 | + 'id_base' => $widget_obj->id_base, | |
| 127 | 169 | // wp_widget_description is not being used because its input parameter is a Widget Id. |
| 128 | 170 | // Widgets id's reference to a specific widget instance. |
| 129 | 171 | // Here we are iterating on all the available widget classes even if no widget instance exists for them. |
| 130 | 172 | 'description' => isset( $widget_obj->widget_options['description'] ) ? |
| @@ -130,9 +172,9 @@ | ||
| 130 | 172 | 'description' => isset( $widget_obj->widget_options['description'] ) ? |
| 131 | 173 | html_entity_decode( $widget_obj->widget_options['description'] ) : |
| 132 | 174 | null, |
| 133 | 175 | 'isReferenceWidget' => false, |
| 134 | - 'isHidden' => in_array( $class, $core_widgets, true ), | |
| 176 | + 'isHidden' => in_array( $class, $widgets_to_exclude_from_legacy_widget_block, true ), | |
| 135 | 177 | ); |
| 136 | 178 | } |
| 137 | 179 | } |
| 138 | 180 | global $wp_registered_widgets; |
| @@ -229,10 +271,8 @@ | ||
| 229 | 271 | ); |
| 230 | 272 | } |
| 231 | 273 | add_action( 'init', 'gutenberg_create_wp_area_post_type' ); |
| 232 | 274 | |
| 233 | -add_filter( 'sidebars_widgets', 'Experimental_WP_Widget_Blocks_Manager::swap_out_sidebars_blocks_for_block_widgets' ); | |
| 234 | - | |
| 235 | 275 | /** |
| 236 | 276 | * Function to enqueue admin-widgets as part of the block editor assets. |
| 237 | 277 | */ |
| 238 | 278 | function gutenberg_enqueue_widget_scripts() { |
| @@ -239,4 +279,47 @@ | ||
| 239 | 279 | wp_enqueue_script( 'admin-widgets' ); |
| 240 | 280 | } |
| 241 | 281 | |
| 242 | 282 | add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_widget_scripts' ); |
| 283 | + | |
| 284 | +/** | |
| 285 | + * Overrides dynamic_sidebar_params to make sure Blocks are not wrapped in <form> tag. | |
| 286 | + * | |
| 287 | + * @param array $arg Dynamic sidebar params. | |
| 288 | + * @return array Updated dynamic sidebar params. | |
| 289 | + */ | |
| 290 | +function gutenberg_override_sidebar_params_for_block_widget( $arg ) { | |
| 291 | + if ( 'Block' === $arg[0]['widget_name'] ) { | |
| 292 | + $arg[0]['before_form'] = ''; | |
| 293 | + $arg[0]['before_widget_content'] = '<div class="widget-content">'; | |
| 294 | + $arg[0]['after_widget_content'] = '</div><form class="block-widget-form">'; | |
| 295 | + $arg[0]['after_form'] = '</form>'; | |
| 296 | + } | |
| 297 | + | |
| 298 | + return $arg; | |
| 299 | +} | |
| 300 | + | |
| 301 | +/** | |
| 302 | + * Registers the WP_Widget_Block widget | |
| 303 | + */ | |
| 304 | +function gutenberg_register_widgets() { | |
| 305 | + if ( ! gutenberg_use_widgets_block_editor() ) { | |
| 306 | + return; | |
| 307 | + } | |
| 308 | + | |
| 309 | + register_widget( 'WP_Widget_Block' ); | |
| 310 | + // By default every widget on widgets.php is wrapped with a <form>. | |
| 311 | + // This means that you can sometimes end up with invalid HTML, e.g. when | |
| 312 | + // one of the widgets is a Search block. | |
| 313 | + // | |
| 314 | + // To fix the problem, let's add a filter that moves the form below the actual | |
| 315 | + // widget content. | |
| 316 | + global $pagenow; | |
| 317 | + if ( 'widgets.php' === $pagenow ) { | |
| 318 | + add_filter( | |
| 319 | + 'dynamic_sidebar_params', | |
| 320 | + 'gutenberg_override_sidebar_params_for_block_widget' | |
| 321 | + ); | |
| 322 | + } | |
| 323 | +} | |
| 324 | + | |
| 325 | +add_action( 'widgets_init', 'gutenberg_register_widgets' ); | |