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-- |
9 | 1 |
<?php |
2 |
/** |
|
3 |
* Server-side rendering of the `core/latest-posts` block. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
||
8 |
/** |
|
16 | 9 |
* The excerpt length set by the Latest Posts core block |
10 |
* set at render time and used by the block itself. |
|
11 |
* |
|
12 |
* @var int |
|
13 |
*/ |
|
18 | 14 |
global $block_core_latest_posts_excerpt_length; |
16 | 15 |
$block_core_latest_posts_excerpt_length = 0; |
16 |
||
17 |
/** |
|
18 |
* Callback for the excerpt_length filter used by |
|
19 |
* the Latest Posts block at render time. |
|
20 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
21 |
* @since 5.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
22 |
* |
16 | 23 |
* @return int Returns the global $block_core_latest_posts_excerpt_length variable |
24 |
* to allow the excerpt_length filter respect the Latest Block setting. |
|
25 |
*/ |
|
26 |
function block_core_latest_posts_get_excerpt_length() { |
|
27 |
global $block_core_latest_posts_excerpt_length; |
|
28 |
return $block_core_latest_posts_excerpt_length; |
|
29 |
} |
|
30 |
||
31 |
/** |
|
9 | 32 |
* Renders the `core/latest-posts` block on server. |
33 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
34 |
* @since 5.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
35 |
* |
9 | 36 |
* @param array $attributes The block attributes. |
37 |
* |
|
38 |
* @return string Returns the post content with latest posts added. |
|
39 |
*/ |
|
40 |
function render_block_core_latest_posts( $attributes ) { |
|
16 | 41 |
global $post, $block_core_latest_posts_excerpt_length; |
42 |
||
9 | 43 |
$args = array( |
19 | 44 |
'posts_per_page' => $attributes['postsToShow'], |
45 |
'post_status' => 'publish', |
|
46 |
'order' => $attributes['order'], |
|
47 |
'orderby' => $attributes['orderBy'], |
|
48 |
'ignore_sticky_posts' => true, |
|
49 |
'no_found_rows' => true, |
|
9 | 50 |
); |
51 |
||
16 | 52 |
$block_core_latest_posts_excerpt_length = $attributes['excerptLength']; |
53 |
add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 ); |
|
54 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
if ( ! empty( $attributes['categories'] ) ) { |
16 | 56 |
$args['category__in'] = array_column( $attributes['categories'], 'id' ); |
57 |
} |
|
58 |
if ( isset( $attributes['selectedAuthor'] ) ) { |
|
59 |
$args['author'] = $attributes['selectedAuthor']; |
|
9 | 60 |
} |
61 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
62 |
$query = new WP_Query(); |
19 | 63 |
$recent_posts = $query->query( $args ); |
64 |
||
65 |
if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) { |
|
66 |
update_post_thumbnail_cache( $query ); |
|
67 |
} |
|
9 | 68 |
|
69 |
$list_items_markup = ''; |
|
70 |
||
71 |
foreach ( $recent_posts as $post ) { |
|
18 | 72 |
$post_link = esc_url( get_permalink( $post ) ); |
19 | 73 |
$title = get_the_title( $post ); |
74 |
||
75 |
if ( ! $title ) { |
|
76 |
$title = __( '(no title)' ); |
|
77 |
} |
|
16 | 78 |
|
79 |
$list_items_markup .= '<li>'; |
|
80 |
||
81 |
if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) { |
|
82 |
$image_style = ''; |
|
83 |
if ( isset( $attributes['featuredImageSizeWidth'] ) ) { |
|
84 |
$image_style .= sprintf( 'max-width:%spx;', $attributes['featuredImageSizeWidth'] ); |
|
85 |
} |
|
86 |
if ( isset( $attributes['featuredImageSizeHeight'] ) ) { |
|
87 |
$image_style .= sprintf( 'max-height:%spx;', $attributes['featuredImageSizeHeight'] ); |
|
88 |
} |
|
89 |
||
90 |
$image_classes = 'wp-block-latest-posts__featured-image'; |
|
91 |
if ( isset( $attributes['featuredImageAlign'] ) ) { |
|
92 |
$image_classes .= ' align' . $attributes['featuredImageAlign']; |
|
93 |
} |
|
94 |
||
18 | 95 |
$featured_image = get_the_post_thumbnail( |
96 |
$post, |
|
97 |
$attributes['featuredImageSizeSlug'], |
|
98 |
array( |
|
19 | 99 |
'style' => esc_attr( $image_style ), |
18 | 100 |
) |
101 |
); |
|
102 |
if ( $attributes['addLinkToFeaturedImage'] ) { |
|
103 |
$featured_image = sprintf( |
|
19 | 104 |
'<a href="%1$s" aria-label="%2$s">%3$s</a>', |
105 |
esc_url( $post_link ), |
|
106 |
esc_attr( $title ), |
|
18 | 107 |
$featured_image |
108 |
); |
|
109 |
} |
|
16 | 110 |
$list_items_markup .= sprintf( |
111 |
'<div class="%1$s">%2$s</div>', |
|
19 | 112 |
esc_attr( $image_classes ), |
18 | 113 |
$featured_image |
16 | 114 |
); |
115 |
} |
|
116 |
||
9 | 117 |
$list_items_markup .= sprintf( |
19 | 118 |
'<a class="wp-block-latest-posts__post-title" href="%1$s">%2$s</a>', |
119 |
esc_url( $post_link ), |
|
9 | 120 |
$title |
121 |
); |
|
122 |
||
16 | 123 |
if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) { |
124 |
$author_display_name = get_the_author_meta( 'display_name', $post->post_author ); |
|
125 |
||
126 |
/* translators: byline. %s: current author. */ |
|
127 |
$byline = sprintf( __( 'by %s' ), $author_display_name ); |
|
128 |
||
129 |
if ( ! empty( $author_display_name ) ) { |
|
130 |
$list_items_markup .= sprintf( |
|
131 |
'<div class="wp-block-latest-posts__post-author">%1$s</div>', |
|
19 | 132 |
$byline |
16 | 133 |
); |
134 |
} |
|
135 |
} |
|
136 |
||
9 | 137 |
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) { |
138 |
$list_items_markup .= sprintf( |
|
139 |
'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>', |
|
140 |
esc_attr( get_the_date( 'c', $post ) ), |
|
19 | 141 |
get_the_date( '', $post ) |
9 | 142 |
); |
143 |
} |
|
144 |
||
16 | 145 |
if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent'] |
146 |
&& isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) { |
|
147 |
||
148 |
$trimmed_excerpt = get_the_excerpt( $post ); |
|
149 |
||
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 |
* Adds a "Read more" link with screen reader text. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
152 |
* […] is the default excerpt ending from wp_trim_excerpt() in Core. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
153 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
154 |
if ( str_ends_with( $trimmed_excerpt, ' […]' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
/** This filter is documented in wp-includes/formatting.php */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
$excerpt_length = (int) apply_filters( 'excerpt_length', $block_core_latest_posts_excerpt_length ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
if ( $excerpt_length <= $block_core_latest_posts_excerpt_length ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
$trimmed_excerpt = substr( $trimmed_excerpt, 0, -11 ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
159 |
$trimmed_excerpt .= sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
160 |
/* translators: 1: A URL to a post, 2: Hidden accessibility text: Post title */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
161 |
__( '… <a class="wp-block-latest-posts__read-more" href="%1$s" rel="noopener noreferrer">Read more<span class="screen-reader-text">: %2$s</span></a>' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
162 |
esc_url( $post_link ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
163 |
esc_html( $title ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
|
18 | 168 |
if ( post_password_required( $post ) ) { |
169 |
$trimmed_excerpt = __( 'This content is password protected.' ); |
|
170 |
} |
|
171 |
||
16 | 172 |
$list_items_markup .= sprintf( |
173 |
'<div class="wp-block-latest-posts__post-excerpt">%1$s</div>', |
|
174 |
$trimmed_excerpt |
|
175 |
); |
|
176 |
} |
|
177 |
||
178 |
if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent'] |
|
179 |
&& isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) { |
|
18 | 180 |
|
19 | 181 |
$post_content = html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) ); |
18 | 182 |
|
183 |
if ( post_password_required( $post ) ) { |
|
184 |
$post_content = __( 'This content is password protected.' ); |
|
185 |
} |
|
186 |
||
16 | 187 |
$list_items_markup .= sprintf( |
188 |
'<div class="wp-block-latest-posts__post-full-content">%1$s</div>', |
|
19 | 189 |
wp_kses_post( $post_content ) |
16 | 190 |
); |
191 |
} |
|
192 |
||
9 | 193 |
$list_items_markup .= "</li>\n"; |
194 |
} |
|
195 |
||
16 | 196 |
remove_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 ); |
197 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
198 |
$classes = array( 'wp-block-latest-posts__list' ); |
9 | 199 |
if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
200 |
$classes[] = 'is-grid'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
201 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
202 |
if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
203 |
$classes[] = 'columns-' . $attributes['columns']; |
9 | 204 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
205 |
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
206 |
$classes[] = 'has-dates'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
207 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
208 |
if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
209 |
$classes[] = 'has-author'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
210 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
211 |
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
|
212 |
$classes[] = 'has-link-color'; |
9 | 213 |
} |
214 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
215 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
9 | 216 |
|
16 | 217 |
return sprintf( |
18 | 218 |
'<ul %1$s>%2$s</ul>', |
219 |
$wrapper_attributes, |
|
9 | 220 |
$list_items_markup |
221 |
); |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* Registers the `core/latest-posts` block on server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
226 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
227 |
* @since 5.0.0 |
9 | 228 |
*/ |
229 |
function register_block_core_latest_posts() { |
|
16 | 230 |
register_block_type_from_metadata( |
231 |
__DIR__ . '/latest-posts', |
|
9 | 232 |
array( |
233 |
'render_callback' => 'render_block_core_latest_posts', |
|
234 |
) |
|
235 |
); |
|
236 |
} |
|
16 | 237 |
add_action( 'init', 'register_block_core_latest_posts' ); |
9 | 238 |
|
16 | 239 |
/** |
240 |
* Handles outdated versions of the `core/latest-posts` block by converting |
|
241 |
* attribute `categories` from a numeric string to an array with key `id`. |
|
242 |
* |
|
243 |
* This is done to accommodate the changes introduced in #20781 that sought to |
|
244 |
* add support for multiple categories to the block. However, given that this |
|
245 |
* block is dynamic, the usual provisions for block migration are insufficient, |
|
246 |
* as they only act when a block is loaded in the editor. |
|
247 |
* |
|
248 |
* TODO: Remove when and if the bottom client-side deprecation for this block |
|
249 |
* is removed. |
|
250 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
251 |
* @since 5.5.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
252 |
* |
16 | 253 |
* @param array $block A single parsed block object. |
254 |
* |
|
255 |
* @return array The migrated block object. |
|
256 |
*/ |
|
257 |
function block_core_latest_posts_migrate_categories( $block ) { |
|
258 |
if ( |
|
259 |
'core/latest-posts' === $block['blockName'] && |
|
260 |
! empty( $block['attrs']['categories'] ) && |
|
261 |
is_string( $block['attrs']['categories'] ) |
|
262 |
) { |
|
263 |
$block['attrs']['categories'] = array( |
|
264 |
array( 'id' => absint( $block['attrs']['categories'] ) ), |
|
265 |
); |
|
266 |
} |
|
267 |
||
268 |
return $block; |
|
269 |
} |
|
270 |
add_filter( 'render_block_data', 'block_core_latest_posts_migrate_categories' ); |