Editor: Introduce a dynamic filter for the content of a single block:

`render_block_{$this->name}`

This complements the existing `render_block` hook and allows for filtering the content of a specific block without having to use conditionals inside the filter callback.

Props manzoorwani.jk, noisysocks, birgire, johnbillion.
Fixes #46187.

git-svn-id: https://develop.svn.wordpress.org/trunk@50123 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2021-02-01 18:04:36 +00:00
parent ea4841d30e
commit b6a04b3b28
2 changed files with 35 additions and 1 deletions
+16 -1
View File
@@ -241,7 +241,22 @@ class WP_Block {
* @param string $block_content The block content about to be appended.
* @param array $block The full block, including name and attributes.
*/
return apply_filters( 'render_block', $block_content, $this->parsed_block );
$block_content = apply_filters( 'render_block', $block_content, $this->parsed_block );
/**
* Filters the content of a single block.
*
* The dynamic portion of the hook name, `$name`, refers to
* the block name, e.g. "core/paragraph".
*
* @since 5.7.0
*
* @param string $block_content The block content about to be appended.
* @param array $block The full block, including name and attributes.
*/
$block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block );
return $block_content;
}
}
+19
View File
@@ -328,7 +328,26 @@ class WP_Block_Test extends WP_UnitTestCase {
remove_filter( 'render_block', array( $this, 'filter_render_block' ) );
$this->assertSame( 'Original: "StaticOriginal: "Inner", from block "core/example"", from block "core/example"', $rendered_content );
}
/**
* @ticket 46187
*/
function test_render_applies_dynamic_render_block_filter() {
$this->registry->register( 'core/example', array() );
add_filter( 'render_block_core/example', array( $this, 'filter_render_block' ), 10, 2 );
$parsed_blocks = parse_blocks( '<!-- wp:example -->Static<!-- wp:example -->Inner<!-- /wp:example --><!-- /wp:example -->' );
$parsed_block = $parsed_blocks[0];
$context = array();
$block = new WP_Block( $parsed_block, $context, $this->registry );
$rendered_content = $block->render();
remove_filter( 'render_block_core/example', array( $this, 'filter_render_block' ) );
$this->assertSame( 'Original: "StaticOriginal: "Inner", from block "core/example"", from block "core/example"', $rendered_content );
}
/**