PluginProbe
Gutenberg / 10.5.4
Gutenberg v10.5.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.5.4, at lib/blocks.php

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