comments-pagination-previous.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/comments-pagination-previous` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/comments-pagination-previous` 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. *
  14. * @return string Returns the previous posts link for the comments pagination.
  15. */
  16. function render_block_core_comments_pagination_previous( $attributes, $content, $block ) {
  17. $default_label = __( 'Older Comments' );
  18. $label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
  19. $pagination_arrow = get_comments_pagination_arrow( $block, 'previous' );
  20. if ( $pagination_arrow ) {
  21. $label = $pagination_arrow . $label;
  22. }
  23. $filter_link_attributes = function() {
  24. return get_block_wrapper_attributes();
  25. };
  26. add_filter( 'previous_comments_link_attributes', $filter_link_attributes );
  27. $previous_comments_link = get_previous_comments_link( $label );
  28. remove_filter( 'previous_comments_link_attributes', $filter_link_attributes );
  29. if ( ! isset( $previous_comments_link ) ) {
  30. return '';
  31. }
  32. return $previous_comments_link;
  33. }
  34. /**
  35. * Registers the `core/comments-pagination-previous` block on the server.
  36. */
  37. function register_block_core_comments_pagination_previous() {
  38. register_block_type_from_metadata(
  39. __DIR__ . '/comments-pagination-previous',
  40. array(
  41. 'render_callback' => 'render_block_core_comments_pagination_previous',
  42. )
  43. );
  44. }
  45. add_action( 'init', 'register_block_core_comments_pagination_previous' );