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

256 lines 9.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 * 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_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 * TODO: The hardcoded array should be replaced with a mechanism to allow
110 * core and third party blocks to specify they already have equivalent
111 * blocks, and maybe even allow them to have a migration function.
112 */
113 $core_widgets = array(
114 'WP_Widget_Pages',
115 'WP_Widget_Calendar',
116 'WP_Widget_Archives',
117 'WP_Widget_Media_Audio',
118 'WP_Widget_Media_Image',
119 'WP_Widget_Media_Gallery',
120 'WP_Widget_Media_Video',
121 'WP_Widget_Meta',
122 'WP_Widget_Search',
123 'WP_Widget_Text',
124 'WP_Widget_Categories',
125 'WP_Widget_Recent_Posts',
126 'WP_Widget_Recent_Comments',
127 'WP_Widget_RSS',
128 'WP_Widget_Tag_Cloud',
129 'WP_Nav_Menu_Widget',
130 'WP_Widget_Custom_HTML',
131 );
132
133 $has_permissions_to_manage_widgets = current_user_can( 'edit_theme_options' );
134 $available_legacy_widgets = array();
135 global $wp_widget_factory;
136 if ( ! empty( $wp_widget_factory ) ) {
137 foreach ( $wp_widget_factory->widgets as $class => $widget_obj ) {
138 $available_legacy_widgets[ $class ] = array(
139 'name' => html_entity_decode( $widget_obj->name ),
140 // wp_widget_description is not being used because its input parameter is a Widget Id.
141 // Widgets id's reference to a specific widget instance.
142 // Here we are iterating on all the available widget classes even if no widget instance exists for them.
143 'description' => isset( $widget_obj->widget_options['description'] ) ?
144 html_entity_decode( $widget_obj->widget_options['description'] ) :
145 null,
146 'isReferenceWidget' => false,
147 'isHidden' => in_array( $class, $core_widgets, true ),
148 );
149 }
150 }
151 global $wp_registered_widgets;
152 if ( ! empty( $wp_registered_widgets ) ) {
153 foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) {
154
155 $block_widget_start = 'blocks-widget-';
156 if (
157 ( is_array( $widget_obj['callback'] ) &&
158 isset( $widget_obj['callback'][0] ) &&
159 ( $widget_obj['callback'][0] instanceof WP_Widget ) ) ||
160 // $widget_id starts with $block_widget_start.
161 strncmp( $widget_id, $block_widget_start, strlen( $block_widget_start ) ) === 0
162 ) {
163 continue;
164 }
165 $available_legacy_widgets[ $widget_id ] = array(
166 'name' => html_entity_decode( $widget_obj['name'] ),
167 'description' => html_entity_decode( wp_widget_description( $widget_id ) ),
168 'isReferenceWidget' => true,
169 );
170 }
171 }
172
173 $settings['hasPermissionsToManageWidgets'] = $has_permissions_to_manage_widgets;
174 $settings['availableLegacyWidgets'] = $available_legacy_widgets;
175
176 return gutenberg_experiments_editor_settings( $settings );
177 }
178
179 /**
180 * Extends default editor settings with values supporting legacy widgets.
181 *
182 * @param array $settings Default editor settings.
183 *
184 * @return array Filtered editor settings.
185 */
186 function gutenberg_legacy_widget_settings( $settings ) {
187 return array_merge( $settings, gutenberg_get_legacy_widget_settings() );
188 }
189 add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' );
190
191 /**
192 * Registers a wp_area post type.
193 */
194 function gutenberg_create_wp_area_post_type() {
195 register_post_type(
196 'wp_area',
197 array(
198 'description' => __( 'Experimental custom post type that will store block areas referenced by themes.', 'gutenberg' ),
199 'labels' => array(
200 'name' => _x( 'Block Area (Experimental)', 'post type general name', 'gutenberg' ),
201 'singular_name' => _x( 'Block Area (Experimental)', 'post type singular name', 'gutenberg' ),
202 'menu_name' => _x( 'Block Areas', 'admin menu', 'gutenberg' ),
203 'name_admin_bar' => _x( 'Block Area', 'add new on admin bar', 'gutenberg' ),
204 'add_new' => _x( 'Add New', 'Block', 'gutenberg' ),
205 'add_new_item' => __( 'Add New Block Area', 'gutenberg' ),
206 'new_item' => __( 'New Block Area', 'gutenberg' ),
207 'edit_item' => __( 'Edit Block Area', 'gutenberg' ),
208 'view_item' => __( 'View Block Area', 'gutenberg' ),
209 'all_items' => __( 'All Block Areas', 'gutenberg' ),
210 'search_items' => __( 'Search Block Areas', 'gutenberg' ),
211 'not_found' => __( 'No block area found.', 'gutenberg' ),
212 'not_found_in_trash' => __( 'No block areas found in Trash.', 'gutenberg' ),
213 'filter_items_list' => __( 'Filter block areas list', 'gutenberg' ),
214 'items_list_navigation' => __( 'Block areas list navigation', 'gutenberg' ),
215 'items_list' => __( 'Block areas list', 'gutenberg' ),
216 'item_published' => __( 'Block area published.', 'gutenberg' ),
217 'item_published_privately' => __( 'Block area published privately.', 'gutenberg' ),
218 'item_reverted_to_draft' => __( 'Block area reverted to draft.', 'gutenberg' ),
219 'item_scheduled' => __( 'Block area scheduled.', 'gutenberg' ),
220 'item_updated' => __( 'Block area updated.', 'gutenberg' ),
221 ),
222 'public' => false,
223 'show_ui' => false,
224 'show_in_menu' => false,
225 'show_in_rest' => true,
226 'rest_base' => '__experimental/block-areas',
227 'capabilities' => array(
228 'read' => 'edit_posts',
229 'create_posts' => 'edit_theme_options',
230 'edit_posts' => 'edit_theme_options',
231 'edit_published_posts' => 'edit_theme_options',
232 'delete_published_posts' => 'edit_theme_options',
233 'edit_others_posts' => 'edit_theme_options',
234 'delete_others_posts' => 'edit_theme_options',
235 ),
236 'map_meta_cap' => true,
237 'supports' => array(
238 'title',
239 'editor',
240 ),
241 )
242 );
243 }
244 add_action( 'init', 'gutenberg_create_wp_area_post_type' );
245
246 add_filter( 'sidebars_widgets', 'Experimental_WP_Widget_Blocks_Manager::swap_out_sidebars_blocks_for_block_widgets' );
247
248 /**
249 * Function to enqueue admin-widgets as part of the block editor assets.
250 */
251 function gutenberg_enqueue_widget_scripts() {
252 wp_enqueue_script( 'admin-widgets' );
253 }
254
255 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_widget_scripts' );
256