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