Skip to content
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

Post Content Block: Disable when editing content (server-side approach) #24114

Closed
Closed
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
26 changes: 26 additions & 0 deletions packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,29 @@ function register_block_core_post_content() {
);
}
add_action( 'init', 'register_block_core_post_content' );

/**
* Only show the `core/post-content` block when editing templates and template parts.
* This is to prevent infinite recursions (post content containing a Post Content block
* that attempts to embed the post content it is part of).
*
* @param bool|array $allowed_block_types Array of block type slugs, or
* boolean to enable/disable all.
* @param WP_Post $post The post resource data.
*/
function disallow_core_post_content_block_outside_templates( $allowed_block_types, $post ) {
/**
* The following if clause can be removed once Gutenberg requires a WordPress version
* that includes https://github.com/WordPress/wordpress-develop/pull/419.
*/
if ( true === $allowed_block_types ) {
$allowed_block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
}

if ( in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ), true ) ) {
return $allowed_block_types;
}
return array_diff( $allowed_block_types, array( 'core/post-content' ) );
}

add_filter( 'allowed_block_types', 'disallow_core_post_content_block_outside_templates', 10, 2 );