From 051f7acb8c80ba2cb0b961ae2709f476a63bfa6d Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Thu, 26 Nov 2026 16:22:06 +0000 Subject: [PATCH 1/2] Abilities API: Insert hooked blocks into the raw content of `core/content-query`. The rendered content of a post includes the blocks hooked to its blocks through Block Hooks, but the raw content held only the stored markup. A client that edited the raw content could not remove a hooked block, and adding it by hand rendered it twice. The raw content now includes the hooked blocks, and each anchor block lists them in its `metadata.ignoredHookedBlocks` attribute. --- .../abilities/class-wp-abilities-content.php | 34 ++- .../wpRegisterCoreContentAbility.php | 250 ++++++++++++++++++ 2 files changed, 282 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index a8d9954e30965..051980385c134 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -967,6 +967,7 @@ private function normalize_fields( array $input ): array { * posts the current user can edit. * * @since 7.2.0 + * @since 7.3.0 The raw content includes the blocks hooked to it. * * @return array Post field definitions. */ @@ -1067,7 +1068,7 @@ private function get_post_properties(): array { 'properties' => array( 'raw' => array( 'type' => 'string', - 'description' => __( 'The raw, unfiltered post content (block markup). Present when the current user can edit the post.' ), + 'description' => __( 'The raw post content (block markup), including the blocks inserted through Block Hooks. Present when the current user can edit the post.' ), ), 'rendered' => array( 'type' => 'string', @@ -1441,6 +1442,7 @@ private function format_post( WP_Post $post, array $fields ): array { * API behavior. * * @since 7.2.0 + * @since 7.3.0 The raw content includes the blocks hooked to it. * * @param WP_Post $post The post object. * @param list $fields The requested field paths. @@ -1500,7 +1502,7 @@ private function build_post_fields( WP_Post $post, array $fields, bool $can_edit $value = array(); if ( $can_edit && $this->is_field_requested( "{$field}.raw", $fields ) ) { - $value['raw'] = $post->$property; + $value['raw'] = 'content' === $field ? $this->get_raw_content( $post ) : $post->$property; } if ( $this->is_field_requested( "{$field}.rendered", $fields ) ) { $value['rendered'] = ( $protectable && $is_protected ) ? '' : $this->$render( $post ); @@ -1616,6 +1618,34 @@ private function get_rendered_excerpt( WP_Post $post ): string { return is_string( $excerpt ) ? $excerpt : ''; } + /** + * Returns the raw post content with the blocks hooked to it inserted. + * + * The rendered content gets the blocks hooked to the post's blocks through `the_content`, + * so the raw content gets them too, and each anchor block lists the block types hooked to + * it in its `metadata.ignoredHookedBlocks` attribute. Content edited from the raw value + * can then keep or remove a hooked block, without the block being inserted again when it + * is rendered. Blocks hooked as the first or last child of the post content have no anchor + * block in the content to hold that list; the post's `_wp_ignored_hooked_blocks` meta + * holds it instead and is not returned. Empty content is returned as is. + * + * @since 7.3.0 + * + * @param WP_Post $post The post object. + * @return string Raw post content. + */ + private function get_raw_content( WP_Post $post ): string { + if ( '' === $post->post_content ) { + return ''; + } + + return apply_block_hooks_to_content_from_post_object( + $post->post_content, + $post, + 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' + ); + } + /** * Returns post content transformed for display. * diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index cf537f5cf0911..cfd804f4a87b7 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -70,6 +70,33 @@ class Tests_Abilities_API_WpRegisterCoreContentAbility extends WP_UnitTestCase { ), ); + /** + * The block type that the Block Hooks tests hook to an anchor block. + * + * @since 7.3.0 + * + * @var string + */ + private const HOOKED_BLOCK_TYPE = 'tests/hooked-block'; + + /** + * The markup that the hooked block type renders. + * + * @since 7.3.0 + * + * @var string + */ + private const HOOKED_BLOCK_MARKUP = '

Hooked block

'; + + /** + * Post content holding a single paragraph block, used as the anchor block. + * + * @since 7.3.0 + * + * @var string + */ + private const ANCHOR_BLOCK_CONTENT = '

Anchor

'; + /** * Shared user IDs keyed by role or fixture name. * @@ -182,6 +209,10 @@ public function tear_down(): void { */ $this->reset_post_types(); + if ( WP_Block_Type_Registry::get_instance()->is_registered( self::HOOKED_BLOCK_TYPE ) ) { + unregister_block_type( self::HOOKED_BLOCK_TYPE ); + } + parent::tear_down(); } @@ -1216,6 +1247,225 @@ public function test_query_by_id_can_return_content_fields(): void { ); } + /** + * Registers the hooked block type and hooks it to an anchor block in a relative position. + * + * @since 7.3.0 + * + * @param string $anchor_block_type The anchor block type. + * @param string $relative_position The position relative to the anchor block. + */ + private function hook_block( string $anchor_block_type, string $relative_position ): void { + register_block_type( + self::HOOKED_BLOCK_TYPE, + array( + 'render_callback' => static function (): string { + return self::HOOKED_BLOCK_MARKUP; + }, + ) + ); + + add_filter( + 'hooked_block_types', + static function ( $hooked_block_types, $position, $anchor ) use ( $anchor_block_type, $relative_position ) { + if ( $anchor_block_type === $anchor && $relative_position === $position ) { + $hooked_block_types[] = self::HOOKED_BLOCK_TYPE; + } + + return $hooked_block_types; + }, + 10, + 3 + ); + } + + /** + * Returns the raw and rendered content of a post, and its rendered content when the raw + * content is not requested. + * + * @since 7.3.0 + * + * @param int $post_id The post ID. + * @return array{raw: string, rendered: string, rendered_alone: string} The content values. + */ + private function query_content( int $post_id ): array { + $ability = wp_get_ability( 'core/content-query' ); + $where = array( + 'type' => 'post', + 'ids' => array( $post_id ), + ); + + $content = $ability->execute( + array( + 'where' => $where, + 'fields' => array( 'content.raw', 'content.rendered' ), + ) + )['items'][0]['content']; + $rendered = $ability->execute( + array( + 'where' => $where, + 'fields' => array( 'content.rendered' ), + ) + )['items'][0]['content']; + + return array( + 'raw' => $content['raw'], + 'rendered' => $content['rendered'], + 'rendered_alone' => $rendered['rendered'], + ); + } + + /** + * Returns the positions a block can be hooked in, with the raw content expected for the + * anchor block content. + * + * @return array The hooked block positions. + */ + public static function data_hooked_block_positions(): array { + return array( + 'before an anchor block' => array( + 'anchor_block_type' => 'core/paragraph', + 'relative_position' => 'before', + 'expected_raw' => '

Anchor

', + ), + 'after an anchor block' => array( + 'anchor_block_type' => 'core/paragraph', + 'relative_position' => 'after', + 'expected_raw' => '

Anchor

', + ), + 'first child of the post content' => array( + 'anchor_block_type' => 'core/post-content', + 'relative_position' => 'first_child', + 'expected_raw' => '' . self::ANCHOR_BLOCK_CONTENT, + ), + 'last child of the post content' => array( + 'anchor_block_type' => 'core/post-content', + 'relative_position' => 'last_child', + 'expected_raw' => self::ANCHOR_BLOCK_CONTENT . '', + ), + ); + } + + /** + * The raw content includes the blocks hooked to the post's blocks, with each anchor block + * listing them as ignored hooked blocks, while the rendered content renders each hooked + * block once. A block hooked as the first or last child of the post content has no anchor + * block in the content, so no ignored hooked blocks are listed for it. + * + * @ticket TBD + * @since 7.3.0 + * + * @dataProvider data_hooked_block_positions + * + * @param string $anchor_block_type The anchor block type. + * @param string $relative_position The position relative to the anchor block. + * @param string $expected_raw The expected raw content. + */ + public function test_raw_content_includes_hooked_blocks( string $anchor_block_type, string $relative_position, string $expected_raw ): void { + $this->login_as( 'administrator' ); + $this->register_ability(); + $post_id = self::factory()->post->create( array( 'post_content' => self::ANCHOR_BLOCK_CONTENT ) ); + $this->hook_block( $anchor_block_type, $relative_position ); + + $content = $this->query_content( $post_id ); + + $this->assertSame( $expected_raw, $content['raw'], 'The raw content should include the hooked block.' ); + $this->assertSame( 1, substr_count( $content['rendered'], self::HOOKED_BLOCK_MARKUP ), 'The rendered content should render the hooked block once.' ); + $this->assertSame( $content['rendered_alone'], $content['rendered'], 'Requesting the raw content should not change the rendered content.' ); + $this->assertSame( self::ANCHOR_BLOCK_CONTENT, get_post( $post_id )->post_content, 'Querying the content should not change the stored content.' ); + } + + /** + * Returns post content of each kind, for posts with no hooked blocks. + * + * @return array The post content. + */ + public static function data_content_without_hooked_blocks(): array { + return array( + 'block content' => array( + 'post_content' => self::ANCHOR_BLOCK_CONTENT, + ), + 'classic content' => array( + 'post_content' => '

Classic

', + ), + ); + } + + /** + * The raw content is the stored content when no block is hooked to the post's blocks. + * + * @ticket TBD + * @since 7.3.0 + * + * @dataProvider data_content_without_hooked_blocks + * + * @param string $post_content The stored post content. + */ + public function test_raw_content_is_stored_content_without_hooked_blocks( string $post_content ): void { + $this->login_as( 'administrator' ); + $this->register_ability(); + $post_id = self::factory()->post->create( array( 'post_content' => $post_content ) ); + + $content = $this->query_content( $post_id ); + + $this->assertSame( $post_content, $content['raw'], 'The raw content should be the stored content.' ); + $this->assertSame( $content['rendered_alone'], $content['rendered'], 'Requesting the raw content should not change the rendered content.' ); + } + + /** + * Returns posts that already list the hooked block as ignored: in the anchor block's + * metadata, or in the post meta for a block hooked as a child of the post content. + * + * @return array The posts. + */ + public static function data_posts_that_ignore_the_hooked_block(): array { + return array( + 'anchor block metadata' => array( + 'anchor_block_type' => 'core/paragraph', + 'relative_position' => 'after', + 'post_content' => '

Anchor

', + 'ignored_at_root' => false, + ), + 'post meta' => array( + 'anchor_block_type' => 'core/post-content', + 'relative_position' => 'first_child', + 'post_content' => self::ANCHOR_BLOCK_CONTENT, + 'ignored_at_root' => true, + ), + ); + } + + /** + * A hooked block that the post already lists as ignored, such as one removed from the + * content, is not inserted into the raw or rendered content. + * + * @ticket TBD + * @since 7.3.0 + * + * @dataProvider data_posts_that_ignore_the_hooked_block + * + * @param string $anchor_block_type The anchor block type. + * @param string $relative_position The position relative to the anchor block. + * @param string $post_content The stored post content. + * @param bool $ignored_at_root Whether the post meta lists the hooked block as ignored. + */ + public function test_raw_content_leaves_out_ignored_hooked_blocks( string $anchor_block_type, string $relative_position, string $post_content, bool $ignored_at_root ): void { + $this->login_as( 'administrator' ); + $this->register_ability(); + $post_id = self::factory()->post->create( + array( + 'post_content' => $post_content, + 'meta_input' => $ignored_at_root ? array( '_wp_ignored_hooked_blocks' => wp_json_encode( array( self::HOOKED_BLOCK_TYPE ) ) ) : array(), + ) + ); + $this->hook_block( $anchor_block_type, $relative_position ); + + $content = $this->query_content( $post_id ); + + $this->assertSame( $post_content, $content['raw'], 'The raw content should be the stored content.' ); + $this->assertStringNotContainsString( self::HOOKED_BLOCK_MARKUP, $content['rendered'], 'The rendered content should not render the ignored hooked block.' ); + } + /** * Returns `where` values whose IDs or slugs match no post of the post type: a missing ID * and a slug that no post has. From 311123687838f5e2e1e2448fc47166d377ddad04 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 27 Nov 2026 11:58:58 +0000 Subject: [PATCH 2/2] Tests: Reference the ticket in the `core/content-query` Block Hooks tests. --- .../tests/abilities-api/wpRegisterCoreContentAbility.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index cfd804f4a87b7..d75e90f016e6e 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -1352,7 +1352,7 @@ public static function data_hooked_block_positions(): array { * block once. A block hooked as the first or last child of the post content has no anchor * block in the content, so no ignored hooked blocks are listed for it. * - * @ticket TBD + * @ticket 66847 * @since 7.3.0 * * @dataProvider data_hooked_block_positions @@ -1394,7 +1394,7 @@ public static function data_content_without_hooked_blocks(): array { /** * The raw content is the stored content when no block is hooked to the post's blocks. * - * @ticket TBD + * @ticket 66847 * @since 7.3.0 * * @dataProvider data_content_without_hooked_blocks @@ -1439,7 +1439,7 @@ public static function data_posts_that_ignore_the_hooked_block(): array { * A hooked block that the post already lists as ignored, such as one removed from the * content, is not inserted into the raw or rendered content. * - * @ticket TBD + * @ticket 66847 * @since 7.3.0 * * @dataProvider data_posts_that_ignore_the_hooked_block