Skip to content

Commit

Permalink
Infer post_type from filter name
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Mar 5, 2024
1 parent 3aace5f commit 376ebdf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
38 changes: 7 additions & 31 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,13 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '
* @param string $post_type The post type of the post object.
* @return stdClass The updated post object.
*/
function inject_ignored_hooked_blocks_metadata_attributes( $post, $request, $post_type ) {
function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) {
$filter_name = current_filter();
if ( ! str_starts_with( $filter_name, 'rest_pre_insert_' ) ) {
return $post;
}
$post_type = str_replace( 'rest_pre_insert_', '', $filter_name );

$hooked_blocks = get_hooked_blocks();
if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
return $post;
Expand All @@ -1467,33 +1473,3 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request, $pos
$post->post_content = $content;
return $post;
}

/**
* Inject ignoredHookedBlocks metadata attributes into a wp_template_part.
*
* Given a `wp_template` or `wp_template_part` post object, locate all blocks that have
* hooked blocks, and inject a `metadata.ignoredHookedBlocks` attribute into the anchor
* blocks to reflect the latter.
*
* @param stdClass $post A post object with post type set to `wp_template` or `wp_template_part`.
* @param WP_REST_Request $request Request object.
* @return stdClass The updated post object.
*/
function inject_ignored_hooked_blocks_metadata_attributes_into_template_part( $post, $request ) {
return inject_ignored_hooked_blocks_metadata_attributes( $post, $request, 'wp_template_part' );
}

/**
* Inject ignoredHookedBlocks metadata attributes into a wp_template.
*
* Given a `wp_template` or `wp_template_part` post object, locate all blocks that have
* hooked blocks, and inject a `metadata.ignoredHookedBlocks` attribute into the anchor
* blocks to reflect the latter.
*
* @param stdClass $post A post object with post type set to `wp_template` or `wp_template_part`.
* @param WP_REST_Request $request Request object.
* @return stdClass The updated post object.
*/
function inject_ignored_hooked_blocks_metadata_attributes_into_template( $post, $request ) {
return inject_ignored_hooked_blocks_metadata_attributes( $post, $request, 'wp_template' );
}
4 changes: 2 additions & 2 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@
add_action( 'init', '_wp_register_default_font_collections' );

// Add ignoredHookedBlocks metadata attribute to the template and template part post types.
add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes_into_template', 10, 2 );
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes_into_template_part', 10, 2 );
add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 );
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 );

unset( $filter, $action );

0 comments on commit 376ebdf

Please sign in to comment.