暫無描述

class-twentytwenty-walker-comment.php 4.7KB

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