read-more.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/read-more` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/read-more` 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 post link.
  14. */
  15. function render_block_core_read_more( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['postId'] ) ) {
  17. return '';
  18. }
  19. $post_ID = $block->context['postId'];
  20. $justify_class_name = empty( $attributes['justifyContent'] ) ? '' : "is-justified-{$attributes['justifyContent']}";
  21. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $justify_class_name ) );
  22. $more_text = ! empty( $attributes['content'] ) ? wp_kses_post( $attributes['content'] ) : __( 'Read more' );
  23. return sprintf(
  24. '<a %1s href="%2s" target="%3s">%4s</a>',
  25. $wrapper_attributes,
  26. get_the_permalink( $post_ID ),
  27. esc_attr( $attributes['linkTarget'] ),
  28. $more_text
  29. );
  30. }
  31. /**
  32. * Registers the `core/read-more` block on the server.
  33. */
  34. function register_block_core_read_more() {
  35. register_block_type_from_metadata(
  36. __DIR__ . '/read-more',
  37. array(
  38. 'render_callback' => 'render_block_core_read_more',
  39. )
  40. );
  41. }
  42. add_action( 'init', 'register_block_core_read_more' );