Make WordPress Core


Ignore:
Timestamp:
12/17/2018 04:50:48 AM (5 years ago)
Author:
desrosj
Message:

Block Editor: Update @wordpress dependencies.

Changes of note:

  • Includes the new Annotations API package.
  • wp-polyfill-ecmascript.js is renamed to wp-polyfill.js.
  • strip_dynamic_blocks() has been removed in favor of excerpt_remove_blocks().
  • The PHP block parser is now syncing from the block-serialization-default-parser package.
  • do_blocks() uses the new parser.
  • The do_block filter has been removed from do_blocks(), in favor of a render_block filter in render_block().

Also, a little cleanup to render_block(). Always normalize $block['attrs’] to array in ’render_block’ filter.
Props pento, azaozz.

Merges [43884] and [43888] to trunk.

See #45145, #45190, #45264, #45282.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/blocks.php

    r44242 r44261  
    115115
    116116/**
    117  * Remove all dynamic blocks from the given content.
    118  *
    119  * @since 5.0.0
    120  *
    121  * @param string $content Content of the current post.
    122  * @return string
    123  */
    124 function strip_dynamic_blocks( $content ) {
    125     return _recurse_strip_dynamic_blocks( parse_blocks( $content ) );
    126 }
    127 
    128 /**
    129  * Helper function for strip_dynamic_blocks(), to recurse through the block tree.
    130  *
    131  * @since 5.0.0
    132  * @access private
    133  *
    134  * @param array $blocks Array of blocks from parse_blocks().
    135  * @return string HTML from the non-dynamic blocks.
    136  */
    137 function _recurse_strip_dynamic_blocks( $blocks ) {
    138     $clean_content  = '';
    139     $dynamic_blocks = get_dynamic_block_names();
    140 
     117 * Parses blocks out of a content string, and renders those appropriate for the excerpt.
     118 *
     119 * As the excerpt should be a small string of text relevant to the full post content,
     120 * this function renders the blocks that are most likely to contain such text.
     121 *
     122 * @since 5.0.0
     123 *
     124 * @param string $content The content to parse.
     125 * @return string The parsed and filtered content.
     126 */
     127function excerpt_remove_blocks( $content ) {
     128    $allowed_blocks = array(
     129        // Classic blocks have their blockName set to null.
     130        null,
     131        'core/columns',
     132        'core/freeform',
     133        'core/heading',
     134        'core/html',
     135        'core/list',
     136        'core/media-text',
     137        'core/paragraph',
     138        'core/preformatted',
     139        'core/pullquote',
     140        'core/quote',
     141        'core/table',
     142        'core/verse',
     143    );
     144    /**
     145     * Filters the list of blocks that can contribute to the excerpt.
     146     *
     147     * If a dynamic block is added to this list, it must not generate another
     148     * excerpt, as this will cause an infinite loop to occur.
     149     *
     150     * @since 4.4.0
     151     *
     152     * @param array $allowed_blocks The list of allowed blocks.
     153     */
     154    $allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks );
     155    $blocks         = parse_blocks( $content );
     156    $output         = '';
    141157    foreach ( $blocks as $block ) {
    142         if ( ! in_array( $block['blockName'], $dynamic_blocks ) ) {
    143             if ( $block['innerBlocks'] ) {
    144                 $clean_content .= _recurse_strip_dynamic_blocks( $block['innerBlocks'] );
    145             } else {
    146                 $clean_content .= $block['innerHTML'];
    147             }
    148         }
    149     }
    150 
    151     return $clean_content;
     158        if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
     159            $output .= render_block( $block );
     160        }
     161    }
     162     return $output;
     163}
     164
     165/**
     166 * Renders a single block into a HTML string.
     167 *
     168 * @since 5.0.0
     169 *
     170 * @global WP_Post $post The post to edit.
     171 *
     172 * @param array $block A single parsed block object.
     173 * @return string String of rendered HTML.
     174 */
     175function render_block( $block ) {
     176    global $post;
     177
     178    $block_type    = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
     179    $is_dynamic    = $block['blockName'] && null !== $block_type && $block_type->is_dynamic();
     180    $block_content = '';
     181    $index         = 0;
     182
     183    foreach ( $block['innerContent'] as $chunk ) {
     184        $block_content .= is_string( $chunk ) ? $chunk : render_block( $block['innerBlocks'][ $index++ ] );
     185    }
     186
     187    if ( ! is_array( $block['attrs'] ) ) {
     188        $block['attrs'] = array();
     189    }
     190
     191    if ( $is_dynamic ) {
     192        $global_post   = $post;
     193        $block_content = $block_type->render( $block['attrs'], $block_content );
     194        $post          = $global_post;
     195    }
     196
     197    /**
     198     * Filters the content of a single block.
     199     *
     200     * @since 5.0.0
     201     *
     202     * @param string $block_content The block content about to be appended.
     203     * @param array  $block         The full block, including name and attributes.
     204     */
     205    return apply_filters( 'render_block', $block_content, $block );
    152206}
    153207
     
    157211 * @since 5.0.0
    158212 *
    159  * @param  string $content Post content.
    160  * @return array  Array of parsed block objects.
     213 * @param string $content Post content.
     214 * @return array Array of parsed block objects.
    161215 */
    162216function parse_blocks( $content ) {
    163     /*
    164      * If there are no blocks in the content, return a single block, rather
    165      * than wasting time trying to parse the string.
    166      */
    167     if ( ! has_blocks( $content ) ) {
    168         return array(
    169             array(
    170                 'blockName'   => null,
    171                 'attrs'       => array(),
    172                 'innerBlocks' => array(),
    173                 'innerHTML'   => $content,
    174             ),
    175         );
    176     }
    177 
    178217    /**
    179218     * Filter to allow plugins to replace the server-side block parser
     
    207246
    208247    $blocks = parse_blocks( $content );
    209     return _recurse_do_blocks( $blocks, $blocks );
     248    $output = '';
     249
     250    foreach ( $blocks as $block ) {
     251        $output .= render_block( $block );
     252    }
     253
     254    return $output;
    210255}
    211256
     
    232277
    233278/**
    234  * Helper function for do_blocks(), to recurse through the block tree.
    235  *
    236  * @since 5.0.0
    237  * @access private
    238  *
    239  * @param array $blocks     Array of blocks from parse_blocks().
    240  * @param array $all_blocks The top level array of blocks.
    241  * @return string The block HTML.
    242  */
    243 function _recurse_do_blocks( $blocks, $all_blocks ) {
    244     global $post;
    245 
    246     /*
    247      * Back up global post, to restore after render callback.
    248      * Allows callbacks to run new WP_Query instances without breaking the global post.
    249      */
    250     $global_post = $post;
    251 
    252     $rendered_content = '';
    253     $dynamic_blocks   = get_dynamic_block_names();
    254 
    255     foreach ( $blocks as $block ) {
    256         $block = (array) $block;
    257         if ( in_array( $block['blockName'], $dynamic_blocks ) ) {
    258             // Find registered block type. We can assume it exists since we use the
    259             // `get_dynamic_block_names` function as a source for pattern matching.
    260             $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    261 
    262             // Replace dynamic block with server-rendered output.
    263             $block_content = $block_type->render( (array) $block['attrs'], $block['innerHTML'] );
    264         } elseif ( $block['innerBlocks'] ) {
    265             $block_content = _recurse_do_blocks( $block['innerBlocks'], $blocks );
    266         } else {
    267             $block_content = $block['innerHTML'];
    268         }
    269 
    270         /**
    271          * Filters the content of a single block.
    272          *
    273          * During the_content, each block is parsed and added to the output individually. This filter allows
    274          * that content to be altered immediately before it's appended.
    275          *
    276          * @since 5.0.0
    277          *
    278          * @param string $block_content The block content about to be appended.
    279          * @param array  $block         The full block, including name and attributes.
    280          * @param array  $all_blocks    The array of all blocks being processed.
    281          */
    282         $rendered_content .= apply_filters( 'do_block', $block_content, $block, $all_blocks );
    283 
    284         // Restore global $post.
    285         $post = $global_post;
    286     }
    287 
    288     // Strip remaining block comment demarcations.
    289     $rendered_content = preg_replace( '/<!--\s+\/?wp:.*?-->/m', '', $rendered_content );
    290 
    291     return $rendered_content;
    292 }
    293 
    294 /**
    295279 * Returns the current version of the block format that the content string is using.
    296280 *
Note: See TracChangeset for help on using the changeset viewer.