Nenhuma Descrição

latest-posts.php 7.0KB

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