PluginProbe
Gutenberg / 18.1.2
Gutenberg v18.1.2
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 / blocks.php

blocks.php in Gutenberg 18.1.2, at lib/blocks.php

511 lines 17.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block functions specific for the Gutenberg editor plugin.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Substitutes the implementation of a core-registered block type, if exists,
10 * with the built result from the plugin.
11 */
12 function gutenberg_reregister_core_block_types() {
13 // Blocks directory may not exist if working from a fresh clone.
14 $blocks_dirs = array(
15 __DIR__ . '/../build/block-library/blocks/' => array(
16 'block_folders' => array(
17 'audio',
18 'button',
19 'buttons',
20 'freeform',
21 'code',
22 'column',
23 'columns',
24 'details',
25 'form-input',
26 'form-submit-button',
27 'group',
28 'html',
29 'list',
30 'list-item',
31 'missing',
32 'more',
33 'nextpage',
34 'paragraph',
35 'preformatted',
36 'pullquote',
37 'quote',
38 'separator',
39 'social-links',
40 'spacer',
41 'table',
42 'table-of-contents',
43 'text-columns',
44 'verse',
45 'video',
46 'embed',
47 ),
48 'block_names' => array(
49 'archives.php' => 'core/archives',
50 'avatar.php' => 'core/avatar',
51 'block.php' => 'core/block',
52 'calendar.php' => 'core/calendar',
53 'categories.php' => 'core/categories',
54 'cover.php' => 'core/cover',
55 'comment-author-avatar.php' => 'core/comment-author-avatar',
56 'comment-author-name.php' => 'core/comment-author-name',
57 'comment-content.php' => 'core/comment-content',
58 'comment-date.php' => 'core/comment-date',
59 'comment-edit-link.php' => 'core/comment-edit-link',
60 'comment-reply-link.php' => 'core/comment-reply-link',
61 'comment-template.php' => 'core/comment-template',
62 'comments-pagination.php' => 'core/comments-pagination',
63 'comments-pagination-next.php' => 'core/comments-pagination-next',
64 'comments-pagination-numbers.php' => 'core/comments-pagination-numbers',
65 'comments-pagination-previous.php' => 'core/comments-pagination-previous',
66 'comments-title.php' => 'core/comments-title',
67 'comments.php' => 'core/comments',
68 'footnotes.php' => 'core/footnotes',
69 'file.php' => 'core/file',
70 'form.php' => 'core/form',
71 'form-input.php' => 'core/form-input',
72 'form-submission-notification.php' => 'core/form-submission-notification',
73 'home-link.php' => 'core/home-link',
74 'image.php' => 'core/image',
75 'gallery.php' => 'core/gallery',
76 'heading.php' => 'core/heading',
77 'latest-comments.php' => 'core/latest-comments',
78 'latest-posts.php' => 'core/latest-posts',
79 'loginout.php' => 'core/loginout',
80 'media-text.php' => 'core/media-text',
81 'navigation.php' => 'core/navigation',
82 'navigation-link.php' => 'core/navigation-link',
83 'navigation-submenu.php' => 'core/navigation-submenu',
84 'page-list.php' => 'core/page-list',
85 'page-list-item.php' => 'core/page-list-item',
86 'pattern.php' => 'core/pattern',
87 'post-author.php' => 'core/post-author',
88 'post-author-name.php' => 'core/post-author-name',
89 'post-author-biography.php' => 'core/post-author-biography',
90 'post-comment.php' => 'core/post-comment',
91 'post-comments-count.php' => 'core/post-comments-count',
92 'post-comments-form.php' => 'core/post-comments-form',
93 'post-comments-link.php' => 'core/post-comments-link',
94 'post-content.php' => 'core/post-content',
95 'post-date.php' => 'core/post-date',
96 'post-excerpt.php' => 'core/post-excerpt',
97 'post-featured-image.php' => 'core/post-featured-image',
98 'post-navigation-link.php' => 'core/post-navigation-link',
99 'post-terms.php' => 'core/post-terms',
100 'post-time-to-read.php' => 'core/post-time-to-read',
101 'post-title.php' => 'core/post-title',
102 'query.php' => 'core/query',
103 'post-template.php' => 'core/post-template',
104 'query-no-results.php' => 'core/query-no-results',
105 'query-pagination.php' => 'core/query-pagination',
106 'query-pagination-next.php' => 'core/query-pagination-next',
107 'query-pagination-numbers.php' => 'core/query-pagination-numbers',
108 'query-pagination-previous.php' => 'core/query-pagination-previous',
109 'query-title.php' => 'core/query-title',
110 'read-more.php' => 'core/read-more',
111 'rss.php' => 'core/rss',
112 'search.php' => 'core/search',
113 'shortcode.php' => 'core/shortcode',
114 'social-link.php' => 'core/social-link',
115 'site-logo.php' => 'core/site-logo',
116 'site-tagline.php' => 'core/site-tagline',
117 'site-title.php' => 'core/site-title',
118 'tag-cloud.php' => 'core/tag-cloud',
119 'template-part.php' => 'core/template-part',
120 'term-description.php' => 'core/term-description',
121 ),
122 ),
123 __DIR__ . '/../build/edit-widgets/blocks/' => array(
124 'block_folders' => array(
125 'widget-area',
126 ),
127 'block_names' => array(),
128 ),
129 __DIR__ . '/../build/widgets/blocks/' => array(
130 'block_folders' => array(
131 'legacy-widget',
132 'widget-group',
133 ),
134 'block_names' => array(
135 'legacy-widget.php' => 'core/legacy-widget',
136 'widget-group.php' => 'core/widget-group',
137 ),
138 ),
139 );
140 foreach ( $blocks_dirs as $blocks_dir => $details ) {
141 $block_folders = $details['block_folders'];
142 $block_names = $details['block_names'];
143
144 foreach ( $block_folders as $folder_name ) {
145 $block_json_file = $blocks_dir . $folder_name . '/block.json';
146
147 // Ideally, all paths to block metadata files should be listed in
148 // WordPress core. In this place we should rather use filter
149 // to replace paths with overrides defined by the plugin.
150 $metadata = json_decode( file_get_contents( $block_json_file ), true );
151 if ( ! is_array( $metadata ) || ! $metadata['name'] ) {
152 continue;
153 }
154
155 gutenberg_deregister_core_block_and_assets( $metadata['name'] );
156 gutenberg_register_core_block_assets( $folder_name );
157 register_block_type_from_metadata( $block_json_file );
158 }
159
160 foreach ( $block_names as $file => $sub_block_names ) {
161 if ( ! file_exists( $blocks_dir . $file ) ) {
162 continue;
163 }
164
165 $sub_block_names_normalized = is_string( $sub_block_names ) ? array( $sub_block_names ) : $sub_block_names;
166 foreach ( $sub_block_names_normalized as $block_name ) {
167 gutenberg_deregister_core_block_and_assets( $block_name );
168 gutenberg_register_core_block_assets( $block_name );
169 }
170
171 require_once $blocks_dir . $file;
172 }
173 }
174 }
175
176 add_action( 'init', 'gutenberg_reregister_core_block_types' );
177
178 /**
179 * Adds the defer loading strategy to all registered blocks.
180 *
181 * This function would not be part of core merge. Instead, the register_block_script_handle() function would be patched
182 * as follows.
183 *
184 * ```
185 * --- a/wp-includes/blocks.php
186 * +++ b/wp-includes/blocks.php
187 * @ @ -153,7 +153,8 @ @ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
188 * $script_handle,
189 * $script_uri,
190 * $script_dependencies,
191 * - isset( $script_asset['version'] ) ? $script_asset['version'] : false
192 * + isset( $script_asset['version'] ) ? $script_asset['version'] : false,
193 * + array( 'strategy' => 'defer' )
194 * );
195 * if ( ! $result ) {
196 * return false;
197 * ```
198 *
199 * @see register_block_script_handle()
200 */
201 function gutenberg_defer_block_view_scripts() {
202 $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
203 foreach ( $block_types as $block_type ) {
204 foreach ( $block_type->view_script_handles as $view_script_handle ) {
205 wp_script_add_data( $view_script_handle, 'strategy', 'defer' );
206 }
207 }
208 }
209
210 add_action( 'init', 'gutenberg_defer_block_view_scripts', 100 );
211
212 /**
213 * Deregisters the existing core block type and its assets.
214 *
215 * @param string $block_name The name of the block.
216 *
217 * @return void
218 */
219 function gutenberg_deregister_core_block_and_assets( $block_name ) {
220 $registry = WP_Block_Type_Registry::get_instance();
221 if ( $registry->is_registered( $block_name ) ) {
222 $block_type = $registry->get_registered( $block_name );
223 if ( ! empty( $block_type->view_script_handles ) ) {
224 foreach ( $block_type->view_script_handles as $view_script_handle ) {
225 if ( str_starts_with( $view_script_handle, 'wp-block-' ) ) {
226 wp_deregister_script( $view_script_handle );
227 }
228 }
229 }
230 $registry->unregister( $block_name );
231 }
232 }
233
234 /**
235 * Registers block styles for a core block.
236 *
237 * @param string $block_name The block-name.
238 *
239 * @return void
240 */
241 function gutenberg_register_core_block_assets( $block_name ) {
242 if ( ! wp_should_load_separate_core_block_assets() ) {
243 return;
244 }
245
246 $block_name = str_replace( 'core/', '', $block_name );
247
248 // When in production, use the plugin's version as the default asset version;
249 // else (for development or test) default to use the current time.
250 $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
251
252 $style_path = "build/block-library/blocks/$block_name/";
253 $stylesheet_url = gutenberg_url( $style_path . 'style.css' );
254 $stylesheet_path = gutenberg_dir_path() . $style_path . ( is_rtl() ? 'style-rtl.css' : 'style.css' );
255
256 if ( file_exists( $stylesheet_path ) ) {
257 wp_deregister_style( "wp-block-{$block_name}" );
258 wp_register_style(
259 "wp-block-{$block_name}",
260 $stylesheet_url,
261 array(),
262 $default_version
263 );
264 wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );
265
266 // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
267 wp_style_add_data( "wp-block-{$block_name}", 'path', $stylesheet_path );
268 } else {
269 wp_register_style( "wp-block-{$block_name}", false, array() );
270 }
271
272 /*
273 * If the current theme supports wp-block-styles, dequeue the core styles
274 * and enqueue the plugin ones instead.
275 */
276 if ( current_theme_supports( 'wp-block-styles' ) ) {
277
278 // Get the path to the block's stylesheet.
279 $theme_style_path = is_rtl()
280 ? "build/block-library/blocks/$block_name/theme-rtl.css"
281 : "build/block-library/blocks/$block_name/theme.css";
282
283 // If the file exists, enqueue it.
284 if ( file_exists( gutenberg_dir_path() . $theme_style_path ) ) {
285 wp_deregister_style( "wp-block-{$block_name}-theme" );
286 wp_register_style(
287 "wp-block-{$block_name}-theme",
288 gutenberg_url( $theme_style_path ),
289 array(),
290 $default_version
291 );
292 wp_style_add_data( "wp-block-{$block_name}-theme", 'path', gutenberg_dir_path() . $theme_style_path );
293 }
294 }
295
296 $editor_style_path = "build/block-library/blocks/$block_name/style-editor.css";
297 if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
298 wp_deregister_style( "wp-block-{$block_name}-editor" );
299 wp_register_style(
300 "wp-block-{$block_name}-editor",
301 gutenberg_url( $editor_style_path ),
302 array(),
303 $default_version
304 );
305 wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
306 } else {
307 wp_register_style( "wp-block-{$block_name}-editor", false );
308 }
309 }
310
311 /**
312 * Complements the implementation of block type `core/social-icon`, whether it
313 * be provided by core or the plugin, with derived block types for each
314 * "service" (WordPress, Twitter, etc.) supported by Social Links.
315 *
316 * This ensures backwards compatibility for any users running the Gutenberg
317 * plugin who have used Social Links prior to their conversion to block
318 * variations.
319 *
320 * This shim is INTENTIONALLY left out of core, as Social Links have never
321 * landed there.
322 *
323 * @see https://github.com/WordPress/gutenberg/pull/19887
324 */
325 function gutenberg_register_legacy_social_link_blocks() {
326 $services = array(
327 'amazon',
328 'bandcamp',
329 'behance',
330 'chain',
331 'codepen',
332 'deviantart',
333 'dribbble',
334 'dropbox',
335 'etsy',
336 'facebook',
337 'feed',
338 'fivehundredpx',
339 'flickr',
340 'foursquare',
341 'goodreads',
342 'google',
343 'github',
344 'instagram',
345 'lastfm',
346 'linkedin',
347 'mail',
348 'mastodon',
349 'meetup',
350 'medium',
351 'pinterest',
352 'pocket',
353 'reddit',
354 'skype',
355 'snapchat',
356 'soundcloud',
357 'spotify',
358 'tumblr',
359 'twitch',
360 'twitter',
361 'vimeo',
362 'vk',
363 'wordpress',
364 'yelp',
365 'youtube',
366 );
367
368 foreach ( $services as $service ) {
369 register_block_type(
370 'core/social-link-' . $service,
371 array(
372 'category' => 'widgets',
373 'attributes' => array(
374 'url' => array(
375 'type' => 'string',
376 ),
377 'service' => array(
378 'type' => 'string',
379 'default' => $service,
380 ),
381 'label' => array(
382 'type' => 'string',
383 ),
384 ),
385 'render_callback' => 'gutenberg_render_block_core_social_link',
386 )
387 );
388 }
389 }
390
391 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
392
393 /**
394 * Migrate the legacy `sync_status` meta key (added 16.1) to the new `wp_pattern_sync_status` meta key (16.1.1).
395 *
396 * This filter is INTENTIONALLY left out of core as the meta key was fist introduced to core in 6.3 as `wp_pattern_sync_status`.
397 * see https://github.com/WordPress/gutenberg/pull/52232
398 *
399 * @param mixed $value The value to return, either a single metadata value or an array of values depending on the value of $single.
400 * @param int $object_id ID of the object metadata is for.
401 * @param string $meta_key Metadata key.
402 * @param bool $single Whether to return only the first value of the specified $meta_key.
403 */
404 function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $single ) {
405 if ( 'wp_pattern_sync_status' !== $meta_key ) {
406 return $value;
407 }
408
409 $sync_status = get_post_meta( $object_id, 'sync_status', $single );
410
411 if ( $single && 'unsynced' === $sync_status ) {
412 return $sync_status;
413 } elseif ( isset( $sync_status[0] ) && 'unsynced' === $sync_status[0] ) {
414 return $sync_status;
415 }
416
417 return $value;
418 }
419
420 add_filter( 'default_post_metadata', 'gutenberg_legacy_wp_block_post_meta', 10, 4 );
421
422
423
424 /**
425 * Strips all HTML from the content of footnotes, and sanitizes the ID.
426 *
427 * This function expects slashed data on the footnotes content.
428 *
429 * @access private
430 *
431 * @param string $footnotes JSON encoded string of an array containing the content and ID of each footnote.
432 * @return string Filtered content without any HTML on the footnote content and with the sanitized id.
433 */
434 function _gutenberg_filter_post_meta_footnotes( $footnotes ) {
435 $footnotes_decoded = json_decode( $footnotes, true );
436 if ( ! is_array( $footnotes_decoded ) ) {
437 return '';
438 }
439 $footnotes_sanitized = array();
440 foreach ( $footnotes_decoded as $footnote ) {
441 if ( ! empty( $footnote['content'] ) && ! empty( $footnote['id'] ) ) {
442 $footnotes_sanitized[] = array(
443 'id' => sanitize_key( $footnote['id'] ),
444 'content' => wp_unslash( wp_filter_post_kses( wp_slash( $footnote['content'] ) ) ),
445 );
446 }
447 }
448 return wp_json_encode( $footnotes_sanitized );
449 }
450
451 /**
452 * Adds the filters to filter footnotes meta field.
453 *
454 * @access private
455 */
456 function _gutenberg_footnotes_kses_init_filters() {
457 add_filter( 'sanitize_post_meta_footnotes', '_gutenberg_filter_post_meta_footnotes' );
458 }
459
460 /**
461 * Removes the filters that filter footnotes meta field.
462 *
463 * @access private
464 */
465 function _gutenberg_footnotes_remove_filters() {
466 remove_filter( 'sanitize_post_meta_footnotes', '_gutenberg_filter_post_meta_footnotes' );
467 }
468
469 /**
470 * Registers the filter of footnotes meta field if the user does not have unfiltered_html capability.
471 *
472 * @access private
473 */
474 function _gutenberg_footnotes_kses_init() {
475 if ( function_exists( '_wp_filter_post_meta_footnotes' ) ) {
476 return;
477 }
478 _gutenberg_footnotes_remove_filters();
479 if ( ! current_user_can( 'unfiltered_html' ) ) {
480 _gutenberg_footnotes_kses_init_filters();
481 }
482 }
483
484 /**
485 * Initializes footnotes meta field filters when imported data should be filtered.
486 *
487 * This filter is the last being executed on force_filtered_html_on_import.
488 * If the input of the filter is true it means we are in an import situation and should
489 * enable kses, independently of the user capabilities.
490 * So in that case we call _gutenberg_footnotes_kses_init_filters;
491 *
492 * @access private
493 *
494 * @param string $arg Input argument of the filter.
495 * @return string Input argument of the filter.
496 */
497 function _gutenberg_footnotes_force_filtered_html_on_import_filter( $arg ) {
498 if ( function_exists( '_wp_filter_post_meta_footnotes' ) ) {
499 return;
500 }
501 // force_filtered_html_on_import is true we need to init the global styles kses filters.
502 if ( $arg ) {
503 _gutenberg_footnotes_kses_init_filters();
504 }
505 return $arg;
506 }
507
508 add_action( 'init', '_gutenberg_footnotes_kses_init' );
509 add_action( 'set_current_user', '_gutenberg_footnotes_kses_init' );
510 add_filter( 'force_filtered_html_on_import', '_gutenberg_footnotes_force_filtered_html_on_import_filter', 999 );
511