Geen omschrijving

query-pagination-previous.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/query-pagination-previous` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/query-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 query.
  15. */
  16. function render_block_core_query_pagination_previous( $attributes, $content, $block ) {
  17. $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
  18. $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  19. $wrapper_attributes = get_block_wrapper_attributes();
  20. $default_label = __( '&laquo; Previous Page' );
  21. $label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
  22. $content = '';
  23. // Check if the pagination is for Query that inherits the global context
  24. // and handle appropriately.
  25. if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
  26. $filter_link_attributes = function() use ( $wrapper_attributes ) {
  27. return $wrapper_attributes;
  28. };
  29. add_filter( 'previous_posts_link_attributes', $filter_link_attributes );
  30. $content = get_previous_posts_link( $label );
  31. remove_filter( 'previous_posts_link_attributes', $filter_link_attributes );
  32. } elseif ( 1 !== $page ) {
  33. $content = sprintf(
  34. '<a href="%1$s" %2$s>%3$s</a>',
  35. esc_url( add_query_arg( $page_key, $page - 1 ) ),
  36. $wrapper_attributes,
  37. $label
  38. );
  39. }
  40. return $content;
  41. }
  42. /**
  43. * Registers the `core/query-pagination-previous` block on the server.
  44. */
  45. function register_block_core_query_pagination_previous() {
  46. register_block_type_from_metadata(
  47. __DIR__ . '/query-pagination-previous',
  48. array(
  49. 'render_callback' => 'render_block_core_query_pagination_previous',
  50. )
  51. );
  52. }
  53. add_action( 'init', 'register_block_core_query_pagination_previous' );