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

Background images: resolve ref and ensure appropriate default values #7137

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Aug 5, 2024

A PR to sync the changes in:

Default values

Block background images have long had "default" values to optimize their appearance.

For example, block styles receive a default background size of "cover" in the editor and the frontend. Or, where the background size is "contain" the background position is "center".

Defaults have always applied to images uploaded by the user in the editor.

The PR brings a bit of consistency to background image styles.

In relation to default values:

  • Site wide background images (uploaded or otherwise) do not receive any default values.
  • Block background images without an "id" (indicating that they're not uploaded to the database) do not receive any default values.
  • Block background images that have been uploaded (images with ids) receive background default values.

Resolving theme.json "ref" values

Add support for ref resolution to "background" style properties.

Testing

This PR will only affect the frontend (PHP).

With the following theme.json applied, check that the "ref" values are correctly resolved.

To test defaults, you can add "id": 123 to the background.backgroundImage object for non-relative path images:

For example:

			"core/verse": {
				"background": {
					"backgroundImage": {
						"url": "http://yoursite/wp-content/themes/twentytwentyfour/assets/images/building-exterior.webp",
						"id": 123
					}
				},
			},

For this case:

  • only blocks should receive default background sizes where none exist
  • site wide images (root level) do not receive default sizes
Example theme.json (hot swap for TT4's)
{
	"$schema": "https://schemas.wp.org/wp/6.5/theme.json",
	"version": 3,
	"settings": {
		"appearanceTools": true,
		"layout": {
			"contentSize": "840px",
			"wideSize": "1100px"
		}
	},
	"styles": {
		"background": {
			"backgroundImage": {
				"url": "file:./assets/images/hotel-facade.webp"
			},
			"backgroundSize": "cover",
			"backgroundAttachment": "fixed",
			"backgroundPosition": "top left"
		},
		"blocks": {
			"core/verse": {
				"background": {
					"backgroundImage": {
						"url": "file:./assets/images/building-exterior.webp"
					}
				},
				"color": {
					"text": "white"
				},
				"dimensions": {
					"minHeight": "100px"
				}
			},
			"core/quote": {
				"background": {
					"backgroundImage": {
						"ref": "styles.background.backgroundImage"
					}
				},
				"dimensions": {
					"minHeight": "100px"
				}
			},
			"core/group": {
				"background": {
					"backgroundImage": {
						"url": "file:./assets/images/abstract-geometric-art.webp"
					},
					"backgroundAttachment": {
						"ref": "styles.blocks.core/verse.background.backgroundAttachment"
					},
					"backgroundSize": {
						"ref": "styles.blocks.core/verse.background.backgroundSize"
					},
					"backgroundPosition":  {
						"ref": "styles.background.backgroundPosition"
					}
				},
				"color": {
					"text": {
						"ref": "styles.blocks.core/verse.color.text"
					}
				},
				"dimensions": {
					"minHeight": "111px"
				}
			}
		}
	}
}
Here is some example block HTML as well
<!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p>Group</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:verse -->
<pre class="wp-block-verse">Verse</pre>
<!-- /wp:verse -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><!-- wp:paragraph -->
<p>Quote</p>
<!-- /wp:paragraph --></blockquote>
<!-- /wp:quote -->

Trac ticket: https://core.trac.wordpress.org/ticket/61858


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

github-actions bot commented Aug 5, 2024

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@ramonjd ramonjd changed the title Background images: ensure appropriate default values Background images: resolve ref and ensure appropriate default values Aug 7, 2024
@noisysocks
Copy link
Member

Could you please create a Trac ticket for this and plop it in the 6.7 milestone?

@ramonjd ramonjd force-pushed the fix/background-image-user-uploaded-defaults branch from e86b5b7 to dd12193 Compare August 12, 2024 07:34
@ramonjd ramonjd self-assigned this Aug 13, 2024
@ramonjd ramonjd marked this pull request as ready for review August 13, 2024 02:12
Copy link

github-actions bot commented Aug 13, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ramonopoly, aaronrobertshaw, andrewserong, noisysocks.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ramonjd ramonjd marked this pull request as draft August 13, 2024 07:47
@ramonjd ramonjd force-pushed the fix/background-image-user-uploaded-defaults branch from be97f10 to a4caf72 Compare August 15, 2024 04:26
@ramonjd ramonjd marked this pull request as ready for review August 15, 2024 04:27
Copy link

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

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

Thanks for putting this together @ramonjd 👍

While comparing the code changes here with their Gutenberg counterparts, it appears this backport doesn't include all the latest changes that landed in GB. For example, the change to merging background image configurations in theme.json data (WordPress/gutenberg@cb34d72).

Some of the later changes on WordPress/gutenberg#64128 are here, like the unit test update but not others.

I can help out tomorrow with bringing across any missing pieces. I'll also hold off on testing until then.

@ramonjd
Copy link
Member Author

ramonjd commented Aug 15, 2024

While comparing the code changes here with their Gutenberg counterparts, it appears this backport doesn't include all the latest changes that landed in GB.

Thanks for double checking! I'll fix this first thing this morning 🙇🏻

@ramonjd ramonjd force-pushed the fix/background-image-user-uploaded-defaults branch from 6d88660 to b32f4ae Compare August 16, 2024 00:41
@ramonjd
Copy link
Member Author

ramonjd commented Aug 16, 2024

I'd left the previous "unset" ref logic (and related test) in there. 🤦🏻

I think it should be pretty close now, but I'm a bit bug-eyed .

@ramonjd
Copy link
Member Author

ramonjd commented Aug 16, 2024

There's one inconsistency between this PR and Gutenberg trunk, where Gutenberg trunk is wrong.

I have a sync PR here:

WordPress/gutenberg#64564

This was due to a bit of flip-flopping on features and I forgot to remove it.

🙇🏻

…eir appearance.

For example, block styles receive a default background size of "cover" in the editor and the frontend. Or, where the background size is "contain" the background position is "center".

Defaults have always applied to images uploaded by the user in the editor.

The PR brings a bit of consistency to background image styles.

Block styles "inherit" values from global styles/theme.json and display the current value (whether the set, inherited or default value) in the editor controls.

In relation to default values:

- Site wide background images (uploaded or otherwise) do not receive any default values.
- Block background images defined in theme.json do not receive any default values.
- Block background images that have been uploaded (images with ids) receive background default values.
…ct, but the latter will appear in the UI controls.
…t merged. The image object represents a single unit. We want to prevent "cross contamination"
…e synced background-size: contain;background-attachment: fixed;
@ramonjd ramonjd force-pushed the fix/background-image-user-uploaded-defaults branch from 45a09be to 03bf346 Compare August 26, 2024 01:00
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.

In general, this is testing well for me on the frontend of my test site 👍

This PR will only affect the frontend (PHP).

Consistent with this, the ref based values are working for me in the post editor but not the site editor (I'm assuming we'll need the packages update before we can test there 👍).

Here's what I've encountered in testing:

✅ Code changes match the final state from the referenced PRs
✅ The ref-based resolution works as expected

To test defaults, you can add "id": 123 to the background.backgroundImage object. For this case:

❓ For some reason I'm having trouble testing this. I've added "id": 123 to the verse block's background.backgroundImage object in my local theme.json file, but on the site frontend I'm not seeing the default cover background-size rule. Instead I'm only seeing the background image rule:

image

Are you able to replicate? It's highly possible I've made a mistake in my local testing environment, so just wanted to double check 🙂

Overall, though, this backport looks to cover the relevant bits of code from the linked PRs 👍

@ramonjd
Copy link
Member Author

ramonjd commented Aug 26, 2024

For some reason I'm having trouble testing this. I've added "id": 123 to the verse block's background.backgroundImage object in my local theme.json file, but on the site frontend I'm not seeing the default cover background-size rule. Instead I'm only seeing the background image rule:

My bad @andrewserong

I forgot to specify that the image cannot be a relative image - is that what you were testing with?

The reason being, relative images won't ever have ids because they're not uploaded to the media library and therefore not saved to the database with an id.

In that way "id" is a reserved attribute for uploaded images.

Any "id"s added to a background image object with a relative path will be stripped out here: https://github.com/wordpress/wordpress-develop/blob/trunk/src/wp-includes/class-wp-theme-json-resolver.php#L945

Now, that's not intentional, it's just the practical reality 😄

The other side of the coin is that if we support more properties in background.backgroundImage, they'll have to be merged correctly for relative path images.

This should work:

			"core/verse": {
				"background": {
					"backgroundImage": {
						"url": "http://localhost:8889/wp-content/themes/twentytwentyfour/assets/images/building-exterior.webp",
						"id": 123
					}
				},
				"color": {
					"text": "white"
				},
				"dimensions": {
					"minHeight": "100px"
				}
			},

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.

This should work:

Ah, wonderful thank you for getting to the bottom of that! That all makes sense to me, I knew I must have been missing something in my local testing but couldn't for the life of me figure out what 😆

Now, that's not intentional, it's just the practical reality 😄

That all sounds reasonable — the defaults logic is really just meant to apply for users adding values via the UI, so I don't see any issues here.

The other side of the coin is that if we support more properties in background.backgroundImage, they'll have to be merged correctly for relative path images.

I can't think of any other circumstances where we'll need to add other properties for relative path images, so I imagine we might not run into any issues there 🤔

After re-testing this looks good to land to me! 🚀

@ramonjd
Copy link
Member Author

ramonjd commented Aug 26, 2024

Committed in:

@ramonjd ramonjd closed this Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants