author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
19 | 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 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
11 |
* @since 6.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
13 |
* @param array $attributes The block attributes. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
14 |
* @param string $content The block rendered content. |
19 | 15 |
* |
16 |
* @return string Returns the cover block markup, if useFeaturedImage is true. |
|
17 |
*/ |
|
18 |
function 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'] ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
30 |
$object_position = round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%'; |
19 | 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`, |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
39 |
* and removes eventual whitespace characters between the two (typically introduced at template level) |
19 | 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(); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
51 |
if ( ! $current_featured_image ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
52 |
return $content; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
53 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
54 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
$processor = new WP_HTML_Tag_Processor( $content ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
56 |
$processor->next_tag(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
57 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
58 |
$styles = $processor->get_attribute( 'style' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
59 |
$merged_styles = ! empty( $styles ) ? $styles . ';' : ''; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
60 |
$merged_styles .= 'background-image:url(' . esc_url( $current_featured_image ) . ');'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
61 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
62 |
$processor->set_attribute( 'style', $merged_styles ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
63 |
$content = $processor->get_updated_html(); |
19 | 64 |
} |
65 |
||
66 |
return $content; |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Registers the `core/cover` block renderer on server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
* @since 6.0.0 |
19 | 73 |
*/ |
74 |
function register_block_core_cover() { |
|
75 |
register_block_type_from_metadata( |
|
76 |
__DIR__ . '/cover', |
|
77 |
array( |
|
78 |
'render_callback' => 'render_block_core_cover', |
|
79 |
) |
|
80 |
); |
|
81 |
} |
|
82 |
add_action( 'init', 'register_block_core_cover' ); |