post-author-biography.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-author-biography` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-author-biography` 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 rendered post author biography block.
  14. */
  15. function render_block_core_post_author_biography( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['postId'] ) ) {
  17. return '';
  18. }
  19. $author_id = get_post_field( 'post_author', $block->context['postId'] );
  20. if ( empty( $author_id ) ) {
  21. return '';
  22. }
  23. $author_biography = get_the_author_meta( 'description', $author_id );
  24. if ( empty( $author_biography ) ) {
  25. return '';
  26. }
  27. $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
  28. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
  29. return sprintf( '<div %1$s>', $wrapper_attributes ) . $author_biography . '</div>';
  30. }
  31. /**
  32. * Registers the `core/post-author-biography` block on the server.
  33. */
  34. function register_block_core_post_author_biography() {
  35. register_block_type_from_metadata(
  36. __DIR__ . '/post-author-biography',
  37. array(
  38. 'render_callback' => 'render_block_core_post_author_biography',
  39. )
  40. );
  41. }
  42. add_action( 'init', 'register_block_core_post_author_biography' );