author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
18 | 1 |
<?php |
2 |
/** |
|
3 |
* Server-side rendering of the `core/query-pagination-numbers` block. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
||
8 |
/** |
|
9 |
* Renders the `core/query-pagination-numbers` 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 pagination numbers for the Query. |
|
20 |
*/ |
|
21 |
function render_block_core_query_pagination_numbers( $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(); |
|
28 |
$content = ''; |
|
29 |
global $wp_query; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
30 |
$mid_size = isset( $block->attributes['midSize'] ) ? (int) $block->attributes['midSize'] : null; |
18 | 31 |
if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { |
32 |
// Take into account if we have set a bigger `max page` |
|
33 |
// than what the query has. |
|
34 |
$total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; |
|
35 |
$paginate_args = array( |
|
36 |
'prev_next' => false, |
|
37 |
'total' => $total, |
|
38 |
); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
39 |
if ( null !== $mid_size ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
40 |
$paginate_args['mid_size'] = $mid_size; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
41 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
42 |
$content = paginate_links( $paginate_args ); |
18 | 43 |
} else { |
44 |
$block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) ); |
|
45 |
// `paginate_links` works with the global $wp_query, so we have to |
|
46 |
// temporarily switch it with our custom query. |
|
47 |
$prev_wp_query = $wp_query; |
|
48 |
$wp_query = $block_query; |
|
49 |
$total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; |
|
50 |
$paginate_args = array( |
|
51 |
'base' => '%_%', |
|
52 |
'format' => "?$page_key=%#%", |
|
53 |
'current' => max( 1, $page ), |
|
54 |
'total' => $total, |
|
55 |
'prev_next' => false, |
|
56 |
); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
57 |
if ( null !== $mid_size ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
58 |
$paginate_args['mid_size'] = $mid_size; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
59 |
} |
19 | 60 |
if ( 1 !== $page ) { |
61 |
/** |
|
62 |
* `paginate_links` doesn't use the provided `format` when the page is `1`. |
|
63 |
* This is great for the main query as it removes the extra query params |
|
64 |
* making the URL shorter, but in the case of multiple custom queries is |
|
65 |
* problematic. It results in returning an empty link which ends up with |
|
66 |
* a link to the current page. |
|
67 |
* |
|
68 |
* A way to address this is to add a `fake` query arg with no value that |
|
69 |
* is the same for all custom queries. This way the link is not empty and |
|
70 |
* preserves all the other existent query args. |
|
71 |
* |
|
72 |
* @see https://developer.wordpress.org/reference/functions/paginate_links/ |
|
73 |
* |
|
74 |
* The proper fix of this should be in core. Track Ticket: |
|
75 |
* @see https://core.trac.wordpress.org/ticket/53868 |
|
76 |
* |
|
77 |
* TODO: After two WP versions (starting from the WP version the core patch landed), |
|
78 |
* we should remove this and call `paginate_links` with the proper new arg. |
|
79 |
*/ |
|
80 |
$paginate_args['add_args'] = array( 'cst' => '' ); |
|
81 |
} |
|
18 | 82 |
// We still need to preserve `paged` query param if exists, as is used |
83 |
// for Queries that inherit from global context. |
|
84 |
$paged = empty( $_GET['paged'] ) ? null : (int) $_GET['paged']; |
|
85 |
if ( $paged ) { |
|
86 |
$paginate_args['add_args'] = array( 'paged' => $paged ); |
|
87 |
} |
|
88 |
$content = paginate_links( $paginate_args ); |
|
89 |
wp_reset_postdata(); // Restore original Post Data. |
|
90 |
$wp_query = $prev_wp_query; |
|
91 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
92 |
|
18 | 93 |
if ( empty( $content ) ) { |
94 |
return ''; |
|
95 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
97 |
if ( $enhanced_pagination ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
98 |
$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
|
99 |
$tag_index = 0; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
100 |
while ( $p->next_tag( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
101 |
array( 'class_name' => 'page-numbers' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
102 |
) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
103 |
if ( null === $p->get_attribute( 'data-wp-key' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
104 |
$p->set_attribute( 'data-wp-key', 'index-' . $tag_index++ ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
if ( 'A' === $p->get_tag() ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
$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
|
108 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
109 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
$content = $p->get_updated_html(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
112 |
|
18 | 113 |
return sprintf( |
114 |
'<div %1$s>%2$s</div>', |
|
115 |
$wrapper_attributes, |
|
116 |
$content |
|
117 |
); |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Registers the `core/query-pagination-numbers` block on the server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
122 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
123 |
* @since 5.8.0 |
18 | 124 |
*/ |
125 |
function register_block_core_query_pagination_numbers() { |
|
126 |
register_block_type_from_metadata( |
|
127 |
__DIR__ . '/query-pagination-numbers', |
|
128 |
array( |
|
129 |
'render_callback' => 'render_block_core_query_pagination_numbers', |
|
130 |
) |
|
131 |
); |
|
132 |
} |
|
133 |
add_action( 'init', 'register_block_core_query_pagination_numbers' ); |