author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
18 | 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 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
11 |
* @since 5.8.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
13 |
* @global WP_Query $wp_query WordPress Query object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
14 |
* |
18 | 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 |
*/ |
|
21 |
function render_block_core_query_pagination_next( $attributes, $content, $block ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
22 |
$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
23 |
$enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination']; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
24 |
$page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
25 |
$max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0; |
18 | 26 |
|
27 |
$wrapper_attributes = get_block_wrapper_attributes(); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
28 |
$show_label = isset( $block->context['showLabel'] ) ? (bool) $block->context['showLabel'] : true; |
19 | 29 |
$default_label = __( 'Next Page' ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
30 |
$label_text = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
31 |
$label = $show_label ? $label_text : ''; |
19 | 32 |
$pagination_arrow = get_query_pagination_arrow( $block, true ); |
33 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
34 |
if ( ! $label ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
35 |
$wrapper_attributes .= ' aria-label="' . $label_text . '"'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
36 |
} |
19 | 37 |
if ( $pagination_arrow ) { |
38 |
$label .= $pagination_arrow; |
|
39 |
} |
|
40 |
$content = ''; |
|
18 | 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'] ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
44 |
$filter_link_attributes = static function () use ( $wrapper_attributes ) { |
18 | 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 ) { |
|
19 | 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 ) { |
|
18 | 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 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
70 |
if ( $enhanced_pagination && isset( $content ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
$p = new WP_HTML_Tag_Processor( $content ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
if ( $p->next_tag( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
74 |
'tag_name' => 'a', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
'class_name' => 'wp-block-query-pagination-next', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
77 |
) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
78 |
$p->set_attribute( 'data-wp-key', 'query-pagination-next' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
79 |
$p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
80 |
$p->set_attribute( 'data-wp-on-async--mouseenter', 'core/query::actions.prefetch' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
81 |
$p->set_attribute( 'data-wp-watch', 'core/query::callbacks.prefetch' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
82 |
$content = $p->get_updated_html(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
83 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
84 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
85 |
|
18 | 86 |
return $content; |
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Registers the `core/query-pagination-next` block on the server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
91 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
92 |
* @since 5.8.0 |
18 | 93 |
*/ |
94 |
function 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 |
} |
|
102 |
add_action( 'init', 'register_block_core_query_pagination_next' ); |