|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Server-side rendering of the `core/query-total` block. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Renders the `query-total` block on the server. |
| 10 | + * |
| 11 | + * @since 6.8.0 |
| 12 | + * |
| 13 | + * @param array $attributes Block attributes. |
| 14 | + * @param string $content Block default content. |
| 15 | + * @param WP_Block $block Block instance. |
| 16 | + * |
| 17 | + * @return string The rendered block content. |
| 18 | + */ |
| 19 | +function render_block_core_query_total( $attributes, $content, $block ) { |
| 20 | + global $wp_query; |
| 21 | + $wrapper_attributes = get_block_wrapper_attributes(); |
| 22 | + if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { |
| 23 | + $query_to_use = $wp_query; |
| 24 | + $current_page = max( 1, get_query_var( 'paged', 1 ) ); |
| 25 | + } else { |
| 26 | + $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; |
| 27 | + $current_page = isset( $_GET[ $page_key ] ) ? (int) $_GET[ $page_key ] : 1; |
| 28 | + $query_to_use = new WP_Query( build_query_vars_from_query_block( $block, $current_page ) ); |
| 29 | + } |
| 30 | + |
| 31 | + $max_rows = $query_to_use->found_posts; |
| 32 | + $posts_per_page = $query_to_use->get( 'posts_per_page' ); |
| 33 | + |
| 34 | + // Calculate the range of posts being displayed. |
| 35 | + $start = ( $current_page - 1 ) * $posts_per_page + 1; |
| 36 | + $end = min( $start + $posts_per_page - 1, $max_rows ); |
| 37 | + |
| 38 | + // Prepare the display based on the `displayType` attribute. |
| 39 | + $output = ''; |
| 40 | + switch ( $attributes['displayType'] ) { |
| 41 | + case 'range-display': |
| 42 | + if ( $start === $end ) { |
| 43 | + $output = sprintf( |
| 44 | + /* translators: 1: Start index of posts, 2: Total number of posts */ |
| 45 | + __( 'Displaying %1$s of %2$s' ), |
| 46 | + $start, |
| 47 | + $max_rows |
| 48 | + ); |
| 49 | + } else { |
| 50 | + $output = sprintf( |
| 51 | + /* translators: 1: Start index of posts, 2: End index of posts, 3: Total number of posts */ |
| 52 | + __( 'Displaying %1$s – %2$s of %3$s' ), |
| 53 | + $start, |
| 54 | + $end, |
| 55 | + $max_rows |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + break; |
| 60 | + |
| 61 | + case 'total-results': |
| 62 | + default: |
| 63 | + // translators: %d: number of results. |
| 64 | + $output = sprintf( _n( '%d result found', '%d results found', $max_rows ), $max_rows ); |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + return sprintf( |
| 69 | + '<div %1$s>%2$s</div>', |
| 70 | + $wrapper_attributes, |
| 71 | + $output |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Registers the `query-total` block. |
| 77 | + * |
| 78 | + * @since 6.8.0 |
| 79 | + */ |
| 80 | +function register_block_core_query_total() { |
| 81 | + register_block_type_from_metadata( |
| 82 | + __DIR__ . '/query-total', |
| 83 | + array( |
| 84 | + 'render_callback' => 'render_block_core_query_total', |
| 85 | + ) |
| 86 | + ); |
| 87 | +} |
| 88 | +add_action( 'init', 'register_block_core_query_total' ); |
0 commit comments