Make WordPress Core


Ignore:
Timestamp:
04/25/2025 07:21:31 PM (8 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/applyBlockHooksToContent.php

    r59523 r60189  
    1818     *
    1919     * @ticket 61902.
     20     * @ticket 63287.
    2021     */
    2122    public static function wpSetUpBeforeClass() {
     
    2425            array(
    2526                'block_hooks' => array(
    26                     'tests/anchor-block' => 'after',
     27                    'core/post-content' => 'after',
    2728                ),
    2829            )
     
    8081    /**
    8182     * @ticket 61902
     83     * @ticket 63287
    8284     */
    8385    public function test_apply_block_hooks_to_content_inserts_hooked_block() {
    8486        $context          = new WP_Block_Template();
    85         $context->content = '<!-- wp:tests/anchor-block /-->';
     87        $context->content = '<!-- wp:post-content /-->';
    8688
    8789        $actual = apply_block_hooks_to_content( $context->content, $context, 'insert_hooked_blocks' );
    8890        $this->assertSame(
    89             '<!-- wp:tests/anchor-block /--><!-- wp:tests/hooked-block /-->',
     91            '<!-- wp:post-content /--><!-- wp:tests/hooked-block /-->',
    9092            $actual
    9193        );
     
    9496    /**
    9597     * @ticket 61074
     98     * @ticket 63287
    9699     */
    97100    public function test_apply_block_hooks_to_content_with_context_set_to_null() {
    98         $content = '<!-- wp:tests/anchor-block /-->';
     101        $content = '<!-- wp:post-content /-->';
    99102
    100103        /*
     
    107110        $actual = apply_block_hooks_to_content( $content, null, 'insert_hooked_blocks' );
    108111        $this->assertSame(
    109             '<!-- wp:tests/anchor-block /--><!-- wp:tests/hooked-block /-->',
     112            '<!-- wp:post-content /--><!-- wp:tests/hooked-block /-->',
    110113            $actual
    111114        );
Note: See TracChangeset for help on using the changeset viewer.