latest-posts.php 7.3 KB

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