PluginProbe
Gutenberg / 14.7.0
Gutenberg v14.7.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / block-library / blocks / latest-posts.php

latest-posts.php in Gutenberg 14.7.0, at build/block-library/blocks/latest-posts.php

246 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/latest-posts` block.
4 *
5 * @package WordPress
6 */
7
8 /**
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 global $block_core_latest_posts_excerpt_length;
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 gutenberg_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 /**
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 gutenberg_render_block_core_latest_posts( $attributes ) {
37 global $post, $block_core_latest_posts_excerpt_length;
38
39 $args = array(
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,
46 );
47
48 $block_core_latest_posts_excerpt_length = $attributes['excerptLength'];
49 add_filter( 'excerpt_length', 'gutenberg_block_core_latest_posts_get_excerpt_length', 20 );
50
51 if ( isset( $attributes['categories'] ) ) {
52 $args['category__in'] = array_column( $attributes['categories'], 'id' );
53 }
54 if ( isset( $attributes['selectedAuthor'] ) ) {
55 $args['author'] = $attributes['selectedAuthor'];
56 }
57
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 }
64
65 $list_items_markup = '';
66
67 foreach ( $recent_posts as $post ) {
68 $post_link = esc_url( get_permalink( $post ) );
69 $title = get_the_title( $post );
70
71 if ( ! $title ) {
72 $title = __( '(no title)' );
73 }
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
91 $featured_image = get_the_post_thumbnail(
92 $post,
93 $attributes['featuredImageSizeSlug'],
94 array(
95 'style' => esc_attr( $image_style ),
96 )
97 );
98 if ( $attributes['addLinkToFeaturedImage'] ) {
99 $featured_image = sprintf(
100 '<a href="%1$s" aria-label="%2$s">%3$s</a>',
101 esc_url( $post_link ),
102 esc_attr( $title ),
103 $featured_image
104 );
105 }
106 $list_items_markup .= sprintf(
107 '<div class="%1$s">%2$s</div>',
108 esc_attr( $image_classes ),
109 $featured_image
110 );
111 }
112
113 $list_items_markup .= sprintf(
114 '<a class="wp-block-latest-posts__post-title" href="%1$s">%2$s</a>',
115 esc_url( $post_link ),
116 $title
117 );
118
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>',
128 $byline
129 );
130 }
131 }
132
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 ) ),
137 get_the_date( '', $post )
138 );
139 }
140
141 if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
142 && isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
143
144 $trimmed_excerpt = get_the_excerpt( $post );
145
146 if ( post_password_required( $post ) ) {
147 $trimmed_excerpt = __( 'This content is password protected.' );
148 }
149
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'] ) {
158
159 $post_content = html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) );
160
161 if ( post_password_required( $post ) ) {
162 $post_content = __( 'This content is password protected.' );
163 }
164
165 $list_items_markup .= sprintf(
166 '<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
167 wp_kses_post( $post_content )
168 );
169 }
170
171 $list_items_markup .= "</li>\n";
172 }
173
174 remove_filter( 'excerpt_length', 'gutenberg_block_core_latest_posts_get_excerpt_length', 20 );
175
176 $class = 'wp-block-latest-posts__list';
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
190 if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
191 $class .= ' has-author';
192 }
193
194 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) );
195
196 return sprintf(
197 '<ul %1$s>%2$s</ul>',
198 $wrapper_attributes,
199 $list_items_markup
200 );
201 }
202
203 /**
204 * Registers the `core/latest-posts` block on server.
205 */
206 function gutenberg_register_block_core_latest_posts() {
207 register_block_type_from_metadata(
208 __DIR__ . '/latest-posts',
209 array(
210 'render_callback' => 'gutenberg_render_block_core_latest_posts',
211 )
212 );
213 }
214 add_action( 'init', 'gutenberg_register_block_core_latest_posts', 20 );
215
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 gutenberg_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', 'gutenberg_block_core_latest_posts_migrate_categories' );
246