PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
← All changes | lib/widgets.php +92 -170 7.4.012.1.0 View file →
@@ -10,8 +10,14 @@
10 10 *
11 11 * @return boolean True if a screen containing the block editor is being loaded.
12 12 */
13 13 function gutenberg_is_block_editor() {
14 + _deprecated_function(
15 + 'gutenberg_is_block_editor',
16 + '10.8',
17 + 'WP_Screen::is_block_editor'
18 + );
19 +
14 20 // If get_current_screen does not exist, we are neither in the standard block editor for posts, or the widget block editor.
15 21 // We can safely return false.
16 22 if ( ! function_exists( 'get_current_screen' ) ) {
17 23 return false;
@@ -19,85 +25,114 @@
19 25 $screen = get_current_screen();
20 26 return ! empty( $screen ) &&
21 27 (
22 28 $screen->is_block_editor() ||
23 - 'gutenberg_page_gutenberg-widgets' === $screen->id ||
24 - 'gutenberg_page_gutenberg-edit-site' === $screen->id
29 + 'appearance_page_gutenberg-widgets' === $screen->id ||
30 + ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $screen->id ) )
25 31 );
26 32 }
27 33
28 34 /**
29 - * Emulates the Widgets screen `admin_print_styles` when at the block editor
30 - * screen.
35 + * Whether or not to use the block editor to manage widgets. Defaults to true
36 + * unless a theme has removed support for widgets-block-editor or a plugin has
37 + * filtered the return value of this function.
38 + *
39 + * @return boolean Whether or not to use the block editor to manage widgets.
31 40 */
32 -function gutenberg_block_editor_admin_print_styles() {
33 - if ( gutenberg_is_block_editor() ) {
34 - /** This action is documented in wp-admin/admin-footer.php */
35 - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
36 - do_action( 'admin_print_styles-widgets.php' );
37 - }
41 +function gutenberg_use_widgets_block_editor() {
42 + /**
43 + * Filters whether or not to use the block editor to manage widgets.
44 + *
45 + * @param boolean $use_widgets_block_editor Whether or not to use the block editor to manage widgets.
46 + */
47 + return apply_filters(
48 + 'gutenberg_use_widgets_block_editor',
49 + get_theme_support( 'widgets-block-editor' )
50 + );
38 51 }
39 -add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );
40 52
41 53 /**
42 - * Emulates the Widgets screen `admin_print_scripts` when at the block editor
43 - * screen.
54 + * Returns the settings required by legacy widgets blocks.
55 + *
56 + * @return array Legacy widget settings.
44 57 */
45 -function gutenberg_block_editor_admin_print_scripts() {
46 - if ( gutenberg_is_block_editor() ) {
47 - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
48 - do_action( 'admin_print_scripts-widgets.php' );
49 - }
58 +function gutenberg_get_legacy_widget_settings() {
59 + $settings = array();
60 +
61 + $widget_types_to_hide_from_legacy_widget_block = apply_filters(
62 + 'widget_types_to_hide_from_legacy_widget_block',
63 + array(
64 + 'pages',
65 + 'calendar',
66 + 'archives',
67 + 'media_audio',
68 + 'media_image',
69 + 'media_gallery',
70 + 'media_video',
71 + 'search',
72 + 'text',
73 + 'categories',
74 + 'recent-posts',
75 + 'recent-comments',
76 + 'rss',
77 + 'tag_cloud',
78 + 'custom_html',
79 + 'block',
80 + )
81 + );
82 +
83 + $settings['widgetTypesToHideFromLegacyWidgetBlock'] = $widget_types_to_hide_from_legacy_widget_block;
84 +
85 + return $settings;
50 86 }
51 -add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' );
52 87
53 88 /**
54 - * Emulates the Widgets screen `admin_print_footer_scripts` when at the block
55 - * editor screen.
89 + * Overrides dynamic_sidebar_params to make sure Blocks are not wrapped in <form> tag.
90 + *
91 + * @param array $arg Dynamic sidebar params.
92 + * @return array Updated dynamic sidebar params.
56 93 */
57 -function gutenberg_block_editor_admin_print_footer_scripts() {
58 - if ( gutenberg_is_block_editor() ) {
59 - /** This action is documented in wp-admin/admin-footer.php */
60 - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
61 - do_action( 'admin_print_footer_scripts-widgets.php' );
94 +function gutenberg_override_sidebar_params_for_block_widget( $arg ) {
95 + if ( 'Block' === $arg[0]['widget_name'] ) {
96 + $arg[0]['before_form'] = '';
97 + $arg[0]['before_widget_content'] = '<div class="widget-content">';
98 + $arg[0]['after_widget_content'] = '</div><form class="block-widget-form">';
99 + $arg[0]['after_form'] = '</form>';
62 100 }
101 +
102 + return $arg;
63 103 }
64 -add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' );
65 104
66 105 /**
67 - * Emulates the Widgets screen `admin_footer` when at the block editor screen.
106 + * Registers the WP_Widget_Block widget.
68 107 */
69 -function gutenberg_block_editor_admin_footer() {
70 - 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 - )
108 +function gutenberg_register_block_widget() {
109 + global $pagenow;
110 +
111 + register_widget( 'WP_Widget_Block' );
112 +
113 + // By default every widget on widgets.php is wrapped with a <form>. This
114 + // means that you can sometimes end up with invalid HTML, e.g. when one of
115 + // the widgets is a Search block. To fix the problem, let's add a filter
116 + // that moves the form below the actual widget content.
117 + if ( 'widgets.php' === $pagenow ) {
118 + add_filter(
119 + 'dynamic_sidebar_params',
120 + 'gutenberg_override_sidebar_params_for_block_widget'
79 121 );
80 - /** This action is documented in wp-admin/admin-footer.php */
81 - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
82 - do_action( 'admin_footer-widgets.php' );
83 122 }
84 123 }
85 -add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' );
86 124
125 +add_action( 'widgets_init', 'gutenberg_register_block_widget' );
87 126
88 127 /**
89 - * Returns the settings required by legacy widgets blocks.
90 - *
91 - * @return array Legacy widget settings.
128 + * Sets show_instance_in_rest to true on all of the core WP_Widget subclasses.
129 + * When merged to Core, this property should be added to WP_Widget and set to
130 + * true on each WP_Widget subclass.
92 131 */
93 -function gutenberg_get_legacy_widget_settings() {
94 - $settings = array();
95 - /**
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.
99 - */
132 +function gutenberg_set_show_instance_in_rest_on_core_widgets() {
133 + global $wp_widget_factory;
134 +
100 135 $core_widgets = array(
101 136 'WP_Widget_Pages',
102 137 'WP_Widget_Calendar',
103 138 'WP_Widget_Archives',
@@ -116,127 +151,14 @@
116 151 'WP_Nav_Menu_Widget',
117 152 'WP_Widget_Custom_HTML',
118 153 );
119 154
120 - $has_permissions_to_manage_widgets = current_user_can( 'edit_theme_options' );
121 - $available_legacy_widgets = array();
122 - global $wp_widget_factory;
123 - if ( ! empty( $wp_widget_factory ) ) {
124 - foreach ( $wp_widget_factory->widgets as $class => $widget_obj ) {
125 - $available_legacy_widgets[ $class ] = array(
126 - 'name' => html_entity_decode( $widget_obj->name ),
127 - // wp_widget_description is not being used because its input parameter is a Widget Id.
128 - // Widgets id's reference to a specific widget instance.
129 - // Here we are iterating on all the available widget classes even if no widget instance exists for them.
130 - 'description' => isset( $widget_obj->widget_options['description'] ) ?
131 - html_entity_decode( $widget_obj->widget_options['description'] ) :
132 - null,
133 - 'isReferenceWidget' => false,
134 - 'isHidden' => in_array( $class, $core_widgets, true ),
135 - );
155 + foreach ( $core_widgets as $widget ) {
156 + if ( isset( $wp_widget_factory->widgets[ $widget ] ) ) {
157 + $wp_widget_factory->widgets[ $widget ]->show_instance_in_rest = true;
136 158 }
137 159 }
138 - global $wp_registered_widgets;
139 - if ( ! empty( $wp_registered_widgets ) ) {
140 - foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) {
141 -
142 - $block_widget_start = 'blocks-widget-';
143 - if (
144 - ( is_array( $widget_obj['callback'] ) &&
145 - isset( $widget_obj['callback'][0] ) &&
146 - ( $widget_obj['callback'][0] instanceof WP_Widget ) ) ||
147 - // $widget_id starts with $block_widget_start.
148 - strncmp( $widget_id, $block_widget_start, strlen( $block_widget_start ) ) === 0
149 - ) {
150 - continue;
151 - }
152 - $available_legacy_widgets[ $widget_id ] = array(
153 - 'name' => html_entity_decode( $widget_obj['name'] ),
154 - 'description' => html_entity_decode( wp_widget_description( $widget_id ) ),
155 - 'isReferenceWidget' => true,
156 - );
157 - }
158 - }
159 -
160 - $settings['hasPermissionsToManageWidgets'] = $has_permissions_to_manage_widgets;
161 - $settings['availableLegacyWidgets'] = $available_legacy_widgets;
162 -
163 - return gutenberg_experiments_editor_settings( $settings );
164 160 }
165 161
166 -/**
167 - * Extends default editor settings with values supporting legacy widgets.
168 - *
169 - * @param array $settings Default editor settings.
170 - *
171 - * @return array Filtered editor settings.
172 - */
173 -function gutenberg_legacy_widget_settings( $settings ) {
174 - return array_merge( $settings, gutenberg_get_legacy_widget_settings() );
162 +if ( ! function_exists( 'wp_use_widgets_block_editor' ) ) {
163 + add_action( 'widgets_init', 'gutenberg_set_show_instance_in_rest_on_core_widgets' );
175 164 }
176 -add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' );
177 -
178 -/**
179 - * Registers a wp_area post type.
180 - */
181 -function gutenberg_create_wp_area_post_type() {
182 - register_post_type(
183 - 'wp_area',
184 - array(
185 - 'description' => __( 'Experimental custom post type that will store block areas referenced by themes.', 'gutenberg' ),
186 - 'labels' => array(
187 - 'name' => _x( 'Block Area (Experimental)', 'post type general name', 'gutenberg' ),
188 - 'singular_name' => _x( 'Block Area (Experimental)', 'post type singular name', 'gutenberg' ),
189 - 'menu_name' => _x( 'Block Areas', 'admin menu', 'gutenberg' ),
190 - 'name_admin_bar' => _x( 'Block Area', 'add new on admin bar', 'gutenberg' ),
191 - 'add_new' => _x( 'Add New', 'Block', 'gutenberg' ),
192 - 'add_new_item' => __( 'Add New Block Area', 'gutenberg' ),
193 - 'new_item' => __( 'New Block Area', 'gutenberg' ),
194 - 'edit_item' => __( 'Edit Block Area', 'gutenberg' ),
195 - 'view_item' => __( 'View Block Area', 'gutenberg' ),
196 - 'all_items' => __( 'All Block Areas', 'gutenberg' ),
197 - 'search_items' => __( 'Search Block Areas', 'gutenberg' ),
198 - 'not_found' => __( 'No block area found.', 'gutenberg' ),
199 - 'not_found_in_trash' => __( 'No block areas found in Trash.', 'gutenberg' ),
200 - 'filter_items_list' => __( 'Filter block areas list', 'gutenberg' ),
201 - 'items_list_navigation' => __( 'Block areas list navigation', 'gutenberg' ),
202 - 'items_list' => __( 'Block areas list', 'gutenberg' ),
203 - 'item_published' => __( 'Block area published.', 'gutenberg' ),
204 - 'item_published_privately' => __( 'Block area published privately.', 'gutenberg' ),
205 - 'item_reverted_to_draft' => __( 'Block area reverted to draft.', 'gutenberg' ),
206 - 'item_scheduled' => __( 'Block area scheduled.', 'gutenberg' ),
207 - 'item_updated' => __( 'Block area updated.', 'gutenberg' ),
208 - ),
209 - 'public' => false,
210 - 'show_ui' => false,
211 - 'show_in_menu' => false,
212 - 'show_in_rest' => true,
213 - 'rest_base' => '__experimental/block-areas',
214 - 'capabilities' => array(
215 - 'read' => 'edit_posts',
216 - 'create_posts' => 'edit_theme_options',
217 - 'edit_posts' => 'edit_theme_options',
218 - 'edit_published_posts' => 'edit_theme_options',
219 - 'delete_published_posts' => 'edit_theme_options',
220 - 'edit_others_posts' => 'edit_theme_options',
221 - 'delete_others_posts' => 'edit_theme_options',
222 - ),
223 - 'map_meta_cap' => true,
224 - 'supports' => array(
225 - 'title',
226 - 'editor',
227 - ),
228 - )
229 - );
230 -}
231 -add_action( 'init', 'gutenberg_create_wp_area_post_type' );
232 -
233 -add_filter( 'sidebars_widgets', 'Experimental_WP_Widget_Blocks_Manager::swap_out_sidebars_blocks_for_block_widgets' );
234 -
235 -/**
236 - * Function to enqueue admin-widgets as part of the block editor assets.
237 - */
238 -function gutenberg_enqueue_widget_scripts() {
239 - wp_enqueue_script( 'admin-widgets' );
240 -}
241 -
242 -add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_widget_scripts' );