PluginProbe
Gutenberg / 10.2.0
Gutenberg v10.2.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.2.0, at lib/blocks.php

352 lines 10.3 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_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 // 'table-of-contents.php' => 'core/table-of-contents',
93 'template-part.php' => 'core/template-part',
94 )
95 ),
96 ),
97 __DIR__ . '/../build/edit-widgets/blocks/' => array(
98 'block_folders' => array(
99 'legacy-widget',
100 'widget-area',
101 ),
102 'block_names' => array(
103 'legacy-widget.php' => 'core/legacy-widget',
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 => $block_names ) {
134 if ( ! file_exists( $blocks_dir . $file ) ) {
135 return;
136 }
137
138 if ( is_string( $block_names ) ) {
139 if ( $registry->is_registered( $block_names ) ) {
140 $registry->unregister( $block_names );
141 }
142 gutenberg_register_core_block_styles( $block_names );
143 } elseif ( is_array( $block_names ) ) {
144 foreach ( $block_names as $block_name ) {
145 if ( $registry->is_registered( $block_name ) ) {
146 $registry->unregister( $block_name );
147 }
148 gutenberg_register_core_block_styles( $block_name );
149 }
150 }
151
152 require $blocks_dir . $file;
153 }
154 }
155 }
156
157 add_action( 'init', 'gutenberg_reregister_core_block_types' );
158
159 /**
160 * Registers block styles for a core block.
161 *
162 * @param string $block_name The block-name.
163 *
164 * @return void
165 */
166 function gutenberg_register_core_block_styles( $block_name ) {
167 if ( ! gutenberg_should_load_separate_block_assets() ) {
168 return;
169 }
170
171 $block_name = str_replace( 'core/', '', $block_name );
172
173 $style_path = "build/block-library/blocks/$block_name/style.css";
174 $editor_style_path = "build/block-library/blocks/$block_name/style-editor.css";
175
176 if ( file_exists( gutenberg_dir_path() . $style_path ) ) {
177 wp_register_style(
178 "wp-block-{$block_name}",
179 gutenberg_url( $style_path ),
180 array(),
181 filemtime( gutenberg_dir_path() . $style_path )
182 );
183 wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );
184
185 // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
186 wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $style_path );
187 }
188
189 if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
190 wp_register_style(
191 "wp-block-{$block_name}-editor",
192 gutenberg_url( $editor_style_path ),
193 array(),
194 filemtime( gutenberg_dir_path() . $editor_style_path )
195 );
196 wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
197 }
198 }
199
200 /**
201 * Change the way styles get loaded depending on their size.
202 *
203 * Optimizes performance and sustainability of styles by inlining smaller stylesheets.
204 *
205 * @return void
206 */
207 function gutenberg_maybe_inline_styles() {
208
209 $total_inline_limit = 20000;
210 /**
211 * The maximum size of inlined styles in bytes.
212 *
213 * @param int $total_inline_limit The file-size threshold, in bytes. Defaults to 20000.
214 * @return int The file-size threshold, in bytes.
215 */
216 $total_inline_limit = apply_filters( 'styles_inline_size_limit', $total_inline_limit );
217
218 global $wp_styles;
219 $styles = array();
220
221 // Build an array of styles that have a path defined.
222 foreach ( $wp_styles->queue as $handle ) {
223 if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) {
224 $styles[] = array(
225 'handle' => $handle,
226 'path' => $wp_styles->registered[ $handle ]->extra['path'],
227 'size' => filesize( $wp_styles->registered[ $handle ]->extra['path'] ),
228 );
229 }
230 }
231
232 if ( ! empty( $styles ) ) {
233 // Reorder styles array based on size.
234 usort(
235 $styles,
236 function( $a, $b ) {
237 return ( $a['size'] <= $b['size'] ) ? -1 : 1;
238 }
239 );
240
241 /**
242 * The total inlined size.
243 *
244 * On each iteration of the loop, if a style gets added inline the value of this var increases
245 * to reflect the total size of inlined styles.
246 */
247 $total_inline_size = 0;
248
249 // Loop styles.
250 foreach ( $styles as $style ) {
251
252 // Size check. Since styles are ordered by size, we can break the loop.
253 if ( $total_inline_size + $style['size'] > $total_inline_limit ) {
254 break;
255 }
256
257 // Get the styles if we don't already have them.
258 $style['css'] = file_get_contents( $style['path'] );
259
260 // Set `src` to `false` and add styles inline.
261 $wp_styles->registered[ $style['handle'] ]->src = false;
262 $wp_styles->registered[ $style['handle'] ]->extra['after'][] = $style['css'];
263
264 // Add the styles size to the $total_inline_size var.
265 $total_inline_size += (int) $style['size'];
266 }
267 }
268 }
269 add_action( 'wp_head', 'gutenberg_maybe_inline_styles', 1 );
270
271 /**
272 * Complements the implementation of block type `core/social-icon`, whether it
273 * be provided by core or the plugin, with derived block types for each
274 * "service" (WordPress, Twitter, etc.) supported by Social Links.
275 *
276 * This ensures backwards compatibility for any users running the Gutenberg
277 * plugin who have used Social Links prior to their conversion to block
278 * variations.
279 *
280 * This shim is INTENTIONALLY left out of core, as Social Links haven't yet
281 * landed there.
282 *
283 * @see https://github.com/WordPress/gutenberg/pull/19887
284 */
285 function gutenberg_register_legacy_social_link_blocks() {
286 $services = array(
287 'amazon',
288 'bandcamp',
289 'behance',
290 'chain',
291 'codepen',
292 'deviantart',
293 'dribbble',
294 'dropbox',
295 'etsy',
296 'facebook',
297 'feed',
298 'fivehundredpx',
299 'flickr',
300 'foursquare',
301 'goodreads',
302 'google',
303 'github',
304 'instagram',
305 'lastfm',
306 'linkedin',
307 'mail',
308 'mastodon',
309 'meetup',
310 'medium',
311 'pinterest',
312 'pocket',
313 'reddit',
314 'skype',
315 'snapchat',
316 'soundcloud',
317 'spotify',
318 'tumblr',
319 'twitch',
320 'twitter',
321 'vimeo',
322 'vk',
323 'wordpress',
324 'yelp',
325 'youtube',
326 );
327
328 foreach ( $services as $service ) {
329 register_block_type(
330 'core/social-link-' . $service,
331 array(
332 'category' => 'widgets',
333 'attributes' => array(
334 'url' => array(
335 'type' => 'string',
336 ),
337 'service' => array(
338 'type' => 'string',
339 'default' => $service,
340 ),
341 'label' => array(
342 'type' => 'string',
343 ),
344 ),
345 'render_callback' => 'gutenberg_render_block_core_social_link',
346 )
347 );
348 }
349 }
350
351 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
352