Make WordPress Core


Ignore:
Timestamp:
10/18/2018 11:53:49 AM (6 years ago)
Author:
pento
Message:

Blocks: Parse blocks when displaying posts.

Posts containing blocks are now correctly handled when displaying on the front end, including dynamic blocks and nested blocks.

See #45109.

File:
1 edited

Legend:

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

    r43751 r43752  
    112112
    113113    return $dynamic_block_names;
     114}
     115
     116/**
     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 */
     124function 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 */
     137function _recurse_strip_dynamic_blocks( $blocks ) {
     138    $clean_content  = '';
     139    $dynamic_blocks = get_dynamic_block_names();
     140
     141    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;
    114152}
    115153
     
    143181     * @since 5.0.0
    144182     *
    145      * @param string $parser_class Name of block parser class
     183     * @param string $parser_class Name of block parser class.
    146184     */
    147185    $parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
     
    150188    return $parser->parse( $content );
    151189}
     190
     191/**
     192 * Parses dynamic blocks out of `post_content` and re-renders them.
     193 *
     194 * @since 5.0.0
     195 * @global WP_Post $post The post to edit.
     196 *
     197 * @param  string $content Post content.
     198 * @return string Updated post content.
     199 */
     200function do_blocks( $content ) {
     201    $blocks = parse_blocks( $content );
     202    return _recurse_do_blocks( $blocks, $blocks );
     203}
     204
     205/**
     206 * Helper function for do_blocks(), to recurse through the block tree.
     207 *
     208 * @since 5.0.0
     209 * @access private
     210 *
     211 * @param array $blocks     Array of blocks from parse_blocks().
     212 * @param array $all_blocks The top level array of blocks.
     213 * @return string The block HTML.
     214 */
     215function _recurse_do_blocks( $blocks, $all_blocks ) {
     216    global $post;
     217
     218    /*
     219     * Back up global post, to restore after render callback.
     220     * Allows callbacks to run new WP_Query instances without breaking the global post.
     221     */
     222    $global_post = $post;
     223
     224    $rendered_content = '';
     225    $dynamic_blocks   = get_dynamic_block_names();
     226
     227    foreach ( $blocks as $block ) {
     228        $block = (array) $block;
     229        if ( in_array( $block['blockName'], $dynamic_blocks ) ) {
     230            // Find registered block type. We can assume it exists since we use the
     231            // `get_dynamic_block_names` function as a source for pattern matching.
     232            $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
     233
     234            // Replace dynamic block with server-rendered output.
     235            $block_content = $block_type->render( (array) $block['attrs'], $block['innerHTML'] );
     236        } else if ( $block['innerBlocks'] ) {
     237            $block_content = _recurse_do_blocks( $block['innerBlocks'], $blocks );
     238        } else {
     239            $block_content = $block['innerHTML'];
     240        }
     241
     242        /**
     243         * Filters the content of a single block.
     244         *
     245         * During the_content, each block is parsed and added to the output individually. This filter allows
     246         * that content to be altered immediately before it's appended.
     247         *
     248         * @since 5.0.0
     249         *
     250         * @param string $block_content The block content about to be appended.
     251         * @param array  $block         The full block, including name and attributes.
     252         * @param array  $all_blocks    The array of all blocks being processed.
     253         */
     254        $rendered_content .= apply_filters( 'do_block', $block_content, $block, $all_blocks );
     255
     256        // Restore global $post.
     257        $post = $global_post;
     258    }
     259
     260    // Strip remaining block comment demarcations.
     261    $rendered_content = preg_replace( '/<!--\s+\/?wp:.*?-->/m', '', $rendered_content );
     262
     263    return $rendered_content;
     264}
Note: See TracChangeset for help on using the changeset viewer.