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

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