Changeset 56710 for trunk/src/wp-includes/blocks/query.php
- Timestamp:
- 09/26/2023 02:20:18 PM (3 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/blocks/query.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/query.php
r50929 r56710 7 7 8 8 /** 9 * Modifies the static `core/query` block on the server. 10 * 11 * @since 6.4.0 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 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 // Use context to send translated strings. 27 $p->set_attribute( 28 'data-wp-context', 29 wp_json_encode( 30 array( 31 'core' => array( 32 'query' => array( 33 'loadingText' => __( 'Loading page, please wait.' ), 34 'loadedText' => __( 'Page Loaded.' ), 35 ), 36 ), 37 ) 38 ) 39 ); 40 $content = $p->get_updated_html(); 41 42 // Mark the block as interactive. 43 $block->block_type->supports['interactivity'] = true; 44 45 // Add a div to announce messages using `aria-live`. 46 $last_div_position = strripos( $content, '</div>' ); 47 $content = substr_replace( 48 $content, 49 '<div 50 class="wp-block-query__enhanced-pagination-navigation-announce" 51 aria-live="polite" 52 data-wp-text="context.core.query.message" 53 ></div> 54 <div 55 class="wp-block-query__enhanced-pagination-animation" 56 data-wp-class--start-animation="selectors.core.query.startAnimation" 57 data-wp-class--finish-animation="selectors.core.query.finishAnimation" 58 ></div>', 59 $last_div_position, 60 0 61 ); 62 } 63 } 64 65 $view_asset = 'wp-block-query-view'; 66 if ( ! wp_script_is( $view_asset ) ) { 67 $script_handles = $block->block_type->view_script_handles; 68 // If the script is not needed, and it is still in the `view_script_handles`, remove it. 69 if ( ! $attributes['enhancedPagination'] && in_array( $view_asset, $script_handles, true ) ) { 70 $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_asset ) ); 71 } 72 // If the script is needed, but it was previously removed, add it again. 73 if ( $attributes['enhancedPagination'] && ! in_array( $view_asset, $script_handles, true ) ) { 74 $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_asset ) ); 75 } 76 } 77 78 $style_asset = 'wp-block-query'; 79 if ( ! wp_style_is( $style_asset ) ) { 80 $style_handles = $block->block_type->style_handles; 81 // If the styles are not needed, and they are still in the `style_handles`, remove them. 82 if ( ! $attributes['enhancedPagination'] && in_array( $style_asset, $style_handles, true ) ) { 83 $block->block_type->style_handles = array_diff( $style_handles, array( $style_asset ) ); 84 } 85 // If the styles are needed, but they were previously removed, add them again. 86 if ( $attributes['enhancedPagination'] && ! in_array( $style_asset, $style_handles, true ) ) { 87 $block->block_type->style_handles = array_merge( $style_handles, array( $style_asset ) ); 88 } 89 } 90 91 return $content; 92 } 93 94 /** 95 * Ensure that the view script has the `wp-interactivity` dependency. 96 * 97 * @since 6.4.0 98 * 99 * @global WP_Scripts $wp_scripts 100 */ 101 function block_core_query_ensure_interactivity_dependency() { 102 global $wp_scripts; 103 if ( 104 isset( $wp_scripts->registered['wp-block-query-view'] ) && 105 ! in_array( 'wp-interactivity', $wp_scripts->registered['wp-block-query-view']->deps, true ) 106 ) { 107 $wp_scripts->registered['wp-block-query-view']->deps[] = 'wp-interactivity'; 108 } 109 } 110 111 add_action( 'wp_print_scripts', 'block_core_query_ensure_interactivity_dependency' ); 112 113 /** 9 114 * Registers the `core/query` block on the server. 10 115 */ 11 116 function register_block_core_query() { 12 117 register_block_type_from_metadata( 13 __DIR__ . '/query' 118 __DIR__ . '/query', 119 array( 120 'render_callback' => 'render_block_core_query', 121 ) 14 122 ); 15 123 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)