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

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