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