[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination']; 20 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; 21 $max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0; 22 23 $wrapper_attributes = get_block_wrapper_attributes(); 24 $content = ''; 25 global $wp_query; 26 $mid_size = isset( $block->attributes['midSize'] ) ? (int) $block->attributes['midSize'] : null; 27 if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { 28 // Take into account if we have set a bigger `max page` 29 // than what the query has. 30 $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; 31 $paginate_args = array( 32 'prev_next' => false, 33 'total' => $total, 34 ); 35 if ( null !== $mid_size ) { 36 $paginate_args['mid_size'] = $mid_size; 37 } 38 $content = paginate_links( $paginate_args ); 39 } else { 40 $block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) ); 41 // `paginate_links` works with the global $wp_query, so we have to 42 // temporarily switch it with our custom query. 43 $prev_wp_query = $wp_query; 44 $wp_query = $block_query; 45 $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; 46 $paginate_args = array( 47 'base' => '%_%', 48 'format' => "?$page_key=%#%", 49 'current' => max( 1, $page ), 50 'total' => $total, 51 'prev_next' => false, 52 ); 53 if ( null !== $mid_size ) { 54 $paginate_args['mid_size'] = $mid_size; 55 } 56 if ( 1 !== $page ) { 57 /** 58 * `paginate_links` doesn't use the provided `format` when the page is `1`. 59 * This is great for the main query as it removes the extra query params 60 * making the URL shorter, but in the case of multiple custom queries is 61 * problematic. It results in returning an empty link which ends up with 62 * a link to the current page. 63 * 64 * A way to address this is to add a `fake` query arg with no value that 65 * is the same for all custom queries. This way the link is not empty and 66 * preserves all the other existent query args. 67 * 68 * @see https://developer.wordpress.org/reference/functions/paginate_links/ 69 * 70 * The proper fix of this should be in core. Track Ticket: 71 * @see https://core.trac.wordpress.org/ticket/53868 72 * 73 * TODO: After two WP versions (starting from the WP version the core patch landed), 74 * we should remove this and call `paginate_links` with the proper new arg. 75 */ 76 $paginate_args['add_args'] = array( 'cst' => '' ); 77 } 78 // We still need to preserve `paged` query param if exists, as is used 79 // for Queries that inherit from global context. 80 $paged = empty( $_GET['paged'] ) ? null : (int) $_GET['paged']; 81 if ( $paged ) { 82 $paginate_args['add_args'] = array( 'paged' => $paged ); 83 } 84 $content = paginate_links( $paginate_args ); 85 wp_reset_postdata(); // Restore original Post Data. 86 $wp_query = $prev_wp_query; 87 } 88 89 if ( empty( $content ) ) { 90 return ''; 91 } 92 93 if ( $enhanced_pagination ) { 94 $p = new WP_HTML_Tag_Processor( $content ); 95 $tag_index = 0; 96 while ( $p->next_tag( 97 array( 'class_name' => 'page-numbers' ) 98 ) ) { 99 if ( null === $p->get_attribute( 'data-wp-key' ) ) { 100 $p->set_attribute( 'data-wp-key', 'index-' . $tag_index++ ); 101 } 102 if ( 'A' === $p->get_tag() ) { 103 $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' ); 104 } 105 } 106 $content = $p->get_updated_html(); 107 } 108 109 return sprintf( 110 '<div %1$s>%2$s</div>', 111 $wrapper_attributes, 112 $content 113 ); 114 } 115 116 /** 117 * Registers the `core/query-pagination-numbers` block on the server. 118 */ 119 function register_block_core_query_pagination_numbers() { 120 register_block_type_from_metadata( 121 __DIR__ . '/query-pagination-numbers', 122 array( 123 'render_callback' => 'render_block_core_query_pagination_numbers', 124 ) 125 ); 126 } 127 add_action( 'init', 'register_block_core_query_pagination_numbers' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Wed Apr 24 08:20:01 2024 | Cross-referenced by PHPXref |