From cd973dabf76d4ea9dc0c1b2199d278a8a5d7d35c Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Fri, 15 Sep 2023 15:34:38 +1000 Subject: [PATCH 01/10] Fix post editor layout when content block has no attributes. --- src/wp-includes/block-editor.php | 4 +- .../style.css | 7 ++ .../templates/index.html | 3 + .../templates/single.html | 2 + .../theme.json | 104 ++++++++++++++++++ tests/phpunit/tests/blocks/editor.php | 8 ++ 6 files changed, 126 insertions(+), 2 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 05bcd4e7c22bc..6847c16f750b7 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -457,7 +457,7 @@ 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']; } } @@ -635,7 +635,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 0000000000000..86f835f8dc8f2 --- /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 0000000000000..e76bcaaf3a3a7 --- /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 0000000000000..45b08fd06db29 --- /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 0000000000000..781d5ed669558 --- /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 13dbf59556547..cc4a5a7517595 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -454,6 +454,14 @@ public function test_wp_get_post_content_block_attributes() { $this->assertSame( $attributes_with_layout, wp_get_post_content_block_attributes() ); } + public function test_wp_get_post_content_block_attributes_no_layout() { + $attributes_empty = array(); + + switch_theme( 'block-theme-post-content-default' ); + + $this->assertSame( $attributes_empty, wp_get_post_content_block_attributes() ); + } + /** * @ticket 53458 */ From bb0636e8cc7ea2eb40deb9983844cdbdfcfad58c Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Fri, 15 Sep 2023 16:45:15 +1000 Subject: [PATCH 02/10] Add new theme to list --- tests/phpunit/tests/theme/themeDir.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/theme/themeDir.php b/tests/phpunit/tests/theme/themeDir.php index e03d7cf06c592..98085ba0fca22 100644 --- a/tests/phpunit/tests/theme/themeDir.php +++ b/tests/phpunit/tests/theme/themeDir.php @@ -184,6 +184,7 @@ public function test_theme_list() { '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', From b4e92428c3407a1b2daeeddadf3692f59bb63ea2 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Mon, 18 Sep 2023 12:08:04 +1000 Subject: [PATCH 03/10] Return null by default. --- src/wp-includes/block-editor.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index 6847c16f750b7..ace086061f85f 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -409,6 +409,7 @@ 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 by default and return empty array when Post Content block exists. but has no attributes. * @access private * * @global int $post_ID @@ -462,7 +463,7 @@ function wp_get_post_content_block_attributes() { } } - return array(); + return null; } /** From cb423e824862ec1835bfe428b5345b0891afb421 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Mon, 18 Sep 2023 14:29:02 +1000 Subject: [PATCH 04/10] Update tests/phpunit/tests/blocks/editor.php remove variable Co-authored-by: Felix Arntz --- tests/phpunit/tests/blocks/editor.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index cc4a5a7517595..071f2e2ba7e7a 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -455,11 +455,9 @@ public function test_wp_get_post_content_block_attributes() { } public function test_wp_get_post_content_block_attributes_no_layout() { - $attributes_empty = array(); - switch_theme( 'block-theme-post-content-default' ); - $this->assertSame( $attributes_empty, wp_get_post_content_block_attributes() ); + $this->assertSame( array(), wp_get_post_content_block_attributes() ); } /** From 2f8c135232a5e608953e31304fe8c32cb4892129 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Mon, 18 Sep 2023 14:35:48 +1000 Subject: [PATCH 05/10] Update function logic and test. --- src/wp-includes/block-editor.php | 4 ++-- tests/phpunit/tests/blocks/editor.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index ace086061f85f..ce5aed0ef992e 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -414,7 +414,7 @@ function wp_get_first_block( $blocks, $block_name ) { * * @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; @@ -422,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 ); diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index 071f2e2ba7e7a..2ec13d2a4c23a 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -447,7 +447,7 @@ public function test_wp_get_post_content_block_attributes() { ), ); // With no block theme, expect an empty array. - $this->assertSame( array(), wp_get_post_content_block_attributes() ); + $this->assertSame( null, wp_get_post_content_block_attributes() ); switch_theme( 'block-theme' ); From 3425504ef544f08e86fc2f03e69c28d99eae71b7 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Mon, 18 Sep 2023 15:46:11 +1000 Subject: [PATCH 06/10] Update tests/phpunit/tests/blocks/editor.php Use assertNull Co-authored-by: Mukesh Panchal --- tests/phpunit/tests/blocks/editor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index 2ec13d2a4c23a..16415d65c8211 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -447,7 +447,7 @@ public function test_wp_get_post_content_block_attributes() { ), ); // With no block theme, expect an empty array. - $this->assertSame( null, wp_get_post_content_block_attributes() ); + $this->assertNull( wp_get_post_content_block_attributes() ); switch_theme( 'block-theme' ); From b55f10e24ecbb1f3ceeb687aac9ba8ec126b6822 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Mon, 18 Sep 2023 15:46:38 +1000 Subject: [PATCH 07/10] Update tests/phpunit/tests/blocks/editor.php Update doc. Co-authored-by: Mukesh Panchal --- tests/phpunit/tests/blocks/editor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index 16415d65c8211..193f44cb4fe35 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -446,7 +446,7 @@ public function test_wp_get_post_content_block_attributes() { 'type' => 'constrained', ), ); - // With no block theme, expect an empty array. + // With no block theme, expect null. $this->assertNull( wp_get_post_content_block_attributes() ); switch_theme( 'block-theme' ); From 9feab8e1139be673cf13b9daeb2efbf61292b41f Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Mon, 18 Sep 2023 15:48:30 +1000 Subject: [PATCH 08/10] Improve comment --- src/wp-includes/block-editor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index ce5aed0ef992e..dba1722dba35a 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -409,7 +409,7 @@ 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 by default and return empty array when Post Content block exists. but has no attributes. + * @since 6.4.0 return null by default or an empty array when the Post Content block exists but has no attributes. * @access private * * @global int $post_ID From c1c2f36d68f604eabb675610de764c987154c751 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Tue, 19 Sep 2023 12:04:58 +1000 Subject: [PATCH 09/10] Update src/wp-includes/block-editor.php SImplify doc Co-authored-by: Felix Arntz --- src/wp-includes/block-editor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index dba1722dba35a..ba92c0ec3f2b7 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -409,7 +409,7 @@ 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 by default or an empty array when the Post Content block exists but has no attributes. + * @since 6.4.0 Return null if there is no post content block. * @access private * * @global int $post_ID From 0e75e53275a9d16fa205d42ed81cfc276634a210 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Tue, 19 Sep 2023 12:17:25 +1000 Subject: [PATCH 10/10] Check post content attributes are not output with classic themes --- tests/phpunit/tests/blocks/editor.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index 193f44cb4fe35..04ccb73b90b51 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -533,6 +533,19 @@ public function test_get_block_editor_settings_theme_json_settings() { 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