18
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Block Editor API. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Editor |
|
7 |
* @since 5.8.0 |
|
8 |
*/ |
|
9 |
|
|
10 |
/** |
|
11 |
* Returns the list of default categories for block types. |
|
12 |
* |
|
13 |
* @since 5.8.0 |
|
14 |
* |
|
15 |
* @return array[] Array of categories for block types. |
|
16 |
*/ |
|
17 |
function get_default_block_categories() { |
|
18 |
return array( |
|
19 |
array( |
|
20 |
'slug' => 'text', |
|
21 |
'title' => _x( 'Text', 'block category' ), |
|
22 |
'icon' => null, |
|
23 |
), |
|
24 |
array( |
|
25 |
'slug' => 'media', |
|
26 |
'title' => _x( 'Media', 'block category' ), |
|
27 |
'icon' => null, |
|
28 |
), |
|
29 |
array( |
|
30 |
'slug' => 'design', |
|
31 |
'title' => _x( 'Design', 'block category' ), |
|
32 |
'icon' => null, |
|
33 |
), |
|
34 |
array( |
|
35 |
'slug' => 'widgets', |
|
36 |
'title' => _x( 'Widgets', 'block category' ), |
|
37 |
'icon' => null, |
|
38 |
), |
|
39 |
array( |
|
40 |
'slug' => 'theme', |
|
41 |
'title' => _x( 'Theme', 'block category' ), |
|
42 |
'icon' => null, |
|
43 |
), |
|
44 |
array( |
|
45 |
'slug' => 'embed', |
|
46 |
'title' => _x( 'Embeds', 'block category' ), |
|
47 |
'icon' => null, |
|
48 |
), |
|
49 |
array( |
|
50 |
'slug' => 'reusable', |
|
51 |
'title' => _x( 'Reusable Blocks', 'block category' ), |
|
52 |
'icon' => null, |
|
53 |
), |
|
54 |
); |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* Returns all the categories for block types that will be shown in the block editor. |
|
59 |
* |
|
60 |
* @since 5.0.0 |
|
61 |
* @since 5.8.0 It is possible to pass the block editor context as param. |
|
62 |
* |
|
63 |
* @param WP_Post|WP_Block_Editor_Context $post_or_block_editor_context The current post object or |
|
64 |
* the block editor context. |
|
65 |
* |
|
66 |
* @return array[] Array of categories for block types. |
|
67 |
*/ |
|
68 |
function get_block_categories( $post_or_block_editor_context ) { |
|
69 |
$block_categories = get_default_block_categories(); |
|
70 |
$block_editor_context = $post_or_block_editor_context instanceof WP_Post ? |
|
71 |
new WP_Block_Editor_Context( |
|
72 |
array( |
|
73 |
'post' => $post_or_block_editor_context, |
|
74 |
) |
|
75 |
) : $post_or_block_editor_context; |
|
76 |
|
|
77 |
/** |
|
78 |
* Filters the default array of categories for block types. |
|
79 |
* |
|
80 |
* @since 5.8.0 |
|
81 |
* |
|
82 |
* @param array[] $block_categories Array of categories for block types. |
|
83 |
* @param WP_Block_Editor_Context $block_editor_context The current block editor context. |
|
84 |
*/ |
|
85 |
$block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context ); |
19
|
86 |
|
18
|
87 |
if ( ! empty( $block_editor_context->post ) ) { |
|
88 |
$post = $block_editor_context->post; |
|
89 |
|
|
90 |
/** |
|
91 |
* Filters the default array of categories for block types. |
|
92 |
* |
|
93 |
* @since 5.0.0 |
|
94 |
* @deprecated 5.8.0 Use the {@see 'block_categories_all'} filter instead. |
|
95 |
* |
|
96 |
* @param array[] $block_categories Array of categories for block types. |
|
97 |
* @param WP_Post $post Post being loaded. |
|
98 |
*/ |
|
99 |
$block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' ); |
|
100 |
} |
|
101 |
|
|
102 |
return $block_categories; |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* Gets the list of allowed block types to use in the block editor. |
|
107 |
* |
|
108 |
* @since 5.8.0 |
|
109 |
* |
|
110 |
* @param WP_Block_Editor_Context $block_editor_context The current block editor context. |
|
111 |
* |
19
|
112 |
* @return bool|string[] Array of block type slugs, or boolean to enable/disable all. |
18
|
113 |
*/ |
|
114 |
function get_allowed_block_types( $block_editor_context ) { |
|
115 |
$allowed_block_types = true; |
|
116 |
|
|
117 |
/** |
|
118 |
* Filters the allowed block types for all editor types. |
|
119 |
* |
|
120 |
* @since 5.8.0 |
|
121 |
* |
19
|
122 |
* @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all. |
18
|
123 |
* Default true (all registered block types supported). |
|
124 |
* @param WP_Block_Editor_Context $block_editor_context The current block editor context. |
|
125 |
*/ |
|
126 |
$allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context ); |
19
|
127 |
|
18
|
128 |
if ( ! empty( $block_editor_context->post ) ) { |
|
129 |
$post = $block_editor_context->post; |
|
130 |
|
|
131 |
/** |
|
132 |
* Filters the allowed block types for the editor. |
|
133 |
* |
|
134 |
* @since 5.0.0 |
|
135 |
* @deprecated 5.8.0 Use the {@see 'allowed_block_types_all'} filter instead. |
|
136 |
* |
19
|
137 |
* @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all. |
|
138 |
* Default true (all registered block types supported) |
|
139 |
* @param WP_Post $post The post resource data. |
18
|
140 |
*/ |
|
141 |
$allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' ); |
|
142 |
} |
|
143 |
|
|
144 |
return $allowed_block_types; |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* Returns the default block editor settings. |
|
149 |
* |
|
150 |
* @since 5.8.0 |
|
151 |
* |
|
152 |
* @return array The default block editor settings. |
|
153 |
*/ |
|
154 |
function get_default_block_editor_settings() { |
|
155 |
// Media settings. |
|
156 |
$max_upload_size = wp_max_upload_size(); |
|
157 |
if ( ! $max_upload_size ) { |
|
158 |
$max_upload_size = 0; |
|
159 |
} |
|
160 |
|
|
161 |
/** This filter is documented in wp-admin/includes/media.php */ |
|
162 |
$image_size_names = apply_filters( |
|
163 |
'image_size_names_choose', |
|
164 |
array( |
|
165 |
'thumbnail' => __( 'Thumbnail' ), |
|
166 |
'medium' => __( 'Medium' ), |
|
167 |
'large' => __( 'Large' ), |
|
168 |
'full' => __( 'Full Size' ), |
|
169 |
) |
|
170 |
); |
|
171 |
|
|
172 |
$available_image_sizes = array(); |
|
173 |
foreach ( $image_size_names as $image_size_slug => $image_size_name ) { |
|
174 |
$available_image_sizes[] = array( |
|
175 |
'slug' => $image_size_slug, |
|
176 |
'name' => $image_size_name, |
|
177 |
); |
|
178 |
} |
|
179 |
|
|
180 |
$default_size = get_option( 'image_default_size', 'large' ); |
|
181 |
$image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $default_size : 'large'; |
|
182 |
|
|
183 |
$image_dimensions = array(); |
|
184 |
$all_sizes = wp_get_registered_image_subsizes(); |
|
185 |
foreach ( $available_image_sizes as $size ) { |
|
186 |
$key = $size['slug']; |
|
187 |
if ( isset( $all_sizes[ $key ] ) ) { |
|
188 |
$image_dimensions[ $key ] = $all_sizes[ $key ]; |
|
189 |
} |
|
190 |
} |
|
191 |
|
19
|
192 |
// These styles are used if the "no theme styles" options is triggered or on |
|
193 |
// themes without their own editor styles. |
|
194 |
$default_editor_styles_file = ABSPATH . WPINC . '/css/dist/block-editor/default-editor-styles.css'; |
|
195 |
if ( file_exists( $default_editor_styles_file ) ) { |
|
196 |
$default_editor_styles = array( |
|
197 |
array( 'css' => file_get_contents( $default_editor_styles_file ) ), |
|
198 |
); |
|
199 |
} else { |
|
200 |
$default_editor_styles = array(); |
|
201 |
} |
|
202 |
|
18
|
203 |
$editor_settings = array( |
19
|
204 |
'alignWide' => get_theme_support( 'align-wide' ), |
|
205 |
'allowedBlockTypes' => true, |
|
206 |
'allowedMimeTypes' => get_allowed_mime_types(), |
|
207 |
'defaultEditorStyles' => $default_editor_styles, |
|
208 |
'blockCategories' => get_default_block_categories(), |
|
209 |
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), |
|
210 |
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), |
|
211 |
'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), |
|
212 |
'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), |
|
213 |
'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), |
|
214 |
'enableCustomUnits' => get_theme_support( 'custom-units' ), |
|
215 |
'isRTL' => is_rtl(), |
|
216 |
'imageDefaultSize' => $image_default_size, |
|
217 |
'imageDimensions' => $image_dimensions, |
|
218 |
'imageEditing' => true, |
|
219 |
'imageSizes' => $available_image_sizes, |
|
220 |
'maxUploadFileSize' => $max_upload_size, |
|
221 |
// The following flag is required to enable the new Gallery block format on the mobile apps in 5.9. |
|
222 |
'__unstableGalleryWithImageBlocks' => true, |
18
|
223 |
); |
|
224 |
|
|
225 |
// Theme settings. |
|
226 |
$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); |
|
227 |
if ( false !== $color_palette ) { |
|
228 |
$editor_settings['colors'] = $color_palette; |
|
229 |
} |
|
230 |
|
|
231 |
$font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); |
|
232 |
if ( false !== $font_sizes ) { |
|
233 |
$editor_settings['fontSizes'] = $font_sizes; |
|
234 |
} |
|
235 |
|
|
236 |
$gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); |
|
237 |
if ( false !== $gradient_presets ) { |
|
238 |
$editor_settings['gradients'] = $gradient_presets; |
|
239 |
} |
|
240 |
|
|
241 |
return $editor_settings; |
|
242 |
} |
|
243 |
|
|
244 |
/** |
|
245 |
* Returns the block editor settings needed to use the Legacy Widget block which |
|
246 |
* is not registered by default. |
|
247 |
* |
|
248 |
* @since 5.8.0 |
|
249 |
* |
|
250 |
* @return array Settings to be used with get_block_editor_settings(). |
|
251 |
*/ |
|
252 |
function get_legacy_widget_block_editor_settings() { |
|
253 |
$editor_settings = array(); |
|
254 |
|
|
255 |
/** |
|
256 |
* Filters the list of widget-type IDs that should **not** be offered by the |
|
257 |
* Legacy Widget block. |
|
258 |
* |
|
259 |
* Returning an empty array will make all widgets available. |
|
260 |
* |
|
261 |
* @since 5.8.0 |
|
262 |
* |
19
|
263 |
* @param string[] $widgets An array of excluded widget-type IDs. |
18
|
264 |
*/ |
|
265 |
$editor_settings['widgetTypesToHideFromLegacyWidgetBlock'] = apply_filters( |
|
266 |
'widget_types_to_hide_from_legacy_widget_block', |
|
267 |
array( |
|
268 |
'pages', |
|
269 |
'calendar', |
|
270 |
'archives', |
|
271 |
'media_audio', |
|
272 |
'media_image', |
|
273 |
'media_gallery', |
|
274 |
'media_video', |
|
275 |
'search', |
|
276 |
'text', |
|
277 |
'categories', |
|
278 |
'recent-posts', |
|
279 |
'recent-comments', |
|
280 |
'rss', |
|
281 |
'tag_cloud', |
|
282 |
'custom_html', |
|
283 |
'block', |
|
284 |
) |
|
285 |
); |
|
286 |
|
|
287 |
return $editor_settings; |
|
288 |
} |
|
289 |
|
|
290 |
/** |
19
|
291 |
* Collect the block editor assets that need to be loaded into the editor's iframe. |
|
292 |
* |
|
293 |
* @since 6.0.0 |
|
294 |
* @access private |
|
295 |
* |
|
296 |
* @global string $pagenow The filename of the current screen. |
|
297 |
* |
|
298 |
* @return array { |
|
299 |
* The block editor assets. |
|
300 |
* |
|
301 |
* @type string|false $styles String containing the HTML for styles. |
|
302 |
* @type string|false $scripts String containing the HTML for scripts. |
|
303 |
* } |
|
304 |
*/ |
|
305 |
function _wp_get_iframed_editor_assets() { |
|
306 |
global $pagenow; |
|
307 |
|
|
308 |
$script_handles = array(); |
|
309 |
$style_handles = array( |
|
310 |
'wp-block-editor', |
|
311 |
'wp-block-library', |
|
312 |
'wp-edit-blocks', |
|
313 |
); |
|
314 |
|
|
315 |
if ( current_theme_supports( 'wp-block-styles' ) ) { |
|
316 |
$style_handles[] = 'wp-block-library-theme'; |
|
317 |
} |
|
318 |
|
|
319 |
if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) { |
|
320 |
$style_handles[] = 'wp-widgets'; |
|
321 |
$style_handles[] = 'wp-edit-widgets'; |
|
322 |
} |
|
323 |
|
|
324 |
$block_registry = WP_Block_Type_Registry::get_instance(); |
|
325 |
|
|
326 |
foreach ( $block_registry->get_all_registered() as $block_type ) { |
|
327 |
if ( ! empty( $block_type->style ) ) { |
|
328 |
$style_handles[] = $block_type->style; |
|
329 |
} |
|
330 |
|
|
331 |
if ( ! empty( $block_type->editor_style ) ) { |
|
332 |
$style_handles[] = $block_type->editor_style; |
|
333 |
} |
|
334 |
|
|
335 |
if ( ! empty( $block_type->script ) ) { |
|
336 |
$script_handles[] = $block_type->script; |
|
337 |
} |
|
338 |
} |
|
339 |
|
|
340 |
$style_handles = array_unique( $style_handles ); |
|
341 |
$done = wp_styles()->done; |
|
342 |
|
|
343 |
ob_start(); |
|
344 |
|
|
345 |
// We do not need reset styles for the iframed editor. |
|
346 |
wp_styles()->done = array( 'wp-reset-editor-styles' ); |
|
347 |
wp_styles()->do_items( $style_handles ); |
|
348 |
wp_styles()->done = $done; |
|
349 |
|
|
350 |
$styles = ob_get_clean(); |
|
351 |
|
|
352 |
$script_handles = array_unique( $script_handles ); |
|
353 |
$done = wp_scripts()->done; |
|
354 |
|
|
355 |
ob_start(); |
|
356 |
|
|
357 |
wp_scripts()->done = array(); |
|
358 |
wp_scripts()->do_items( $script_handles ); |
|
359 |
wp_scripts()->done = $done; |
|
360 |
|
|
361 |
$scripts = ob_get_clean(); |
|
362 |
|
|
363 |
return array( |
|
364 |
'styles' => $styles, |
|
365 |
'scripts' => $scripts, |
|
366 |
); |
|
367 |
} |
|
368 |
|
|
369 |
/** |
18
|
370 |
* Returns the contextualized block editor settings for a selected editor context. |
|
371 |
* |
|
372 |
* @since 5.8.0 |
|
373 |
* |
|
374 |
* @param array $custom_settings Custom settings to use with the given editor type. |
|
375 |
* @param WP_Block_Editor_Context $block_editor_context The current block editor context. |
|
376 |
* |
|
377 |
* @return array The contextualized block editor settings. |
|
378 |
*/ |
|
379 |
function get_block_editor_settings( array $custom_settings, $block_editor_context ) { |
|
380 |
$editor_settings = array_merge( |
|
381 |
get_default_block_editor_settings(), |
|
382 |
array( |
|
383 |
'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ), |
|
384 |
'blockCategories' => get_block_categories( $block_editor_context ), |
|
385 |
), |
|
386 |
$custom_settings |
|
387 |
); |
|
388 |
|
19
|
389 |
$global_styles = array(); |
|
390 |
$presets = array( |
|
391 |
array( |
|
392 |
'css' => 'variables', |
|
393 |
'__unstableType' => 'presets', |
|
394 |
'isGlobalStyles' => true, |
|
395 |
), |
|
396 |
array( |
|
397 |
'css' => 'presets', |
|
398 |
'__unstableType' => 'presets', |
|
399 |
'isGlobalStyles' => true, |
|
400 |
), |
|
401 |
); |
|
402 |
foreach ( $presets as $preset_style ) { |
|
403 |
$actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) ); |
|
404 |
if ( '' !== $actual_css ) { |
|
405 |
$preset_style['css'] = $actual_css; |
|
406 |
$global_styles[] = $preset_style; |
|
407 |
} |
|
408 |
} |
18
|
409 |
|
|
410 |
if ( WP_Theme_JSON_Resolver::theme_has_support() ) { |
19
|
411 |
$block_classes = array( |
|
412 |
'css' => 'styles', |
|
413 |
'__unstableType' => 'theme', |
|
414 |
'isGlobalStyles' => true, |
18
|
415 |
); |
19
|
416 |
$actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); |
|
417 |
if ( '' !== $actual_css ) { |
|
418 |
$block_classes['css'] = $actual_css; |
|
419 |
$global_styles[] = $block_classes; |
|
420 |
} |
18
|
421 |
} |
|
422 |
|
19
|
423 |
$editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() ); |
|
424 |
|
|
425 |
$editor_settings['__experimentalFeatures'] = wp_get_global_settings(); |
18
|
426 |
// These settings may need to be updated based on data coming from theme.json sources. |
|
427 |
if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) { |
|
428 |
$colors_by_origin = $editor_settings['__experimentalFeatures']['color']['palette']; |
19
|
429 |
$editor_settings['colors'] = isset( $colors_by_origin['custom'] ) ? |
|
430 |
$colors_by_origin['custom'] : ( |
18
|
431 |
isset( $colors_by_origin['theme'] ) ? |
|
432 |
$colors_by_origin['theme'] : |
19
|
433 |
$colors_by_origin['default'] |
18
|
434 |
); |
|
435 |
} |
|
436 |
if ( isset( $editor_settings['__experimentalFeatures']['color']['gradients'] ) ) { |
|
437 |
$gradients_by_origin = $editor_settings['__experimentalFeatures']['color']['gradients']; |
19
|
438 |
$editor_settings['gradients'] = isset( $gradients_by_origin['custom'] ) ? |
|
439 |
$gradients_by_origin['custom'] : ( |
18
|
440 |
isset( $gradients_by_origin['theme'] ) ? |
|
441 |
$gradients_by_origin['theme'] : |
19
|
442 |
$gradients_by_origin['default'] |
18
|
443 |
); |
|
444 |
} |
|
445 |
if ( isset( $editor_settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { |
|
446 |
$font_sizes_by_origin = $editor_settings['__experimentalFeatures']['typography']['fontSizes']; |
19
|
447 |
$editor_settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ? |
|
448 |
$font_sizes_by_origin['custom'] : ( |
18
|
449 |
isset( $font_sizes_by_origin['theme'] ) ? |
|
450 |
$font_sizes_by_origin['theme'] : |
19
|
451 |
$font_sizes_by_origin['default'] |
18
|
452 |
); |
|
453 |
} |
|
454 |
if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) { |
|
455 |
$editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom']; |
|
456 |
unset( $editor_settings['__experimentalFeatures']['color']['custom'] ); |
|
457 |
} |
|
458 |
if ( isset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ) ) { |
|
459 |
$editor_settings['disableCustomGradients'] = ! $editor_settings['__experimentalFeatures']['color']['customGradient']; |
|
460 |
unset( $editor_settings['__experimentalFeatures']['color']['customGradient'] ); |
|
461 |
} |
|
462 |
if ( isset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ) ) { |
|
463 |
$editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize']; |
|
464 |
unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ); |
|
465 |
} |
19
|
466 |
if ( isset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ) ) { |
|
467 |
$editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['lineHeight']; |
|
468 |
unset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ); |
18
|
469 |
} |
|
470 |
if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) { |
|
471 |
$editor_settings['enableCustomUnits'] = $editor_settings['__experimentalFeatures']['spacing']['units']; |
|
472 |
unset( $editor_settings['__experimentalFeatures']['spacing']['units'] ); |
|
473 |
} |
19
|
474 |
if ( isset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ) ) { |
|
475 |
$editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['padding']; |
|
476 |
unset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ); |
18
|
477 |
} |
|
478 |
|
19
|
479 |
$editor_settings['__unstableResolvedAssets'] = _wp_get_iframed_editor_assets(); |
|
480 |
$editor_settings['localAutosaveInterval'] = 15; |
|
481 |
$editor_settings['__experimentalDiscussionSettings'] = array( |
|
482 |
'commentOrder' => get_option( 'comment_order' ), |
|
483 |
'commentsPerPage' => get_option( 'comments_per_page' ), |
|
484 |
'defaultCommentsPage' => get_option( 'default_comments_page' ), |
|
485 |
'pageComments' => get_option( 'page_comments' ), |
|
486 |
'threadComments' => get_option( 'thread_comments' ), |
|
487 |
'threadCommentsDepth' => get_option( 'thread_comments_depth' ), |
|
488 |
'defaultCommentStatus' => get_option( 'default_comment_status' ), |
|
489 |
'avatarURL' => get_avatar_url( |
|
490 |
'', |
|
491 |
array( |
|
492 |
'size' => 96, |
|
493 |
'force_default' => true, |
|
494 |
'default' => get_option( 'avatar_default' ), |
|
495 |
) |
|
496 |
), |
|
497 |
); |
|
498 |
|
18
|
499 |
/** |
|
500 |
* Filters the settings to pass to the block editor for all editor type. |
|
501 |
* |
|
502 |
* @since 5.8.0 |
|
503 |
* |
|
504 |
* @param array $editor_settings Default editor settings. |
|
505 |
* @param WP_Block_Editor_Context $block_editor_context The current block editor context. |
|
506 |
*/ |
|
507 |
$editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context ); |
19
|
508 |
|
18
|
509 |
if ( ! empty( $block_editor_context->post ) ) { |
|
510 |
$post = $block_editor_context->post; |
|
511 |
|
|
512 |
/** |
|
513 |
* Filters the settings to pass to the block editor. |
|
514 |
* |
|
515 |
* @since 5.0.0 |
|
516 |
* @deprecated 5.8.0 Use the {@see 'block_editor_settings_all'} filter instead. |
|
517 |
* |
|
518 |
* @param array $editor_settings Default editor settings. |
|
519 |
* @param WP_Post $post Post being edited. |
|
520 |
*/ |
|
521 |
$editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' ); |
|
522 |
} |
|
523 |
|
|
524 |
return $editor_settings; |
|
525 |
} |
|
526 |
|
|
527 |
/** |
|
528 |
* Preloads common data used with the block editor by specifying an array of |
|
529 |
* REST API paths that will be preloaded for a given block editor context. |
|
530 |
* |
|
531 |
* @since 5.8.0 |
|
532 |
* |
19
|
533 |
* @global WP_Post $post Global post object. |
|
534 |
* @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
|
535 |
* @global WP_Styles $wp_styles The WP_Styles object for printing styles. |
18
|
536 |
* |
19
|
537 |
* @param string[] $preload_paths List of paths to preload. |
18
|
538 |
* @param WP_Block_Editor_Context $block_editor_context The current block editor context. |
|
539 |
*/ |
|
540 |
function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) { |
19
|
541 |
global $post, $wp_scripts, $wp_styles; |
18
|
542 |
|
|
543 |
/** |
|
544 |
* Filters the array of REST API paths that will be used to preloaded common data for the block editor. |
|
545 |
* |
|
546 |
* @since 5.8.0 |
|
547 |
* |
19
|
548 |
* @param string[] $preload_paths Array of paths to preload. |
|
549 |
* @param WP_Block_Editor_Context $block_editor_context The current block editor context. |
18
|
550 |
*/ |
|
551 |
$preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context ); |
19
|
552 |
|
18
|
553 |
if ( ! empty( $block_editor_context->post ) ) { |
|
554 |
$selected_post = $block_editor_context->post; |
|
555 |
|
|
556 |
/** |
|
557 |
* Filters the array of paths that will be preloaded. |
|
558 |
* |
|
559 |
* Preload common data by specifying an array of REST API paths that will be preloaded. |
|
560 |
* |
|
561 |
* @since 5.0.0 |
|
562 |
* @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead. |
|
563 |
* |
|
564 |
* @param string[] $preload_paths Array of paths to preload. |
|
565 |
* @param WP_Post $selected_post Post being edited. |
|
566 |
*/ |
|
567 |
$preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' ); |
|
568 |
} |
|
569 |
|
|
570 |
if ( empty( $preload_paths ) ) { |
|
571 |
return; |
|
572 |
} |
|
573 |
|
|
574 |
/* |
19
|
575 |
* Ensure the global $post, $wp_scripts, and $wp_styles remain the same after |
|
576 |
* API data is preloaded. |
18
|
577 |
* Because API preloading can call the_content and other filters, plugins |
19
|
578 |
* can unexpectedly modify the global $post or enqueue assets which are not |
|
579 |
* intended for the block editor. |
18
|
580 |
*/ |
|
581 |
$backup_global_post = ! empty( $post ) ? clone $post : $post; |
19
|
582 |
$backup_wp_scripts = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts; |
|
583 |
$backup_wp_styles = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles; |
|
584 |
|
|
585 |
foreach ( $preload_paths as &$path ) { |
|
586 |
if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) { |
|
587 |
$path = '/' . $path; |
|
588 |
continue; |
|
589 |
} |
|
590 |
|
|
591 |
if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) { |
|
592 |
$path[0] = '/' . $path[0]; |
|
593 |
} |
|
594 |
} |
|
595 |
|
|
596 |
unset( $path ); |
18
|
597 |
|
|
598 |
$preload_data = array_reduce( |
|
599 |
$preload_paths, |
|
600 |
'rest_preload_api_request', |
|
601 |
array() |
|
602 |
); |
|
603 |
|
19
|
604 |
// Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading. |
|
605 |
$post = $backup_global_post; |
|
606 |
$wp_scripts = $backup_wp_scripts; |
|
607 |
$wp_styles = $backup_wp_styles; |
18
|
608 |
|
|
609 |
wp_add_inline_script( |
|
610 |
'wp-api-fetch', |
|
611 |
sprintf( |
|
612 |
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
|
613 |
wp_json_encode( $preload_data ) |
|
614 |
), |
|
615 |
'after' |
|
616 |
); |
|
617 |
} |
|
618 |
|
|
619 |
/** |
|
620 |
* Creates an array of theme styles to load into the block editor. |
|
621 |
* |
|
622 |
* @since 5.8.0 |
|
623 |
* |
|
624 |
* @global array $editor_styles |
|
625 |
* |
19
|
626 |
* @return array An array of theme styles for the block editor. |
18
|
627 |
*/ |
|
628 |
function get_block_editor_theme_styles() { |
|
629 |
global $editor_styles; |
|
630 |
|
19
|
631 |
$styles = array(); |
18
|
632 |
|
|
633 |
if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) { |
|
634 |
foreach ( $editor_styles as $style ) { |
|
635 |
if ( preg_match( '~^(https?:)?//~', $style ) ) { |
|
636 |
$response = wp_remote_get( $style ); |
|
637 |
if ( ! is_wp_error( $response ) ) { |
|
638 |
$styles[] = array( |
|
639 |
'css' => wp_remote_retrieve_body( $response ), |
|
640 |
'__unstableType' => 'theme', |
19
|
641 |
'isGlobalStyles' => false, |
18
|
642 |
); |
|
643 |
} |
|
644 |
} else { |
|
645 |
$file = get_theme_file_path( $style ); |
|
646 |
if ( is_file( $file ) ) { |
|
647 |
$styles[] = array( |
|
648 |
'css' => file_get_contents( $file ), |
|
649 |
'baseURL' => get_theme_file_uri( $style ), |
|
650 |
'__unstableType' => 'theme', |
19
|
651 |
'isGlobalStyles' => false, |
18
|
652 |
); |
|
653 |
} |
|
654 |
} |
|
655 |
} |
|
656 |
} |
|
657 |
|
|
658 |
return $styles; |
|
659 |
} |