Skip to content

Instantly share code, notes, and snippets.

@mcsf
Created May 1, 2018 13:51
Show Gist options
  • Save mcsf/573fd7c42943f89c44ab31efe29d1a81 to your computer and use it in GitHub Desktop.
Save mcsf/573fd7c42943f89c44ab31efe29d1a81 to your computer and use it in GitHub Desktop.
diff --git a/lib/blocks.php b/lib/blocks.php
index e28971bad..a2c1d0a89 100644
--- a/lib/blocks.php
+++ b/lib/blocks.php
@@ -177,14 +177,18 @@ function do_blocks( $content ) {
}
}
- // Replace dynamic block with server-rendered output.
- $rendered_content .= $block_type->render( $attributes );
+ // Generate server-rendered output for dynamic block.
+ $dynamic_output = $block_type->render( $attributes );
- if ( ! $is_self_closing ) {
+ if ( $is_self_closing ) {
+ // Replace dynamic block with server-rendered output.
+ $rendered_content .= $dynamic_output;
+ } else {
$end_tag_pattern = '/<!--\s+\/wp:' . str_replace( '/', '\/', 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.
+ $rendered_content .= $dynamic_output;
break;
}
@@ -192,6 +196,17 @@ function do_blocks( $content ) {
$end_tag = $block_match_end[0][0];
$end_offset = $block_match_end[0][1];
+ // In the case of dynamic blocks that also exhibit static content
+ // between their opening and closing tags, consider the server
+ // output first.
+ if ( $dynamic_output ) {
+ $rendered_content .= $dynamic_output;
+ } else {
+ // Otherwise, fall back to the block's static content.
+ $inner_html = substr( $content, 0, $end_offset );
+ $rendered_content .= $inner_html;
+ }
+
$content = substr( $content, $end_offset + strlen( $end_tag ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment