latest-comments.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/latest-comments` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Get the post title.
  9. *
  10. * The post title is fetched and if it is blank then a default string is
  11. * returned.
  12. *
  13. * Copied from `wp-admin/includes/template.php`, but we can't include that
  14. * file because:
  15. *
  16. * 1. It causes bugs with test fixture generation and strange Docker 255 error
  17. * codes.
  18. * 2. It's in the admin; ideally we *shouldn't* be including files from the
  19. * admin for a block's output. It's a very small/simple function as well,
  20. * so duplicating it isn't too terrible.
  21. *
  22. * @since 3.3.0
  23. *
  24. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
  25. * @return string The post title if set; "(no title)" if no title is set.
  26. */
  27. function wp_latest_comments_draft_or_post_title( $post = 0 ) {
  28. $title = get_the_title( $post );
  29. if ( empty( $title ) ) {
  30. $title = __( '(no title)' );
  31. }
  32. return $title;
  33. }
  34. /**
  35. * Renders the `core/latest-comments` block on server.
  36. *
  37. * @param array $attributes The block attributes.
  38. *
  39. * @return string Returns the post content with latest comments added.
  40. */
  41. function render_block_core_latest_comments( $attributes = array() ) {
  42. $comments = get_comments(
  43. /** This filter is documented in wp-includes/widgets/class-wp-widget-recent-comments.php */
  44. apply_filters(
  45. 'widget_comments_args',
  46. array(
  47. 'number' => $attributes['commentsToShow'],
  48. 'status' => 'approve',
  49. 'post_status' => 'publish',
  50. ),
  51. array()
  52. )
  53. );
  54. $list_items_markup = '';
  55. if ( ! empty( $comments ) ) {
  56. // Prime the cache for associated posts. This is copied from \WP_Widget_Recent_Comments::widget().
  57. $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
  58. _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
  59. foreach ( $comments as $comment ) {
  60. $list_items_markup .= '<li class="wp-block-latest-comments__comment">';
  61. if ( $attributes['displayAvatar'] ) {
  62. $avatar = get_avatar(
  63. $comment,
  64. 48,
  65. '',
  66. '',
  67. array(
  68. 'class' => 'wp-block-latest-comments__comment-avatar',
  69. )
  70. );
  71. if ( $avatar ) {
  72. $list_items_markup .= $avatar;
  73. }
  74. }
  75. $list_items_markup .= '<article>';
  76. $list_items_markup .= '<footer class="wp-block-latest-comments__comment-meta">';
  77. $author_url = get_comment_author_url( $comment );
  78. if ( empty( $author_url ) && ! empty( $comment->user_id ) ) {
  79. $author_url = get_author_posts_url( $comment->user_id );
  80. }
  81. $author_markup = '';
  82. if ( $author_url ) {
  83. $author_markup .= '<a class="wp-block-latest-comments__comment-author" href="' . esc_url( $author_url ) . '">' . get_comment_author( $comment ) . '</a>';
  84. } else {
  85. $author_markup .= '<span class="wp-block-latest-comments__comment-author">' . get_comment_author( $comment ) . '</span>';
  86. }
  87. // `_draft_or_post_title` calls `esc_html()` so we don't need to wrap that call in
  88. // `esc_html`.
  89. $post_title = '<a class="wp-block-latest-comments__comment-link" href="' . esc_url( get_comment_link( $comment ) ) . '">' . wp_latest_comments_draft_or_post_title( $comment->comment_post_ID ) . '</a>';
  90. $list_items_markup .= sprintf(
  91. /* translators: 1: author name (inside <a> or <span> tag, based on if they have a URL), 2: post title related to this comment */
  92. __( '%1$s on %2$s' ),
  93. $author_markup,
  94. $post_title
  95. );
  96. if ( $attributes['displayDate'] ) {
  97. $list_items_markup .= sprintf(
  98. '<time datetime="%1$s" class="wp-block-latest-comments__comment-date">%2$s</time>',
  99. esc_attr( get_comment_date( 'c', $comment ) ),
  100. date_i18n( get_option( 'date_format' ), get_comment_date( 'U', $comment ) )
  101. );
  102. }
  103. $list_items_markup .= '</footer>';
  104. if ( $attributes['displayExcerpt'] ) {
  105. $list_items_markup .= '<div class="wp-block-latest-comments__comment-excerpt">' . wpautop( get_comment_excerpt( $comment ) ) . '</div>';
  106. }
  107. $list_items_markup .= '</article></li>';
  108. }
  109. }
  110. $classnames = array();
  111. if ( $attributes['displayAvatar'] ) {
  112. $classnames[] = 'has-avatars';
  113. }
  114. if ( $attributes['displayDate'] ) {
  115. $classnames[] = 'has-dates';
  116. }
  117. if ( $attributes['displayExcerpt'] ) {
  118. $classnames[] = 'has-excerpts';
  119. }
  120. if ( empty( $comments ) ) {
  121. $classnames[] = 'no-comments';
  122. }
  123. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
  124. return ! empty( $comments ) ? sprintf(
  125. '<ol %1$s>%2$s</ol>',
  126. $wrapper_attributes,
  127. $list_items_markup
  128. ) : sprintf(
  129. '<div %1$s>%2$s</div>',
  130. $wrapper_attributes,
  131. __( 'No comments to show.' )
  132. );
  133. }
  134. /**
  135. * Registers the `core/latest-comments` block.
  136. */
  137. function register_block_core_latest_comments() {
  138. register_block_type_from_metadata(
  139. __DIR__ . '/latest-comments',
  140. array(
  141. 'render_callback' => 'render_block_core_latest_comments',
  142. )
  143. );
  144. }
  145. add_action( 'init', 'register_block_core_latest_comments' );