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

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