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