PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.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 / block-library / blocks / query-pagination-next.php

query-pagination-next.php in Gutenberg 12.6.0, at build/block-library/blocks/query-pagination-next.php

74 lines 2.7 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 * @param array $attributes Block attributes.
12 * @param string $content Block default content.
13 * @param WP_Block $block Block instance.
14 *
15 * @return string Returns the next posts link for the query pagination.
16 */
17 function gutenberg_render_block_core_query_pagination_next( $attributes, $content, $block ) {
18 $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
19 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
20 $max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0;
21
22 $wrapper_attributes = get_block_wrapper_attributes();
23 $default_label = __( 'Next Page' );
24 $label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label;
25 $pagination_arrow = get_query_pagination_arrow( $block, true );
26
27 if ( $pagination_arrow ) {
28 $label .= $pagination_arrow;
29 }
30 $content = '';
31
32 // Check if the pagination is for Query that inherits the global context.
33 if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
34 $filter_link_attributes = function() use ( $wrapper_attributes ) {
35 return $wrapper_attributes;
36 };
37 add_filter( 'next_posts_link_attributes', $filter_link_attributes );
38 // Take into account if we have set a bigger `max page`
39 // than what the query has.
40 global $wp_query;
41 if ( $max_page > $wp_query->max_num_pages ) {
42 $max_page = $wp_query->max_num_pages;
43 }
44 $content = get_next_posts_link( $label, $max_page );
45 remove_filter( 'next_posts_link_attributes', $filter_link_attributes );
46 } elseif ( ! $max_page || $max_page > $page ) {
47 $custom_query = new WP_Query( gutenberg_build_query_vars_from_query_block( $block, $page ) );
48 $custom_query_max_pages = (int) $custom_query->max_num_pages;
49 if ( $custom_query_max_pages && $custom_query_max_pages !== $page ) {
50 $content = sprintf(
51 '<a href="%1$s" %2$s>%3$s</a>',
52 esc_url( add_query_arg( $page_key, $page + 1 ) ),
53 $wrapper_attributes,
54 $label
55 );
56 }
57 wp_reset_postdata(); // Restore original Post Data.
58 }
59 return $content;
60 }
61
62 /**
63 * Registers the `core/query-pagination-next` block on the server.
64 */
65 function gutenberg_register_block_core_query_pagination_next() {
66 register_block_type_from_metadata(
67 __DIR__ . '/query-pagination-next',
68 array(
69 'render_callback' => 'gutenberg_render_block_core_query_pagination_next',
70 )
71 );
72 }
73 add_action( 'init', 'gutenberg_register_block_core_query_pagination_next', 20 );
74