From bee2dc4ba20b6ad2bd5ec4a4327b36383407497a Mon Sep 17 00:00:00 2001 From: Isabel Brison Date: Wed, 20 Sep 2023 01:24:32 +0000 Subject: [PATCH] Editor: Fix post editor layout when Post Content has no attributes. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes output of `wp_get_post_content_block_attributes` to return null if Post Content block doesn’t exist or empty array if it has no attributes. Props flixos90, mukesh27. Fixes #59358. git-svn-id: https://develop.svn.wordpress.org/trunk@56629 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-editor.php | 11 +- .../style.css | 7 ++ .../templates/index.html | 3 + .../templates/single.html | 2 + .../theme.json | 104 ++++++++++++++++++ tests/phpunit/tests/blocks/editor.php | 23 +++- tests/phpunit/tests/theme/themeDir.php | 1 + 7 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 tests/phpunit/data/themedir1/block-theme-post-content-default/style.css create mode 100644 tests/phpunit/data/themedir1/block-theme-post-content-default/templates/index.html create mode 100644 tests/phpunit/data/themedir1/block-theme-post-content-default/templates/single.html create mode 100644 tests/phpunit/data/themedir1/block-theme-post-content-default/theme.json diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index 05bcd4e7c2..ba92c0ec3f 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -409,11 +409,12 @@ function wp_get_first_block( $blocks, $block_name ) { * Retrieves Post Content block attributes from the current post template. * * @since 6.3.0 + * @since 6.4.0 Return null if there is no post content block. * @access private * * @global int $post_ID * - * @return array Post Content block attributes or empty array if they don't exist. + * @return array|null Post Content block attributes array or null if Post Content block doesn't exist. */ function wp_get_post_content_block_attributes() { global $post_ID; @@ -421,7 +422,7 @@ function wp_get_post_content_block_attributes() { $is_block_theme = wp_is_block_theme(); if ( ! $is_block_theme || ! $post_ID ) { - return array(); + return null; } $template_slug = get_page_template_slug( $post_ID ); @@ -457,12 +458,12 @@ function wp_get_post_content_block_attributes() { $template_blocks = parse_blocks( $current_template[0]->content ); $post_content_block = wp_get_first_block( $template_blocks, 'core/post-content' ); - if ( ! empty( $post_content_block['attrs'] ) ) { + if ( isset( $post_content_block['attrs'] ) ) { return $post_content_block['attrs']; } } - return array(); + return null; } /** @@ -635,7 +636,7 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex $post_content_block_attributes = wp_get_post_content_block_attributes(); - if ( ! empty( $post_content_block_attributes ) ) { + if ( isset( $post_content_block_attributes ) ) { $editor_settings['postContentAttributes'] = $post_content_block_attributes; } diff --git a/tests/phpunit/data/themedir1/block-theme-post-content-default/style.css b/tests/phpunit/data/themedir1/block-theme-post-content-default/style.css new file mode 100644 index 0000000000..86f835f8dc --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme-post-content-default/style.css @@ -0,0 +1,7 @@ +/* +Theme Name: Block Theme Post Content Default +Theme URI: https://wordpress.org/ +Description: For testing purposes only. +Version: 1.0.0 +Text Domain: block-theme-post-content-default +*/ diff --git a/tests/phpunit/data/themedir1/block-theme-post-content-default/templates/index.html b/tests/phpunit/data/themedir1/block-theme-post-content-default/templates/index.html new file mode 100644 index 0000000000..e76bcaaf3a --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme-post-content-default/templates/index.html @@ -0,0 +1,3 @@ + +

Index Template

+ \ No newline at end of file diff --git a/tests/phpunit/data/themedir1/block-theme-post-content-default/templates/single.html b/tests/phpunit/data/themedir1/block-theme-post-content-default/templates/single.html new file mode 100644 index 0000000000..45b08fd06d --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme-post-content-default/templates/single.html @@ -0,0 +1,2 @@ + + diff --git a/tests/phpunit/data/themedir1/block-theme-post-content-default/theme.json b/tests/phpunit/data/themedir1/block-theme-post-content-default/theme.json new file mode 100644 index 0000000000..781d5ed669 --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme-post-content-default/theme.json @@ -0,0 +1,104 @@ +{ + "version": 1, + "title": "Block theme", + "settings": { + "color": { + "palette": [ + { + "slug": "light", + "name": "Light", + "color": "#f5f7f9" + }, + { + "slug": "dark", + "name": "Dark", + "color": "#000" + } + ], + "gradients": [ + { + "name": "Custom gradient", + "gradient": "linear-gradient(135deg,rgba(0,0,0) 0%,rgb(0,0,0) 100%)", + "slug": "custom-gradient" + } + ], + "duotone": [ + { + "colors": [ "#333333", "#aaaaaa" ], + "slug": "custom-duotone", + "name": "Custom Duotone" + } + ], + "custom": false, + "customGradient": false + }, + "typography": { + "fontSizes": [ + { + "name": "Custom", + "slug": "custom", + "size": "100px" + } + ], + "customFontSize": false, + "customLineHeight": true + }, + "spacing": { + "units": ["rem"], + "customPadding": true, + "blockGap": true + }, + "blocks": { + "core/paragraph": { + "color": { + "palette": [ + { + "slug": "light", + "name": "Light", + "color": "#f5f7f9" + } + ] + } + } + } + }, + "styles" : { + "blocks" :{ + "core/post-featured-image": { + "shadow": "10px 10px 5px 0px rgba(0,0,0,0.66)", + "filter": { + "duotone": "var(--wp--preset--duotone--custom-duotone)" + } + } + }, + "elements": { + "button": { + "shadow": "10px 10px 5px 0px rgba(0,0,0,0.66)" + }, + "link": { + "typography": { + "textDecoration": "none" + }, + "border": { + "bottom": { + "width": "2px", + "color": "currentColor", + "style": "solid" + } + }, + ":hover": { + "typography": { + "textDecoration": "none" + }, + "border": { + "bottom": { + "width": "2px", + "color": "#000", + "style": "dotted" + } + } + } + } + } + } +} diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index ee5458e06e..a6f400e167 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -446,14 +446,20 @@ class Tests_Blocks_Editor extends WP_UnitTestCase { 'type' => 'constrained', ), ); - // With no block theme, expect an empty array. - $this->assertSame( array(), wp_get_post_content_block_attributes() ); + // With no block theme, expect null. + $this->assertNull( wp_get_post_content_block_attributes() ); switch_theme( 'block-theme' ); $this->assertSame( $attributes_with_layout, wp_get_post_content_block_attributes() ); } + public function test_wp_get_post_content_block_attributes_no_layout() { + switch_theme( 'block-theme-post-content-default' ); + + $this->assertSame( array(), wp_get_post_content_block_attributes() ); + } + /** * @ticket 53458 */ @@ -527,6 +533,19 @@ class Tests_Blocks_Editor extends WP_UnitTestCase { switch_theme( WP_DEFAULT_THEME ); } + /** + * @ticket 59358 + */ + public function test_get_block_editor_settings_without_post_content_block() { + + $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); + + $settings = get_block_editor_settings( array(), $post_editor_context ); + + $this->assertArrayNotHasKey( 'postContentAttributes', $settings ); + + } + /** * @ticket 52920 * @expectedDeprecated block_editor_settings diff --git a/tests/phpunit/tests/theme/themeDir.php b/tests/phpunit/tests/theme/themeDir.php index 253ae5f51d..eedd92bcf6 100644 --- a/tests/phpunit/tests/theme/themeDir.php +++ b/tests/phpunit/tests/theme/themeDir.php @@ -185,6 +185,7 @@ class Tests_Theme_ThemeDir extends WP_UnitTestCase { 'Block Theme [0.4.0]', 'Block Theme [1.0.0] in subdirectory', 'Block Theme Deprecated Path', + 'Block Theme Post Content Default', 'Block Theme with defined Typography Fonts', 'Empty `fontFace` in theme.json - no webfonts defined', 'A theme with the Update URI header',