Changeset 57049 for branches/6.4/src/wp-includes/blocks/query.php
- Timestamp:
- 11/01/2023 06:09:01 PM (11 months ago)
- Location:
- branches/6.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/6.4
-
branches/6.4/src/wp-includes/blocks/query.php
r56816 r57049 11 11 * @since 6.4.0 12 12 * 13 * @param array $attributes Block attributes.14 * @param string $content Block default content.15 * @param string $block Block instance.13 * @param array $attributes Block attributes. 14 * @param string $content Block default content. 15 * @param WP_Block $block The block instance. 16 16 * 17 17 * @return string Returns the modified output of the query block. 18 18 */ 19 19 function render_block_core_query( $attributes, $content, $block ) { 20 if ( $attributes['enhancedPagination'] ) {20 if ( $attributes['enhancedPagination'] && isset( $attributes['queryId'] ) ) { 21 21 $p = new WP_HTML_Tag_Processor( $content ); 22 22 if ( $p->next_tag() ) { … … 49 49 $content, 50 50 '<div 51 class="wp-block-query__enhanced-pagination-navigation-announce "51 class="wp-block-query__enhanced-pagination-navigation-announce screen-reader-text" 52 52 aria-live="polite" 53 53 data-wp-text="context.core.query.message" … … 68 68 $script_handles = $block->block_type->view_script_handles; 69 69 // If the script is not needed, and it is still in the `view_script_handles`, remove it. 70 if ( ! $attributes['enhancedPagination'] && in_array( $view_asset, $script_handles, true ) ) { 70 if ( 71 ( ! $attributes['enhancedPagination'] || ! isset( $attributes['queryId'] ) ) 72 && in_array( $view_asset, $script_handles, true ) 73 ) { 71 74 $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_asset ) ); 72 75 } 73 76 // If the script is needed, but it was previously removed, add it again. 74 if ( $attributes['enhancedPagination'] && ! in_array( $view_asset, $script_handles, true ) ) {77 if ( $attributes['enhancedPagination'] && isset( $attributes['queryId'] ) && ! in_array( $view_asset, $script_handles, true ) ) { 75 78 $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_asset ) ); 76 79 } … … 81 84 $style_handles = $block->block_type->style_handles; 82 85 // If the styles are not needed, and they are still in the `style_handles`, remove them. 83 if ( ! $attributes['enhancedPagination'] && in_array( $style_asset, $style_handles, true ) ) { 86 if ( 87 ( ! $attributes['enhancedPagination'] || ! isset( $attributes['queryId'] ) ) 88 && in_array( $style_asset, $style_handles, true ) 89 ) { 84 90 $block->block_type->style_handles = array_diff( $style_handles, array( $style_asset ) ); 85 91 } 86 92 // If the styles are needed, but they were previously removed, add them again. 87 if ( $attributes['enhancedPagination'] && ! in_array( $style_asset, $style_handles, true ) ) {93 if ( $attributes['enhancedPagination'] && isset( $attributes['queryId'] ) && ! in_array( $style_asset, $style_handles, true ) ) { 88 94 $block->block_type->style_handles = array_merge( $style_handles, array( $style_asset ) ); 89 95 } … … 124 130 } 125 131 add_action( 'init', 'register_block_core_query' ); 132 133 /** 134 * Traverse the tree of blocks looking for any plugin block (i.e., a block from 135 * an installed plugin) inside a Query block with the enhanced pagination 136 * enabled. If at least one is found, the enhanced pagination is effectively 137 * disabled to prevent any potential incompatibilities. 138 * 139 * @since 6.4.0 140 * 141 * @param array $parsed_block The block being rendered. 142 * @return string Returns the parsed block, unmodified. 143 */ 144 function block_core_query_disable_enhanced_pagination( $parsed_block ) { 145 static $enhanced_query_stack = array(); 146 static $dirty_enhanced_queries = array(); 147 static $render_query_callback = null; 148 149 $block_name = $parsed_block['blockName']; 150 151 if ( 152 'core/query' === $block_name && 153 isset( $parsed_block['attrs']['enhancedPagination'] ) && 154 true === $parsed_block['attrs']['enhancedPagination'] && 155 isset( $parsed_block['attrs']['queryId'] ) 156 ) { 157 $enhanced_query_stack[] = $parsed_block['attrs']['queryId']; 158 159 if ( ! isset( $render_query_callback ) ) { 160 /** 161 * Filter that disables the enhanced pagination feature during block 162 * rendering when a plugin block has been found inside. It does so 163 * by adding an attribute called `data-wp-navigation-disabled` which 164 * is later handled by the front-end logic. 165 * 166 * @param string $content The block content. 167 * @param array $block The full block, including name and attributes. 168 * @return string Returns the modified output of the query block. 169 */ 170 $render_query_callback = static function ( $content, $block ) use ( &$enhanced_query_stack, &$dirty_enhanced_queries, &$render_query_callback ) { 171 $has_enhanced_pagination = 172 isset( $block['attrs']['enhancedPagination'] ) && 173 true === $block['attrs']['enhancedPagination'] && 174 isset( $block['attrs']['queryId'] ); 175 176 if ( ! $has_enhanced_pagination ) { 177 return $content; 178 } 179 180 if ( isset( $dirty_enhanced_queries[ $block['attrs']['queryId'] ] ) ) { 181 $p = new WP_HTML_Tag_Processor( $content ); 182 if ( $p->next_tag() ) { 183 $p->set_attribute( 'data-wp-navigation-disabled', 'true' ); 184 } 185 $content = $p->get_updated_html(); 186 $dirty_enhanced_queries[ $block['attrs']['queryId'] ] = null; 187 } 188 189 array_pop( $enhanced_query_stack ); 190 191 if ( empty( $enhanced_query_stack ) ) { 192 remove_filter( 'render_block_core/query', $render_query_callback ); 193 $render_query_callback = null; 194 } 195 196 return $content; 197 }; 198 199 add_filter( 'render_block_core/query', $render_query_callback, 10, 2 ); 200 } 201 } elseif ( 202 ! empty( $enhanced_query_stack ) && 203 isset( $block_name ) && 204 ( ! str_starts_with( $block_name, 'core/' ) || 'core/post-content' === $block_name ) 205 ) { 206 foreach ( $enhanced_query_stack as $query_id ) { 207 $dirty_enhanced_queries[ $query_id ] = true; 208 } 209 } 210 211 return $parsed_block; 212 } 213 214 add_filter( 'render_block_data', 'block_core_query_disable_enhanced_pagination', 10, 1 );
Note: See TracChangeset
for help on using the changeset viewer.