comment-content.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/comment-content` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/comment-content` block on the server.
  9. *
  10. * @param array $attributes Block attributes.
  11. * @param string $content Block default content.
  12. * @param WP_Block $block Block instance.
  13. * @return string Return the post comment's content.
  14. */
  15. function render_block_core_comment_content( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['commentId'] ) ) {
  17. return '';
  18. }
  19. $comment = get_comment( $block->context['commentId'] );
  20. $commenter = wp_get_current_commenter();
  21. $show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author'];
  22. if ( empty( $comment ) ) {
  23. return '';
  24. }
  25. $args = array();
  26. $comment_text = get_comment_text( $comment, $args );
  27. if ( ! $comment_text ) {
  28. return '';
  29. }
  30. /** This filter is documented in wp-includes/comment-template.php */
  31. $comment_text = apply_filters( 'comment_text', $comment_text, $comment, $args );
  32. $moderation_note = '';
  33. if ( '0' === $comment->comment_approved ) {
  34. $commenter = wp_get_current_commenter();
  35. if ( $commenter['comment_author_email'] ) {
  36. $moderation_note = __( 'Your comment is awaiting moderation.' );
  37. } else {
  38. $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
  39. }
  40. $moderation_note = '<p><em class="comment-awaiting-moderation">' . $moderation_note . '</em></p>';
  41. if ( ! $show_pending_links ) {
  42. $comment_text = wp_kses( $comment_text, array() );
  43. }
  44. }
  45. $classes = '';
  46. if ( isset( $attributes['textAlign'] ) ) {
  47. $classes .= 'has-text-align-' . $attributes['textAlign'];
  48. }
  49. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  50. return sprintf(
  51. '<div %1$s>%2$s%3$s</div>',
  52. $wrapper_attributes,
  53. $moderation_note,
  54. $comment_text
  55. );
  56. }
  57. /**
  58. * Registers the `core/comment-content` block on the server.
  59. */
  60. function register_block_core_comment_content() {
  61. register_block_type_from_metadata(
  62. __DIR__ . '/comment-content',
  63. array(
  64. 'render_callback' => 'render_block_core_comment_content',
  65. )
  66. );
  67. }
  68. add_action( 'init', 'register_block_core_comment_content' );