PluginProbe
Gutenberg / 16.6.0
Gutenberg v16.6.0
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.php

query.php in Gutenberg 16.6.0, at build/block-library/blocks/query.php

110 lines 3.6 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` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Modifies the static `core/query` block on the server.
10 *
11 * @since X.X.X
12 *
13 * @param array $attributes Block attributes.
14 * @param string $content Block default content.
15 * @param string $block Block instance.
16 *
17 * @return string Returns the modified output of the query block.
18 */
19 function gutenberg_render_block_core_query( $attributes, $content, $block ) {
20 if ( $attributes['enhancedPagination'] ) {
21 $p = new WP_HTML_Tag_Processor( $content );
22 if ( $p->next_tag() ) {
23 // Add the necessary directives.
24 $p->set_attribute( 'data-wp-interactive', true );
25 $p->set_attribute( 'data-wp-navigation-id', 'query-' . $attributes['queryId'] );
26 $p->set_attribute(
27 'data-wp-context',
28 wp_json_encode( array( 'core' => array( 'query' => (object) array() ) ) )
29 );
30 $content = $p->get_updated_html();
31
32 // Mark the block as interactive.
33 $block->block_type->supports['interactivity'] = true;
34
35 // Add a div to announce messages using `aria-live`.
36 $last_div_position = strripos( $content, '</div>' );
37 $content = substr_replace(
38 $content,
39 '<div
40 class="wp-block-query__enhanced-pagination-navigation-announce"
41 aria-live="polite"
42 data-wp-text="context.core.query.message"
43 ></div>
44 <div
45 class="wp-block-query__enhanced-pagination-animation"
46 data-wp-class--start-animation="selectors.core.query.startAnimation"
47 data-wp-class--finish-animation="selectors.core.query.finishAnimation"
48 ></div>',
49 $last_div_position,
50 0
51 );
52
53 // Use state to send translated strings.
54 wp_store(
55 array(
56 'state' => array(
57 'core' => array(
58 'query' => array(
59 'loadingText' => __( 'Loading page, please wait.' ),
60 'loadedText' => __( 'Page Loaded.' ),
61 ),
62 ),
63 ),
64 )
65 );
66 }
67 }
68
69 $view_asset = 'wp-block-query-view';
70 if ( ! wp_script_is( $view_asset ) ) {
71 $script_handles = $block->block_type->view_script_handles;
72 // If the script is not needed, and it is still in the `view_script_handles`, remove it.
73 if ( ! $attributes['enhancedPagination'] && in_array( $view_asset, $script_handles, true ) ) {
74 $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_asset ) );
75 }
76 // If the script is needed, but it was previously removed, add it again.
77 if ( $attributes['enhancedPagination'] && ! in_array( $view_asset, $script_handles, true ) ) {
78 $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_asset ) );
79 }
80 }
81
82 $style_asset = 'wp-block-query';
83 if ( ! wp_style_is( $style_asset ) ) {
84 $style_handles = $block->block_type->style_handles;
85 // If the styles are not needed, and they are still in the `style_handles`, remove them.
86 if ( ! $attributes['enhancedPagination'] && in_array( $style_asset, $style_handles, true ) ) {
87 $block->block_type->style_handles = array_diff( $style_handles, array( $style_asset ) );
88 }
89 // If the styles are needed, but they were previously removed, add them again.
90 if ( $attributes['enhancedPagination'] && ! in_array( $style_asset, $style_handles, true ) ) {
91 $block->block_type->style_handles = array_merge( $style_handles, array( $style_asset ) );
92 }
93 }
94
95 return $content;
96 }
97
98 /**
99 * Registers the `core/query` block on the server.
100 */
101 function gutenberg_register_block_core_query() {
102 register_block_type_from_metadata(
103 __DIR__ . '/query',
104 array(
105 'render_callback' => 'gutenberg_render_block_core_query',
106 )
107 );
108 }
109 add_action( 'init', 'gutenberg_register_block_core_query', 20 );
110