PluginProbe
Gutenberg / 10.4.1
Gutenberg v10.4.1
24.0.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 All 403 releases
gutenberg / build / block-library / blocks / query-pagination-numbers.php

query-pagination-numbers.php in Gutenberg 10.4.1, at build/block-library/blocks/query-pagination-numbers.php

80 lines 2.8 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-numbers` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/query-pagination-numbers` 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 pagination numbers for the Query.
16 */
17 function gutenberg_render_block_core_query_pagination_numbers( $attributes, $content, $block ) {
18 $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
19 $page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT );
20 $max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0;
21
22 $wrapper_attributes = get_block_wrapper_attributes();
23 $content = '';
24 global $wp_query;
25 if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
26 // Take into account if we have set a bigger `max page`
27 // than what the query has.
28 $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page;
29 $paginate_args = array(
30 'prev_next' => false,
31 'total' => $total,
32 );
33 $content = paginate_links( $paginate_args );
34 } else {
35 $block_query = new WP_Query( construct_wp_query_args( $block, $page ) );
36 // `paginate_links` works with the global $wp_query, so we have to
37 // temporarily switch it with our custom query.
38 $prev_wp_query = $wp_query;
39 $wp_query = $block_query;
40 $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page;
41 $paginate_args = array(
42 'base' => '%_%',
43 'format' => "?$page_key=%#%",
44 'current' => max( 1, $page ),
45 'total' => $total,
46 'prev_next' => false,
47 );
48 // We still need to preserve `paged` query param if exists, as is used
49 // for Queries that inherit from global context.
50 $paged = empty( $_GET['paged'] ) ? null : (int) $_GET['paged'];
51 if ( $paged ) {
52 $paginate_args['add_args'] = array( 'paged' => $paged );
53 }
54 $content = paginate_links( $paginate_args );
55 wp_reset_postdata(); // Restore original Post Data.
56 $wp_query = $prev_wp_query;
57 }
58 if ( empty( $content ) ) {
59 return '';
60 }
61 return sprintf(
62 '<div %1$s>%2$s</div>',
63 $wrapper_attributes,
64 $content
65 );
66 }
67
68 /**
69 * Registers the `core/query-pagination-numbers` block on the server.
70 */
71 function gutenberg_register_block_core_query_pagination_numbers() {
72 register_block_type_from_metadata(
73 __DIR__ . '/query-pagination-numbers',
74 array(
75 'render_callback' => 'gutenberg_render_block_core_query_pagination_numbers',
76 )
77 );
78 }
79 add_action( 'init', 'gutenberg_register_block_core_query_pagination_numbers', 20 );
80