| 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 gutenberg_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'] ) ? esc_html( $attributes['label'] ) : $default_label; |
| 24 |
$pagination_arrow = get_query_pagination_arrow( $block, false ); |
| 25 |
if ( $pagination_arrow ) { |
| 26 |
$label = $pagination_arrow . $label; |
| 27 |
} |
| 28 |
$content = ''; |
| 29 |
// Check if the pagination is for Query that inherits the global context |
| 30 |
// and handle appropriately. |
| 31 |
if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { |
| 32 |
$filter_link_attributes = function() use ( $wrapper_attributes ) { |
| 33 |
return $wrapper_attributes; |
| 34 |
}; |
| 35 |
|
| 36 |
add_filter( 'previous_posts_link_attributes', $filter_link_attributes ); |
| 37 |
$content = get_previous_posts_link( $label ); |
| 38 |
remove_filter( 'previous_posts_link_attributes', $filter_link_attributes ); |
| 39 |
} elseif ( 1 !== $page ) { |
| 40 |
$content = sprintf( |
| 41 |
'<a href="%1$s" %2$s>%3$s</a>', |
| 42 |
esc_url( add_query_arg( $page_key, $page - 1 ) ), |
| 43 |
$wrapper_attributes, |
| 44 |
$label |
| 45 |
); |
| 46 |
} |
| 47 |
return $content; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Registers the `core/query-pagination-previous` block on the server. |
| 52 |
*/ |
| 53 |
function gutenberg_register_block_core_query_pagination_previous() { |
| 54 |
register_block_type_from_metadata( |
| 55 |
__DIR__ . '/query-pagination-previous', |
| 56 |
array( |
| 57 |
'render_callback' => 'gutenberg_render_block_core_query_pagination_previous', |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
add_action( 'init', 'gutenberg_register_block_core_query_pagination_previous', 20 ); |
| 62 |
|