Skip to content

Commit

Permalink
rebase on master based on innerContent
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Nov 7, 2018
1 parent 1ac70e1 commit 03ca73d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 86 deletions.
105 changes: 20 additions & 85 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,33 @@ function get_dynamic_blocks_regex() {
* @return string String of rendered HTML.
*/
function gutenberg_render_block( $block ) {
$block_name = isset( $block['blockName'] ) ? $block['blockName'] : null;
global $post;
$attributes = is_array( $block['attrs'] ) ? $block['attrs'] : array();
$raw_content = isset( $block['innerHTML'] ) ? $block['innerHTML'] : null;

if ( $block_name ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
if ( $block[ 'blockName' ] ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block[ 'blockName' ] );
if ( null !== $block_type && $block_type->is_dynamic() ) {
return $block_type->render( $attributes );
}
}

if ( $raw_content ) {
return $raw_content;
if ( empty( $block[ 'innerBlocks' ] ) ) {
return $block[ 'innerHTML' ];
}

return '';
$output = '';
$index = 0;
foreach ( $block[ 'innerContent' ] as $chunk ) {
if ( is_string( $chunk ) ) {
$output .= $chunk;
} else {
$global_post = $post;
$output .= gutenberg_render_block( $block[ 'innerBlocks' ][ $index++ ] );
$post = $global_post;
}
}

return $output;
}

if ( ! function_exists( 'do_blocks' ) ) {
Expand All @@ -182,85 +193,9 @@ function gutenberg_render_block( $block ) {
* @return string Updated post content.
*/
function do_blocks( $content ) {
global $post;

$rendered_content = '';
$dynamic_block_pattern = get_dynamic_blocks_regex();

/*
* 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;

while ( preg_match( $dynamic_block_pattern, $content, $block_match, PREG_OFFSET_CAPTURE ) ) {
$opening_tag = $block_match[0][0];
$offset = $block_match[0][1];
$block_name = $block_match[1][0];
$is_self_closing = isset( $block_match[4] );

// Reset attributes JSON to prevent scope bleed from last iteration.
$block_attributes_json = null;
if ( isset( $block_match[3] ) ) {
$block_attributes_json = $block_match[3][0];
}

// Since content is a working copy since the last match, append to
// rendered content up to the matched offset...
$rendered_content .= substr( $content, 0, $offset );

// ...then update the working copy of content.
$content = substr( $content, $offset + strlen( $opening_tag ) );

// Make implicit core namespace explicit.
$is_implicit_core_namespace = ( false === strpos( $block_name, '/' ) );
$normalized_block_name = $is_implicit_core_namespace ? 'core/' . $block_name : $block_name;

// 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( $normalized_block_name );

// Attempt to parse attributes JSON, if available.
$attributes = array();
if ( ! empty( $block_attributes_json ) ) {
$decoded_attributes = json_decode( $block_attributes_json, true );
if ( ! is_null( $decoded_attributes ) ) {
$attributes = $decoded_attributes;
}
}

$inner_content = '';

if ( ! $is_self_closing ) {
$end_tag_pattern = '/<!--\s+\/wp:' . preg_quote( $block_name, '/' ) . '\s+-->/';
if ( ! preg_match( $end_tag_pattern, $content, $block_match_end, PREG_OFFSET_CAPTURE ) ) {
// If no closing tag is found, abort all matching, and continue
// to append remainder of content to rendered output.
break;
}

// Update content to omit text up to and including closing tag.
$end_tag = $block_match_end[0][0];
$end_offset = $block_match_end[0][1];

$inner_content = substr( $content, 0, $end_offset );
$content = substr( $content, $end_offset + strlen( $end_tag ) );
}

// Replace dynamic block with server-rendered output.
$rendered_content .= $block_type->render( $attributes, $inner_content );

// Restore global $post.
$post = $global_post;
}

// Append remaining unmatched content.
$rendered_content .= $content;

// Strip remaining block comment demarcations.
$rendered_content = preg_replace( '/<!--\s+\/?wp:.*?-->\r?\n?/m', '', $rendered_content );
$blocks = gutenberg_parse_blocks( $content );

return $rendered_content;
return implode( '', array_map( 'gutenberg_render_block', $blocks ) );
}

add_filter( 'the_content', 'do_blocks', 7 ); // BEFORE do_shortcode() and oembed.
Expand Down
2 changes: 1 addition & 1 deletion packages/block-serialization-default-parser/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ function add_freeform( $length = null ) {
*/
function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) {
$parent = $this->stack[ count( $this->stack ) - 1 ];
$parent->block->innerBlocks[] = $block;
$parent->block->innerBlocks[] = (array) $block;
$html = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset );

if ( ! empty( $html ) ) {
Expand Down

0 comments on commit 03ca73d

Please sign in to comment.