comment-date.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/comment-date` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/comment-date` 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 date.
  14. */
  15. function render_block_core_comment_date( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['commentId'] ) ) {
  17. return '';
  18. }
  19. $comment = get_comment( $block->context['commentId'] );
  20. if ( empty( $comment ) ) {
  21. return '';
  22. }
  23. $classes = '';
  24. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  25. $formatted_date = get_comment_date(
  26. isset( $attributes['format'] ) ? $attributes['format'] : '',
  27. $comment
  28. );
  29. $link = get_comment_link( $comment );
  30. if ( ! empty( $attributes['isLink'] ) ) {
  31. $formatted_date = sprintf( '<a href="%1s">%2s</a>', esc_url( $link ), $formatted_date );
  32. }
  33. return sprintf(
  34. '<div %1$s><time datetime="%2$s">%3$s</time></div>',
  35. $wrapper_attributes,
  36. esc_attr( get_comment_date( 'c', $comment ) ),
  37. $formatted_date
  38. );
  39. }
  40. /**
  41. * Registers the `core/comment-date` block on the server.
  42. */
  43. function register_block_core_comment_date() {
  44. register_block_type_from_metadata(
  45. __DIR__ . '/comment-date',
  46. array(
  47. 'render_callback' => 'render_block_core_comment_date',
  48. )
  49. );
  50. }
  51. add_action( 'init', 'register_block_core_comment_date' );