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

Fix layout when post content is root block #54485

Merged
merged 3 commits into from
Sep 15, 2023

Conversation

tellthemachines
Copy link
Contributor

What?

In #54371 we reintroduced content size in the post editor when the Post Content block has a default layout type and is nested inside other blocks. However, if Post Content is at root level and has default layout, we should assume that it is meant to stretch full width and let that happen.

The problem is we're using the Post Content attributes to check whether the template being used is a block template; if layout is set to default there are no attributes (because it's using the default layout type) and we don't add anything to the editor settings. This results in the fallback layout (with content width) being used instead of the default.

This PR proposes changing the check that adds the Post Content attributes to the editor settings so that it still outputs an empty array if the block exists but the attributes are empty.

An alternative to this would be using the __unstableIsBlockBasedTheme editor setting to check whether the theme is a block theme, but that wouldn't work for hybrid themes that might have one or two templates with Post Content blocks.

Testing Instructions

  1. Create a post template with a Post Content block at root level, and make sure the "Inner blocks use content width" toggle is off;
  2. Check that posts using that template have no max content width in the post editor;
  3. Check that Post Content blocks with "Inner blocks use content width" toggled on, and with it toggled off but with the block nested inside other blocks, still correctly display a max content width in the post editor.

Testing Instructions for Keyboard

Screenshots or screencast

@tellthemachines tellthemachines added [Type] Bug An existing feature does not function as intended [Package] Edit Post /packages/edit-post [Feature] Layout Layout block support, its UI controls, and style output. labels Sep 15, 2023
@tellthemachines tellthemachines self-assigned this Sep 15, 2023
@tellthemachines tellthemachines added the Needs PHP backport Needs PHP backport to Core label Sep 15, 2023
@github-actions
Copy link

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/compat/wordpress-6.3/block-editor-settings.php

@github-actions
Copy link

Flaky tests detected in 089d6d7.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6193239818
📝 Reported issues:

@@ -78,7 +78,7 @@ function gutenberg_get_block_editor_settings_experimental( $settings ) {
$template_blocks = parse_blocks( $current_template[0]->content );
$post_content_block = gutenberg_find_first_block( 'core/post-content', $template_blocks );

if ( ! empty( $post_content_block['attrs'] ) ) {
if ( is_array( $post_content_block['attrs'] ) || is_object( $post_content_block['attrs'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under which condition is post_content_block['attrs'] an object?

This PR proposes changing the check that adds the Post Content attributes to the editor settings so that it still outputs an empty array if the block exists but the attributes are empty.

If we always want an empty array regardless, would this work instead (removing the if statement)?

$settings['postContentAttributes'] = ! empty( $post_content_block['attrs'] ) ? $post_content_block['attrs'] : array();

I think empty() will return false for objects, even "empty" ones but it wouldn't matter since we'd be returning it or an empty array, which I think is what this PR does anyway.

$empty_object = new stdClass();
var_dump(empty($empty_object)); // false!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under which condition is post_content_block['attrs'] an object?

If it has attributes in it, though is_array seems to work either way, so perhaps we don't need the second check.

If we always want an empty array regardless

We only want an empty array if the Post Content block exists but doesn't have attributes. For classic themes we still want this to be undefined. The bug I'm trying to fix is with Post Content blocks with default layout, which don't have any explicit attributes; in that case we need to know whether the block exists or not (presumably if it doesn't exist, we're in a classic theme).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If switching to an is_array() check, we might need an isset() to go before it, too, to prevent an Undefined index warning in case $post_content_block is empty? Just testing this out locally in PHP:

image

If it has attributes in it, though is_array seems to work either way, so perhaps we don't need the second check.

If it has attributes in it, then it looks like is_array should still work, because it'll return true if it's an associative array, so I don't think we'd need the is_object check 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want an empty array if the Post Content block exists but doesn't have attributes. For classic themes we still want this to be undefined.

Ah, got it, thanks!

If it has attributes in it, though is_array seems to work either way, so perhaps we don't need the second check.

I think that'd be safe. In everything under block-supports for example, we're always accessing $block['attrs'] as though it's an array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If switching to an is_array() check, we might need an isset() to go before it

Actually we could just use isset! That also tells us that the Post Content block exists.

Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant for 6.3 or 6.4? Assuming 6.3 since it's in the compat/6.3 folder. Just checking!

In 2023 theme, I created a single post template with just a Post Content block in it.

Before - content width

Screenshot 2023-09-15 at 1 37 56 pm

After - no content width (full width)

Screenshot 2023-09-15 at 1 37 32 pm

After (with "Inner blocks use content width" turned on in the post content block)

Screenshot 2023-09-15 at 1 48 41 pm

Inserting the Post Content block (with "Inner blocks use content width" turned off) inside a Group block had the same effect as the last screen shot.

So it LGTM, I just had a question about whether we need to do any type checking at all.

@tellthemachines
Copy link
Contributor Author

Is this meant for 6.3 or 6.4? Assuming 6.3 since it's in the compat/6.3 folder. Just checking!

Oh, that's a good point. It's meant for 6.4 so I should probably move this into the 6.4 folder.

@tellthemachines
Copy link
Contributor Author

Actually thinking better of this, we probably don't need it at all because this wasn't an issue before #54371. If I make this fix directly in core, things should work as expected for all versions.

@andrewserong
Copy link
Contributor

Actually thinking better of this, we probably don't need it at all because this wasn't an issue before #54371.

Wouldn't we still need it for folks running WP <= 6.3 and the Gutenberg plugin?

@tellthemachines
Copy link
Contributor Author

Wouldn't we still need it for folks running WP <= 6.3 and the Gutenberg plugin?

Good point! So I guess this can stay as it is then.

Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com>
Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix, and great catch!

In the other PR, I'd only tested when adjusting an existing post content block that already had a layout type set, so toggling the Inner blocks use content width set an explicit type: 'default' for the layout attributes of the block, which meant it displayed properly for me in the editor, so I never noticed the issue!

To reproduce for this one, I created a new post content block in a template without touching the controls, and that revealed the issue 👍

Testing nicely, in a template where Post Content is at the root:

Before After
image image

LGTM! ✨

@tellthemachines
Copy link
Contributor Author

Thanks for the reviews folks! I'll put up the core patch soon.

@tellthemachines tellthemachines merged commit 72b0a82 into trunk Sep 15, 2023
49 checks passed
@tellthemachines tellthemachines deleted the fix/post-content-root-layout branch September 15, 2023 05:35
@github-actions github-actions bot added this to the Gutenberg 16.7 milestone Sep 15, 2023
@SiobhyB SiobhyB removed the Needs PHP backport Needs PHP backport to Core label Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Layout Layout block support, its UI controls, and style output. [Package] Edit Post /packages/edit-post [Type] Bug An existing feature does not function as intended
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants