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

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