Make WordPress Core


Ignore:
Timestamp:
02/12/2024 01:13:38 PM (3 years 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.