Make WordPress Core


Ignore:
Timestamp:
11/01/2023 06:09:01 PM (3 years ago)
Author:
hellofromTonya
Message:

Editor: 2nd update of npm packages for 6.4 RC3.

This second update for RC3 includes the following fixes:

Follow up to [57034], [56987], [56961], [56849], [56818], [56816].

Reviewed by davidbaumwald , jorbin.
Merges [57048] to the 6.4 branch.

Props afercia, aristath, artemiosans, czapla, darerodz, glendaviesnz, hellofromTonya, jameskoster, joen, luisherranz, mikachan, ocean90, peterwilsoncc, ramonopoly, rajinsharwar, swissspidy.
Fixes #59411.

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  
    1111 * @since 6.4.0
    1212 *
    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.
    1616 *
    1717 * @return string Returns the modified output of the query block.
    1818 */
    1919function render_block_core_query( $attributes, $content, $block ) {
    20         if ( $attributes['enhancedPagination'] ) {
     20        if ( $attributes['enhancedPagination'] && isset( $attributes['queryId'] ) ) {
    2121                $p = new WP_HTML_Tag_Processor( $content );
    2222                if ( $p->next_tag() ) {
     
    4949                                $content,
    5050                                '<div
    51                                         class="wp-block-query__enhanced-pagination-navigation-announce"
     51                                        class="wp-block-query__enhanced-pagination-navigation-announce screen-reader-text"
    5252                                        aria-live="polite"
    5353                                        data-wp-text="context.core.query.message"
     
    6868                $script_handles = $block->block_type->view_script_handles;
    6969                // 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                ) {
    7174                        $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_asset ) );
    7275                }
    7376                // 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 ) ) {
    7578                        $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_asset ) );
    7679                }
     
    8184                $style_handles = $block->block_type->style_handles;
    8285                // 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                ) {
    8490                        $block->block_type->style_handles = array_diff( $style_handles, array( $style_asset ) );
    8591                }
    8692                // 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 ) ) {
    8894                        $block->block_type->style_handles = array_merge( $style_handles, array( $style_asset ) );
    8995                }
     
    124130}
    125131add_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 */
     144function 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
     214add_filter( 'render_block_data', 'block_core_query_disable_enhanced_pagination', 10, 1 );
Note: See TracChangeset for help on using the changeset viewer.