Skip to content

Commit

Permalink
SSR: Simplify the block server side rendering algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored and nylen committed May 5, 2017
1 parent f7ac9f3 commit e706f6e
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ function do_blocks( $content ) {
$open_matcher = '/<!--\s*wp:([a-z](?:[a-z0-9\/]+)*)\s+((?:(?!-->).)*)-->.*<!--\s*\/wp:\g1\s+-->/';
preg_match_all( $open_matcher, $content, $matches, PREG_OFFSET_CAPTURE );

$offset = 0;
$new_content = $content;
foreach ( $matches[ 0 ] as $index => $block_match ) {
$block_name = $matches[ 1 ][ $index ][ 0 ];
Expand All @@ -111,7 +110,7 @@ function do_blocks( $content ) {
continue;
}

$block_length = mb_strlen( $block_match[ 0 ] );
$block_markup = $block_match[ 0 ];
$block_position = $block_match[ 1 ];
$block_attributes_string = $matches[ 2 ][ $index ][ 0 ];
$block_attributes = parse_block_attributes( $block_attributes_string );
Expand All @@ -120,11 +119,7 @@ function do_blocks( $content ) {
$output = call_user_func( $registered_blocks[ $block_name ][ 'render' ], $block_attributes );

// Replace the matched block with the dynamic output
$new_content =
mb_substr( $new_content, 0, $block_position + $offset ) .
$output .
mb_substr( $new_content, $block_position + $block_length + $offset );
$offset += mb_strlen( $output ) - mb_strlen( $block_length );
$new_content = str_replace( $block_markup, $output, $new_content );
}

return $new_content;
Expand Down

0 comments on commit e706f6e

Please sign in to comment.