mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-01 03:04:34 +00:00
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. Props pento. Merges [43752] to trunk. See #45109. git-svn-id: https://develop.svn.wordpress.org/trunk@44118 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -113,6 +113,44 @@ function get_dynamic_block_names() {
|
||||
return $dynamic_block_names;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all dynamic blocks from the given content.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param string $content Content of the current post.
|
||||
* @return string
|
||||
*/
|
||||
function strip_dynamic_blocks( $content ) {
|
||||
return _recurse_strip_dynamic_blocks( parse_blocks( $content ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for strip_dynamic_blocks(), to recurse through the block tree.
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @access private
|
||||
*
|
||||
* @param array $blocks Array of blocks from parse_blocks().
|
||||
* @return string HTML from the non-dynamic blocks.
|
||||
*/
|
||||
function _recurse_strip_dynamic_blocks( $blocks ) {
|
||||
$clean_content = '';
|
||||
$dynamic_blocks = get_dynamic_block_names();
|
||||
|
||||
foreach ( $blocks as $block ) {
|
||||
if ( ! in_array( $block['blockName'], $dynamic_blocks ) ) {
|
||||
if ( $block['innerBlocks'] ) {
|
||||
$clean_content .= _recurse_strip_dynamic_blocks( $block['innerBlocks'] );
|
||||
} else {
|
||||
$clean_content .= $block['innerHTML'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $clean_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses blocks out of a content string.
|
||||
*
|
||||
@@ -142,10 +180,85 @@ function parse_blocks( $content ) {
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param string $parser_class Name of block parser class
|
||||
* @param string $parser_class Name of block parser class.
|
||||
*/
|
||||
$parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
|
||||
|
||||
$parser = new $parser_class();
|
||||
return $parser->parse( $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses dynamic blocks out of `post_content` and re-renders them.
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @global WP_Post $post The post to edit.
|
||||
*
|
||||
* @param string $content Post content.
|
||||
* @return string Updated post content.
|
||||
*/
|
||||
function do_blocks( $content ) {
|
||||
$blocks = parse_blocks( $content );
|
||||
return _recurse_do_blocks( $blocks, $blocks );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for do_blocks(), to recurse through the block tree.
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @access private
|
||||
*
|
||||
* @param array $blocks Array of blocks from parse_blocks().
|
||||
* @param array $all_blocks The top level array of blocks.
|
||||
* @return string The block HTML.
|
||||
*/
|
||||
function _recurse_do_blocks( $blocks, $all_blocks ) {
|
||||
global $post;
|
||||
|
||||
/*
|
||||
* Back up global post, to restore after render callback.
|
||||
* Allows callbacks to run new WP_Query instances without breaking the global post.
|
||||
*/
|
||||
$global_post = $post;
|
||||
|
||||
$rendered_content = '';
|
||||
$dynamic_blocks = get_dynamic_block_names();
|
||||
|
||||
foreach ( $blocks as $block ) {
|
||||
$block = (array) $block;
|
||||
if ( in_array( $block['blockName'], $dynamic_blocks ) ) {
|
||||
// Find registered block type. We can assume it exists since we use the
|
||||
// `get_dynamic_block_names` function as a source for pattern matching.
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
|
||||
// Replace dynamic block with server-rendered output.
|
||||
$block_content = $block_type->render( (array) $block['attrs'], $block['innerHTML'] );
|
||||
} elseif ( $block['innerBlocks'] ) {
|
||||
$block_content = _recurse_do_blocks( $block['innerBlocks'], $blocks );
|
||||
} else {
|
||||
$block_content = $block['innerHTML'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the content of a single block.
|
||||
*
|
||||
* During the_content, each block is parsed and added to the output individually. This filter allows
|
||||
* that content to be altered immediately before it's appended.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param string $block_content The block content about to be appended.
|
||||
* @param array $block The full block, including name and attributes.
|
||||
* @param array $all_blocks The array of all blocks being processed.
|
||||
*/
|
||||
$rendered_content .= apply_filters( 'do_block', $block_content, $block, $all_blocks );
|
||||
|
||||
// Restore global $post.
|
||||
$post = $global_post;
|
||||
}
|
||||
|
||||
// Strip remaining block comment demarcations.
|
||||
$rendered_content = preg_replace( '/<!--\s+\/?wp:.*?-->/m', '', $rendered_content );
|
||||
|
||||
return $rendered_content;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user