latest-posts.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/latest-posts` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/latest-posts` block on server.
  9. *
  10. * @param array $attributes The block attributes.
  11. *
  12. * @return string Returns the post content with latest posts added.
  13. */
  14. function render_block_core_latest_posts( $attributes ) {
  15. $args = array(
  16. 'posts_per_page' => $attributes['postsToShow'],
  17. 'post_status' => 'publish',
  18. 'order' => $attributes['order'],
  19. 'orderby' => $attributes['orderBy'],
  20. 'suppress_filters' => false,
  21. );
  22. if ( isset( $attributes['categories'] ) ) {
  23. $args['category'] = $attributes['categories'];
  24. }
  25. $recent_posts = get_posts( $args );
  26. $list_items_markup = '';
  27. $excerpt_length = $attributes['excerptLength'];
  28. foreach ( $recent_posts as $post ) {
  29. $title = get_the_title( $post );
  30. if ( ! $title ) {
  31. $title = __( '(no title)' );
  32. }
  33. $list_items_markup .= sprintf(
  34. '<li><a href="%1$s">%2$s</a>',
  35. esc_url( get_permalink( $post ) ),
  36. $title
  37. );
  38. if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
  39. $list_items_markup .= sprintf(
  40. '<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
  41. esc_attr( get_the_date( 'c', $post ) ),
  42. esc_html( get_the_date( '', $post ) )
  43. );
  44. }
  45. if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
  46. && isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
  47. $post_excerpt = $post->post_excerpt;
  48. if ( ! ( $post_excerpt ) ) {
  49. $post_excerpt = $post->post_content;
  50. }
  51. $trimmed_excerpt = esc_html( wp_trim_words( $post_excerpt, $excerpt_length, ' &hellip; ' ) );
  52. $list_items_markup .= sprintf(
  53. '<div class="wp-block-latest-posts__post-excerpt">%1$s',
  54. $trimmed_excerpt
  55. );
  56. if ( strpos( $trimmed_excerpt, ' &hellip; ' ) !== false ) {
  57. $list_items_markup .= sprintf(
  58. '<a href="%1$s">%2$s</a></div>',
  59. esc_url( get_permalink( $post ) ),
  60. __( 'Read more' )
  61. );
  62. } else {
  63. $list_items_markup .= sprintf(
  64. '</div>'
  65. );
  66. }
  67. }
  68. if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
  69. && isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
  70. $list_items_markup .= sprintf(
  71. '<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
  72. wp_kses_post( html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) ) )
  73. );
  74. }
  75. $list_items_markup .= "</li>\n";
  76. }
  77. $class = 'wp-block-latest-posts wp-block-latest-posts__list';
  78. if ( isset( $attributes['align'] ) ) {
  79. $class .= ' align' . $attributes['align'];
  80. }
  81. if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
  82. $class .= ' is-grid';
  83. }
  84. if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) {
  85. $class .= ' columns-' . $attributes['columns'];
  86. }
  87. if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
  88. $class .= ' has-dates';
  89. }
  90. if ( isset( $attributes['className'] ) ) {
  91. $class .= ' ' . $attributes['className'];
  92. }
  93. return sprintf(
  94. '<ul class="%1$s">%2$s</ul>',
  95. esc_attr( $class ),
  96. $list_items_markup
  97. );
  98. }
  99. /**
  100. * Registers the `core/latest-posts` block on server.
  101. */
  102. function register_block_core_latest_posts() {
  103. register_block_type(
  104. 'core/latest-posts',
  105. array(
  106. 'attributes' => array(
  107. 'align' => array(
  108. 'type' => 'string',
  109. 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
  110. ),
  111. 'className' => array(
  112. 'type' => 'string',
  113. ),
  114. 'categories' => array(
  115. 'type' => 'string',
  116. ),
  117. 'postsToShow' => array(
  118. 'type' => 'number',
  119. 'default' => 5,
  120. ),
  121. 'displayPostContent' => array(
  122. 'type' => 'boolean',
  123. 'default' => false,
  124. ),
  125. 'displayPostContentRadio' => array(
  126. 'type' => 'string',
  127. 'default' => 'excerpt',
  128. ),
  129. 'excerptLength' => array(
  130. 'type' => 'number',
  131. 'default' => 55,
  132. ),
  133. 'displayPostDate' => array(
  134. 'type' => 'boolean',
  135. 'default' => false,
  136. ),
  137. 'postLayout' => array(
  138. 'type' => 'string',
  139. 'default' => 'list',
  140. ),
  141. 'columns' => array(
  142. 'type' => 'number',
  143. 'default' => 3,
  144. ),
  145. 'order' => array(
  146. 'type' => 'string',
  147. 'default' => 'desc',
  148. ),
  149. 'orderBy' => array(
  150. 'type' => 'string',
  151. 'default' => 'date',
  152. ),
  153. ),
  154. 'render_callback' => 'render_block_core_latest_posts',
  155. )
  156. );
  157. }
  158. add_action( 'init', 'register_block_core_latest_posts' );