post-author.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-author` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-author` 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 author block.
  14. */
  15. function render_block_core_post_author( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['postId'] ) ) {
  17. $author_id = get_query_var( 'author' );
  18. } else {
  19. $author_id = get_post_field( 'post_author', $block->context['postId'] );
  20. }
  21. if ( empty( $author_id ) ) {
  22. return '';
  23. }
  24. $avatar = ! empty( $attributes['avatarSize'] ) ? get_avatar(
  25. $author_id,
  26. $attributes['avatarSize']
  27. ) : null;
  28. $byline = ! empty( $attributes['byline'] ) ? $attributes['byline'] : false;
  29. $classes = array_merge(
  30. isset( $attributes['itemsJustification'] ) ? array( 'items-justified-' . $attributes['itemsJustification'] ) : array(),
  31. isset( $attributes['textAlign'] ) ? array( 'has-text-align-' . $attributes['textAlign'] ) : array()
  32. );
  33. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
  34. return sprintf( '<div %1$s>', $wrapper_attributes ) .
  35. ( ! empty( $attributes['showAvatar'] ) ? '<div class="wp-block-post-author__avatar">' . $avatar . '</div>' : '' ) .
  36. '<div class="wp-block-post-author__content">' .
  37. ( ! empty( $byline ) ? '<p class="wp-block-post-author__byline">' . wp_kses_post( $byline ) . '</p>' : '' ) .
  38. '<p class="wp-block-post-author__name">' . get_the_author_meta( 'display_name', $author_id ) . '</p>' .
  39. ( ! empty( $attributes['showBio'] ) ? '<p class="wp-block-post-author__bio">' . get_the_author_meta( 'user_description', $author_id ) . '</p>' : '' ) .
  40. '</div>' .
  41. '</div>';
  42. }
  43. /**
  44. * Registers the `core/post-author` block on the server.
  45. */
  46. function register_block_core_post_author() {
  47. register_block_type_from_metadata(
  48. __DIR__ . '/post-author',
  49. array(
  50. 'render_callback' => 'render_block_core_post_author',
  51. )
  52. );
  53. }
  54. add_action( 'init', 'register_block_core_post_author' );