From 17b11b0f11c5439947d6f39d09cfdf7a2e390737 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 16 Jul 2020 21:45:17 +0200 Subject: [PATCH 1/6] 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 cca80b41dc9226..9df9fabb41b62e 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 883524babc869dc2c608b0294ffae51c109f6a9b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 21 Jul 2020 15:35:38 +0200 Subject: [PATCH 2/6] Don't register Post Content block on the client side if editing content --- packages/block-library/src/index.js | 19 +++++++++++++++---- packages/edit-post/src/index.js | 2 +- packages/edit-site/src/index.js | 4 +++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index c962c8172dec66..b158c9ae94342a 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -174,7 +174,9 @@ export const registerCoreBlocks = () => { /** * Function to register experimental core blocks depending on editor settings. * - * @param {Object} settings Editor settings. + * @param {Object} settings Editor settings. + * @param {Object} context Post context. + * @param {string} context.postType Post Type * * @example * ```js @@ -185,7 +187,7 @@ export const registerCoreBlocks = () => { */ export const __experimentalRegisterExperimentalCoreBlocks = process.env.GUTENBERG_PHASE === 2 - ? ( settings ) => { + ? ( settings, { postType } ) => { const { __experimentalEnableLegacyWidgetBlock, __experimentalEnableFullSiteEditing, @@ -208,7 +210,16 @@ export const __experimentalRegisterExperimentalCoreBlocks = queryLoop, queryPagination, postTitle, - postContent, + /* + * Only provide the Post Content block in templates, + * so there's no risk of posts including themselves through + * a Post Content block (leading to infinite recursion). + */ + [ 'wp_template', 'wp_template_part' ].includes( + postType + ) + ? postContent + : null, postAuthor, postComments, postCommentsCount, @@ -217,7 +228,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = postExcerpt, postFeaturedImage, postTags, - ] + ].filter( Boolean ) : [] ), ].forEach( registerBlock ); } diff --git a/packages/edit-post/src/index.js b/packages/edit-post/src/index.js index 2bd1cc75e07518..59d013e7ebe43a 100644 --- a/packages/edit-post/src/index.js +++ b/packages/edit-post/src/index.js @@ -96,7 +96,7 @@ export function initializeEditor( ); registerCoreBlocks(); if ( process.env.GUTENBERG_PHASE === 2 ) { - __experimentalRegisterExperimentalCoreBlocks( settings ); + __experimentalRegisterExperimentalCoreBlocks( settings, { postType } ); } // Show a console log warning if the browser is not in Standards rendering mode. diff --git a/packages/edit-site/src/index.js b/packages/edit-site/src/index.js index 08a0e3993f0e9b..10cccc717e66d0 100644 --- a/packages/edit-site/src/index.js +++ b/packages/edit-site/src/index.js @@ -58,7 +58,9 @@ export function initialize( id, settings ) { registerCoreBlocks(); if ( process.env.GUTENBERG_PHASE === 2 ) { - __experimentalRegisterExperimentalCoreBlocks( settings ); + __experimentalRegisterExperimentalCoreBlocks( settings, { + postType: 'wp_template', + } ); } render( , document.getElementById( id ) ); From 3581cf0f26fcc2622d54980951ae015b5258e3ab Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 21 Jul 2020 15:36:51 +0200 Subject: [PATCH 3/6] Revert "Post Content Block: Disable when editing content" This reverts commit fb6bf8fe38a2bc8b34eb24f7c81f851507928a1d. --- .../block-library/src/post-content/index.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 9df9fabb41b62e..cca80b41dc9226 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -37,21 +37,3 @@ 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 c2532e14920c3a524410008169ef266952b8357f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 21 Jul 2020 17:09:06 +0200 Subject: [PATCH 4/6] Make extra arg optional --- packages/block-library/src/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index b158c9ae94342a..c947a4e54c45f8 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -174,9 +174,9 @@ export const registerCoreBlocks = () => { /** * Function to register experimental core blocks depending on editor settings. * - * @param {Object} settings Editor settings. - * @param {Object} context Post context. - * @param {string} context.postType Post Type + * @param {Object} settings Editor settings. + * @param {Object} [context] Post context. + * @param {string} [context.postType] Post Type * * @example * ```js @@ -187,7 +187,7 @@ export const registerCoreBlocks = () => { */ export const __experimentalRegisterExperimentalCoreBlocks = process.env.GUTENBERG_PHASE === 2 - ? ( settings, { postType } ) => { + ? ( settings, { postType } = {} ) => { const { __experimentalEnableLegacyWidgetBlock, __experimentalEnableFullSiteEditing, From d5e6b1447d2b7d5691c6c0e0b95f46df0e3aa2cd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 21 Jul 2020 17:22:19 +0200 Subject: [PATCH 5/6] Rephrase comment --- packages/block-library/src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index c947a4e54c45f8..870b0cc6656259 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -211,9 +211,9 @@ export const __experimentalRegisterExperimentalCoreBlocks = queryPagination, postTitle, /* - * Only provide the Post Content block in templates, - * so there's no risk of posts including themselves through - * a Post Content block (leading to infinite recursion). + * Only provide the Post Content block when editing templates or, + * template parts so there's no risk of posts including themselves + * through a Post Content block (leading to infinite recursion). */ [ 'wp_template', 'wp_template_part' ].includes( postType From 43e4a90ffabe5817ef5f20b3aeab41cdc9a98115 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 23 Jul 2020 16:22:05 +0200 Subject: [PATCH 6/6] Load FSE blocks with wp_template context in test --- test/integration/full-content/full-content.test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/integration/full-content/full-content.test.js b/test/integration/full-content/full-content.test.js index 008239bd612b61..040001690a82d8 100644 --- a/test/integration/full-content/full-content.test.js +++ b/test/integration/full-content/full-content.test.js @@ -73,7 +73,15 @@ describe( 'full post content fixture', () => { require( '../../../packages/editor/src/hooks' ); registerCoreBlocks(); if ( process.env.GUTENBERG_PHASE === 2 ) { - __experimentalRegisterExperimentalCoreBlocks( settings ); + /** + * We're loading these blocks with a `wp_template` post context, + * since most of them are meant to be used in Site Editor templates, + * to the point that e.g. the Post Content block is unavailable in + * a Post Editor (`post`) context. + */ + __experimentalRegisterExperimentalCoreBlocks( settings, { + postType: 'wp_template', + } ); } } );