comment-author-name.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/comment-author-name` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/comment-author-name` 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 author.
  14. */
  15. function render_block_core_comment_author_name( $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. $classes = '';
  26. if ( isset( $attributes['textAlign'] ) ) {
  27. $classes .= 'has-text-align-' . $attributes['textAlign'];
  28. }
  29. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  30. $comment_author = get_comment_author( $comment );
  31. $link = get_comment_author_url( $comment );
  32. if ( ! empty( $link ) && ! empty( $attributes['isLink'] ) && ! empty( $attributes['linkTarget'] ) ) {
  33. $comment_author = sprintf( '<a rel="external nofollow ugc" href="%1s" target="%2s" >%3s</a>', esc_url( $link ), esc_attr( $attributes['linkTarget'] ), $comment_author );
  34. }
  35. if ( '0' === $comment->comment_approved && ! $show_pending_links ) {
  36. $comment_author = wp_kses( $comment_author, array() );
  37. }
  38. return sprintf(
  39. '<div %1$s>%2$s</div>',
  40. $wrapper_attributes,
  41. $comment_author
  42. );
  43. }
  44. /**
  45. * Registers the `core/comment-author-name` block on the server.
  46. */
  47. function register_block_core_comment_author_name() {
  48. register_block_type_from_metadata(
  49. __DIR__ . '/comment-author-name',
  50. array(
  51. 'render_callback' => 'render_block_core_comment_author_name',
  52. )
  53. );
  54. }
  55. add_action( 'init', 'register_block_core_comment_author_name' );