PluginProbe
Gutenberg / 22.9.0
Gutenberg v22.9.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / query-pagination-next.php

query-pagination-next.php in Gutenberg 22.9.0, at build/scripts/block-library/query-pagination-next.php

103 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 */
21 function gutenberg_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 = (bool) ( $block->context['enhancedPagination'] ?? false );
24 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
25 $max_page = (int) ( $block->context['query']['pages'] ?? 0 );
26
27 $wrapper_attributes = get_block_wrapper_attributes();
28 $show_label = (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 */
94 function gutenberg_register_block_core_query_pagination_next() {
95 register_block_type_from_metadata(
96 __DIR__ . '/query-pagination-next',
97 array(
98 'render_callback' => 'gutenberg_render_block_core_query_pagination_next',
99 )
100 );
101 }
102 add_action( 'init', 'gutenberg_register_block_core_query_pagination_next', 20 );
103