WordPress.org

Make WordPress Core

Changeset 45265


Ignore:
Timestamp:
04/24/2019 09:38:21 PM (5 months ago)
Author:
azaozz
Message:

Fix parsing of inner blocks when auto-generating an excerpt. Helps to prevent cases where dynamic inner blocks may cause an infinite loop if trying to auto-generate an excerpt.

Props desrosj, pento, gziolo, azaozz.
Fixes #46133.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r45159 r45265  
    126126 */
    127127function excerpt_remove_blocks( $content ) {
    128     $allowed_blocks = array(
     128    $allowed_inner_blocks = array(
    129129        // Classic blocks have their blockName set to null.
    130130        null,
    131         'core/columns',
    132131        'core/freeform',
    133132        'core/heading',
     
    142141        'core/verse',
    143142    );
     143
     144    $allowed_blocks = array_merge( $allowed_inner_blocks, array( 'core/columns' ) );
     145
    144146    /**
    145147     * Filters the list of blocks that can contribute to the excerpt.
     
    155157    $blocks         = parse_blocks( $content );
    156158    $output         = '';
     159
    157160    foreach ( $blocks as $block ) {
    158161        if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
     162            if ( ! empty( $block['innerBlocks'] ) ) {
     163                if ( 'core/columns' === $block['blockName'] ) {
     164                    $output .= _excerpt_render_inner_columns_blocks( $block, $allowed_inner_blocks );
     165                    continue;
     166                }
     167
     168                // Skip the block if it has disallowed or nested inner blocks.
     169                foreach ( $block['innerBlocks'] as $inner_block ) {
     170                    if (
     171                        ! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) ||
     172                        ! empty( $inner_block['innerBlocks'] )
     173                    ) {
     174                        continue 2;
     175                    }
     176                }
     177            }
     178
    159179            $output .= render_block( $block );
    160180        }
    161181    }
    162      return $output;
     182
     183    return $output;
     184}
     185
     186/**
     187 * Render inner blocks from the `core/columns` block for generating an excerpt.
     188 *
     189 * @since 5.2.0
     190 * @access private
     191 *
     192 * @param array $columns        The parsed columns block.
     193 * @param array $allowed_blocks The list of allowed inner blocks.
     194 * @return string The rendered inner blocks.
     195 */
     196function _excerpt_render_inner_columns_blocks( $columns, $allowed_blocks ) {
     197    $output = '';
     198
     199    foreach ( $columns['innerBlocks'] as $column ) {
     200        foreach ( $column['innerBlocks'] as $inner_block ) {
     201            if ( in_array( $inner_block['blockName'], $allowed_blocks, true ) && empty( $inner_block['innerBlocks'] ) ) {
     202                $output .= render_block( $inner_block );
     203            }
     204        }
     205    }
     206
     207    return $output;
    163208}
    164209
Note: See TracChangeset for help on using the changeset viewer.