WordPress.org

Make WordPress Core

Ticket #46187: 46187.2.diff

File 46187.2.diff, 2.3 KB (added by noisysocks, 4 months ago)
  • src/wp-includes/class-wp-block.php

     
    241241                 * @param string $block_content The block content about to be appended.
    242242                 * @param array  $block         The full block, including name and attributes.
    243243                 */
    244                 return apply_filters( 'render_block', $block_content, $this->parsed_block );
     244                $block_content = apply_filters( 'render_block', $block_content, $this->parsed_block );
     245
     246                /**
     247                 * Filters the content of a single block.
     248                 *
     249                 * The dynamic portion of the hook name, `$name`, refers to
     250                 * the block name, e.g. "core/paragraph".
     251                 *
     252                 * @since 5.7.0
     253                 *
     254                 * @param string $block_content The block content about to be appended.
     255                 * @param array  $block         The full block, including name and attributes.
     256                 */
     257                return apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block );
    245258        }
    246259
    247260}
  • tests/phpunit/tests/blocks/block.php

     
    328328                remove_filter( 'render_block', array( $this, 'filter_render_block' ) );
    329329
    330330                $this->assertSame( 'Original: "StaticOriginal: "Inner", from block "core/example"", from block "core/example"', $rendered_content );
     331        }
     332
     333        /**
     334         * @ticket 46187
     335         */
     336        function test_render_applies_dynamic_render_block_filter() {
     337                $this->registry->register( 'core/example', array() );
     338
     339                add_filter( 'render_block_core/example', array( $this, 'filter_render_block' ), 10, 2 );
     340
     341                $parsed_blocks = parse_blocks( '<!-- wp:example -->Static<!-- wp:example -->Inner<!-- /wp:example --><!-- /wp:example -->' );
     342                $parsed_block  = $parsed_blocks[0];
     343                $context       = array();
     344                $block         = new WP_Block( $parsed_block, $context, $this->registry );
    331345
     346                $rendered_content = $block->render();
     347
     348                remove_filter( 'render_block_core/example', array( $this, 'filter_render_block' ) );
     349
     350                $this->assertSame( 'Original: "StaticOriginal: "Inner", from block "core/example"", from block "core/example"', $rendered_content );
    332351        }
    333352
    334353        /**