PluginProbe
Gutenberg / 10.4.1
Gutenberg v10.4.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.4.1, at lib/blocks.php

396 lines 11.8 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 'file',
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 'cover.php' => 'core/cover',
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-content.php' => 'core/post-content',
77 'post-date.php' => 'core/post-date',
78 'post-excerpt.php' => 'core/post-excerpt',
79 'post-featured-image.php' => 'core/post-featured-image',
80 'post-hierarchical-terms.php' => 'core/post-hierarchical-terms',
81 'post-navigation-link.php' => 'core/post-navigation-link',
82 'post-tags.php' => 'core/post-tags',
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 $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_register_style(
172 "wp-block-{$block_name}",
173 gutenberg_url( $style_path ),
174 array(),
175 filemtime( gutenberg_dir_path() . $style_path )
176 );
177 wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );
178
179 // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
180 wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $style_path );
181 }
182
183 if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
184 wp_register_style(
185 "wp-block-{$block_name}-editor",
186 gutenberg_url( $editor_style_path ),
187 array(),
188 filemtime( gutenberg_dir_path() . $editor_style_path )
189 );
190 wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
191 }
192 }
193
194 /**
195 * Change the way styles get loaded depending on their size.
196 *
197 * Optimizes performance and sustainability of styles by inlining smaller stylesheets.
198 *
199 * @return void
200 */
201 function gutenberg_maybe_inline_styles() {
202
203 $total_inline_limit = 20000;
204 /**
205 * The maximum size of inlined styles in bytes.
206 *
207 * @param int $total_inline_limit The file-size threshold, in bytes. Defaults to 20000.
208 * @return int The file-size threshold, in bytes.
209 */
210 $total_inline_limit = apply_filters( 'styles_inline_size_limit', $total_inline_limit );
211
212 global $wp_styles;
213 $styles = array();
214
215 // Build an array of styles that have a path defined.
216 foreach ( $wp_styles->queue as $handle ) {
217 if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) {
218 $styles[] = array(
219 'handle' => $handle,
220 'path' => $wp_styles->registered[ $handle ]->extra['path'],
221 'size' => filesize( $wp_styles->registered[ $handle ]->extra['path'] ),
222 );
223 }
224 }
225
226 if ( ! empty( $styles ) ) {
227 // Reorder styles array based on size.
228 usort(
229 $styles,
230 function( $a, $b ) {
231 return ( $a['size'] <= $b['size'] ) ? -1 : 1;
232 }
233 );
234
235 /**
236 * The total inlined size.
237 *
238 * On each iteration of the loop, if a style gets added inline the value of this var increases
239 * to reflect the total size of inlined styles.
240 */
241 $total_inline_size = 0;
242
243 // Loop styles.
244 foreach ( $styles as $style ) {
245
246 // Size check. Since styles are ordered by size, we can break the loop.
247 if ( $total_inline_size + $style['size'] > $total_inline_limit ) {
248 break;
249 }
250
251 // Get the styles if we don't already have them.
252 $style['css'] = file_get_contents( $style['path'] );
253
254 // Set `src` to `false` and add styles inline.
255 $wp_styles->registered[ $style['handle'] ]->src = false;
256 $wp_styles->registered[ $style['handle'] ]->extra['after'][] = $style['css'];
257
258 // Add the styles size to the $total_inline_size var.
259 $total_inline_size += (int) $style['size'];
260 }
261 }
262 }
263 add_action( 'wp_head', 'gutenberg_maybe_inline_styles', 1 );
264
265 /**
266 * Complements the implementation of block type `core/social-icon`, whether it
267 * be provided by core or the plugin, with derived block types for each
268 * "service" (WordPress, Twitter, etc.) supported by Social Links.
269 *
270 * This ensures backwards compatibility for any users running the Gutenberg
271 * plugin who have used Social Links prior to their conversion to block
272 * variations.
273 *
274 * This shim is INTENTIONALLY left out of core, as Social Links haven't yet
275 * landed there.
276 *
277 * @see https://github.com/WordPress/gutenberg/pull/19887
278 */
279 function gutenberg_register_legacy_social_link_blocks() {
280 $services = array(
281 'amazon',
282 'bandcamp',
283 'behance',
284 'chain',
285 'codepen',
286 'deviantart',
287 'dribbble',
288 'dropbox',
289 'etsy',
290 'facebook',
291 'feed',
292 'fivehundredpx',
293 'flickr',
294 'foursquare',
295 'goodreads',
296 'google',
297 'github',
298 'instagram',
299 'lastfm',
300 'linkedin',
301 'mail',
302 'mastodon',
303 'meetup',
304 'medium',
305 'pinterest',
306 'pocket',
307 'reddit',
308 'skype',
309 'snapchat',
310 'soundcloud',
311 'spotify',
312 'tumblr',
313 'twitch',
314 'twitter',
315 'vimeo',
316 'vk',
317 'wordpress',
318 'yelp',
319 'youtube',
320 );
321
322 foreach ( $services as $service ) {
323 register_block_type(
324 'core/social-link-' . $service,
325 array(
326 'category' => 'widgets',
327 'attributes' => array(
328 'url' => array(
329 'type' => 'string',
330 ),
331 'service' => array(
332 'type' => 'string',
333 'default' => $service,
334 ),
335 'label' => array(
336 'type' => 'string',
337 ),
338 ),
339 'render_callback' => 'gutenberg_render_block_core_social_link',
340 )
341 );
342 }
343 }
344
345 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
346
347 /**
348 * Filters the default block categories array to add a new one for themes.
349 *
350 * This can be removed when plugin support requires WordPress 5.8.0+.
351 *
352 * @see https://core.trac.wordpress.org/ticket/52883
353 *
354 * @param array[] $categories The list of default block categories.
355 *
356 * @return array[] Filtered block categories.
357 */
358 function gutenberg_register_theme_block_category( $categories ) {
359 foreach ( $categories as $category ) {
360 // Skip when the category is already set in WordPress core.
361 if (
362 isset( $category['slug'] ) &&
363 'theme' === $category['slug']
364 ) {
365 return $categories;
366 }
367 }
368
369 $categories[] = array(
370 'slug' => 'theme',
371 'title' => _x( 'Theme', 'block category', 'gutenberg' ),
372 'icon' => null,
373 );
374 return $categories;
375 }
376
377 add_filter( 'block_categories', 'gutenberg_register_theme_block_category' );
378
379 /**
380 * Checks whether the current block type supports the feature requested.
381 *
382 * @param WP_Block_Type $block_type Block type to check for support.
383 * @param string $feature Name of the feature to check support for.
384 * @param mixed $default Fallback value for feature support, defaults to false.
385 *
386 * @return boolean Whether or not the feature is supported.
387 */
388 function gutenberg_block_has_support( $block_type, $feature, $default = false ) {
389 $block_support = $default;
390 if ( $block_type && property_exists( $block_type, 'supports' ) ) {
391 $block_support = _wp_array_get( $block_type->supports, $feature, $default );
392 }
393
394 return true === $block_support || is_array( $block_support );
395 }
396