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

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