Skip to content

Commit

Permalink
Block Hooks: Fix duplicated insertion in Post Content (#69142)
Browse files Browse the repository at this point in the history
* Block Hooks: Fix duplicated insertion in Post Content

* Add backport changelog

* Move function def and filters inside conditional

Co-authored-by: ockham <[email protected]>
Co-authored-by: gziolo <[email protected]>
  • Loading branch information
3 people authored Feb 14, 2025
1 parent 4c6306b commit 3df54ec
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
1 change: 1 addition & 0 deletions backport-changelog/6.8/8212.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
https://github.com/WordPress/wordpress-develop/pull/8212

* https://github.com/WordPress/gutenberg/pull/68926
* https://github.com/WordPress/gutenberg/pull/69142
87 changes: 46 additions & 41 deletions lib/compat/wordpress-6.8/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,56 +77,61 @@ function apply_block_hooks_to_content_from_post_object( $content, WP_Post $post
add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', 8 );
// Remove apply_block_hooks_to_content filter (previously added in Core).
remove_filter( 'the_content', 'apply_block_hooks_to_content', 8 );
}

/**
* Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks.
*
* @since 6.6.0
* @since 6.8.0 Support non-`wp_navigation` post types.
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post Post object.
* @return WP_REST_Response The response object.
*/
function gutenberg_insert_hooked_blocks_into_rest_response( $response, $post ) {
if ( empty( $response->data['content']['raw'] ) ) {
return $response;
}
/**
* Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks.
*
* @since 6.6.0
* @since 6.8.0 Support non-`wp_navigation` post types.
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post Post object.
* @return WP_REST_Response The response object.
*/
function gutenberg_insert_hooked_blocks_into_rest_response( $response, $post ) {
if ( empty( $response->data['content']['raw'] ) ) {
return $response;
}

$response->data['content']['raw'] = apply_block_hooks_to_content_from_post_object(
$response->data['content']['raw'],
$post,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);
$response->data['content']['raw'] = apply_block_hooks_to_content_from_post_object(
$response->data['content']['raw'],
$post,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);

// If the rendered content was previously empty, we leave it like that.
if ( empty( $response->data['content']['rendered'] ) ) {
return $response;
}
// If the rendered content was previously empty, we leave it like that.
if ( empty( $response->data['content']['rendered'] ) ) {
return $response;
}

// No need to inject hooked blocks twice.
$priority = has_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object' );
if ( false !== $priority ) {
remove_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
}
// No need to inject hooked blocks twice.
$priority = has_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object' );
if ( false !== $priority ) {
remove_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
}

/** This filter is documented in wp-includes/post-template.php */
$response->data['content']['rendered'] = apply_filters(
'the_content',
$response->data['content']['raw']
);
/** This filter is documented in wp-includes/post-template.php */
$response->data['content']['rendered'] = apply_filters(
'the_content',
$response->data['content']['raw']
);

// Add back the filter.
if ( false !== $priority ) {
add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
}

// Add back the filter.
if ( false !== $priority ) {
add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
return $response;
}
remove_filter( 'rest_prepare_page', 'insert_hooked_blocks_into_rest_response' );
add_filter( 'rest_prepare_page', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 );

remove_filter( 'rest_prepare_post', 'insert_hooked_blocks_into_rest_response' );
add_filter( 'rest_prepare_post', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 );

return $response;
remove_filter( 'rest_prepare_wp_block', 'insert_hooked_blocks_into_rest_response' );
add_filter( 'rest_prepare_wp_block', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 );
}
add_filter( 'rest_prepare_page', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 );
add_filter( 'rest_prepare_post', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 );
add_filter( 'rest_prepare_wp_block', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 );

/**
* Updates the wp_postmeta with the list of ignored hooked blocks
Expand Down

0 comments on commit 3df54ec

Please sign in to comment.