PluginProbe
Gutenberg / 10.5.3
Gutenberg v10.5.3
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
gutenberg / lib / widgets.php

widgets.php in Gutenberg 10.5.3, at lib/widgets.php

309 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions used in making widgets interopable with block editors.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Checks if a screen containing the block editor is being loaded.
10 *
11 * @return boolean True if a screen containing the block editor is being loaded.
12 */
13 function gutenberg_is_block_editor() {
14 // If get_current_screen does not exist, we are neither in the standard block editor for posts, or the widget block editor.
15 // We can safely return false.
16 if ( ! function_exists( 'get_current_screen' ) ) {
17 return false;
18 }
19 $screen = get_current_screen();
20 return ! empty( $screen ) &&
21 (
22 $screen->is_block_editor() ||
23 'appearance_page_gutenberg-widgets' === $screen->id ||
24 ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $screen->id ) )
25 );
26 }
27
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 /**
48 * Emulates the Widgets screen `admin_print_styles` when at the block editor
49 * screen.
50 */
51 function gutenberg_block_editor_admin_print_styles() {
52 if ( gutenberg_is_block_editor() ) {
53 /** This action is documented in wp-admin/admin-footer.php */
54 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
55 do_action( 'admin_print_styles-widgets.php' );
56 }
57 }
58 add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );
59
60 /**
61 * Emulates the Widgets screen `admin_print_scripts` when at the block editor
62 * screen.
63 */
64 function gutenberg_block_editor_admin_print_scripts() {
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' );
72 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
73 do_action( 'admin_print_scripts-widgets.php' );
74 }
75 }
76 add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' );
77
78 /**
79 * Emulates the Widgets screen `admin_print_footer_scripts` when at the block
80 * editor screen.
81 */
82 function gutenberg_block_editor_admin_print_footer_scripts() {
83 if ( gutenberg_is_block_editor() ) {
84 /** This action is documented in wp-admin/admin-footer.php */
85 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
86 do_action( 'admin_print_footer_scripts-widgets.php' );
87 }
88 }
89 add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' );
90
91 /**
92 * Emulates the Widgets screen `admin_footer` when at the block editor screen.
93 */
94 function gutenberg_block_editor_admin_footer() {
95 if ( gutenberg_is_block_editor() ) {
96 /** This action is documented in wp-admin/admin-footer.php */
97 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
98 do_action( 'admin_footer-widgets.php' );
99 }
100 }
101 add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' );
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' );
118
119
120 /**
121 * Returns the settings required by legacy widgets blocks.
122 *
123 * @return array Legacy widget settings.
124 */
125 function gutenberg_get_legacy_widget_settings() {
126 global $wp_widget_factory;
127
128 $settings = array();
129
130 $widget_types_to_hide_from_legacy_widget_block = apply_filters(
131 'widget_types_to_hide_from_legacy_widget_block',
132 array(
133 'pages',
134 'calendar',
135 'archives',
136 'media_audio',
137 'media_image',
138 'media_gallery',
139 'media_video',
140 'meta',
141 'search',
142 'text',
143 'categories',
144 'recent-posts',
145 'recent-comments',
146 'rss',
147 'tag_cloud',
148 'nav_menu',
149 'custom_html',
150 'block',
151 )
152 );
153
154 // Backwards compatibility. Remove this in or after Gutenberg 10.5.
155 if ( has_filter( 'widgets_to_exclude_from_legacy_widget_block' ) ) {
156 /**
157 * Filters the list of widget classes that should **not** be offered by the legacy widget block.
158 *
159 * Returning an empty array will make all the widgets available.
160 *
161 * @param array $widgets An array of excluded widgets classnames.
162 *
163 * @since 5.6.0
164 */
165 $widgets_to_exclude_from_legacy_widget_block = apply_filters(
166 'widgets_to_exclude_from_legacy_widget_block',
167 array(
168 'WP_Widget_Block',
169 'WP_Widget_Pages',
170 'WP_Widget_Calendar',
171 'WP_Widget_Archives',
172 'WP_Widget_Media_Audio',
173 'WP_Widget_Media_Image',
174 'WP_Widget_Media_Gallery',
175 'WP_Widget_Media_Video',
176 'WP_Widget_Meta',
177 'WP_Widget_Search',
178 'WP_Widget_Text',
179 'WP_Widget_Categories',
180 'WP_Widget_Recent_Posts',
181 'WP_Widget_Recent_Comments',
182 'WP_Widget_RSS',
183 'WP_Widget_Tag_Cloud',
184 'WP_Nav_Menu_Widget',
185 'WP_Widget_Custom_HTML',
186 )
187 );
188
189 _deprecated_hook(
190 'widgets_to_exclude_from_legacy_widget_block',
191 '10.3',
192 "wp.hooks.addFilter( 'legacyWidget.isWidgetTypeHidden', ... )"
193 );
194
195 foreach ( $wp_widget_factory->widgets as $widget ) {
196 if (
197 in_array( get_class( $widget ), $widgets_to_exclude_from_legacy_widget_block, true ) &&
198 ! in_array( $widget->id_base, $widget_types_to_hide_from_legacy_widget_block, true )
199 ) {
200 $widget_types_to_hide_from_legacy_widget_block[] = $widget->id_base;
201 }
202 }
203 }
204
205 $settings['widgetTypesToHideFromLegacyWidgetBlock'] = $widget_types_to_hide_from_legacy_widget_block;
206
207 return $settings;
208 }
209
210 /**
211 * Extends default editor settings with values supporting legacy widgets.
212 *
213 * @param array $settings Default editor settings.
214 *
215 * @return array Filtered editor settings.
216 */
217 function gutenberg_legacy_widget_settings( $settings ) {
218 return array_merge( $settings, gutenberg_get_legacy_widget_settings() );
219 }
220 add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' );
221
222 /**
223 * Function to enqueue admin-widgets as part of the block editor assets.
224 */
225 function gutenberg_enqueue_widget_scripts() {
226 wp_enqueue_script( 'admin-widgets' );
227 }
228
229 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_widget_scripts' );
230
231 /**
232 * Overrides dynamic_sidebar_params to make sure Blocks are not wrapped in <form> tag.
233 *
234 * @param array $arg Dynamic sidebar params.
235 * @return array Updated dynamic sidebar params.
236 */
237 function gutenberg_override_sidebar_params_for_block_widget( $arg ) {
238 if ( 'Block' === $arg[0]['widget_name'] ) {
239 $arg[0]['before_form'] = '';
240 $arg[0]['before_widget_content'] = '<div class="widget-content">';
241 $arg[0]['after_widget_content'] = '</div><form class="block-widget-form">';
242 $arg[0]['after_form'] = '</form>';
243 }
244
245 return $arg;
246 }
247
248 /**
249 * Registers the WP_Widget_Block widget
250 */
251 function gutenberg_register_widgets() {
252 if ( ! gutenberg_use_widgets_block_editor() ) {
253 return;
254 }
255
256 register_widget( 'WP_Widget_Block' );
257 // By default every widget on widgets.php is wrapped with a <form>.
258 // This means that you can sometimes end up with invalid HTML, e.g. when
259 // one of the widgets is a Search block.
260 //
261 // To fix the problem, let's add a filter that moves the form below the actual
262 // widget content.
263 global $pagenow;
264 if ( 'widgets.php' === $pagenow ) {
265 add_filter(
266 'dynamic_sidebar_params',
267 'gutenberg_override_sidebar_params_for_block_widget'
268 );
269 }
270 }
271
272 add_action( 'widgets_init', 'gutenberg_register_widgets' );
273
274 /**
275 * Sets show_instance_in_rest to true on all of the core WP_Widget subclasses.
276 * When merge dto Core, this property should be added to WP_Widget and set to
277 * true on each WP_Widget subclass.
278 */
279 function gutenberg_set_show_instance_in_rest_on_core_widgets() {
280 global $wp_widget_factory;
281
282 $core_widgets = array(
283 'WP_Widget_Pages',
284 'WP_Widget_Calendar',
285 'WP_Widget_Archives',
286 'WP_Widget_Media_Audio',
287 'WP_Widget_Media_Image',
288 'WP_Widget_Media_Gallery',
289 'WP_Widget_Media_Video',
290 'WP_Widget_Meta',
291 'WP_Widget_Search',
292 'WP_Widget_Text',
293 'WP_Widget_Categories',
294 'WP_Widget_Recent_Posts',
295 'WP_Widget_Recent_Comments',
296 'WP_Widget_RSS',
297 'WP_Widget_Tag_Cloud',
298 'WP_Nav_Menu_Widget',
299 'WP_Widget_Custom_HTML',
300 );
301
302 foreach ( $core_widgets as $widget ) {
303 if ( isset( $wp_widget_factory->widgets[ $widget ] ) ) {
304 $wp_widget_factory->widgets[ $widget ]->show_instance_in_rest = true;
305 }
306 }
307 }
308 add_action( 'widgets_init', 'gutenberg_set_show_instance_in_rest_on_core_widgets' );
309