PluginProbe
Gutenberg / 18.9.0
Gutenberg v18.9.0
24.0.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 All 403 releases
gutenberg / build / block-library / blocks / cover.php

cover.php in Gutenberg 18.9.0, at build/block-library/blocks/cover.php

83 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/cover` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/cover` block on server.
10 *
11 * @since 6.0.0
12 *
13 * @param array $attributes The block attributes.
14 * @param string $content The block rendered content.
15 *
16 * @return string Returns the cover block markup, if useFeaturedImage is true.
17 */
18 function gutenberg_render_block_core_cover( $attributes, $content ) {
19 if ( 'image' !== $attributes['backgroundType'] || false === $attributes['useFeaturedImage'] ) {
20 return $content;
21 }
22
23 if ( ! ( $attributes['hasParallax'] || $attributes['isRepeated'] ) ) {
24 $attr = array(
25 'class' => 'wp-block-cover__image-background',
26 'data-object-fit' => 'cover',
27 );
28
29 if ( isset( $attributes['focalPoint'] ) ) {
30 $object_position = round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%';
31 $attr['data-object-position'] = $object_position;
32 $attr['style'] = 'object-position: ' . $object_position;
33 }
34
35 $image = get_the_post_thumbnail( null, 'post-thumbnail', $attr );
36
37 /*
38 * Inserts the featured image between the (1st) cover 'background' `span` and 'inner_container' `div`,
39 * and removes eventual whitespace characters between the two (typically introduced at template level)
40 */
41 $inner_container_start = '/<div\b[^>]+wp-block-cover__inner-container[\s|"][^>]*>/U';
42 if ( 1 === preg_match( $inner_container_start, $content, $matches, PREG_OFFSET_CAPTURE ) ) {
43 $offset = $matches[0][1];
44 $content = substr( $content, 0, $offset ) . $image . substr( $content, $offset );
45 }
46 } else {
47 if ( in_the_loop() ) {
48 update_post_thumbnail_cache();
49 }
50 $current_featured_image = get_the_post_thumbnail_url();
51 if ( ! $current_featured_image ) {
52 return $content;
53 }
54
55 $processor = new WP_HTML_Tag_Processor( $content );
56 $processor->next_tag();
57
58 $styles = $processor->get_attribute( 'style' );
59 $merged_styles = ! empty( $styles ) ? $styles . ';' : '';
60 $merged_styles .= 'background-image:url(' . esc_url( $current_featured_image ) . ');';
61
62 $processor->set_attribute( 'style', $merged_styles );
63 $content = $processor->get_updated_html();
64 }
65
66 return $content;
67 }
68
69 /**
70 * Registers the `core/cover` block renderer on server.
71 *
72 * @since 6.0.0
73 */
74 function gutenberg_register_block_core_cover() {
75 register_block_type_from_metadata(
76 __DIR__ . '/cover',
77 array(
78 'render_callback' => 'gutenberg_render_block_core_cover',
79 )
80 );
81 }
82 add_action( 'init', 'gutenberg_register_block_core_cover', 20 );
83