Changeset 48177 for trunk/src/wp-includes/blocks/latest-posts.php
- Timestamp:
- 06/26/2020 01:31:11 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/latest-posts.php
r47408 r48177 34 34 */ 35 35 function render_block_core_latest_posts( $attributes ) { 36 global $ block_core_latest_posts_excerpt_length;36 global $post, $block_core_latest_posts_excerpt_length; 37 37 38 38 $args = array( … … 48 48 49 49 if ( isset( $attributes['categories'] ) ) { 50 $args['category'] = $attributes['categories']; 50 $args['category__in'] = array_column( $attributes['categories'], 'id' ); 51 } 52 if ( isset( $attributes['selectedAuthor'] ) ) { 53 $args['author'] = $attributes['selectedAuthor']; 51 54 } 52 55 … … 56 59 57 60 foreach ( $recent_posts as $post ) { 61 58 62 $list_items_markup .= '<li>'; 59 63 … … 95 99 ); 96 100 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 97 115 if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) { 98 116 $list_items_markup .= sprintf( … … 109 127 110 128 $list_items_markup .= sprintf( 111 '<div class="wp-block-latest-posts__post-excerpt">%1$s ',129 '<div class="wp-block-latest-posts__post-excerpt">%1$s</div>', 112 130 $trimmed_excerpt 113 131 ); 114 115 if ( strpos( $trimmed_excerpt, ' … ' ) !== false ) {116 $list_items_markup .= sprintf(117 '<a href="%1$s">%2$s</a></div>',118 esc_url( get_permalink( $post ) ),119 __( 'Read more' )120 );121 } else {122 $list_items_markup .= sprintf(123 '</div>'124 );125 }126 132 } 127 133 … … 154 160 if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) { 155 161 $class .= ' has-dates'; 162 } 163 164 if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) { 165 $class .= ' has-author'; 156 166 } 157 167 … … 171 181 */ 172 182 function register_block_core_latest_posts() { 173 register_block_type (174 'core/latest-posts',183 register_block_type_from_metadata( 184 __DIR__ . '/latest-posts', 175 185 array( 176 'attributes' => array(177 'align' => array(178 'type' => 'string',179 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),180 ),181 'className' => array(182 'type' => 'string',183 ),184 'categories' => array(185 'type' => 'string',186 ),187 'postsToShow' => array(188 'type' => 'number',189 'default' => 5,190 ),191 'displayPostContent' => array(192 'type' => 'boolean',193 'default' => false,194 ),195 'displayPostContentRadio' => array(196 'type' => 'string',197 'default' => 'excerpt',198 ),199 'excerptLength' => array(200 'type' => 'number',201 'default' => 55,202 ),203 'displayPostDate' => array(204 'type' => 'boolean',205 'default' => false,206 ),207 'postLayout' => array(208 'type' => 'string',209 'default' => 'list',210 ),211 'columns' => array(212 'type' => 'number',213 'default' => 3,214 ),215 'order' => array(216 'type' => 'string',217 'default' => 'desc',218 ),219 'orderBy' => array(220 'type' => 'string',221 'default' => 'date',222 ),223 'displayFeaturedImage' => array(224 'type' => 'boolean',225 'default' => false,226 ),227 'featuredImageAlign' => array(228 'type' => 'string',229 'enum' => array( 'left', 'center', 'right' ),230 ),231 'featuredImageSizeSlug' => array(232 'type' => 'string',233 'default' => 'thumbnail',234 ),235 'featuredImageSizeWidth' => array(236 'type' => 'number',237 'default' => null,238 ),239 'featuredImageSizeHeight' => array(240 'type' => 'number',241 'default' => null,242 ),243 ),244 186 'render_callback' => 'render_block_core_latest_posts', 245 187 ) … … 247 189 } 248 190 add_action( 'init', 'register_block_core_latest_posts' ); 191 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' );
Note: See TracChangeset
for help on using the changeset viewer.