Skip to content

Commit

Permalink
use recursive version again
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Nov 8, 2018
1 parent e490050 commit 16a4175
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 89 deletions.
58 changes: 26 additions & 32 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,47 +150,36 @@ function get_dynamic_blocks_regex() {
* @since 1.9.0
* @global WP_Post $post The post to edit.
*
* @param array $blocks A single parsed block object or an array thereof.
* @param array $block A single parsed block object.
* @return string String of rendered HTML.
*/
function gutenberg_render_block( $blocks ) {
function gutenberg_render_block( $block ) {
global $post;

$tree = WP_Block_Tree_Iterator::create( $blocks );
$stack = array();
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic();
$has_blocks = ! empty( $block['innerBlocks'] );

foreach ( $tree as $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic();
$has_blocks = ! empty( $block['innerBlocks'] );
if ( $is_dynamic ) {
$attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array();
$global_post = $post;
$output = $block_type->render( $attributes, $block['innerHTML'] );
$post = $global_post;

if ( $is_dynamic ) {
$attributes = is_array( $block['attrs'] ) ? $block['attrs'] : array();
$global_post = $post;
$stack[] = $block_type->render( $attributes, $block['innerHTML'] );
$post = $global_post;

continue;
}

if ( ! $has_blocks ) {
$stack[] = $block['innerHTML'];

continue;
}

$inner_blocks = array_splice( $stack, - 1 * count( $block['innerBlocks'] ) );
return $output;
}

$output = '';
$index = 0;
foreach ( $block['innerContent'] as $chunk ) {
$output .= is_string( $chunk ) ? $chunk : $inner_blocks[ $index++ ];
}
if ( ! $has_blocks ) {
return $block['innerHTML'];
}

$stack[] = $output;
$output = '';
$index = 0;
foreach ( $block['innerContent'] as $chunk ) {
$output .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] );
}

return implode( '', $stack );
return $output;
}

if ( ! function_exists( 'do_blocks' ) ) {
Expand All @@ -204,8 +193,13 @@ function gutenberg_render_block( $blocks ) {
*/
function do_blocks( $content ) {
$blocks = gutenberg_parse_blocks( $content );
$output = '';

foreach( $blocks as $block ) {
$output .= gutenberg_render_block( $block );
}

return gutenberg_render_block( $blocks );
return $output;
}

add_filter( 'the_content', 'do_blocks', 7 ); // BEFORE do_shortcode() and oembed.
Expand Down
54 changes: 0 additions & 54 deletions lib/class-wp-block-tree-iterator.php

This file was deleted.

3 changes: 0 additions & 3 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
}

require dirname( __FILE__ ) . '/meta-box-partial-page.php';
if ( ! class_exists( 'WP_Block_Recursive_Iterator_Filter' ) ) {
require dirname( __FILE__ ) . '/class-wp-block-tree-iterator.php';
}
if ( ! class_exists( 'WP_Block_Type' ) ) {
require dirname( __FILE__ ) . '/class-wp-block-type.php';
}
Expand Down

0 comments on commit 16a4175

Please sign in to comment.