6 */ |
6 */ |
7 |
7 |
8 /** |
8 /** |
9 * Renders the `core/query-pagination-next` block on the server. |
9 * Renders the `core/query-pagination-next` 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 next posts link for the query pagination. |
19 * @return string Returns the next posts link for the query pagination. |
16 */ |
20 */ |
17 function render_block_core_query_pagination_next( $attributes, $content, $block ) { |
21 function render_block_core_query_pagination_next( $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(); |
|
28 $show_label = isset( $block->context['showLabel'] ) ? (bool) $block->context['showLabel'] : true; |
23 $default_label = __( 'Next Page' ); |
29 $default_label = __( 'Next Page' ); |
24 $label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label; |
30 $label_text = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label; |
|
31 $label = $show_label ? $label_text : ''; |
25 $pagination_arrow = get_query_pagination_arrow( $block, true ); |
32 $pagination_arrow = get_query_pagination_arrow( $block, true ); |
26 |
33 |
|
34 if ( ! $label ) { |
|
35 $wrapper_attributes .= ' aria-label="' . $label_text . '"'; |
|
36 } |
27 if ( $pagination_arrow ) { |
37 if ( $pagination_arrow ) { |
28 $label .= $pagination_arrow; |
38 $label .= $pagination_arrow; |
29 } |
39 } |
30 $content = ''; |
40 $content = ''; |
31 |
41 |
32 // Check if the pagination is for Query that inherits the global context. |
42 // Check if the pagination is for Query that inherits the global context. |
33 if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { |
43 if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { |
34 $filter_link_attributes = function() use ( $wrapper_attributes ) { |
44 $filter_link_attributes = static function () use ( $wrapper_attributes ) { |
35 return $wrapper_attributes; |
45 return $wrapper_attributes; |
36 }; |
46 }; |
37 add_filter( 'next_posts_link_attributes', $filter_link_attributes ); |
47 add_filter( 'next_posts_link_attributes', $filter_link_attributes ); |
38 // Take into account if we have set a bigger `max page` |
48 // Take into account if we have set a bigger `max page` |
39 // than what the query has. |
49 // than what the query has. |
54 $label |
64 $label |
55 ); |
65 ); |
56 } |
66 } |
57 wp_reset_postdata(); // Restore original Post Data. |
67 wp_reset_postdata(); // Restore original Post Data. |
58 } |
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-async--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 |
59 return $content; |
86 return $content; |
60 } |
87 } |
61 |
88 |
62 /** |
89 /** |
63 * Registers the `core/query-pagination-next` block on the server. |
90 * Registers the `core/query-pagination-next` block on the server. |
|
91 * |
|
92 * @since 5.8.0 |
64 */ |
93 */ |
65 function register_block_core_query_pagination_next() { |
94 function register_block_core_query_pagination_next() { |
66 register_block_type_from_metadata( |
95 register_block_type_from_metadata( |
67 __DIR__ . '/query-pagination-next', |
96 __DIR__ . '/query-pagination-next', |
68 array( |
97 array( |