PluginProbe
Gutenberg / 8.8.0
Gutenberg v8.8.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 8.8.0, at lib/widgets.php

263 lines 9.6 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 ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $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 /** This action is documented in wp-admin/includes/ajax-actions.php */
48 do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
49 /** This action is documented in wp-admin/includes/ajax-actions.php */
50 do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
51 /** This action is documented in wp-admin/widgets.php */
52 do_action( 'sidebar_admin_setup' );
53 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
54 do_action( 'admin_print_scripts-widgets.php' );
55 }
56 }
57 add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' );
58
59 /**
60 * Emulates the Widgets screen `admin_print_footer_scripts` when at the block
61 * editor screen.
62 */
63 function gutenberg_block_editor_admin_print_footer_scripts() {
64 if ( gutenberg_is_block_editor() ) {
65 /** This action is documented in wp-admin/admin-footer.php */
66 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
67 do_action( 'admin_print_footer_scripts-widgets.php' );
68 }
69 }
70 add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' );
71
72 /**
73 * Emulates the Widgets screen `admin_footer` when at the block editor screen.
74 */
75 function gutenberg_block_editor_admin_footer() {
76 if ( gutenberg_is_block_editor() ) {
77 /** This action is documented in wp-admin/admin-footer.php */
78 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
79 do_action( 'admin_footer-widgets.php' );
80 }
81 }
82 add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' );
83
84 /**
85 * Adds a save widgets nonce required by the legacy widgets block.
86 */
87 function gutenberg_print_save_widgets_nonce() {
88 // The function wpWidgets.save needs this nonce to work as expected.
89 echo implode(
90 "\n",
91 array(
92 '<form method="post">',
93 wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ),
94 '</form>',
95 )
96 );
97 }
98 add_action( 'admin_footer-widgets.php', 'gutenberg_print_save_widgets_nonce' );
99
100
101 /**
102 * Returns the settings required by legacy widgets blocks.
103 *
104 * @return array Legacy widget settings.
105 */
106 function gutenberg_get_legacy_widget_settings() {
107 $settings = array();
108
109 /**
110 * Filters the list of widget classes that should **not** be offered by the legacy widget block.
111 *
112 * Returning an empty array will make all the widgets available.
113 *
114 * @param array $widgets An array of excluded widgets classnames.
115 *
116 * @since 5.6.0
117 */
118 $widgets_to_exclude_from_legacy_widget_block = apply_filters(
119 'widgets_to_exclude_from_legacy_widget_block',
120 array(
121 'WP_Widget_Pages',
122 'WP_Widget_Calendar',
123 'WP_Widget_Archives',
124 'WP_Widget_Media_Audio',
125 'WP_Widget_Media_Image',
126 'WP_Widget_Media_Gallery',
127 'WP_Widget_Media_Video',
128 'WP_Widget_Meta',
129 'WP_Widget_Search',
130 'WP_Widget_Text',
131 'WP_Widget_Categories',
132 'WP_Widget_Recent_Posts',
133 'WP_Widget_Recent_Comments',
134 'WP_Widget_RSS',
135 'WP_Widget_Tag_Cloud',
136 'WP_Nav_Menu_Widget',
137 'WP_Widget_Custom_HTML',
138 )
139 );
140
141 $has_permissions_to_manage_widgets = current_user_can( 'edit_theme_options' );
142 $available_legacy_widgets = array();
143 global $wp_widget_factory;
144 if ( ! empty( $wp_widget_factory ) ) {
145 foreach ( $wp_widget_factory->widgets as $class => $widget_obj ) {
146 $available_legacy_widgets[ $class ] = array(
147 'name' => html_entity_decode( $widget_obj->name ),
148 'id_base' => $widget_obj->id_base,
149 // wp_widget_description is not being used because its input parameter is a Widget Id.
150 // Widgets id's reference to a specific widget instance.
151 // Here we are iterating on all the available widget classes even if no widget instance exists for them.
152 'description' => isset( $widget_obj->widget_options['description'] ) ?
153 html_entity_decode( $widget_obj->widget_options['description'] ) :
154 null,
155 'isReferenceWidget' => false,
156 'isHidden' => in_array( $class, $widgets_to_exclude_from_legacy_widget_block, true ),
157 );
158 }
159 }
160 global $wp_registered_widgets;
161 if ( ! empty( $wp_registered_widgets ) ) {
162 foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) {
163
164 $block_widget_start = 'blocks-widget-';
165 if (
166 ( is_array( $widget_obj['callback'] ) &&
167 isset( $widget_obj['callback'][0] ) &&
168 ( $widget_obj['callback'][0] instanceof WP_Widget ) ) ||
169 // $widget_id starts with $block_widget_start.
170 strncmp( $widget_id, $block_widget_start, strlen( $block_widget_start ) ) === 0
171 ) {
172 continue;
173 }
174 $available_legacy_widgets[ $widget_id ] = array(
175 'name' => html_entity_decode( $widget_obj['name'] ),
176 'description' => html_entity_decode( wp_widget_description( $widget_id ) ),
177 'isReferenceWidget' => true,
178 );
179 }
180 }
181
182 $settings['hasPermissionsToManageWidgets'] = $has_permissions_to_manage_widgets;
183 $settings['availableLegacyWidgets'] = $available_legacy_widgets;
184
185 return gutenberg_experiments_editor_settings( $settings );
186 }
187
188 /**
189 * Extends default editor settings with values supporting legacy widgets.
190 *
191 * @param array $settings Default editor settings.
192 *
193 * @return array Filtered editor settings.
194 */
195 function gutenberg_legacy_widget_settings( $settings ) {
196 return array_merge( $settings, gutenberg_get_legacy_widget_settings() );
197 }
198 add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' );
199
200 /**
201 * Registers a wp_area post type.
202 */
203 function gutenberg_create_wp_area_post_type() {
204 register_post_type(
205 'wp_area',
206 array(
207 'description' => __( 'Experimental custom post type that will store block areas referenced by themes.', 'gutenberg' ),
208 'labels' => array(
209 'name' => _x( 'Block Area (Experimental)', 'post type general name', 'gutenberg' ),
210 'singular_name' => _x( 'Block Area (Experimental)', 'post type singular name', 'gutenberg' ),
211 'menu_name' => _x( 'Block Areas', 'admin menu', 'gutenberg' ),
212 'name_admin_bar' => _x( 'Block Area', 'add new on admin bar', 'gutenberg' ),
213 'add_new' => _x( 'Add New', 'Block', 'gutenberg' ),
214 'add_new_item' => __( 'Add New Block Area', 'gutenberg' ),
215 'new_item' => __( 'New Block Area', 'gutenberg' ),
216 'edit_item' => __( 'Edit Block Area', 'gutenberg' ),
217 'view_item' => __( 'View Block Area', 'gutenberg' ),
218 'all_items' => __( 'All Block Areas', 'gutenberg' ),
219 'search_items' => __( 'Search Block Areas', 'gutenberg' ),
220 'not_found' => __( 'No block area found.', 'gutenberg' ),
221 'not_found_in_trash' => __( 'No block areas found in Trash.', 'gutenberg' ),
222 'filter_items_list' => __( 'Filter block areas list', 'gutenberg' ),
223 'items_list_navigation' => __( 'Block areas list navigation', 'gutenberg' ),
224 'items_list' => __( 'Block areas list', 'gutenberg' ),
225 'item_published' => __( 'Block area published.', 'gutenberg' ),
226 'item_published_privately' => __( 'Block area published privately.', 'gutenberg' ),
227 'item_reverted_to_draft' => __( 'Block area reverted to draft.', 'gutenberg' ),
228 'item_scheduled' => __( 'Block area scheduled.', 'gutenberg' ),
229 'item_updated' => __( 'Block area updated.', 'gutenberg' ),
230 ),
231 'public' => false,
232 'show_ui' => false,
233 'show_in_menu' => false,
234 'show_in_rest' => true,
235 'rest_base' => '__experimental/block-areas',
236 'capabilities' => array(
237 'read' => 'edit_posts',
238 'create_posts' => 'edit_theme_options',
239 'edit_posts' => 'edit_theme_options',
240 'edit_published_posts' => 'edit_theme_options',
241 'delete_published_posts' => 'edit_theme_options',
242 'edit_others_posts' => 'edit_theme_options',
243 'delete_others_posts' => 'edit_theme_options',
244 ),
245 'map_meta_cap' => true,
246 'supports' => array(
247 'title',
248 'editor',
249 ),
250 )
251 );
252 }
253 add_action( 'init', 'gutenberg_create_wp_area_post_type' );
254
255 /**
256 * Function to enqueue admin-widgets as part of the block editor assets.
257 */
258 function gutenberg_enqueue_widget_scripts() {
259 wp_enqueue_script( 'admin-widgets' );
260 }
261
262 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_widget_scripts' );
263