|
1 <?php |
|
2 /** |
|
3 * Server-side rendering of the `core/query-no-results` block. |
|
4 * |
|
5 * @package WordPress |
|
6 */ |
|
7 |
|
8 /** |
|
9 * Renders the `core/query-no-results` 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 wrapper for the no results block. |
|
16 */ |
|
17 function render_block_core_query_no_results( $attributes, $content, $block ) { |
|
18 if ( empty( trim( $content ) ) ) { |
|
19 return ''; |
|
20 } |
|
21 |
|
22 $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; |
|
23 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; |
|
24 |
|
25 // Override the custom query with the global query if needed. |
|
26 $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); |
|
27 if ( $use_global_query ) { |
|
28 global $wp_query; |
|
29 $query = $wp_query; |
|
30 } else { |
|
31 $query_args = build_query_vars_from_query_block( $block, $page ); |
|
32 $query = new WP_Query( $query_args ); |
|
33 } |
|
34 |
|
35 if ( $query->have_posts() ) { |
|
36 return ''; |
|
37 } |
|
38 |
|
39 if ( ! $use_global_query ) { |
|
40 wp_reset_postdata(); |
|
41 } |
|
42 |
|
43 return sprintf( |
|
44 '<div %1$s>%2$s</div>', |
|
45 get_block_wrapper_attributes(), |
|
46 $content |
|
47 ); |
|
48 } |
|
49 |
|
50 /** |
|
51 * Registers the `core/query-no-results` block on the server. |
|
52 */ |
|
53 function register_block_core_query_no_results() { |
|
54 register_block_type_from_metadata( |
|
55 __DIR__ . '/query-no-results', |
|
56 array( |
|
57 'render_callback' => 'render_block_core_query_no_results', |
|
58 ) |
|
59 ); |
|
60 } |
|
61 add_action( 'init', 'register_block_core_query_no_results' ); |