Skip to content

Block Hooks: Enable for content-like Custom Post Types #9245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -776,16 +776,4 @@
add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes' );
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' );

// Update ignoredHookedBlocks postmeta for some post types.
add_filter( 'rest_pre_insert_page', 'update_ignored_hooked_blocks_postmeta' );
add_filter( 'rest_pre_insert_post', 'update_ignored_hooked_blocks_postmeta' );
add_filter( 'rest_pre_insert_wp_block', 'update_ignored_hooked_blocks_postmeta' );
add_filter( 'rest_pre_insert_wp_navigation', 'update_ignored_hooked_blocks_postmeta' );

// Inject hooked blocks into the Posts endpoint REST response for some given post types.
add_filter( 'rest_prepare_page', 'insert_hooked_blocks_into_rest_response', 10, 2 );
add_filter( 'rest_prepare_post', 'insert_hooked_blocks_into_rest_response', 10, 2 );
add_filter( 'rest_prepare_wp_block', 'insert_hooked_blocks_into_rest_response', 10, 2 );
add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 );

unset( $filter, $action );
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,35 @@ protected function prepare_item_for_database( $request ) {
$prepared_post->page_template = null;
}

/**
* Apply Block Hooks to content-like post types.
*
* Content-like post types are those that support the editor and would benefit
* from Block Hooks functionality. This replaces the individual post type filters
* that were previously hardcoded in default-filters.php.
*
* @since 6.9.0
*/
$content_like_post_types = array( 'post', 'page', 'wp_block', 'wp_navigation' );

/**
* Filters which post types should have Block Hooks applied.
*
* Allows themes and plugins to add or remove post types that should
* have Block Hooks functionality enabled in the REST API.
*
* @since 6.9.0
*
* @param array $content_like_post_types Array of post type names that support Block Hooks.
* @param string $post_type The current post type being processed.
* @param object $prepared_post The prepared post object.
*/
$content_like_post_types = apply_filters( 'rest_block_hooks_post_types', $content_like_post_types, $this->post_type, $prepared_post );

if ( in_array( $this->post_type, $content_like_post_types, true ) ) {
$prepared_post = update_ignored_hooked_blocks_postmeta( $prepared_post );
}

/**
* Filters a post before it is inserted via the REST API.
*
Expand Down Expand Up @@ -2116,6 +2145,34 @@ public function prepare_item_for_response( $item, $request ) {
}
}

/**
* Apply Block Hooks to content-like post types for REST response.
*
* This replaces the individual post type filters that were previously hardcoded
* in default-filters.php.
*
* @since 6.9.0
*/
$content_like_post_types = array( 'post', 'page', 'wp_block', 'wp_navigation' );

/**
* Filters which post types should have Block Hooks applied.
*
* Allows themes and plugins to add or remove post types that should
* have Block Hooks functionality enabled in the REST API.
*
* @since 6.9.0
*
* @param array $content_like_post_types Array of post type names that support Block Hooks.
* @param string $post_type The current post type being processed.
* @param WP_Post $post The post object.
*/
$content_like_post_types = apply_filters( 'rest_block_hooks_post_types', $content_like_post_types, $this->post_type, $post );

if ( in_array( $this->post_type, $content_like_post_types, true ) ) {
$response = insert_hooked_blocks_into_rest_response( $response, $post );
}

/**
* Filters the post data for a REST API response.
*
Expand Down
Loading