PluginProbe
Gutenberg / 10.7.4
Gutenberg v10.7.4
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 10.7.4, at lib/blocks.php

415 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block and style registration functions.
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 'cover',
25 'gallery',
26 'group',
27 'heading',
28 'html',
29 'home-link',
30 'image',
31 'list',
32 'media-text',
33 'missing',
34 'more',
35 'navigation-link',
36 'nextpage',
37 'paragraph',
38 'preformatted',
39 'pullquote',
40 'quote',
41 'separator',
42 'social-links',
43 'spacer',
44 'table',
45 // 'table-of-contents',
46 'text-columns',
47 'verse',
48 'video',
49 'embed',
50 ),
51 'block_names' => array(
52 'archives.php' => 'core/archives',
53 'block.php' => 'core/block',
54 'calendar.php' => 'core/calendar',
55 'categories.php' => 'core/categories',
56 'file.php' => 'core/file',
57 'latest-comments.php' => 'core/latest-comments',
58 'latest-posts.php' => 'core/latest-posts',
59 'legacy-widget.php' => 'core/legacy-widget',
60 'loginout.php' => 'core/loginout',
61 'navigation.php' => 'core/navigation',
62 'navigation-link.php' => 'core/navigation-link',
63 'home-link.php' => 'core/home-link',
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 'page-list.php' => 'core/page-list',
70 'post-author.php' => 'core/post-author',
71 'post-comment.php' => 'core/post-comment',
72 'post-comment-author.php' => 'core/post-comment-author',
73 'post-comment-content.php' => 'core/post-comment-content',
74 'post-comment-date.php' => 'core/post-comment-date',
75 'post-comments.php' => 'core/post-comments',
76 'post-comments-count.php' => 'core/post-comments-count',
77 'post-comments-form.php' => 'core/post-comments-form',
78 'post-comments-link.php' => 'core/post-comments-link',
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-terms.php' => 'core/post-terms',
84 'post-navigation-link.php' => 'core/post-navigation-link',
85 'post-title.php' => 'core/post-title',
86 'query.php' => 'core/query',
87 'query-loop.php' => 'core/query-loop',
88 'query-title.php' => 'core/query-title',
89 'query-pagination.php' => 'core/query-pagination',
90 'query-pagination-next.php' => 'core/query-pagination-next',
91 'query-pagination-numbers.php' => 'core/query-pagination-numbers',
92 'query-pagination-previous.php' => 'core/query-pagination-previous',
93 'site-logo.php' => 'core/site-logo',
94 'site-tagline.php' => 'core/site-tagline',
95 'site-title.php' => 'core/site-title',
96 // 'table-of-contents.php' => 'core/table-of-contents',
97 'template-part.php' => 'core/template-part',
98 'term-description.php' => 'core/term-description',
99 ),
100 ),
101 __DIR__ . '/../build/edit-widgets/blocks/' => array(
102 'block_folders' => array(
103 'widget-area',
104 ),
105 'block_names' => array(
106 'widget-area.php' => 'core/widget-area',
107 ),
108 ),
109 );
110 foreach ( $blocks_dirs as $blocks_dir => $details ) {
111 $block_folders = $details['block_folders'];
112 $block_names = $details['block_names'];
113
114 $registry = WP_Block_Type_Registry::get_instance();
115
116 foreach ( $block_folders as $folder_name ) {
117 $block_json_file = $blocks_dir . $folder_name . '/block.json';
118
119 // Ideally, all paths to block metadata files should be listed in
120 // WordPress core. In this place we should rather use filter
121 // to replace paths with overrides defined by the plugin.
122 $metadata = json_decode( file_get_contents( $block_json_file ), true );
123 if ( ! is_array( $metadata ) || ! $metadata['name'] ) {
124 return false;
125 }
126
127 if ( $registry->is_registered( $metadata['name'] ) ) {
128 $registry->unregister( $metadata['name'] );
129 }
130
131 gutenberg_register_core_block_styles( $folder_name );
132 register_block_type_from_metadata( $block_json_file );
133 }
134
135 foreach ( $block_names as $file => $sub_block_names ) {
136 if ( ! file_exists( $blocks_dir . $file ) ) {
137 return;
138 }
139
140 $sub_block_names_normalized = is_string( $sub_block_names ) ? array( $sub_block_names ) : $sub_block_names;
141 foreach ( $sub_block_names_normalized as $block_name ) {
142 if ( $registry->is_registered( $block_name ) ) {
143 $registry->unregister( $block_name );
144 }
145 gutenberg_register_core_block_styles( $block_name );
146 }
147
148 require_once $blocks_dir . $file;
149 }
150 }
151 }
152
153 add_action( 'init', 'gutenberg_reregister_core_block_types' );
154
155 /**
156 * Registers block styles for a core block.
157 *
158 * @param string $block_name The block-name.
159 *
160 * @return void
161 */
162 function gutenberg_register_core_block_styles( $block_name ) {
163 if ( ! gutenberg_should_load_separate_block_assets() ) {
164 return;
165 }
166
167 $block_name = str_replace( 'core/', '', $block_name );
168
169 $style_path = "build/block-library/blocks/$block_name/style.css";
170 $editor_style_path = "build/block-library/blocks/$block_name/style-editor.css";
171
172 if ( file_exists( gutenberg_dir_path() . $style_path ) ) {
173 wp_deregister_style( "wp-block-{$block_name}" );
174 wp_register_style(
175 "wp-block-{$block_name}",
176 gutenberg_url( $style_path ),
177 array(),
178 filemtime( gutenberg_dir_path() . $style_path )
179 );
180 wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );
181
182 // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
183 wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $style_path );
184 }
185
186 if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
187 wp_deregister_style( "wp-block-{$block_name}-editor" );
188 wp_register_style(
189 "wp-block-{$block_name}-editor",
190 gutenberg_url( $editor_style_path ),
191 array(),
192 filemtime( gutenberg_dir_path() . $editor_style_path )
193 );
194 wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
195 }
196 }
197
198 /**
199 * Change the way styles get loaded depending on their size.
200 *
201 * Optimizes performance and sustainability of styles by inlining smaller stylesheets.
202 *
203 * @todo Remove this function when the minimum supported version is WordPress 5.8.
204 *
205 * @return void
206 */
207 function gutenberg_maybe_inline_styles() {
208
209 // Early exit if the "wp_maybe_inline_styles" function exists.
210 if ( function_exists( 'wp_maybe_inline_styles' ) ) {
211 return;
212 }
213
214 $total_inline_limit = 20000;
215 /**
216 * The maximum size of inlined styles in bytes.
217 *
218 * @param int $total_inline_limit The file-size threshold, in bytes. Defaults to 20000.
219 * @return int The file-size threshold, in bytes.
220 */
221 $total_inline_limit = apply_filters( 'styles_inline_size_limit', $total_inline_limit );
222
223 global $wp_styles;
224 $styles = array();
225
226 // Build an array of styles that have a path defined.
227 foreach ( $wp_styles->queue as $handle ) {
228 if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) {
229 $styles[] = array(
230 'handle' => $handle,
231 'path' => $wp_styles->registered[ $handle ]->extra['path'],
232 'size' => filesize( $wp_styles->registered[ $handle ]->extra['path'] ),
233 );
234 }
235 }
236
237 if ( ! empty( $styles ) ) {
238 // Reorder styles array based on size.
239 usort(
240 $styles,
241 function( $a, $b ) {
242 return ( $a['size'] <= $b['size'] ) ? -1 : 1;
243 }
244 );
245
246 /**
247 * The total inlined size.
248 *
249 * On each iteration of the loop, if a style gets added inline the value of this var increases
250 * to reflect the total size of inlined styles.
251 */
252 $total_inline_size = 0;
253
254 // Loop styles.
255 foreach ( $styles as $style ) {
256
257 // Size check. Since styles are ordered by size, we can break the loop.
258 if ( $total_inline_size + $style['size'] > $total_inline_limit ) {
259 break;
260 }
261
262 // Get the styles if we don't already have them.
263 $style['css'] = file_get_contents( $style['path'] );
264
265 // Set `src` to `false` and add styles inline.
266 $wp_styles->registered[ $style['handle'] ]->src = false;
267 if ( empty( $wp_styles->registered[ $style['handle'] ]->extra['after'] ) ) {
268 $wp_styles->registered[ $style['handle'] ]->extra['after'] = array();
269 }
270 array_unshift( $wp_styles->registered[ $style['handle'] ]->extra['after'], $style['css'] );
271
272 // Add the styles size to the $total_inline_size var.
273 $total_inline_size += (int) $style['size'];
274 }
275 }
276 }
277 // Run for styles enqueued in <head>.
278 add_action( 'wp_head', 'gutenberg_maybe_inline_styles', 1 );
279 // Run for late-loaded styles in the footer.
280 add_action( 'wp_footer', 'gutenberg_maybe_inline_styles', 1 );
281
282 /**
283 * Complements the implementation of block type `core/social-icon`, whether it
284 * be provided by core or the plugin, with derived block types for each
285 * "service" (WordPress, Twitter, etc.) supported by Social Links.
286 *
287 * This ensures backwards compatibility for any users running the Gutenberg
288 * plugin who have used Social Links prior to their conversion to block
289 * variations.
290 *
291 * This shim is INTENTIONALLY left out of core, as Social Links haven't yet
292 * landed there.
293 *
294 * @see https://github.com/WordPress/gutenberg/pull/19887
295 */
296 function gutenberg_register_legacy_social_link_blocks() {
297 $services = array(
298 'amazon',
299 'bandcamp',
300 'behance',
301 'chain',
302 'codepen',
303 'deviantart',
304 'dribbble',
305 'dropbox',
306 'etsy',
307 'facebook',
308 'feed',
309 'fivehundredpx',
310 'flickr',
311 'foursquare',
312 'goodreads',
313 'google',
314 'github',
315 'instagram',
316 'lastfm',
317 'linkedin',
318 'mail',
319 'mastodon',
320 'meetup',
321 'medium',
322 'pinterest',
323 'pocket',
324 'reddit',
325 'skype',
326 'snapchat',
327 'soundcloud',
328 'spotify',
329 'tumblr',
330 'twitch',
331 'twitter',
332 'vimeo',
333 'vk',
334 'wordpress',
335 'yelp',
336 'youtube',
337 );
338
339 foreach ( $services as $service ) {
340 register_block_type(
341 'core/social-link-' . $service,
342 array(
343 'category' => 'widgets',
344 'attributes' => array(
345 'url' => array(
346 'type' => 'string',
347 ),
348 'service' => array(
349 'type' => 'string',
350 'default' => $service,
351 ),
352 'label' => array(
353 'type' => 'string',
354 ),
355 ),
356 'render_callback' => 'gutenberg_render_block_core_social_link',
357 )
358 );
359 }
360 }
361
362 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
363
364 /**
365 * Filters the default block categories array to add a new one for themes.
366 *
367 * This can be removed when plugin support requires WordPress 5.8.0+.
368 *
369 * @see https://core.trac.wordpress.org/ticket/52883
370 *
371 * @param array[] $categories The list of default block categories.
372 *
373 * @return array[] Filtered block categories.
374 */
375 function gutenberg_register_theme_block_category( $categories ) {
376 foreach ( $categories as $category ) {
377 // Skip when the category is already set in WordPress core.
378 if (
379 isset( $category['slug'] ) &&
380 'theme' === $category['slug']
381 ) {
382 return $categories;
383 }
384 }
385
386 $categories[] = array(
387 'slug' => 'theme',
388 'title' => _x( 'Theme', 'block category', 'gutenberg' ),
389 'icon' => null,
390 );
391 return $categories;
392 }
393 // This can be removed when plugin support requires WordPress 5.8.0+.
394 if ( ! function_exists( 'get_default_block_categories' ) ) {
395 add_filter( 'block_categories', 'gutenberg_register_theme_block_category' );
396 }
397
398 /**
399 * Checks whether the current block type supports the feature requested.
400 *
401 * @param WP_Block_Type $block_type Block type to check for support.
402 * @param array $feature Path of the feature to check support for.
403 * @param mixed $default Fallback value for feature support, defaults to false.
404 *
405 * @return boolean Whether or not the feature is supported.
406 */
407 function gutenberg_block_has_support( $block_type, $feature, $default = false ) {
408 $block_support = $default;
409 if ( $block_type && property_exists( $block_type, 'supports' ) ) {
410 $block_support = _wp_array_get( $block_type->supports, $feature, $default );
411 }
412
413 return true === $block_support || is_array( $block_support );
414 }
415