PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 / compat / wordpress-5.8 / block-editor.php

block-editor.php in Gutenberg 12.1.0, at lib/compat/wordpress-5.8/block-editor.php

399 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block Editor API.
4 *
5 * @package Gutenberg
6 * @subpackage Editor
7 * @since 10.5.0
8 */
9
10 /**
11 * Returns the list of default categories for block types.
12 *
13 * This is a temporary solution until the Gutenberg plugin sets
14 * the required WordPress version to 5.8.
15 *
16 * @see https://core.trac.wordpress.org/ticket/52920
17 *
18 * @since 10.5.0.
19 *
20 * @return array[] Array of categories for block types.
21 */
22 function gutenberg_get_default_block_categories() {
23 return array(
24 array(
25 'slug' => 'text',
26 'title' => _x( 'Text', 'block category', 'gutenberg' ),
27 'icon' => null,
28 ),
29 array(
30 'slug' => 'media',
31 'title' => _x( 'Media', 'block category', 'gutenberg' ),
32 'icon' => null,
33 ),
34 array(
35 'slug' => 'design',
36 'title' => _x( 'Design', 'block category', 'gutenberg' ),
37 'icon' => null,
38 ),
39 array(
40 'slug' => 'widgets',
41 'title' => _x( 'Widgets', 'block category', 'gutenberg' ),
42 'icon' => null,
43 ),
44 array(
45 'slug' => 'theme',
46 'title' => _x( 'Theme', 'block category', 'gutenberg' ),
47 'icon' => null,
48 ),
49 array(
50 'slug' => 'embed',
51 'title' => _x( 'Embeds', 'block category', 'gutenberg' ),
52 'icon' => null,
53 ),
54 array(
55 'slug' => 'reusable',
56 'title' => _x( 'Reusable Blocks', 'block category', 'gutenberg' ),
57 'icon' => null,
58 ),
59 );
60 }
61
62 /**
63 * Returns all the categories for block types that will be shown in the block editor.
64 *
65 * This is a temporary solution until the Gutenberg plugin sets
66 * the required WordPress version to 5.8.
67 *
68 * @see https://core.trac.wordpress.org/ticket/52920
69 *
70 * @since 10.5.0
71 *
72 * @param WP_Post|WP_Block_Editor_Context $post_or_block_editor_context The current post object or
73 * the block editor context.
74 * @return array[] Array of categories for block types.
75 */
76 function gutenberg_get_block_categories( $post_or_block_editor_context ) {
77 $block_categories = gutenberg_get_default_block_categories();
78 $block_editor_context = $post_or_block_editor_context instanceof WP_Post ?
79 new WP_Block_Editor_Context(
80 array(
81 'post' => $post_or_block_editor_context,
82 )
83 ) : $post_or_block_editor_context;
84
85 /**
86 * Filters the default array of categories for block types.
87 *
88 * @since 5.8.0
89 *
90 * @param array[] $block_categories Array of categories for block types.
91 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
92 */
93 $block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );
94 if ( ! empty( $block_editor_context->post ) ) {
95 $post = $block_editor_context->post;
96
97 /**
98 * Filters the default array of categories for block types.
99 *
100 * @since 5.0.0
101 * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain the $post instance.
102 *
103 * @param array[] $block_categories Array of categories for block types.
104 * @param WP_Post $post Post being loaded.
105 */
106 $block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' );
107 }
108
109 return $block_categories;
110 }
111
112 /**
113 * Gets the list of allowed block types to use in the block editor.
114 *
115 * This is a temporary solution until the Gutenberg plugin sets
116 * the required WordPress version to 5.8.
117 *
118 * @see https://core.trac.wordpress.org/ticket/52920
119 *
120 * @since 10.5.0
121 *
122 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
123 *
124 * @return bool|array Array of block type slugs, or boolean to enable/disable all.
125 */
126 function gutenberg_get_allowed_block_types( $block_editor_context ) {
127 $allowed_block_types = true;
128
129 /**
130 * Filters the allowed block types for all editor types, defaulting to `true`
131 * (all registered block types supported).
132 *
133 * @since 5.8.0
134 *
135 * @param bool|array $allowed_block_types Array of block type slugs, or
136 * boolean to enable/disable all.
137 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
138 */
139 $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );
140 if ( ! empty( $block_editor_context->post ) ) {
141 $post = $block_editor_context->post;
142
143 /**
144 * Filters the allowed block types for the editor, defaulting to true (all
145 * block types supported).
146 *
147 * @since 5.0.0
148 * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance.
149 *
150 * @param bool|array $allowed_block_types Array of block type slugs, or
151 * boolean to enable/disable all.
152 * @param WP_Post $post The post resource data.
153 */
154 $allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' );
155 }
156
157 return $allowed_block_types;
158 }
159
160 /**
161 * Returns the default block editor settings.
162 *
163 * This is a temporary solution until the Gutenberg plugin sets
164 * the required WordPress version to 5.8.
165 *
166 * @see https://core.trac.wordpress.org/ticket/52920
167 *
168 * @since 10.5.0
169 *
170 * @return array The default block editor settings.
171 */
172 function gutenberg_get_default_block_editor_settings() {
173 // Media settings.
174 $max_upload_size = wp_max_upload_size();
175 if ( ! $max_upload_size ) {
176 $max_upload_size = 0;
177 }
178
179 /** This filter is documented in wp-admin/includes/media.php */
180 $image_size_names = apply_filters(
181 'image_size_names_choose',
182 array(
183 'thumbnail' => __( 'Thumbnail', 'gutenberg' ),
184 'medium' => __( 'Medium', 'gutenberg' ),
185 'large' => __( 'Large', 'gutenberg' ),
186 'full' => __( 'Full Size', 'gutenberg' ),
187 )
188 );
189
190 $available_image_sizes = array();
191 foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
192 $available_image_sizes[] = array(
193 'slug' => $image_size_slug,
194 'name' => $image_size_name,
195 );
196 }
197
198 $default_size = get_option( 'image_default_size', 'large' );
199 $image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $default_size : 'large';
200
201 $image_dimensions = array();
202 $all_sizes = wp_get_registered_image_subsizes();
203 foreach ( $available_image_sizes as $size ) {
204 $key = $size['slug'];
205 if ( isset( $all_sizes[ $key ] ) ) {
206 $image_dimensions[ $key ] = $all_sizes[ $key ];
207 }
208 }
209
210 $editor_settings = array(
211 '__unstableEnableFullSiteEditingBlocks' => gutenberg_supports_block_templates(),
212 'alignWide' => get_theme_support( 'align-wide' ),
213 'allowedBlockTypes' => true,
214 'allowedMimeTypes' => get_allowed_mime_types(),
215 'blockCategories' => gutenberg_get_default_block_categories(),
216 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
217 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
218 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
219 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
220 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ),
221 'enableCustomUnits' => get_theme_support( 'custom-units' ),
222 'isRTL' => is_rtl(),
223 'imageDefaultSize' => $image_default_size,
224 'imageDimensions' => $image_dimensions,
225 'imageEditing' => true,
226 'imageSizes' => $available_image_sizes,
227 'maxUploadFileSize' => $max_upload_size,
228 );
229
230 // Theme settings.
231 $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
232 if ( false !== $color_palette ) {
233 $editor_settings['colors'] = $color_palette;
234 }
235
236 $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
237 if ( false !== $font_sizes ) {
238 $editor_settings['fontSizes'] = $font_sizes;
239 }
240
241 $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
242 if ( false !== $gradient_presets ) {
243 $editor_settings['gradients'] = $gradient_presets;
244 }
245
246 return $editor_settings;
247 }
248
249 /**
250 * Returns the contextualized block editor settings settings for a selected editor type.
251 *
252 * This is a temporary solution until the Gutenberg plugin sets
253 * the required WordPress version to 5.8.
254 *
255 * @see https://core.trac.wordpress.org/ticket/52920
256 *
257 * @since 10.5.0
258 *
259 * @param array $custom_settings Custom settings to use with the given editor type.
260 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
261 *
262 * @return array The contextualized block editor settings.
263 */
264 function gutenberg_get_block_editor_settings( $custom_settings, $block_editor_context ) {
265 $editor_settings = array_merge(
266 gutenberg_get_default_block_editor_settings(),
267 array(
268 'allowedBlockTypes' => gutenberg_get_allowed_block_types( $block_editor_context ),
269 'blockCategories' => gutenberg_get_block_categories( $block_editor_context ),
270 ),
271 $custom_settings
272 );
273
274 /**
275 * Filters the settings to pass to the block editor for all editor type.
276 *
277 * @since 5.8.0
278 *
279 * @param array $editor_settings Default editor settings.
280 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
281 */
282 $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
283 if ( ! empty( $block_editor_context->post ) ) {
284 $post = $block_editor_context->post;
285
286 /**
287 * Filters the settings to pass to the block editor.
288 *
289 * @since 5.0.0
290 * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance.
291 *
292 * @param array $editor_settings Default editor settings.
293 * @param WP_Post $post Post being edited.
294 */
295 $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' );
296 }
297
298 return $editor_settings;
299 }
300
301 /**
302 * Preloads common data used with the block editor by specifying an array of
303 * REST API paths that will be preloaded for a given block editor context.
304 *
305 * @see https://core.trac.wordpress.org/ticket/52920
306 *
307 * @since 10.7.0
308 *
309 * @global WP_Post $post Global post object.
310 *
311 * @param array $preload_paths List of paths to preload.
312 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
313 *
314 * @return void
315 */
316 function gutenberg_block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
317 global $post;
318
319 /**
320 * Filters the array of REST API paths that will be used to preloaded common data
321 * to use with the block editor.
322 *
323 * @since 5.8.0
324 *
325 * @param string[] $preload_paths Array of paths to preload.
326 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
327 */
328 $preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context );
329 if ( ! empty( $block_editor_context->post ) ) {
330 $selected_post = $block_editor_context->post;
331
332 /**
333 * Preload common data by specifying an array of REST API paths that will be preloaded.
334 *
335 * Filters the array of paths that will be preloaded.
336 *
337 * @since 5.0.0
338 * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance.
339 *
340 * @param string[] $preload_paths Array of paths to preload.
341 * @param WP_Post $selected_post Post being edited.
342 */
343 $preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' );
344 }
345
346 if ( empty( $preload_paths ) ) {
347 return;
348 }
349
350 /*
351 * Ensure the global $post remains the same after API data is preloaded.
352 * Because API preloading can call the_content and other filters, plugins
353 * can unexpectedly modify $post.
354 */
355 $backup_global_post = ! empty( $post ) ? clone $post : $post;
356
357 $preload_data = array_reduce(
358 $preload_paths,
359 'rest_preload_api_request',
360 array()
361 );
362
363 // Restore the global $post as it was before API preloading.
364 $post = $backup_global_post;
365
366 wp_add_inline_script(
367 'wp-api-fetch',
368 sprintf(
369 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
370 wp_json_encode( $preload_data )
371 ),
372 'after'
373 );
374 }
375
376 /**
377 * Filters the arguments for registering a block type.
378 *
379 * @todo Remove from the Gutenberg plugin when WordPress 5.8 is the minimum required version.
380 *
381 * @param array $args Array of arguments for registering a block type.
382 *
383 * @return array Returns the $metadata with any missing `style` and `editorStyle` added.
384 */
385 function gutenberg_add_missing_styles_to_core_block_json( $args ) {
386 if ( ! empty( $args['name'] ) && 0 === strpos( $args['name'], 'core/' ) ) {
387 $block_name = str_replace( 'core/', '', $args['name'] );
388
389 if ( ! isset( $args['style'] ) ) {
390 $args['style'] = "wp-block-$block_name";
391 }
392 if ( ! isset( $args['editor_style'] ) ) {
393 $args['editor_style'] = "wp-block-$block_name-editor";
394 }
395 }
396 return $args;
397 }
398 add_filter( 'register_block_type_args', 'gutenberg_add_missing_styles_to_core_block_json' );
399