class-twentytwenty-walker-comment.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Custom comment walker for this theme.
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Twenty
  7. * @since Twenty Twenty 1.0
  8. */
  9. if ( ! class_exists( 'TwentyTwenty_Walker_Comment' ) ) {
  10. /**
  11. * CUSTOM COMMENT WALKER
  12. * A custom walker for comments, based on the walker in Twenty Nineteen.
  13. */
  14. class TwentyTwenty_Walker_Comment extends Walker_Comment {
  15. /**
  16. * Outputs a comment in the HTML5 format.
  17. *
  18. * @see wp_list_comments()
  19. * @see https://developer.wordpress.org/reference/functions/get_comment_author_url/
  20. * @see https://developer.wordpress.org/reference/functions/get_comment_author/
  21. * @see https://developer.wordpress.org/reference/functions/get_avatar/
  22. * @see https://developer.wordpress.org/reference/functions/get_comment_reply_link/
  23. * @see https://developer.wordpress.org/reference/functions/get_edit_comment_link/
  24. *
  25. * @param WP_Comment $comment Comment to display.
  26. * @param int $depth Depth of the current comment.
  27. * @param array $args An array of arguments.
  28. */
  29. protected function html5_comment( $comment, $depth, $args ) {
  30. $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
  31. ?>
  32. <<?php echo $tag; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static output ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
  33. <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  34. <footer class="comment-meta">
  35. <div class="comment-author vcard">
  36. <?php
  37. $comment_author_url = get_comment_author_url( $comment );
  38. $comment_author = get_comment_author( $comment );
  39. $avatar = get_avatar( $comment, $args['avatar_size'] );
  40. if ( 0 !== $args['avatar_size'] ) {
  41. if ( empty( $comment_author_url ) ) {
  42. echo wp_kses_post( $avatar );
  43. } else {
  44. printf( '<a href="%s" rel="external nofollow" class="url">', $comment_author_url ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Escaped in https://developer.wordpress.org/reference/functions/get_comment_author_url/
  45. echo wp_kses_post( $avatar );
  46. }
  47. }
  48. printf(
  49. '<span class="fn">%1$s</span><span class="screen-reader-text says">%2$s</span>',
  50. esc_html( $comment_author ),
  51. __( 'says:', 'twentytwenty' )
  52. );
  53. if ( ! empty( $comment_author_url ) ) {
  54. echo '</a>';
  55. }
  56. ?>
  57. </div><!-- .comment-author -->
  58. <div class="comment-metadata">
  59. <a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
  60. <?php
  61. /* translators: 1: Comment date, 2: Comment time. */
  62. $comment_timestamp = sprintf( __( '%1$s at %2$s', 'twentytwenty' ), get_comment_date( '', $comment ), get_comment_time() );
  63. ?>
  64. <time datetime="<?php comment_time( 'c' ); ?>" title="<?php echo esc_attr( $comment_timestamp ); ?>">
  65. <?php echo esc_html( $comment_timestamp ); ?>
  66. </time>
  67. </a>
  68. <?php
  69. if ( get_edit_comment_link() ) {
  70. echo ' <span aria-hidden="true">&bull;</span> <a class="comment-edit-link" href="' . esc_url( get_edit_comment_link() ) . '">' . __( 'Edit', 'twentytwenty' ) . '</a>';
  71. }
  72. ?>
  73. </div><!-- .comment-metadata -->
  74. </footer><!-- .comment-meta -->
  75. <div class="comment-content entry-content">
  76. <?php
  77. comment_text();
  78. if ( '0' === $comment->comment_approved ) {
  79. ?>
  80. <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwenty' ); ?></p>
  81. <?php
  82. }
  83. ?>
  84. </div><!-- .comment-content -->
  85. <?php
  86. $comment_reply_link = get_comment_reply_link(
  87. array_merge(
  88. $args,
  89. array(
  90. 'add_below' => 'div-comment',
  91. 'depth' => $depth,
  92. 'max_depth' => $args['max_depth'],
  93. 'before' => '<span class="comment-reply">',
  94. 'after' => '</span>',
  95. )
  96. )
  97. );
  98. $by_post_author = twentytwenty_is_comment_by_post_author( $comment );
  99. if ( $comment_reply_link || $by_post_author ) {
  100. ?>
  101. <footer class="comment-footer-meta">
  102. <?php
  103. if ( $comment_reply_link ) {
  104. echo $comment_reply_link; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Link is escaped in https://developer.wordpress.org/reference/functions/get_comment_reply_link/
  105. }
  106. if ( $by_post_author ) {
  107. echo '<span class="by-post-author">' . __( 'By Post Author', 'twentytwenty' ) . '</span>';
  108. }
  109. ?>
  110. </footer>
  111. <?php
  112. }
  113. ?>
  114. </article><!-- .comment-body -->
  115. <?php
  116. }
  117. }
  118. }