-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathwidgets.php
123 lines (115 loc) · 4 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
<?php
/**
* Functions used in making widgets interopable with block editors.
*
* @package gutenberg
*/
/**
* Emulates the Widgets screen `admin_print_styles` when at the block editor
* screen.
*/
function gutenberg_block_editor_admin_print_styles() {
if ( get_current_screen()->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 ( get_current_screen()->is_block_editor() ) {
// 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 ( get_current_screen()->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 ( get_current_screen()->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' );
/**
* Extends default editor settings with values supporting legacy widgets.
*
* @param array $settings Default editor settings.
*
* @return array Filtered editor settings.
*/
function gutenberg_legacy_widget_settings( $settings ) {
/**
* TODO: The hardcoded array should be replaced with a mechanism to allow
* core and third party blocks to specify they already have equivalent
* blocks, and maybe even allow them to have a migration function.
*/
$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',
);
$has_permissions_to_manage_widgets = current_user_can( 'edit_theme_options' );
$available_legacy_widgets = array();
global $wp_widget_factory, $wp_registered_widgets;
foreach ( $wp_widget_factory->widgets as $class => $widget_obj ) {
if ( ! in_array( $class, $core_widgets ) ) {
$available_legacy_widgets[ $class ] = array(
'name' => html_entity_decode( $widget_obj->name ),
'description' => html_entity_decode( $widget_obj->widget_options['description'] ),
'isCallbackWidget' => false,
);
}
}
foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) {
if (
is_array( $widget_obj['callback'] ) &&
isset( $widget_obj['callback'][0] ) &&
( $widget_obj['callback'][0] instanceof WP_Widget )
) {
continue;
}
$available_legacy_widgets[ $widget_id ] = array(
'name' => html_entity_decode( $widget_obj['name'] ),
'description' => null,
'isCallbackWidget' => true,
);
}
$settings['hasPermissionsToManageWidgets'] = $has_permissions_to_manage_widgets;
$settings['availableLegacyWidgets'] = $available_legacy_widgets;
return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' );