From 90b0319ca7286c23e52cb7ab3b3c8e317418ebcd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 16 Jul 2020 21:45:17 +0200 Subject: [PATCH 1/2] Post Content Block: Disable when editing content --- .../block-library/src/post-content/index.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index cca80b41dc922..9df9fabb41b62 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -37,3 +37,21 @@ 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 ) { + 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 ); From c707d544fc4dfe54cc57beb3c77c283e25d782f0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 22 Jul 2020 13:53:33 +0200 Subject: [PATCH 2/2] Add allowed blocks array if it's not present --- packages/block-library/src/post-content/index.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 9df9fabb41b62..7e83f2ea2e8cc 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -48,6 +48,14 @@ function register_block_core_post_content() { * @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; }