Skip to content

Commit dc5ea43

Browse files
committed
Support Forums: Apply the block limits where the blocks are rendered
Closes WordPress#925. git-svn-id: https://meta.svn.wordpress.org/sites/trunk@15207 74240141-8908-4e6f-9713-ba540dce6ec7
1 parent 923e113 commit dc5ea43

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

‎wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-blocks.php‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public function __construct() {
6565
// Before Blocks Everywhere renders the stored content, which it does at priority 8.
6666
add_filter( "bbp_get_{$type}_content", [ $this, 'limit_blocks' ], 7 );
6767
}
68+
69+
// And again where the blocks actually run, past anything that could change the content.
70+
add_filter( 'pre_render_block', [ $this, 'block_pre_render' ], 10, 2 );
6871
}
6972

7073
/**
@@ -219,7 +222,47 @@ public function limit_blocks( $content ) {
219222
$output .= serialize_block( $block );
220223
}
221224

222-
return ltrim( $output );
225+
$output = ltrim( $output );
226+
227+
// Unbalanced delimiters stay literal innerContent, invisible to the filter and live again after the next parse.
228+
if ( $this->has_unsupported_block( parse_blocks( $output ) ) ) {
229+
// Delimiters are HTML comments, and a save restores only bare wp: ones; anything else comes back escaped.
230+
$content = preg_replace( '/<!--.*?-->/s', '', $content ) ?? $content;
231+
232+
// An opener with no closer would comment out everything after it, so drop it and keep that content.
233+
$content = str_replace( '<!--', '', $content );
234+
235+
// Removing one comment can join what surrounded it into another, so fall back to text if it did.
236+
return $this->has_unsupported_block( parse_blocks( $content ) ) ? wp_strip_all_tags( $content ) : $content;
237+
}
238+
239+
return $output;
240+
}
241+
242+
/**
243+
* Keep an unsupported block from rendering in forum content.
244+
*
245+
* ::limit_blocks() guards the stored string, which later filters can still change; this
246+
* guards the parsed block itself, at every depth and before its callback runs.
247+
*
248+
* @param string|null $pre_render Pre-rendered content, or null to render the block.
249+
* @param array $parsed_block The block about to be rendered.
250+
* @return string|null Empty string to drop the block, otherwise $pre_render.
251+
*/
252+
public function block_pre_render( $pre_render, $parsed_block ) {
253+
if ( null !== $pre_render || empty( $parsed_block['blockName'] ) ) {
254+
return $pre_render;
255+
}
256+
257+
foreach ( [ 'topic', 'reply', 'forum' ] as $type ) {
258+
if ( ! doing_filter( "bbp_get_{$type}_content" ) ) {
259+
continue;
260+
}
261+
262+
return in_array( $parsed_block['blockName'], $this->supported_blocks(), true ) ? $pre_render : '';
263+
}
264+
265+
return $pre_render;
223266
}
224267

225268
/**

0 commit comments

Comments
 (0)