6 */ |
6 */ |
7 |
7 |
8 /** |
8 /** |
9 * Renders the `core/cover` block on server. |
9 * Renders the `core/cover` block on server. |
10 * |
10 * |
11 * @param array $attributes The block attributes. |
11 * @since 6.0.0 |
12 * @param array $content The block rendered content. |
12 * |
|
13 * @param array $attributes The block attributes. |
|
14 * @param string $content The block rendered content. |
13 * |
15 * |
14 * @return string Returns the cover block markup, if useFeaturedImage is true. |
16 * @return string Returns the cover block markup, if useFeaturedImage is true. |
15 */ |
17 */ |
16 function render_block_core_cover( $attributes, $content ) { |
18 function render_block_core_cover( $attributes, $content ) { |
17 if ( 'image' !== $attributes['backgroundType'] || false === $attributes['useFeaturedImage'] ) { |
19 if ( 'image' !== $attributes['backgroundType'] || false === $attributes['useFeaturedImage'] ) { |
23 'class' => 'wp-block-cover__image-background', |
25 'class' => 'wp-block-cover__image-background', |
24 'data-object-fit' => 'cover', |
26 'data-object-fit' => 'cover', |
25 ); |
27 ); |
26 |
28 |
27 if ( isset( $attributes['focalPoint'] ) ) { |
29 if ( isset( $attributes['focalPoint'] ) ) { |
28 $object_position = round( $attributes['focalPoint']['x'] * 100 ) . '%' . ' ' . round( $attributes['focalPoint']['y'] * 100 ) . '%'; |
30 $object_position = round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%'; |
29 $attr['data-object-position'] = $object_position; |
31 $attr['data-object-position'] = $object_position; |
30 $attr['style'] = 'object-position: ' . $object_position; |
32 $attr['style'] = 'object-position: ' . $object_position; |
31 } |
33 } |
32 |
34 |
33 $image = get_the_post_thumbnail( null, 'post-thumbnail', $attr ); |
35 $image = get_the_post_thumbnail( null, 'post-thumbnail', $attr ); |
34 |
36 |
35 /* |
37 /* |
36 * Inserts the featured image between the (1st) cover 'background' `span` and 'inner_container' `div`, |
38 * Inserts the featured image between the (1st) cover 'background' `span` and 'inner_container' `div`, |
37 * and removes eventual withespace characters between the two (typically introduced at template level) |
39 * and removes eventual whitespace characters between the two (typically introduced at template level) |
38 */ |
40 */ |
39 $inner_container_start = '/<div\b[^>]+wp-block-cover__inner-container[\s|"][^>]*>/U'; |
41 $inner_container_start = '/<div\b[^>]+wp-block-cover__inner-container[\s|"][^>]*>/U'; |
40 if ( 1 === preg_match( $inner_container_start, $content, $matches, PREG_OFFSET_CAPTURE ) ) { |
42 if ( 1 === preg_match( $inner_container_start, $content, $matches, PREG_OFFSET_CAPTURE ) ) { |
41 $offset = $matches[0][1]; |
43 $offset = $matches[0][1]; |
42 $content = substr( $content, 0, $offset ) . $image . substr( $content, $offset ); |
44 $content = substr( $content, 0, $offset ) . $image . substr( $content, $offset ); |
44 } else { |
46 } else { |
45 if ( in_the_loop() ) { |
47 if ( in_the_loop() ) { |
46 update_post_thumbnail_cache(); |
48 update_post_thumbnail_cache(); |
47 } |
49 } |
48 $current_featured_image = get_the_post_thumbnail_url(); |
50 $current_featured_image = get_the_post_thumbnail_url(); |
49 $content = preg_replace( |
51 if ( ! $current_featured_image ) { |
50 '/class=\".*?\"/', |
52 return $content; |
51 '${0} style="background-image:url(' . esc_url( $current_featured_image ) . ')"', |
53 } |
52 $content, |
54 |
53 1 |
55 $processor = new WP_HTML_Tag_Processor( $content ); |
54 ); |
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(); |
55 } |
64 } |
56 |
65 |
57 return $content; |
66 return $content; |
58 } |
67 } |
59 |
68 |
60 /** |
69 /** |
61 * Registers the `core/cover` block renderer on server. |
70 * Registers the `core/cover` block renderer on server. |
|
71 * |
|
72 * @since 6.0.0 |
62 */ |
73 */ |
63 function register_block_core_cover() { |
74 function register_block_core_cover() { |
64 register_block_type_from_metadata( |
75 register_block_type_from_metadata( |
65 __DIR__ . '/cover', |
76 __DIR__ . '/cover', |
66 array( |
77 array( |