author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
18 | 1 |
<?php |
2 |
/** |
|
3 |
* Server-side rendering of the `core/post-template` block. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
||
8 |
/** |
|
19 | 9 |
* Determines whether a block list contains a block that uses the featured image. |
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 |
* |
19 | 13 |
* @param WP_Block_List $inner_blocks Inner block instance. |
14 |
* |
|
15 |
* @return bool Whether the block list contains a block that uses the featured image. |
|
16 |
*/ |
|
17 |
function block_core_post_template_uses_featured_image( $inner_blocks ) { |
|
18 |
foreach ( $inner_blocks as $block ) { |
|
19 |
if ( 'core/post-featured-image' === $block->name ) { |
|
20 |
return true; |
|
21 |
} |
|
22 |
if ( |
|
23 |
'core/cover' === $block->name && |
|
24 |
! empty( $block->attributes['useFeaturedImage'] ) |
|
25 |
) { |
|
26 |
return true; |
|
27 |
} |
|
28 |
if ( $block->inner_blocks && block_core_post_template_uses_featured_image( $block->inner_blocks ) ) { |
|
29 |
return true; |
|
30 |
} |
|
31 |
} |
|
32 |
||
33 |
return false; |
|
34 |
} |
|
35 |
||
36 |
/** |
|
18 | 37 |
* Renders the `core/post-template` block on the server. |
38 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
39 |
* @since 6.3.0 Changed render_block_context priority to `1`. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
40 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
41 |
* @global WP_Query $wp_query WordPress Query object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
42 |
* |
18 | 43 |
* @param array $attributes Block attributes. |
44 |
* @param string $content Block default content. |
|
45 |
* @param WP_Block $block Block instance. |
|
46 |
* |
|
47 |
* @return string Returns the output of the query, structured using the layout defined by the block's inner blocks. |
|
48 |
*/ |
|
49 |
function render_block_core_post_template( $attributes, $content, $block ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
50 |
$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
51 |
$enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination']; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
52 |
$page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; |
18 | 53 |
|
19 | 54 |
// Use global query if needed. |
18 | 55 |
$use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); |
56 |
if ( $use_global_query ) { |
|
57 |
global $wp_query; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
58 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
59 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
60 |
* If already in the main query loop, duplicate the query instance to not tamper with the main instance. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
61 |
* Since this is a nested query, it should start at the beginning, therefore rewind posts. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
62 |
* Otherwise, the main query loop has not started yet and this block is responsible for doing so. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
63 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
64 |
if ( in_the_loop() ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
65 |
$query = clone $wp_query; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
66 |
$query->rewind_posts(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
67 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
68 |
$query = $wp_query; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
} |
19 | 70 |
} else { |
71 |
$query_args = build_query_vars_from_query_block( $block, $page ); |
|
72 |
$query = new WP_Query( $query_args ); |
|
18 | 73 |
} |
74 |
||
75 |
if ( ! $query->have_posts() ) { |
|
76 |
return ''; |
|
77 |
} |
|
78 |
||
19 | 79 |
if ( block_core_post_template_uses_featured_image( $block->inner_blocks ) ) { |
80 |
update_post_thumbnail_cache( $query ); |
|
81 |
} |
|
82 |
||
18 | 83 |
$classnames = ''; |
84 |
if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) { |
|
85 |
if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) { |
|
86 |
$classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}"; |
|
87 |
} |
|
88 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
89 |
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
90 |
$classnames .= ' has-link-color'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
91 |
} |
18 | 92 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
93 |
// Ensure backwards compatibility by flagging the number of columns via classname when using grid layout. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
94 |
if ( isset( $attributes['layout']['type'] ) && 'grid' === $attributes['layout']['type'] && ! empty( $attributes['layout']['columnCount'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
$classnames .= ' ' . sanitize_title( 'columns-' . $attributes['layout']['columnCount'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
97 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
98 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => trim( $classnames ) ) ); |
18 | 99 |
|
100 |
$content = ''; |
|
101 |
while ( $query->have_posts() ) { |
|
102 |
$query->the_post(); |
|
19 | 103 |
|
104 |
// Get an instance of the current Post Template block. |
|
105 |
$block_instance = $block->parsed_block; |
|
106 |
||
107 |
// Set the block name to one that does not correspond to an existing registered block. |
|
108 |
// This ensures that for the inner instances of the Post Template block, we do not render any block supports. |
|
109 |
$block_instance['blockName'] = 'core/null'; |
|
110 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
$post_id = get_the_ID(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
112 |
$post_type = get_post_type(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
$filter_block_context = static function ( $context ) use ( $post_id, $post_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
114 |
$context['postType'] = $post_type; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
$context['postId'] = $post_id; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
return $context; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
117 |
}; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
119 |
// Use an early priority to so that other 'render_block_context' filters have access to the values. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
120 |
add_filter( 'render_block_context', $filter_block_context, 1 ); |
19 | 121 |
// Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling |
122 |
// `render_callback` and ensure that no wrapper markup is included. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
123 |
$block_content = ( new WP_Block( $block_instance ) )->render( array( 'dynamic' => false ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
124 |
remove_filter( 'render_block_context', $filter_block_context, 1 ); |
19 | 125 |
|
126 |
// Wrap the render inner blocks in a `li` element with the appropriate post classes. |
|
127 |
$post_classes = implode( ' ', get_post_class( 'wp-block-post' ) ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
128 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
129 |
$inner_block_directives = $enhanced_pagination ? ' data-wp-key="post-template-item-' . $post_id . '"' : ''; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
130 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
$content .= '<li' . $inner_block_directives . ' class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>'; |
18 | 132 |
} |
133 |
||
19 | 134 |
/* |
135 |
* Use this function to restore the context of the template tags |
|
136 |
* from a secondary query loop back to the main query loop. |
|
137 |
* Since we use two custom loops, it's safest to always restore. |
|
138 |
*/ |
|
18 | 139 |
wp_reset_postdata(); |
140 |
||
141 |
return sprintf( |
|
142 |
'<ul %1$s>%2$s</ul>', |
|
143 |
$wrapper_attributes, |
|
144 |
$content |
|
145 |
); |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Registers the `core/post-template` block on the server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
150 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
* @since 5.8.0 |
18 | 152 |
*/ |
153 |
function register_block_core_post_template() { |
|
154 |
register_block_type_from_metadata( |
|
155 |
__DIR__ . '/post-template', |
|
156 |
array( |
|
157 |
'render_callback' => 'render_block_core_post_template', |
|
158 |
'skip_inner_blocks' => true, |
|
159 |
) |
|
160 |
); |
|
161 |
} |
|
162 |
add_action( 'init', 'register_block_core_post_template' ); |