3 * Used to set up all core blocks used with the block editor. |
3 * Used to set up all core blocks used with the block editor. |
4 * |
4 * |
5 * @package WordPress |
5 * @package WordPress |
6 */ |
6 */ |
7 |
7 |
|
8 define( 'BLOCKS_PATH', ABSPATH . WPINC . '/blocks/' ); |
|
9 |
8 // Include files required for core blocks registration. |
10 // Include files required for core blocks registration. |
9 require ABSPATH . WPINC . '/blocks/archives.php'; |
11 require BLOCKS_PATH . 'legacy-widget.php'; |
10 require ABSPATH . WPINC . '/blocks/avatar.php'; |
12 require BLOCKS_PATH . 'widget-group.php'; |
11 require ABSPATH . WPINC . '/blocks/block.php'; |
13 require BLOCKS_PATH . 'require-dynamic-blocks.php'; |
12 require ABSPATH . WPINC . '/blocks/calendar.php'; |
14 |
13 require ABSPATH . WPINC . '/blocks/categories.php'; |
15 /** |
14 require ABSPATH . WPINC . '/blocks/comment-author-name.php'; |
16 * Registers core block style handles. |
15 require ABSPATH . WPINC . '/blocks/comment-content.php'; |
17 * |
16 require ABSPATH . WPINC . '/blocks/comment-date.php'; |
18 * While {@see register_block_style_handle()} is typically used for that, the way it is |
17 require ABSPATH . WPINC . '/blocks/comment-edit-link.php'; |
19 * implemented is inefficient for core block styles. Registering those style handles here |
18 require ABSPATH . WPINC . '/blocks/comment-reply-link.php'; |
20 * avoids unnecessary logic and filesystem lookups in the other function. |
19 require ABSPATH . WPINC . '/blocks/comment-template.php'; |
21 * |
20 require ABSPATH . WPINC . '/blocks/comments-pagination-next.php'; |
22 * @since 6.3.0 |
21 require ABSPATH . WPINC . '/blocks/comments-pagination-numbers.php'; |
23 * |
22 require ABSPATH . WPINC . '/blocks/comments-pagination-previous.php'; |
24 * @global string $wp_version The WordPress version string. |
23 require ABSPATH . WPINC . '/blocks/comments-pagination.php'; |
25 */ |
24 require ABSPATH . WPINC . '/blocks/comments-title.php'; |
26 function register_core_block_style_handles() { |
25 require ABSPATH . WPINC . '/blocks/cover.php'; |
27 global $wp_version; |
26 require ABSPATH . WPINC . '/blocks/file.php'; |
28 |
27 require ABSPATH . WPINC . '/blocks/gallery.php'; |
29 if ( ! wp_should_load_separate_core_block_assets() ) { |
28 require ABSPATH . WPINC . '/blocks/home-link.php'; |
30 return; |
29 require ABSPATH . WPINC . '/blocks/image.php'; |
31 } |
30 require ABSPATH . WPINC . '/blocks/latest-comments.php'; |
32 |
31 require ABSPATH . WPINC . '/blocks/latest-posts.php'; |
33 $blocks_url = includes_url( 'blocks/' ); |
32 require ABSPATH . WPINC . '/blocks/legacy-widget.php'; |
34 $suffix = wp_scripts_get_suffix(); |
33 require ABSPATH . WPINC . '/blocks/loginout.php'; |
35 $wp_styles = wp_styles(); |
34 require ABSPATH . WPINC . '/blocks/navigation-link.php'; |
36 $style_fields = array( |
35 require ABSPATH . WPINC . '/blocks/navigation-submenu.php'; |
37 'style' => 'style', |
36 require ABSPATH . WPINC . '/blocks/navigation.php'; |
38 'editorStyle' => 'editor', |
37 require ABSPATH . WPINC . '/blocks/page-list.php'; |
39 ); |
38 require ABSPATH . WPINC . '/blocks/pattern.php'; |
40 |
39 require ABSPATH . WPINC . '/blocks/post-author-biography.php'; |
41 static $core_blocks_meta; |
40 require ABSPATH . WPINC . '/blocks/post-author.php'; |
42 if ( ! $core_blocks_meta ) { |
41 require ABSPATH . WPINC . '/blocks/post-comments.php'; |
43 $core_blocks_meta = require BLOCKS_PATH . 'blocks-json.php'; |
42 require ABSPATH . WPINC . '/blocks/post-comments-form.php'; |
44 } |
43 require ABSPATH . WPINC . '/blocks/post-content.php'; |
45 |
44 require ABSPATH . WPINC . '/blocks/post-date.php'; |
46 $files = false; |
45 require ABSPATH . WPINC . '/blocks/post-excerpt.php'; |
47 $transient_name = 'wp_core_block_css_files'; |
46 require ABSPATH . WPINC . '/blocks/post-featured-image.php'; |
48 |
47 require ABSPATH . WPINC . '/blocks/post-navigation-link.php'; |
49 /* |
48 require ABSPATH . WPINC . '/blocks/post-template.php'; |
50 * Ignore transient cache when the development mode is set to 'core'. Why? To avoid interfering with |
49 require ABSPATH . WPINC . '/blocks/post-terms.php'; |
51 * the core developer's workflow. |
50 require ABSPATH . WPINC . '/blocks/post-title.php'; |
52 */ |
51 require ABSPATH . WPINC . '/blocks/query-no-results.php'; |
53 $can_use_cached = ! wp_is_development_mode( 'core' ); |
52 require ABSPATH . WPINC . '/blocks/query-pagination-next.php'; |
54 |
53 require ABSPATH . WPINC . '/blocks/query-pagination-numbers.php'; |
55 if ( $can_use_cached ) { |
54 require ABSPATH . WPINC . '/blocks/query-pagination-previous.php'; |
56 $cached_files = get_transient( $transient_name ); |
55 require ABSPATH . WPINC . '/blocks/query-pagination.php'; |
57 |
56 require ABSPATH . WPINC . '/blocks/query-title.php'; |
58 // Check the validity of cached values by checking against the current WordPress version. |
57 require ABSPATH . WPINC . '/blocks/query.php'; |
59 if ( |
58 require ABSPATH . WPINC . '/blocks/read-more.php'; |
60 is_array( $cached_files ) |
59 require ABSPATH . WPINC . '/blocks/rss.php'; |
61 && isset( $cached_files['version'] ) |
60 require ABSPATH . WPINC . '/blocks/search.php'; |
62 && $cached_files['version'] === $wp_version |
61 require ABSPATH . WPINC . '/blocks/shortcode.php'; |
63 && isset( $cached_files['files'] ) |
62 require ABSPATH . WPINC . '/blocks/site-logo.php'; |
64 ) { |
63 require ABSPATH . WPINC . '/blocks/site-tagline.php'; |
65 $files = $cached_files['files']; |
64 require ABSPATH . WPINC . '/blocks/site-title.php'; |
66 } |
65 require ABSPATH . WPINC . '/blocks/social-link.php'; |
67 } |
66 require ABSPATH . WPINC . '/blocks/tag-cloud.php'; |
68 |
67 require ABSPATH . WPINC . '/blocks/template-part.php'; |
69 if ( ! $files ) { |
68 require ABSPATH . WPINC . '/blocks/term-description.php'; |
70 $files = glob( wp_normalize_path( BLOCKS_PATH . '**/**.css' ) ); |
69 require ABSPATH . WPINC . '/blocks/widget-group.php'; |
71 |
|
72 // Normalize BLOCKS_PATH prior to substitution for Windows environments. |
|
73 $normalized_blocks_path = wp_normalize_path( BLOCKS_PATH ); |
|
74 |
|
75 $files = array_map( |
|
76 static function ( $file ) use ( $normalized_blocks_path ) { |
|
77 return str_replace( $normalized_blocks_path, '', $file ); |
|
78 }, |
|
79 $files |
|
80 ); |
|
81 |
|
82 // Save core block style paths in cache when not in development mode. |
|
83 if ( $can_use_cached ) { |
|
84 set_transient( |
|
85 $transient_name, |
|
86 array( |
|
87 'version' => $wp_version, |
|
88 'files' => $files, |
|
89 ) |
|
90 ); |
|
91 } |
|
92 } |
|
93 |
|
94 $register_style = static function ( $name, $filename, $style_handle ) use ( $blocks_url, $suffix, $wp_styles, $files ) { |
|
95 $style_path = "{$name}/{$filename}{$suffix}.css"; |
|
96 $path = wp_normalize_path( BLOCKS_PATH . $style_path ); |
|
97 |
|
98 if ( ! in_array( $style_path, $files, true ) ) { |
|
99 $wp_styles->add( |
|
100 $style_handle, |
|
101 false |
|
102 ); |
|
103 return; |
|
104 } |
|
105 |
|
106 $wp_styles->add( $style_handle, $blocks_url . $style_path ); |
|
107 $wp_styles->add_data( $style_handle, 'path', $path ); |
|
108 |
|
109 $rtl_file = "{$name}/{$filename}-rtl{$suffix}.css"; |
|
110 if ( is_rtl() && in_array( $rtl_file, $files, true ) ) { |
|
111 $wp_styles->add_data( $style_handle, 'rtl', 'replace' ); |
|
112 $wp_styles->add_data( $style_handle, 'suffix', $suffix ); |
|
113 $wp_styles->add_data( $style_handle, 'path', str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $path ) ); |
|
114 } |
|
115 }; |
|
116 |
|
117 foreach ( $core_blocks_meta as $name => $schema ) { |
|
118 /** This filter is documented in wp-includes/blocks.php */ |
|
119 $schema = apply_filters( 'block_type_metadata', $schema ); |
|
120 |
|
121 // Backfill these properties similar to `register_block_type_from_metadata()`. |
|
122 if ( ! isset( $schema['style'] ) ) { |
|
123 $schema['style'] = "wp-block-{$name}"; |
|
124 } |
|
125 if ( ! isset( $schema['editorStyle'] ) ) { |
|
126 $schema['editorStyle'] = "wp-block-{$name}-editor"; |
|
127 } |
|
128 |
|
129 // Register block theme styles. |
|
130 $register_style( $name, 'theme', "wp-block-{$name}-theme" ); |
|
131 |
|
132 foreach ( $style_fields as $style_field => $filename ) { |
|
133 $style_handle = $schema[ $style_field ]; |
|
134 if ( is_array( $style_handle ) ) { |
|
135 continue; |
|
136 } |
|
137 $register_style( $name, $filename, $style_handle ); |
|
138 } |
|
139 } |
|
140 } |
|
141 add_action( 'init', 'register_core_block_style_handles', 9 ); |
70 |
142 |
71 /** |
143 /** |
72 * Registers core block types using metadata files. |
144 * Registers core block types using metadata files. |
73 * Dynamic core blocks are registered separately. |
145 * Dynamic core blocks are registered separately. |
74 * |
146 * |
75 * @since 5.5.0 |
147 * @since 5.5.0 |
76 */ |
148 */ |
77 function register_core_block_types_from_metadata() { |
149 function register_core_block_types_from_metadata() { |
78 $block_folders = array( |
150 $block_folders = require BLOCKS_PATH . 'require-static-blocks.php'; |
79 'audio', |
|
80 'button', |
|
81 'buttons', |
|
82 'code', |
|
83 'column', |
|
84 'columns', |
|
85 'comments-query-loop', |
|
86 'embed', |
|
87 'freeform', |
|
88 'group', |
|
89 'heading', |
|
90 'html', |
|
91 'list', |
|
92 'media-text', |
|
93 'missing', |
|
94 'more', |
|
95 'nextpage', |
|
96 'paragraph', |
|
97 'preformatted', |
|
98 'pullquote', |
|
99 'quote', |
|
100 'separator', |
|
101 'social-links', |
|
102 'spacer', |
|
103 'table', |
|
104 'text-columns', |
|
105 'verse', |
|
106 'video', |
|
107 ); |
|
108 |
|
109 foreach ( $block_folders as $block_folder ) { |
151 foreach ( $block_folders as $block_folder ) { |
110 register_block_type( |
152 register_block_type_from_metadata( |
111 ABSPATH . WPINC . '/blocks/' . $block_folder |
153 BLOCKS_PATH . $block_folder |
112 ); |
154 ); |
113 } |
155 } |
114 } |
156 } |
115 add_action( 'init', 'register_core_block_types_from_metadata' ); |
157 add_action( 'init', 'register_core_block_types_from_metadata' ); |