PluginProbe
Gutenberg / 7.4.0
Gutenberg v7.4.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 7.4.0, at lib/widgets.php

243 lines 8.8 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 'gutenberg_page_gutenberg-widgets' === $screen->id ||
24 'gutenberg_page_gutenberg-edit-site' === $screen->id
25 );
26 }
27
28 /**
29 * Emulates the Widgets screen `admin_print_styles` when at the block editor
30 * screen.
31 */
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 }
38 }
39 add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );
40
41 /**
42 * Emulates the Widgets screen `admin_print_scripts` when at the block editor
43 * screen.
44 */
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 }
50 }
51 add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' );
52
53 /**
54 * Emulates the Widgets screen `admin_print_footer_scripts` when at the block
55 * editor screen.
56 */
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' );
62 }
63 }
64 add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' );
65
66 /**
67 * Emulates the Widgets screen `admin_footer` when at the block editor screen.
68 */
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 )
79 );
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 }
84 }
85 add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' );
86
87
88 /**
89 * Returns the settings required by legacy widgets blocks.
90 *
91 * @return array Legacy widget settings.
92 */
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 */
100 $core_widgets = array(
101 'WP_Widget_Pages',
102 'WP_Widget_Calendar',
103 'WP_Widget_Archives',
104 'WP_Widget_Media_Audio',
105 'WP_Widget_Media_Image',
106 'WP_Widget_Media_Gallery',
107 'WP_Widget_Media_Video',
108 'WP_Widget_Meta',
109 'WP_Widget_Search',
110 'WP_Widget_Text',
111 'WP_Widget_Categories',
112 'WP_Widget_Recent_Posts',
113 'WP_Widget_Recent_Comments',
114 'WP_Widget_RSS',
115 'WP_Widget_Tag_Cloud',
116 'WP_Nav_Menu_Widget',
117 'WP_Widget_Custom_HTML',
118 );
119
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 );
136 }
137 }
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 }
165
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() );
175 }
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' );
243