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