wp/wp-includes/blocks/query-pagination-numbers.php
changeset 18 be944660c56a
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
       
     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 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 : (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 	$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( build_query_vars_from_query_block( $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 register_block_core_query_pagination_numbers() {
       
    72 	register_block_type_from_metadata(
       
    73 		__DIR__ . '/query-pagination-numbers',
       
    74 		array(
       
    75 			'render_callback' => 'render_block_core_query_pagination_numbers',
       
    76 		)
       
    77 	);
       
    78 }
       
    79 add_action( 'init', 'register_block_core_query_pagination_numbers' );