PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wp-includes/blocks/latest-comments.php

https://bitbucket.org/sebastianpisula/wordpress-starter
PHP | 163 lines | 110 code | 13 blank | 40 comment | 16 complexity | c52a83cc12c7b6beeb097cf36dc0cd1c MD5 | raw file
Possible License(s): GPL-2.0, 0BSD
  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 esc_html( $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. // This filter is documented in wp-includes/widgets/class-wp-widget-recent-comments.php.
  43. $comments = get_comments(
  44. apply_filters(
  45. 'widget_comments_args',
  46. array(
  47. 'number' => $attributes['commentsToShow'],
  48. 'status' => 'approve',
  49. 'post_status' => 'publish',
  50. )
  51. )
  52. );
  53. $list_items_markup = '';
  54. if ( ! empty( $comments ) ) {
  55. // Prime the cache for associated posts. This is copied from \WP_Widget_Recent_Comments::widget().
  56. $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
  57. _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
  58. foreach ( $comments as $comment ) {
  59. $list_items_markup .= '<li class="wp-block-latest-comments__comment">';
  60. if ( $attributes['displayAvatar'] ) {
  61. $avatar = get_avatar(
  62. $comment,
  63. 48,
  64. '',
  65. '',
  66. array(
  67. 'class' => 'wp-block-latest-comments__comment-avatar',
  68. )
  69. );
  70. if ( $avatar ) {
  71. $list_items_markup .= $avatar;
  72. }
  73. }
  74. $list_items_markup .= '<article>';
  75. $list_items_markup .= '<footer class="wp-block-latest-comments__comment-meta">';
  76. $author_url = get_comment_author_url( $comment );
  77. if ( empty( $author_url ) && ! empty( $comment->user_id ) ) {
  78. $author_url = get_author_posts_url( $comment->user_id );
  79. }
  80. $author_markup = '';
  81. if ( $author_url ) {
  82. $author_markup .= '<a class="wp-block-latest-comments__comment-author" href="' . esc_url( $author_url ) . '">' . get_comment_author( $comment ) . '</a>';
  83. } else {
  84. $author_markup .= '<span class="wp-block-latest-comments__comment-author">' . get_comment_author( $comment ) . '</span>';
  85. }
  86. // `_draft_or_post_title` calls `esc_html()` so we don't need to wrap that call in
  87. // `esc_html`.
  88. $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>';
  89. $list_items_markup .= sprintf(
  90. /* translators: 1: author name (inside <a> or <span> tag, based on if they have a URL), 2: post title related to this comment */
  91. __( '%1$s on %2$s' ),
  92. $author_markup,
  93. $post_title
  94. );
  95. if ( $attributes['displayDate'] ) {
  96. $list_items_markup .= sprintf(
  97. '<time datetime="%1$s" class="wp-block-latest-comments__comment-date">%2$s</time>',
  98. esc_attr( get_comment_date( 'c', $comment ) ),
  99. date_i18n( get_option( 'date_format' ), get_comment_date( 'U', $comment ) )
  100. );
  101. }
  102. $list_items_markup .= '</footer>';
  103. if ( $attributes['displayExcerpt'] ) {
  104. $list_items_markup .= '<div class="wp-block-latest-comments__comment-excerpt">' . wpautop( get_comment_excerpt( $comment ) ) . '</div>';
  105. }
  106. $list_items_markup .= '</article></li>';
  107. }
  108. }
  109. $class = 'wp-block-latest-comments';
  110. if ( ! empty( $attributes['className'] ) ) {
  111. $class .= ' ' . $attributes['className'];
  112. }
  113. if ( isset( $attributes['align'] ) ) {
  114. $class .= " align{$attributes['align']}";
  115. }
  116. if ( $attributes['displayAvatar'] ) {
  117. $class .= ' has-avatars';
  118. }
  119. if ( $attributes['displayDate'] ) {
  120. $class .= ' has-dates';
  121. }
  122. if ( $attributes['displayExcerpt'] ) {
  123. $class .= ' has-excerpts';
  124. }
  125. if ( empty( $comments ) ) {
  126. $class .= ' no-comments';
  127. }
  128. $classnames = esc_attr( $class );
  129. return ! empty( $comments ) ? sprintf(
  130. '<ol class="%1$s">%2$s</ol>',
  131. $classnames,
  132. $list_items_markup
  133. ) : sprintf(
  134. '<div class="%1$s">%2$s</div>',
  135. $classnames,
  136. __( 'No comments to show.' )
  137. );
  138. }
  139. /**
  140. * Registers the `core/latest-comments` block.
  141. */
  142. function register_block_core_latest_comments() {
  143. register_block_type_from_metadata(
  144. __DIR__ . '/latest-comments',
  145. array(
  146. 'render_callback' => 'render_block_core_latest_comments',
  147. )
  148. );
  149. }
  150. add_action( 'init', 'register_block_core_latest_comments' );