1<?php
2/**
3 * Server-side rendering of the `core/query-pagination-next` block.
4 *
5 * @package WordPress
6 */
7
8/**
9 * Renders the `core/query-pagination-next` block on the server.
10 *
11 * @since 5.8.0
12 *
13 * @global WP_Query $wp_query WordPress Query object.
14 *
15 * @param array $attributes Block attributes.
16 * @param string $content Block default content.
17 * @param WP_Block $block Block instance.
18 *
19 * @return string Returns the next posts link for the query pagination.
20 */
21function render_block_core_query_pagination_next( $attributes, $content, $block ) {
22 $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
23 $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination'];
24 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
25 $max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0;
26
27 $wrapper_attributes = get_block_wrapper_attributes();
28 $show_label = isset( $block->context['showLabel'] ) ? (bool) $block->context['showLabel'] : true;
29 $default_label = __( 'Next Page' );
30 $label_text = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label;
31 $label = $show_label ? $label_text : '';
32 $pagination_arrow = get_query_pagination_arrow( $block, true );
33
34 if ( ! $label ) {
35 $wrapper_attributes .= ' aria-label="' . $label_text . '"';
36 }
37 if ( $pagination_arrow ) {
38 $label .= $pagination_arrow;
39 }
40 $content = '';
41
42 // Check if the pagination is for Query that inherits the global context.
43 if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
44 $filter_link_attributes = static function () use ( $wrapper_attributes ) {
45 return $wrapper_attributes;
46 };
47 add_filter( 'next_posts_link_attributes', $filter_link_attributes );
48 // Take into account if we have set a bigger `max page`
49 // than what the query has.
50 global $wp_query;
51 if ( $max_page > $wp_query->max_num_pages ) {
52 $max_page = $wp_query->max_num_pages;
53 }
54 $content = get_next_posts_link( $label, $max_page );
55 remove_filter( 'next_posts_link_attributes', $filter_link_attributes );
56 } elseif ( ! $max_page || $max_page > $page ) {
57 $custom_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
58 $custom_query_max_pages = (int) $custom_query->max_num_pages;
59 if ( $custom_query_max_pages && $custom_query_max_pages !== $page ) {
60 $content = sprintf(
61 '<a href="%1$s" %2$s>%3$s</a>',
62 esc_url( add_query_arg( $page_key, $page + 1 ) ),
63 $wrapper_attributes,
64 $label
65 );
66 }
67 wp_reset_postdata(); // Restore original Post Data.
68 }
69
70 if ( $enhanced_pagination && isset( $content ) ) {
71 $p = new WP_HTML_Tag_Processor( $content );
72 if ( $p->next_tag(
73 array(
74 'tag_name' => 'a',
75 'class_name' => 'wp-block-query-pagination-next',
76 )
77 ) ) {
78 $p->set_attribute( 'data-wp-key', 'query-pagination-next' );
79 $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
80 $p->set_attribute( 'data-wp-on--mouseenter', 'core/query::actions.prefetch' );
81 $p->set_attribute( 'data-wp-watch', 'core/query::callbacks.prefetch' );
82 $content = $p->get_updated_html();
83 }
84 }
85
86 return $content;
87}
88
89/**
90 * Registers the `core/query-pagination-next` block on the server.
91 *
92 * @since 5.8.0
93 */
94function register_block_core_query_pagination_next() {
95 register_block_type_from_metadata(
96 __DIR__ . '/query-pagination-next',
97 array(
98 'render_callback' => 'render_block_core_query_pagination_next',
99 )
100 );
101}
102add_action( 'init', 'register_block_core_query_pagination_next' );
103