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

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