Aucune description

comment-date.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. if ( isset( $attributes['fontSize'] ) ) {
  25. $classes .= 'has-' . esc_attr( $attributes['fontSize'] ) . '-font-size';
  26. }
  27. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  28. $formatted_date = get_comment_date(
  29. isset( $attributes['format'] ) ? $attributes['format'] : '',
  30. $comment
  31. );
  32. $link = get_comment_link( $comment );
  33. if ( ! empty( $attributes['isLink'] ) ) {
  34. $formatted_date = sprintf( '<a href="%1s">%2s</a>', esc_url( $link ), $formatted_date );
  35. }
  36. return sprintf(
  37. '<div %1$s><time datetime="%2$s">%3$s</time></div>',
  38. $wrapper_attributes,
  39. esc_attr( get_comment_date( 'c', $comment ) ),
  40. $formatted_date
  41. );
  42. }
  43. /**
  44. * Registers the `core/comment-date` block on the server.
  45. */
  46. function register_block_core_comment_date() {
  47. register_block_type_from_metadata(
  48. __DIR__ . '/comment-date',
  49. array(
  50. 'render_callback' => 'render_block_core_comment_date',
  51. )
  52. );
  53. }
  54. add_action( 'init', 'register_block_core_comment_date' );