Make WordPress Core

Changeset 57594


Ignore:
Timestamp:
02/12/2024 01:13:38 PM (7 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Inject hooked blocks into modified templates and parts.

Using the new technique introduced in [57157] of using a metadata.ignoredHookedBlocks attribute in the anchor block to store information about whether or not a hooked block should be considered for injection, extend said injection to encompass modified templates and parts.

Fixes #59646.
Props gziolo, matveb.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r57349 r57594  
    902902    }
    903903
     904    $hooked_blocks = get_hooked_blocks();
     905    if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
     906        $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
     907        $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template );
     908        $blocks               = parse_blocks( $template->content );
     909        $template->content    = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
     910    }
     911
    904912    return $template;
    905913}
  • trunk/tests/phpunit/tests/block-templates/base.php

    r54269 r57594  
    4040                'post_name'    => 'my_template',
    4141                'post_title'   => 'My Template',
    42                 'post_content' => 'Content',
     42                'post_content' => '<!-- wp:heading {"level":1} --><h1>Template</h1><!-- /wp:heading -->',
    4343                'post_excerpt' => 'Description of my template',
    4444                'tax_input'    => array(
     
    5858                'post_name'    => 'my_template_part',
    5959                'post_title'   => 'My Template Part',
    60                 'post_content' => 'Content',
     60                'post_content' => '<!-- wp:heading {"level":2} --><h2>Template Part</h2><!-- /wp:heading -->',
    6161                'post_excerpt' => 'Description of my template part',
    6262                'tax_input'    => array(
  • trunk/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php

    r56983 r57594  
    88 */
    99class Tests_Block_Templates_BuildBlockTemplateResultFromPost extends WP_Block_Templates_UnitTestCase {
     10
     11    /**
     12     * Tear down each test method.
     13     *
     14     * @since 6.5.0
     15     */
     16    public function tear_down() {
     17        $registry = WP_Block_Type_Registry::get_instance();
     18
     19        if ( $registry->is_registered( 'tests/my-block' ) ) {
     20            $registry->unregister( 'tests/my-block' );
     21        }
     22
     23        parent::tear_down();
     24    }
    1025
    1126    /**
     
    5065        $this->assertSame( self::$template_part_post->post_modified, $template_part->modified, 'Template part result properties match' );
    5166    }
     67
     68    /**
     69     * @ticket 59646
     70     */
     71    public function test_should_inject_hooked_block_into_template() {
     72        register_block_type(
     73            'tests/my-block',
     74            array(
     75                'block_hooks' => array(
     76                    'core/heading' => 'before',
     77                ),
     78            )
     79        );
     80
     81        $template = _build_block_template_result_from_post(
     82            self::$template_post,
     83            'wp_template'
     84        );
     85        $this->assertStringStartsWith( '<!-- wp:tests/my-block /-->', $template->content );
     86        $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template->content );
     87    }
     88
     89    /**
     90     * @ticket 59646
     91     */
     92    public function test_should_inject_hooked_block_into_template_part() {
     93        register_block_type(
     94            'tests/my-block',
     95            array(
     96                'block_hooks' => array(
     97                    'core/heading' => 'after',
     98                ),
     99            )
     100        );
     101
     102        $template_part = _build_block_template_result_from_post(
     103            self::$template_part_post,
     104            'wp_template_part'
     105        );
     106        $this->assertStringEndsWith( '<!-- wp:tests/my-block /-->', $template_part->content );
     107        $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template_part->content );
     108    }
    52109}
Note: See TracChangeset for help on using the changeset viewer.