Skip to content

Commit e706f6e

Browse files
youknowriadnylen
authored andcommitted
SSR: Simplify the block server side rendering algorithm
1 parent f7ac9f3 commit e706f6e

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

index.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ function do_blocks( $content ) {
102102
$open_matcher = '/<!--\s*wp:([a-z](?:[a-z0-9\/]+)*)\s+((?:(?!-->).)*)-->.*<!--\s*\/wp:\g1\s+-->/';
103103
preg_match_all( $open_matcher, $content, $matches, PREG_OFFSET_CAPTURE );
104104

105-
$offset = 0;
106105
$new_content = $content;
107106
foreach ( $matches[ 0 ] as $index => $block_match ) {
108107
$block_name = $matches[ 1 ][ $index ][ 0 ];
@@ -111,7 +110,7 @@ function do_blocks( $content ) {
111110
continue;
112111
}
113112

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

122121
// Replace the matched block with the dynamic output
123-
$new_content =
124-
mb_substr( $new_content, 0, $block_position + $offset ) .
125-
$output .
126-
mb_substr( $new_content, $block_position + $block_length + $offset );
127-
$offset += mb_strlen( $output ) - mb_strlen( $block_length );
122+
$new_content = str_replace( $block_markup, $output, $new_content );
128123
}
129124

130125
return $new_content;

0 commit comments

Comments
 (0)