PluginProbe
Gutenberg / 7.0.0
Gutenberg v7.0.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.0.0, at lib/widgets.php

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