PluginProbe
Gutenberg / 5.6.0
Gutenberg v5.6.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
gutenberg / lib / widgets.php

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

129 lines 4.4 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 * Emulates the Widgets screen `admin_print_styles` when at the block editor
10 * screen.
11 */
12 function gutenberg_block_editor_admin_print_styles() {
13 if ( get_current_screen()->is_block_editor() ) {
14 /** This action is documented in wp-admin/admin-footer.php */
15 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
16 do_action( 'admin_print_styles-widgets.php' );
17 }
18 }
19 add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );
20
21 /**
22 * Emulates the Widgets screen `admin_print_scripts` when at the block editor
23 * screen.
24 */
25 function gutenberg_block_editor_admin_print_scripts() {
26 if ( get_current_screen()->is_block_editor() ) {
27 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
28 do_action( 'admin_print_scripts-widgets.php' );
29 }
30 }
31 add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' );
32
33 /**
34 * Emulates the Widgets screen `admin_print_footer_scripts` when at the block
35 * editor screen.
36 */
37 function gutenberg_block_editor_admin_print_footer_scripts() {
38 if ( get_current_screen()->is_block_editor() ) {
39 /** This action is documented in wp-admin/admin-footer.php */
40 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
41 do_action( 'admin_print_footer_scripts-widgets.php' );
42 }
43 }
44 add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' );
45
46 /**
47 * Emulates the Widgets screen `admin_footer` when at the block editor screen.
48 */
49 function gutenberg_block_editor_admin_footer() {
50 if ( get_current_screen()->is_block_editor() ) {
51 /** This action is documented in wp-admin/admin-footer.php */
52 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
53 do_action( 'admin_footer-widgets.php' );
54 }
55 }
56 add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' );
57
58 /**
59 * Extends default editor settings with values supporting legacy widgets.
60 *
61 * @param array $settings Default editor settings.
62 *
63 * @return array Filtered editor settings.
64 */
65 function gutenberg_legacy_widget_settings( $settings ) {
66 /**
67 * TODO: The hardcoded array should be replaced with a mechanism to allow
68 * core and third party blocks to specify they already have equivalent
69 * blocks, and maybe even allow them to have a migration function.
70 */
71 $core_widgets = array(
72 'WP_Widget_Pages',
73 'WP_Widget_Calendar',
74 'WP_Widget_Archives',
75 'WP_Widget_Media_Audio',
76 'WP_Widget_Media_Image',
77 'WP_Widget_Media_Gallery',
78 'WP_Widget_Media_Video',
79 'WP_Widget_Meta',
80 'WP_Widget_Search',
81 'WP_Widget_Text',
82 'WP_Widget_Categories',
83 'WP_Widget_Recent_Posts',
84 'WP_Widget_Recent_Comments',
85 'WP_Widget_RSS',
86 'WP_Widget_Tag_Cloud',
87 'WP_Nav_Menu_Widget',
88 'WP_Widget_Custom_HTML',
89 );
90
91 $has_permissions_to_manage_widgets = current_user_can( 'edit_theme_options' );
92 $available_legacy_widgets = array();
93 global $wp_widget_factory, $wp_registered_widgets;
94 foreach ( $wp_widget_factory->widgets as $class => $widget_obj ) {
95 if ( ! in_array( $class, $core_widgets ) ) {
96 $available_legacy_widgets[ $class ] = array(
97 'name' => html_entity_decode( $widget_obj->name ),
98 // wp_widget_description is not being used because its input parameter is a Widget Id.
99 // Widgets id's reference to a specific widget instance.
100 // Here we are iterating on all the available widget classes even if no widget instance exists for them.
101 'description' => isset( $widget_obj->widget_options['description'] ) ?
102 html_entity_decode( $widget_obj->widget_options['description'] ) :
103 null,
104 'isCallbackWidget' => false,
105 );
106 }
107 }
108 foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) {
109 if (
110 is_array( $widget_obj['callback'] ) &&
111 isset( $widget_obj['callback'][0] ) &&
112 ( $widget_obj['callback'][0] instanceof WP_Widget )
113 ) {
114 continue;
115 }
116 $available_legacy_widgets[ $widget_id ] = array(
117 'name' => html_entity_decode( $widget_obj['name'] ),
118 'description' => html_entity_decode( wp_widget_description( $widget_id ) ),
119 'isCallbackWidget' => true,
120 );
121 }
122
123 $settings['hasPermissionsToManageWidgets'] = $has_permissions_to_manage_widgets;
124 $settings['availableLegacyWidgets'] = $available_legacy_widgets;
125
126 return $settings;
127 }
128 add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' );
129