Нет описания

post-date.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-date` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-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 Returns the filtered post date for the current post wrapped inside "time" tags.
  14. */
  15. function render_block_core_post_date( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['postId'] ) ) {
  17. return '';
  18. }
  19. $post_ID = $block->context['postId'];
  20. $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
  21. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
  22. $formatted_date = get_the_date( isset( $attributes['format'] ) ? $attributes['format'] : '', $post_ID );
  23. if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
  24. $formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $formatted_date );
  25. }
  26. return sprintf(
  27. '<div %1$s><time datetime="%2$s">%3$s</time></div>',
  28. $wrapper_attributes,
  29. get_the_date( 'c', $post_ID ),
  30. $formatted_date
  31. );
  32. }
  33. /**
  34. * Registers the `core/post-date` block on the server.
  35. */
  36. function register_block_core_post_date() {
  37. register_block_type_from_metadata(
  38. __DIR__ . '/post-date',
  39. array(
  40. 'render_callback' => 'render_block_core_post_date',
  41. )
  42. );
  43. }
  44. add_action( 'init', 'register_block_core_post_date' );