Make WordPress Core


Ignore:
Timestamp:
04/25/2025 07:21:31 PM (7 weeks ago)
Author:
jorbin
Message:

Block Hooks: Suppress insertion next to post content wrapper block.

As of [59523], Block Hooks are applied to post content. In order to allow for insertion of a hooked block as first_child or last_child of the containing Post Content block, we wrap the block's post content (as obtained from the DB) in a temporary <!-- wp:post-content --> wrapper block, apply the Block Hooks algorithm to the resulting markup, and remove the wrapper block. (The same technique is applied for the Synced Pattern block -- see [59543] -- as well as the Navigation block.)

However, this caused a problem when a hooked block was marked for insertion before before or after a Post Content block: The logic that's supposed to remove the temporary wrapper block after the Block Hooks algorithm runs erroneously removed that hooked block's delimiter instead of the wrapper block, producing garbled markup as a result.

This changeset fixes the issue by adding a hooked_block_types filter (with PHP_INT_MAX priority) that removes any blocks hooked before or after a Post Content block, if the current context is a post object. This prevents any blocks hooked that way from being "absorbed" into the corresponding post object's content; it retains the ability to hook blocks before and after a Post Content block in any other context (e.g. a template). (The same principle is applied to Synced Pattern and Navigation blocks.)

Reviewed by Jorbin.
Merges [60173] to 6.8 branch.

Props obenland, jorbin, gziolo, bernhard-reiter.
Fixes #63287.

Location:
branches/6.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.8

  • branches/6.8/tests/phpunit/tests/blocks/applyBlockHooksToContentFromPostObject.php

    r59838 r60189  
    8989            )
    9090        );
     91
     92        register_block_type(
     93            'tests/hooked-block-after-post-content',
     94            array(
     95                'block_hooks' => array(
     96                    'core/post-content' => 'after',
     97                ),
     98            )
     99        );
     100
     101        register_block_type( 'tests/dynamically-hooked-block-before-post-content' );
    91102    }
    92103
     
    101112        $registry->unregister( 'tests/hooked-block' );
    102113        $registry->unregister( 'tests/hooked-block-first-child' );
     114        $registry->unregister( 'tests/hooked-block-after-post-content' );
     115        $registry->unregister( 'tests/dynamically-hooked-block-before-post-content' );
    103116    }
    104117
     
    132145
    133146    /**
     147     * @ticket 63287
     148     */
     149    public function test_apply_block_hooks_to_content_from_post_object_does_not_insert_hooked_block_before_container_block() {
     150        $filter = function ( $hooked_block_types, $relative_position, $anchor_block_type ) {
     151            if ( 'core/post-content' === $anchor_block_type && 'before' === $relative_position ) {
     152                $hooked_block_types[] = 'tests/dynamically-hooked-block-before-post-content';
     153            }
     154
     155            return $hooked_block_types;
     156        };
     157
     158        $expected = '<!-- wp:tests/hooked-block-first-child /-->' .
     159            self::$post->post_content .
     160            '<!-- wp:tests/hooked-block /-->';
     161
     162        add_filter( 'hooked_block_types', $filter, 10, 3 );
     163        $actual = apply_block_hooks_to_content_from_post_object(
     164            self::$post->post_content,
     165            self::$post,
     166            'insert_hooked_blocks'
     167        );
     168        remove_filter( 'hooked_block_types', $filter, 10 );
     169
     170        $this->assertSame( $expected, $actual );
     171    }
     172
     173    /**
    134174     * @ticket 62716
    135175     */
Note: See TracChangeset for help on using the changeset viewer.