Skip to content

Commit ee278ed

Browse files
committed
Editor: Enable Block Hooks for content-like Custom Post Types.
This changeset moves the Block Hooks logic from individual post type filters to the REST controller. Props bernhard-reiter, iamadisingh, audrasjb, r1k0. Fixes #62715. git-svn-id: https://develop.svn.wordpress.org/trunk@61707 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5c5f645 commit ee278ed

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

src/wp-includes/default-filters.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -800,16 +800,4 @@
800800
add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes' );
801801
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' );
802802

803-
// Update ignoredHookedBlocks postmeta for some post types.
804-
add_filter( 'rest_pre_insert_page', 'update_ignored_hooked_blocks_postmeta' );
805-
add_filter( 'rest_pre_insert_post', 'update_ignored_hooked_blocks_postmeta' );
806-
add_filter( 'rest_pre_insert_wp_block', 'update_ignored_hooked_blocks_postmeta' );
807-
add_filter( 'rest_pre_insert_wp_navigation', 'update_ignored_hooked_blocks_postmeta' );
808-
809-
// Inject hooked blocks into the Posts endpoint REST response for some given post types.
810-
add_filter( 'rest_prepare_page', 'insert_hooked_blocks_into_rest_response', 10, 2 );
811-
add_filter( 'rest_prepare_post', 'insert_hooked_blocks_into_rest_response', 10, 2 );
812-
add_filter( 'rest_prepare_wp_block', 'insert_hooked_blocks_into_rest_response', 10, 2 );
813-
add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 );
814-
815803
unset( $filter, $action );

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,35 @@ protected function prepare_item_for_database( $request ) {
14761476
$prepared_post->page_template = null;
14771477
}
14781478

1479+
/**
1480+
* Applies Block Hooks to content-like post types.
1481+
*
1482+
* Content-like post types are those that support the editor and would benefit
1483+
* from Block Hooks functionality. This replaces the individual post type filters
1484+
* that were previously hardcoded in default-filters.php.
1485+
*
1486+
* @since 7.0.0
1487+
*/
1488+
$content_like_post_types = array( 'post', 'page', 'wp_block', 'wp_navigation' );
1489+
1490+
/**
1491+
* Filters which post types should have Block Hooks applied.
1492+
*
1493+
* Allows themes and plugins to add or remove post types that should
1494+
* have Block Hooks functionality enabled in the REST API.
1495+
*
1496+
* @since 7.0.0
1497+
*
1498+
* @param array $content_like_post_types Array of post type names that support Block Hooks.
1499+
* @param string $post_type The current post type being processed.
1500+
* @param object $prepared_post The prepared post object.
1501+
*/
1502+
$content_like_post_types = apply_filters( 'rest_block_hooks_post_types', $content_like_post_types, $this->post_type, $prepared_post );
1503+
1504+
if ( in_array( $this->post_type, $content_like_post_types, true ) ) {
1505+
$prepared_post = update_ignored_hooked_blocks_postmeta( $prepared_post );
1506+
}
1507+
14791508
/**
14801509
* Filters a post before it is inserted via the REST API.
14811510
*
@@ -2127,6 +2156,34 @@ public function prepare_item_for_response( $item, $request ) {
21272156
}
21282157
}
21292158

2159+
/**
2160+
* Applies Block Hooks to content-like post types for REST response.
2161+
*
2162+
* This replaces the individual post type filters that were previously hardcoded
2163+
* in default-filters.php.
2164+
*
2165+
* @since 7.0.0
2166+
*/
2167+
$content_like_post_types = array( 'post', 'page', 'wp_block', 'wp_navigation' );
2168+
2169+
/**
2170+
* Filters which post types should have Block Hooks applied.
2171+
*
2172+
* Allows themes and plugins to add or remove post types that should
2173+
* have Block Hooks functionality enabled in the REST API.
2174+
*
2175+
* @since 7.0.0
2176+
*
2177+
* @param array $content_like_post_types Array of post type names that support Block Hooks.
2178+
* @param string $post_type The current post type being processed.
2179+
* @param WP_Post $post The post object.
2180+
*/
2181+
$content_like_post_types = apply_filters( 'rest_block_hooks_post_types', $content_like_post_types, $this->post_type, $post );
2182+
2183+
if ( in_array( $this->post_type, $content_like_post_types, true ) ) {
2184+
$response = insert_hooked_blocks_into_rest_response( $response, $post );
2185+
}
2186+
21302187
/**
21312188
* Filters the post data for a REST API response.
21322189
*

0 commit comments

Comments
 (0)