6 */ |
6 */ |
7 |
7 |
8 /** |
8 /** |
9 * Renders the `core/query-pagination-numbers` block on the server. |
9 * Renders the `core/query-pagination-numbers` block on the server. |
10 * |
10 * |
|
11 * @since 5.8.0 |
|
12 * |
|
13 * @global WP_Query $wp_query WordPress Query object. |
|
14 * |
11 * @param array $attributes Block attributes. |
15 * @param array $attributes Block attributes. |
12 * @param string $content Block default content. |
16 * @param string $content Block default content. |
13 * @param WP_Block $block Block instance. |
17 * @param WP_Block $block Block instance. |
14 * |
18 * |
15 * @return string Returns the pagination numbers for the Query. |
19 * @return string Returns the pagination numbers for the Query. |
16 */ |
20 */ |
17 function render_block_core_query_pagination_numbers( $attributes, $content, $block ) { |
21 function render_block_core_query_pagination_numbers( $attributes, $content, $block ) { |
18 $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; |
22 $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; |
19 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; |
23 $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination']; |
20 $max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0; |
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; |
21 |
26 |
22 $wrapper_attributes = get_block_wrapper_attributes(); |
27 $wrapper_attributes = get_block_wrapper_attributes(); |
23 $content = ''; |
28 $content = ''; |
24 global $wp_query; |
29 global $wp_query; |
|
30 $mid_size = isset( $block->attributes['midSize'] ) ? (int) $block->attributes['midSize'] : null; |
25 if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { |
31 if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { |
26 // Take into account if we have set a bigger `max page` |
32 // Take into account if we have set a bigger `max page` |
27 // than what the query has. |
33 // than what the query has. |
28 $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; |
34 $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; |
29 $paginate_args = array( |
35 $paginate_args = array( |
30 'prev_next' => false, |
36 'prev_next' => false, |
31 'total' => $total, |
37 'total' => $total, |
32 ); |
38 ); |
33 $content = paginate_links( $paginate_args ); |
39 if ( null !== $mid_size ) { |
|
40 $paginate_args['mid_size'] = $mid_size; |
|
41 } |
|
42 $content = paginate_links( $paginate_args ); |
34 } else { |
43 } else { |
35 $block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) ); |
44 $block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) ); |
36 // `paginate_links` works with the global $wp_query, so we have to |
45 // `paginate_links` works with the global $wp_query, so we have to |
37 // temporarily switch it with our custom query. |
46 // temporarily switch it with our custom query. |
38 $prev_wp_query = $wp_query; |
47 $prev_wp_query = $wp_query; |
43 'format' => "?$page_key=%#%", |
52 'format' => "?$page_key=%#%", |
44 'current' => max( 1, $page ), |
53 'current' => max( 1, $page ), |
45 'total' => $total, |
54 'total' => $total, |
46 'prev_next' => false, |
55 'prev_next' => false, |
47 ); |
56 ); |
|
57 if ( null !== $mid_size ) { |
|
58 $paginate_args['mid_size'] = $mid_size; |
|
59 } |
48 if ( 1 !== $page ) { |
60 if ( 1 !== $page ) { |
49 /** |
61 /** |
50 * `paginate_links` doesn't use the provided `format` when the page is `1`. |
62 * `paginate_links` doesn't use the provided `format` when the page is `1`. |
51 * This is great for the main query as it removes the extra query params |
63 * This is great for the main query as it removes the extra query params |
52 * making the URL shorter, but in the case of multiple custom queries is |
64 * making the URL shorter, but in the case of multiple custom queries is |
75 } |
87 } |
76 $content = paginate_links( $paginate_args ); |
88 $content = paginate_links( $paginate_args ); |
77 wp_reset_postdata(); // Restore original Post Data. |
89 wp_reset_postdata(); // Restore original Post Data. |
78 $wp_query = $prev_wp_query; |
90 $wp_query = $prev_wp_query; |
79 } |
91 } |
|
92 |
80 if ( empty( $content ) ) { |
93 if ( empty( $content ) ) { |
81 return ''; |
94 return ''; |
82 } |
95 } |
|
96 |
|
97 if ( $enhanced_pagination ) { |
|
98 $p = new WP_HTML_Tag_Processor( $content ); |
|
99 $tag_index = 0; |
|
100 while ( $p->next_tag( |
|
101 array( 'class_name' => 'page-numbers' ) |
|
102 ) ) { |
|
103 if ( null === $p->get_attribute( 'data-wp-key' ) ) { |
|
104 $p->set_attribute( 'data-wp-key', 'index-' . $tag_index++ ); |
|
105 } |
|
106 if ( 'A' === $p->get_tag() ) { |
|
107 $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' ); |
|
108 } |
|
109 } |
|
110 $content = $p->get_updated_html(); |
|
111 } |
|
112 |
83 return sprintf( |
113 return sprintf( |
84 '<div %1$s>%2$s</div>', |
114 '<div %1$s>%2$s</div>', |
85 $wrapper_attributes, |
115 $wrapper_attributes, |
86 $content |
116 $content |
87 ); |
117 ); |
88 } |
118 } |
89 |
119 |
90 /** |
120 /** |
91 * Registers the `core/query-pagination-numbers` block on the server. |
121 * Registers the `core/query-pagination-numbers` block on the server. |
|
122 * |
|
123 * @since 5.8.0 |
92 */ |
124 */ |
93 function register_block_core_query_pagination_numbers() { |
125 function register_block_core_query_pagination_numbers() { |
94 register_block_type_from_metadata( |
126 register_block_type_from_metadata( |
95 __DIR__ . '/query-pagination-numbers', |
127 __DIR__ . '/query-pagination-numbers', |
96 array( |
128 array( |