PluginProbe
Gutenberg / 8.3.0
Gutenberg v8.3.0
24.0.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 All 403 releases
← All changes | lib/blocks.php +176 -266 23.7.28.3.0 View file →
@@ -1,7 +1,7 @@
1 1 <?php
2 2 /**
3 - * Block functions specific for the Gutenberg editor plugin.
3 + * Block and style registration functions.
4 4 *
5 5 * @package gutenberg
6 6 */
7 7
@@ -9,183 +9,133 @@
9 9 * Substitutes the implementation of a core-registered block type, if exists,
10 10 * with the built result from the plugin.
11 11 */
12 12 function gutenberg_reregister_core_block_types() {
13 - $blocks_dirs = array(
14 - __DIR__ . '/../build/scripts/block-library/',
15 - __DIR__ . '/../build/scripts/edit-widgets/blocks/',
16 - __DIR__ . '/../build/scripts/widgets/blocks/',
13 + // Blocks directory may not exist if working from a fresh clone.
14 + $blocks_dir = dirname( __FILE__ ) . '/../build/block-library/blocks/';
15 + if ( ! file_exists( $blocks_dir ) ) {
16 + return;
17 + }
18 +
19 + $block_folders = array(
20 + 'audio',
21 + 'button',
22 + 'buttons',
23 + 'classic',
24 + 'code',
25 + 'column',
26 + 'columns',
27 + 'file',
28 + 'gallery',
29 + 'group',
30 + 'heading',
31 + 'html',
32 + 'image',
33 + 'list',
34 + 'media-text',
35 + 'missing',
36 + 'more',
37 + 'navigation-link',
38 + 'nextpage',
39 + 'paragraph',
40 + 'preformatted',
41 + 'pullquote',
42 + 'quote',
43 + 'separator',
44 + 'social-links',
45 + 'spacer',
46 + 'subhead',
47 + 'table',
48 + 'text-columns',
49 + 'verse',
50 + 'video',
51 + 'widget-area',
17 52 );
18 53
19 - foreach ( $blocks_dirs as $blocks_dir ) {
20 - $manifest_path = $blocks_dir . 'blocks-manifest.php';
21 - $blocks = require $manifest_path;
54 + $block_names = array(
55 + 'archives.php' => 'core/archives',
56 + 'block.php' => 'core/block',
57 + 'calendar.php' => 'core/calendar',
58 + 'categories.php' => 'core/categories',
59 + 'cover.php' => 'core/cover',
60 + 'latest-comments.php' => 'core/latest-comments',
61 + 'latest-posts.php' => 'core/latest-posts',
62 + 'legacy-widget.php' => 'core/legacy-widget',
63 + 'navigation.php' => 'core/navigation',
64 + 'rss.php' => 'core/rss',
65 + 'search.php' => 'core/search',
66 + 'shortcode.php' => 'core/shortcode',
67 + 'social-link.php' => 'core/social-link',
68 + 'tag-cloud.php' => 'core/tag-cloud',
69 + );
22 70
23 - foreach ( $blocks as $block_name_folder => $metadata ) {
24 - if ( ! is_array( $metadata ) || ! isset( $metadata['name'] ) ) {
25 - continue;
26 - }
71 + if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' ) ) {
72 + $block_names = array_merge(
73 + $block_names,
74 + array(
75 + 'post-author.php' => 'core/post-author',
76 + 'post-comments.php' => 'core/post-comments',
77 + 'post-comments-count.php' => 'core/post-comments-count',
78 + 'post-comments-form.php' => 'core/post-comments-form',
79 + 'post-content.php' => 'core/post-content',
80 + 'post-date.php' => 'core/post-date',
81 + 'post-excerpt.php' => 'core/post-excerpt',
82 + 'post-featured-image.php' => 'core/post-featured-image',
83 + 'post-tags.php' => 'core/post-tags',
84 + 'post-title.php' => 'core/post-title',
85 + 'query.php' => 'core/query',
86 + 'query-loop.php' => 'core/query-loop',
87 + 'query-pagination.php' => 'core/query-pagination',
88 + 'site-title.php' => 'core/site-title',
89 + 'template-part.php' => 'core/template-part',
90 + )
91 + );
92 + }
27 93
28 - gutenberg_deregister_core_block_and_assets( $metadata['name'] );
29 - gutenberg_register_core_block_assets( $block_name_folder );
94 + $registry = WP_Block_Type_Registry::get_instance();
30 95
31 - $php_file = $blocks_dir . $block_name_folder . '.php';
32 - if ( file_exists( $php_file ) ) {
33 - require_once $php_file;
34 - } else {
35 - register_block_type_from_metadata( $blocks_dir . $block_name_folder );
36 - }
96 + foreach ( $block_folders as $folder_name ) {
97 + $block_json_file = $blocks_dir . '/' . $folder_name . '/block.json';
98 + if ( ! file_exists( $block_json_file ) ) {
99 + return;
37 100 }
38 - }
39 -}
40 101
41 -add_action( 'init', 'gutenberg_reregister_core_block_types' );
102 + // Ideally, all paths to block metadata files should be listed in
103 + // WordPress core. In this place we should rather use filter
104 + // to replace paths with overrides defined by the plugin.
105 + $metadata = json_decode( file_get_contents( $block_json_file ), true );
106 + if ( ! is_array( $metadata ) || ! $metadata['name'] ) {
107 + return false;
108 + }
42 109
43 -/**
44 - * Adds the defer loading strategy to all registered blocks.
45 - *
46 - * This function would not be part of core merge. Instead, the register_block_script_handle() function would be patched
47 - * as follows.
48 - *
49 - * ```
50 - * --- a/wp-includes/blocks.php
51 - * +++ b/wp-includes/blocks.php
52 - * @ @ -153,7 +153,8 @ @ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
53 - * $script_handle,
54 - * $script_uri,
55 - * $script_dependencies,
56 - * - $script_asset['version'] ?? false
57 - * + $script_asset['version'] ?? false,
58 - * + array( 'strategy' => 'defer' )
59 - * );
60 - * if ( ! $result ) {
61 - * return false;
62 - * ```
63 - *
64 - * @see register_block_script_handle()
65 - */
66 -function gutenberg_defer_block_view_scripts() {
67 - $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
68 - foreach ( $block_types as $block_type ) {
69 - foreach ( $block_type->view_script_handles as $view_script_handle ) {
70 - wp_script_add_data( $view_script_handle, 'strategy', 'defer' );
110 + if ( $registry->is_registered( $metadata['name'] ) ) {
111 + $registry->unregister( $metadata['name'] );
71 112 }
113 +
114 + register_block_type_from_metadata( $block_json_file );
72 115 }
73 -}
74 116
75 -add_action( 'init', 'gutenberg_defer_block_view_scripts', 100 );
117 + foreach ( $block_names as $file => $block_names ) {
118 + if ( ! file_exists( $blocks_dir . $file ) ) {
119 + return;
120 + }
76 121
77 -/**
78 - * Deregisters the existing core block type and its assets.
79 - *
80 - * @param string $block_name The name of the block.
81 - *
82 - * @return void
83 - */
84 -function gutenberg_deregister_core_block_and_assets( $block_name ) {
85 - $registry = WP_Block_Type_Registry::get_instance();
86 - if ( $registry->is_registered( $block_name ) ) {
87 - $block_type = $registry->get_registered( $block_name );
88 - if ( ! empty( $block_type->view_script_handles ) ) {
89 - foreach ( $block_type->view_script_handles as $view_script_handle ) {
90 - if ( str_starts_with( $view_script_handle, 'wp-block-' ) ) {
91 - wp_deregister_script( $view_script_handle );
122 + if ( is_string( $block_names ) ) {
123 + if ( $registry->is_registered( $block_names ) ) {
124 + $registry->unregister( $block_names );
125 + }
126 + } elseif ( is_array( $block_names ) ) {
127 + foreach ( $block_names as $block_name ) {
128 + if ( $registry->is_registered( $block_name ) ) {
129 + $registry->unregister( $block_name );
92 130 }
93 131 }
94 132 }
95 - $registry->unregister( $block_name );
96 - }
97 -}
98 133
99 -/**
100 - * Registers block styles for a core block.
101 - *
102 - * @param string $block_name The block-name.
103 - *
104 - * @return void
105 - */
106 -function gutenberg_register_core_block_assets( $block_name ) {
107 - static $gutenberg_url_root = null;
108 - // Running `gutenberg_url` inside of a loop can be expensive in systems with
109 - // many callbacks attached to the `plugins_url` hook.
110 - // Since all of the paths have the same root, we can instead retrieve the
111 - // corresponding URL root once, and manually concatenate the URL below.
112 - if ( is_null( $gutenberg_url_root ) ) {
113 - $gutenberg_url_root = gutenberg_url( '/' );
134 + require $blocks_dir . $file;
114 135 }
115 -
116 - if ( ! wp_should_load_separate_core_block_assets() ) {
117 - return;
118 - }
119 -
120 - $block_name = str_replace( 'core/', '', $block_name );
121 -
122 - // When in production, use the plugin's version as the default asset version;
123 - // else (for development or test) default to use the current time.
124 - $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
125 - $suffix = SCRIPT_DEBUG ? '' : '.min';
126 -
127 - $style_path = "build/styles/block-library/$block_name/";
128 - $stylesheet_url = $gutenberg_url_root . $style_path . 'style' . $suffix . '.css';
129 - $stylesheet_path = gutenberg_dir_path() . $style_path . ( is_rtl() ? 'style-rtl' . $suffix . '.css' : 'style' . $suffix . '.css' );
130 -
131 - if ( file_exists( $stylesheet_path ) ) {
132 -
133 - wp_deregister_style( "wp-block-{$block_name}" );
134 - wp_register_style(
135 - "wp-block-{$block_name}",
136 - $stylesheet_url,
137 - array(),
138 - $default_version
139 - );
140 - wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );
141 - wp_style_add_data( "wp-block-{$block_name}", 'suffix', $suffix );
142 - // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
143 - wp_style_add_data( "wp-block-{$block_name}", 'path', $stylesheet_path );
144 - } else {
145 - wp_register_style( "wp-block-{$block_name}", false, array() );
146 - }
147 -
148 - /*
149 - * If the current theme supports wp-block-styles, dequeue the core styles
150 - * and enqueue the plugin ones instead.
151 - */
152 - if ( current_theme_supports( 'wp-block-styles' ) ) {
153 -
154 - // Get the path to the block's stylesheet.
155 - $theme_style_path = is_rtl()
156 - ? "build/styles/block-library/$block_name/theme-rtl{$suffix}.css"
157 - : "build/styles/block-library/$block_name/theme{$suffix}.css";
158 -
159 - // If the file exists, enqueue it.
160 - if ( file_exists( gutenberg_dir_path() . $theme_style_path ) ) {
161 - wp_deregister_style( "wp-block-{$block_name}-theme" );
162 - wp_register_style(
163 - "wp-block-{$block_name}-theme",
164 - $gutenberg_url_root . $theme_style_path,
165 - array(),
166 - $default_version
167 - );
168 - wp_style_add_data( "wp-block-{$block_name}-theme", 'path', gutenberg_dir_path() . $theme_style_path );
169 - wp_style_add_data( "wp-block-{$block_name}-theme", 'suffix', $suffix );
170 - }
171 - }
172 -
173 - $editor_style_path = "build/styles/block-library/$block_name/style-editor{$suffix}.css";
174 - if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
175 - wp_deregister_style( "wp-block-{$block_name}-editor" );
176 - wp_register_style(
177 - "wp-block-{$block_name}-editor",
178 - $gutenberg_url_root . $editor_style_path,
179 - array(),
180 - $default_version
181 - );
182 - wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
183 - wp_style_add_data( "wp-block-{$block_name}-editor", 'suffix', $suffix );
184 - } else {
185 - wp_register_style( "wp-block-{$block_name}-editor", false );
186 - }
187 136 }
137 +add_action( 'init', 'gutenberg_reregister_core_block_types' );
188 138
189 139 /**
190 140 * Complements the implementation of block type `core/social-icon`, whether it
191 141 * be provided by core or the plugin, with derived block types for each
@@ -194,12 +144,12 @@
194 144 * This ensures backwards compatibility for any users running the Gutenberg
195 145 * plugin who have used Social Links prior to their conversion to block
196 146 * variations.
197 147 *
198 - * This shim is INTENTIONALLY left out of core, as Social Links have never
148 + * This shim is INTENTIONALLY left out of core, as Social Links haven't yet
199 149 * landed there.
200 150 *
201 - * @link https://github.com/WordPress/gutenberg/pull/19887
151 + * @see https://github.com/WordPress/gutenberg/pull/19887
202 152 */
203 153 function gutenberg_register_legacy_social_link_blocks() {
204 154 $services = array(
205 155 'amazon',
@@ -264,125 +214,85 @@
264 214 )
265 215 );
266 216 }
267 217 }
268 -
269 218 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
270 219
271 -/**
272 - * Migrate the legacy `sync_status` meta key (added 16.1) to the new `wp_pattern_sync_status` meta key (16.1.1).
273 - *
274 - * 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`.
275 - * see https://github.com/WordPress/gutenberg/pull/52232
276 - *
277 - * @param mixed $value The value to return, either a single metadata value or an array of values depending on the value of $single.
278 - * @param int $object_id ID of the object metadata is for.
279 - * @param string $meta_key Metadata key.
280 - * @param bool $single Whether to return only the first value of the specified $meta_key.
281 - */
282 -function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $single ) {
283 - if ( 'wp_pattern_sync_status' !== $meta_key ) {
284 - return $value;
220 +if ( ! function_exists( 'register_block_style' ) ) {
221 + /**
222 + * Registers a new block style.
223 + *
224 + * @param string $block_name Block type name including namespace.
225 + * @param array $style_properties Array containing the properties of the style name, label, style (name of the stylesheet to be enqueued), inline_style (string containing the CSS to be added).
226 + *
227 + * @return boolean True if the block style was registered with success and false otherwise.
228 + */
229 + function register_block_style( $block_name, $style_properties ) {
230 + return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties );
285 231 }
232 +}
286 233
287 - $sync_status = get_post_meta( $object_id, 'sync_status', $single );
288 -
289 - if ( $single && 'unsynced' === $sync_status ) {
290 - return $sync_status;
291 - } elseif ( isset( $sync_status[0] ) && 'unsynced' === $sync_status[0] ) {
292 - return $sync_status;
234 +if ( ! function_exists( 'unregister_block_style' ) ) {
235 + /**
236 + * Unregisters a block style.
237 + *
238 + * @param string $block_name Block type name including namespace.
239 + * @param array $block_style_name Block style name.
240 + *
241 + * @return boolean True if the block style was unregistered with success and false otherwise.
242 + */
243 + function unregister_block_style( $block_name, $block_style_name ) {
244 + return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
293 245 }
294 -
295 - return $value;
296 246 }
297 247
298 -add_filter( 'default_post_metadata', 'gutenberg_legacy_wp_block_post_meta', 10, 4 );
248 +if ( ! has_action( 'enqueue_block_assets', 'enqueue_block_styles_assets' ) ) {
249 + /**
250 + * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
251 + */
252 + function gutenberg_enqueue_block_styles_assets() {
253 + $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
299 254
300 -
301 -
302 -/**
303 - * Strips all HTML from the content of footnotes, and sanitizes the ID.
304 - *
305 - * This function expects slashed data on the footnotes content.
306 - *
307 - * @access private
308 - *
309 - * @param string $footnotes JSON encoded string of an array containing the content and ID of each footnote.
310 - * @return string Filtered content without any HTML on the footnote content and with the sanitized id.
311 - */
312 -function _gutenberg_filter_post_meta_footnotes( $footnotes ) {
313 - $footnotes_decoded = json_decode( $footnotes, true );
314 - if ( ! is_array( $footnotes_decoded ) ) {
315 - return '';
316 - }
317 - $footnotes_sanitized = array();
318 - foreach ( $footnotes_decoded as $footnote ) {
319 - if ( ! empty( $footnote['content'] ) && ! empty( $footnote['id'] ) ) {
320 - $footnotes_sanitized[] = array(
321 - 'id' => sanitize_key( $footnote['id'] ),
322 - 'content' => wp_unslash( wp_filter_post_kses( wp_slash( $footnote['content'] ) ) ),
323 - );
255 + foreach ( $block_styles as $styles ) {
256 + foreach ( $styles as $style_properties ) {
257 + if ( isset( $style_properties['style_handle'] ) ) {
258 + wp_enqueue_style( $style_properties['style_handle'] );
259 + }
260 + if ( isset( $style_properties['inline_style'] ) ) {
261 + wp_add_inline_style( 'wp-block-library', $style_properties['inline_style'] );
262 + }
263 + }
324 264 }
325 265 }
326 - return wp_json_encode( $footnotes_sanitized );
266 + add_action( 'enqueue_block_assets', 'gutenberg_enqueue_block_styles_assets', 30 );
327 267 }
268 +if ( ! has_action( 'enqueue_block_editor_assets', 'enqueue_editor_block_styles_assets' ) ) {
269 + /**
270 + * Function responsible for enqueuing the assets required for block styles functionality on the editor.
271 + */
272 + function gutenberg_enqueue_editor_block_styles_assets() {
273 + $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
328 274
329 -/**
330 - * Adds the filters to filter footnotes meta field.
331 - *
332 - * @access private
333 - */
334 -function _gutenberg_footnotes_kses_init_filters() {
335 - add_filter( 'sanitize_post_meta_footnotes', '_gutenberg_filter_post_meta_footnotes' );
336 -}
275 + $register_script_lines = array( '( function() {' );
276 + foreach ( $block_styles as $block_name => $styles ) {
277 + foreach ( $styles as $style_properties ) {
278 + $register_script_lines[] = sprintf(
279 + ' wp.blocks.registerBlockStyle( \'%s\', %s );',
280 + $block_name,
281 + wp_json_encode(
282 + array(
283 + 'name' => $style_properties['name'],
284 + 'label' => $style_properties['label'],
285 + )
286 + )
287 + );
288 + }
289 + }
290 + $register_script_lines[] = '} )();';
291 + $inline_script = implode( "\n", $register_script_lines );
337 292
338 -/**
339 - * Removes the filters that filter footnotes meta field.
340 - *
341 - * @access private
342 - */
343 -function _gutenberg_footnotes_remove_filters() {
344 - remove_filter( 'sanitize_post_meta_footnotes', '_gutenberg_filter_post_meta_footnotes' );
345 -}
346 -
347 -/**
348 - * Registers the filter of footnotes meta field if the user does not have unfiltered_html capability.
349 - *
350 - * @access private
351 - */
352 -function _gutenberg_footnotes_kses_init() {
353 - if ( function_exists( '_wp_filter_post_meta_footnotes' ) ) {
354 - return;
293 + wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, true );
294 + wp_add_inline_script( 'wp-block-styles', $inline_script );
295 + wp_enqueue_script( 'wp-block-styles' );
355 296 }
356 - _gutenberg_footnotes_remove_filters();
357 - if ( ! current_user_can( 'unfiltered_html' ) ) {
358 - _gutenberg_footnotes_kses_init_filters();
359 - }
297 + add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_editor_block_styles_assets' );
360 298 }
361 -
362 -/**
363 - * Initializes footnotes meta field filters when imported data should be filtered.
364 - *
365 - * This filter is the last being executed on force_filtered_html_on_import.
366 - * If the input of the filter is true it means we are in an import situation and should
367 - * enable kses, independently of the user capabilities.
368 - * So in that case we call _gutenberg_footnotes_kses_init_filters;
369 - *
370 - * @access private
371 - *
372 - * @param string $arg Input argument of the filter.
373 - * @return string Input argument of the filter.
374 - */
375 -function _gutenberg_footnotes_force_filtered_html_on_import_filter( $arg ) {
376 - if ( function_exists( '_wp_filter_post_meta_footnotes' ) ) {
377 - return $arg;
378 - }
379 - // force_filtered_html_on_import is true we need to init the global styles kses filters.
380 - if ( $arg ) {
381 - _gutenberg_footnotes_kses_init_filters();
382 - }
383 - return $arg;
384 -}
385 -
386 -add_action( 'init', '_gutenberg_footnotes_kses_init' );
387 -add_action( 'set_current_user', '_gutenberg_footnotes_kses_init' );
388 -add_filter( 'force_filtered_html_on_import', '_gutenberg_footnotes_force_filtered_html_on_import_filter', 999 );