-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
widgets.php
309 lines (281 loc) · 9.3 KB
/
widgets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
* Functions used in making widgets interopable with block editors.
*
* @package gutenberg
*/
/**
* Checks if a screen containing the block editor is being loaded.
*
* @return boolean True if a screen containing the block editor is being loaded.
*/
function gutenberg_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' ) ) {
return false;
}
$screen = get_current_screen();
return ! empty( $screen ) &&
(
$screen->is_block_editor() ||
'appearance_page_gutenberg-widgets' === $screen->id ||
( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $screen->id ) )
);
}
/**
* Whether or not to use the block editor to manage widgets. Defaults to true
* unless a theme has removed support for widgets-block-editor or a plugin has
* filtered the return value of this function.
*
* @return boolean Whether or not to use the block editor to manage widgets.
*/
function gutenberg_use_widgets_block_editor() {
/**
* Filters whether or not to use the block editor to manage widgets.
*
* @param boolean $use_widgets_block_editor Whether or not to use the block editor to manage widgets.
*/
return apply_filters(
'gutenberg_use_widgets_block_editor',
get_theme_support( '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.
*
* @return array Legacy widget settings.
*/
function gutenberg_get_legacy_widget_settings() {
global $wp_widget_factory;
$settings = array();
$widget_types_to_hide_from_legacy_widget_block = apply_filters(
'widget_types_to_hide_from_legacy_widget_block',
array(
'pages',
'calendar',
'archives',
'media_audio',
'media_image',
'media_gallery',
'media_video',
'meta',
'search',
'text',
'categories',
'recent-posts',
'recent-comments',
'rss',
'tag_cloud',
'custom_html',
'block',
)
);
// 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 <form> tag.
*
* @param array $arg Dynamic sidebar params.
* @return array Updated dynamic sidebar params.
*/
function gutenberg_override_sidebar_params_for_block_widget( $arg ) {
if ( 'Block' === $arg[0]['widget_name'] ) {
$arg[0]['before_form'] = '';
$arg[0]['before_widget_content'] = '<div class="widget-content">';
$arg[0]['after_widget_content'] = '</div><form class="block-widget-form">';
$arg[0]['after_form'] = '</form>';
}
return $arg;
}
/**
* Registers the WP_Widget_Block widget.
*/
function gutenberg_register_block_widget() {
global $pagenow;
register_widget( 'WP_Widget_Block' );
// By default every widget on widgets.php is wrapped with a <form>. This
// means that you can sometimes end up with invalid HTML, e.g. when one of
// the widgets is a Search block. To fix the problem, let's add a filter
// that moves the form below the actual widget content.
if ( 'widgets.php' === $pagenow ) {
add_filter(
'dynamic_sidebar_params',
'gutenberg_override_sidebar_params_for_block_widget'
);
}
}
add_action( 'widgets_init', 'gutenberg_register_block_widget' );
/**
* Sets show_instance_in_rest to true on all of the core WP_Widget subclasses.
* When merged to Core, this property should be added to WP_Widget and set to
* true on each WP_Widget subclass.
*/
function gutenberg_set_show_instance_in_rest_on_core_widgets() {
global $wp_widget_factory;
$core_widgets = array(
'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',
);
foreach ( $core_widgets as $widget ) {
if ( isset( $wp_widget_factory->widgets[ $widget ] ) ) {
$wp_widget_factory->widgets[ $widget ]->show_instance_in_rest = true;
}
}
}
add_action( 'widgets_init', 'gutenberg_set_show_instance_in_rest_on_core_widgets' );