PluginProbe
Gutenberg / 8.7.1
Gutenberg v8.7.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 8.7.1, at lib/blocks.php

431 lines 14.1 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_dir = dirname( __FILE__ ) . '/../build/block-library/blocks/';
15 if ( ! file_exists( $blocks_dir ) ) {
16 return;
17 }
18
19 $block_folders = array(
20 'audio',
21 'button',
22 'buttons',
23 'classic',
24 'code',
25 'column',
26 'columns',
27 'file',
28 'gallery',
29 'group',
30 'heading',
31 'html',
32 'image',
33 'list',
34 'media-text',
35 'missing',
36 'more',
37 'navigation-link',
38 'nextpage',
39 'paragraph',
40 'preformatted',
41 'pullquote',
42 'quote',
43 'separator',
44 'social-links',
45 'spacer',
46 'subhead',
47 'table',
48 'text-columns',
49 'verse',
50 'video',
51 'widget-area',
52 );
53
54 $block_names = array(
55 'archives.php' => 'core/archives',
56 'block.php' => 'core/block',
57 'calendar.php' => 'core/calendar',
58 'categories.php' => 'core/categories',
59 'cover.php' => 'core/cover',
60 'latest-comments.php' => 'core/latest-comments',
61 'latest-posts.php' => 'core/latest-posts',
62 'legacy-widget.php' => 'core/legacy-widget',
63 'navigation.php' => 'core/navigation',
64 'navigation-link.php' => 'core/navigation-link',
65 'rss.php' => 'core/rss',
66 'search.php' => 'core/search',
67 'shortcode.php' => 'core/shortcode',
68 'social-link.php' => 'core/social-link',
69 'tag-cloud.php' => 'core/tag-cloud',
70 );
71
72 if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' ) ) {
73 $block_names = array_merge(
74 $block_names,
75 array(
76 'post-author.php' => 'core/post-author',
77 'post-comments.php' => 'core/post-comments',
78 'post-comments-count.php' => 'core/post-comments-count',
79 'post-comments-form.php' => 'core/post-comments-form',
80 'post-content.php' => 'core/post-content',
81 'post-date.php' => 'core/post-date',
82 'post-excerpt.php' => 'core/post-excerpt',
83 'post-featured-image.php' => 'core/post-featured-image',
84 'post-tags.php' => 'core/post-tags',
85 'post-title.php' => 'core/post-title',
86 'query.php' => 'core/query',
87 'query-loop.php' => 'core/query-loop',
88 'query-pagination.php' => 'core/query-pagination',
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
97 $registry = WP_Block_Type_Registry::get_instance();
98
99 foreach ( $block_folders as $folder_name ) {
100 $block_json_file = $blocks_dir . '/' . $folder_name . '/block.json';
101 if ( ! file_exists( $block_json_file ) ) {
102 return;
103 }
104
105 // Ideally, all paths to block metadata files should be listed in
106 // WordPress core. In this place we should rather use filter
107 // to replace paths with overrides defined by the plugin.
108 $metadata = json_decode( file_get_contents( $block_json_file ), true );
109 if ( ! is_array( $metadata ) || ! $metadata['name'] ) {
110 return false;
111 }
112
113 if ( $registry->is_registered( $metadata['name'] ) ) {
114 $registry->unregister( $metadata['name'] );
115 }
116
117 register_block_type_from_metadata( $block_json_file );
118 }
119
120 foreach ( $block_names as $file => $block_names ) {
121 if ( ! file_exists( $blocks_dir . $file ) ) {
122 return;
123 }
124
125 if ( is_string( $block_names ) ) {
126 if ( $registry->is_registered( $block_names ) ) {
127 $registry->unregister( $block_names );
128 }
129 } elseif ( is_array( $block_names ) ) {
130 foreach ( $block_names as $block_name ) {
131 if ( $registry->is_registered( $block_name ) ) {
132 $registry->unregister( $block_name );
133 }
134 }
135 }
136
137 require $blocks_dir . $file;
138 }
139 }
140 add_action( 'init', 'gutenberg_reregister_core_block_types' );
141
142 /**
143 * Complements the implementation of block type `core/social-icon`, whether it
144 * be provided by core or the plugin, with derived block types for each
145 * "service" (WordPress, Twitter, etc.) supported by Social Links.
146 *
147 * This ensures backwards compatibility for any users running the Gutenberg
148 * plugin who have used Social Links prior to their conversion to block
149 * variations.
150 *
151 * This shim is INTENTIONALLY left out of core, as Social Links haven't yet
152 * landed there.
153 *
154 * @see https://github.com/WordPress/gutenberg/pull/19887
155 */
156 function gutenberg_register_legacy_social_link_blocks() {
157 $services = array(
158 'amazon',
159 'bandcamp',
160 'behance',
161 'chain',
162 'codepen',
163 'deviantart',
164 'dribbble',
165 'dropbox',
166 'etsy',
167 'facebook',
168 'feed',
169 'fivehundredpx',
170 'flickr',
171 'foursquare',
172 'goodreads',
173 'google',
174 'github',
175 'instagram',
176 'lastfm',
177 'linkedin',
178 'mail',
179 'mastodon',
180 'meetup',
181 'medium',
182 'pinterest',
183 'pocket',
184 'reddit',
185 'skype',
186 'snapchat',
187 'soundcloud',
188 'spotify',
189 'tumblr',
190 'twitch',
191 'twitter',
192 'vimeo',
193 'vk',
194 'wordpress',
195 'yelp',
196 'youtube',
197 );
198
199 foreach ( $services as $service ) {
200 register_block_type(
201 'core/social-link-' . $service,
202 array(
203 'category' => 'widgets',
204 'attributes' => array(
205 'url' => array(
206 'type' => 'string',
207 ),
208 'service' => array(
209 'type' => 'string',
210 'default' => $service,
211 ),
212 'label' => array(
213 'type' => 'string',
214 ),
215 ),
216 'render_callback' => 'gutenberg_render_block_core_social_link',
217 )
218 );
219 }
220 }
221 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
222
223 /**
224 * Renders the classNames and styles for blocks
225 *
226 * @param string $block_content Output of the current block.
227 * @param array $block Block Object.
228 * @return string New block output.
229 */
230 function gutenberg_experimental_apply_classnames_and_styles( $block_content, $block ) {
231 if ( ! isset( $block['attrs'] ) ) {
232 return $block_content;
233 }
234
235 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
236 // If no render_callback, assume styles have been previously handled.
237 if ( ! $block_type || ! $block_type->render_callback || empty( $block_type->supports ) ) {
238 return $block_content;
239 }
240
241 // Check what style features the block supports.
242 $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports );
243
244 $attributes = array();
245 $attributes = gutenberg_experimental_build_css_colors( $attributes, $block['attrs'], $supports );
246 $attributes = gutenberg_experimental_build_css_typography( $attributes, $block['attrs'], $supports );
247 $attributes = gutenberg_build_css_block_alignment( $attributes, $block['attrs'], $supports );
248
249 if ( ! count( $attributes ) ) {
250 return $block_content;
251 }
252
253 $dom = new DOMDocument( '1.0', 'utf-8' );
254
255 // Suppress warnings from this method from polluting the front-end.
256 // @codingStandardsIgnoreStart
257 if ( ! @$dom->loadHTML( mb_convert_encoding( $block_content, 'HTML-ENTITIES', 'UTF-8' ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ) ) {
258 // @codingStandardsIgnoreEnd
259 return $block_content;
260 }
261
262 $xpath = new DOMXPath( $dom );
263 $block_root = $xpath->query( '/*' )[0];
264
265 if ( empty( $block_root ) ) {
266 return $block_content;
267 }
268
269 // Some inline styles may be added without ending ';', add this if necessary.
270 $current_styles = trim( $block_root->getAttribute( 'style' ), ' ' );
271 if ( strlen( $current_styles ) > 0 && substr( $current_styles, -1 ) !== ';' ) {
272 $current_styles = $current_styles . ';';
273 };
274
275 // Merge and dedupe new and existing classes and styles.
276 $classes_to_add = esc_attr( implode( ' ', array_key_exists( 'css_classes', $attributes ) ? $attributes['css_classes'] : array() ) );
277 $styles_to_add = esc_attr( implode( ' ', array_key_exists( 'inline_styles', $attributes ) ? $attributes['inline_styles'] : array() ) );
278 $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . $classes_to_add ) ) );
279 $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . ' ' . $styles_to_add ) ) );
280
281 // Apply new styles and classes.
282 if ( ! empty( $new_classes ) ) {
283 $block_root->setAttribute( 'class', $new_classes );
284 }
285
286 if ( ! empty( $new_styles ) ) {
287 $block_root->setAttribute( 'style', $new_styles );
288 }
289
290 return mb_convert_encoding( $dom->saveHtml(), 'UTF-8', 'HTML-ENTITIES' );
291 }
292 add_filter( 'render_block', 'gutenberg_experimental_apply_classnames_and_styles', 10, 2 );
293
294 /**
295 * Add CSS classes and inline styles for colors to the incoming attributes array.
296 * This will be applied to the block markup in the front-end.
297 *
298 * @param array $attributes comprehensive list of attributes to be applied.
299 * @param array $block_attributes block attributes.
300 * @param array $supports style features the block attributes.
301 * @return array Colors CSS classes and inline styles.
302 */
303 function gutenberg_experimental_build_css_colors( $attributes, $block_attributes, $supports ) {
304 // Text Colors.
305 // Check support for text colors.
306 if ( in_array( 'color', $supports, true ) ) {
307 $has_named_text_color = array_key_exists( 'textColor', $block_attributes );
308 $has_custom_text_color = isset( $block_attributes['style']['color']['text'] );
309
310 // Apply required generic class.
311 if ( $has_custom_text_color || $has_named_text_color ) {
312 $attributes['css_classes'][] = 'has-text-color';
313 }
314 // Apply color class or inline style.
315 if ( $has_named_text_color ) {
316 $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_attributes['textColor'] );
317 } elseif ( $has_custom_text_color ) {
318 $attributes['inline_styles'][] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
319 }
320 }
321
322 // Link Colors.
323 if ( in_array( '--wp--style--color--link', $supports, true ) ) {
324 $has_link_color = isset( $block_attributes['style']['color']['link'] );
325 // Apply required class and style.
326 if ( $has_link_color ) {
327 $attributes['css_classes'][] = 'has-link-color';
328 // If link is a named color.
329 if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) {
330 // Get the name from the string and add proper styles.
331 $index_to_splice = strrpos( $block_attributes['style']['color']['link'], '|' ) + 1;
332 $link_color_name = substr( $block_attributes['style']['color']['link'], $index_to_splice );
333 $attributes['inline_styles'][] = sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name );
334 } else {
335 $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] );
336 }
337 }
338 }
339
340 // Background Colors.
341 if ( in_array( 'background-color', $supports, true ) ) {
342 $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes );
343 $has_custom_background_color = isset( $block_attributes['style']['color']['background'] );
344
345 // Apply required background class.
346 if ( $has_custom_background_color || $has_named_background_color ) {
347 $attributes['css_classes'][] = 'has-background';
348 }
349 // Apply background color classes or styles.
350 if ( $has_named_background_color ) {
351 $attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] );
352 } elseif ( $has_custom_background_color ) {
353 $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
354 }
355 }
356
357 // Gradients.
358 if ( in_array( 'background', $supports, true ) ) {
359 $has_named_gradient = array_key_exists( 'gradient', $block_attributes );
360 $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );
361
362 if ( $has_named_gradient || $has_custom_gradient ) {
363 $attributes['css_classes'][] = 'has-background';
364 }
365 // Apply required background class.
366 if ( $has_named_gradient ) {
367 $attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] );
368 } elseif ( $has_custom_gradient ) {
369 $attributes['inline_styles'][] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
370 }
371 }
372
373 return $attributes;
374 }
375
376 /**
377 * Add CSS classes and inline styles for font sizes to the incoming attributes array.
378 * This will be applied to the block markup in the front-end.
379 *
380 * @param array $attributes comprehensive list of attributes to be applied.
381 * @param array $block_attributes block attributes.
382 * @param array $supports style features the block attributes.
383 * @return array Font size CSS classes and inline styles.
384 */
385 function gutenberg_experimental_build_css_typography( $attributes, $block_attributes, $supports ) {
386 // Font Size.
387 if ( in_array( 'font-size', $supports, true ) ) {
388 $has_named_font_size = array_key_exists( 'fontSize', $block_attributes );
389 $has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] );
390
391 // Apply required class or style.
392 if ( $has_named_font_size ) {
393 $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] );
394 } elseif ( $has_custom_font_size ) {
395 $attributes['inline_styles'][] = sprintf( 'font-size: %spx;', $block_attributes['style']['typography']['fontSize'] );
396 }
397 }
398
399 // Line Height.
400 if ( in_array( 'line-height', $supports, true ) ) {
401 $has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] );
402 // Add the style (no classes for line-height).
403 if ( $has_line_height ) {
404 $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] );
405 }
406 }
407
408 return $attributes;
409 }
410
411 /**
412 * Add CSS classes for block alignment to the incoming attributes array.
413 * This will be applied to the block markup in the front-end.
414 *
415 * @param array $attributes comprehensive list of attributes to be applied.
416 * @param array $block_attributes block attributes.
417 * @param array $supports style features the block attributes.
418 * @return array Block alignment CSS classes and inline styles.
419 */
420 function gutenberg_build_css_block_alignment( $attributes, $block_attributes, $supports ) {
421 if ( in_array( 'block-align', $supports, true ) ) {
422 $has_block_alignment = array_key_exists( 'align', $block_attributes );
423
424 if ( $has_block_alignment ) {
425 $attributes['css_classes'][] = sprintf( 'align%s', $block_attributes['align'] );
426 }
427 }
428
429 return $attributes;
430 }
431