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 image: add support for relative theme path URLs in top-level theme.json styles #6608

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented May 22, 2024

What

This PR syncs the PHP changes from:

Testing

Important

Until the latest Gutenberg packages are synced, the editor won't parse the relative paths correctly.

I'm using twentytwentyfour to test.

In the theme.json for that theme, add the following to the "styles" object:

		"background": {
			"backgroundImage": {
				"url": "file:./assets/images/windows.webp"
			},
			"backgroundSize": "contain"
		},

Next, add a background image to a style variations, for example src/wp-content/themes/twentytwentyfour/styles/ember.json

		"background": {
			"backgroundImage": {
				"url": "file:./assets/images/abstract-geometric-art.webp"
			},
			"backgroundSize": "cover"
		},

Check the frontend to ensure the correct image is displayed in the background:

Screenshot 2024-05-29 at 2 48 43 pm

Now head over to the site editor and enable the ember variation under Styles > Browse styles. Save and check the frontend.

Ensure the correct image displays (the one from the variation ember.json above).

Screenshot 2024-05-30 at 4 34 42 pm

You can check that _links is added correctly to the REST response through the core data selectors in the browser's console.

To do that, head back to the site editor and open the browser console.

// Get user global styles
await wp.data.resolveSelect( 'core' ).getEntityRecord( 'root', 'globalStyles', await wp.data.resolveSelect( 'core' ).__experimentalGetCurrentGlobalStylesId() )

Check that user styles has a _links entry with theme-uris. It should if you saved the switch to the ember variation.

// Get global styles items (variations)
await wp.data.resolveSelect( 'core' ).__experimentalGetCurrentThemeGlobalStylesVariations()

Check that the Ember item has a _links entry with theme-uris.

// Get all revisions.
await wp.data.resolveSelect( 'core' ).getRevisions( 'root', 'globalStyles', await wp.data.resolveSelect( 'core' ).__experimentalGetCurrentGlobalStylesId() ) 

Check that the latest revision (the one where you saved the Ember variation) item has a _links entry with theme-uris.

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


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

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.

@noisysocks
Copy link
Member

noisysocks commented May 28, 2024

I created a local branch containing this PR and #6482 and, with the Gutenberg plugin disabled, I tested creating a new block theme that uses a local file:./ image. The relative URL was not being turned into an absolute URL in the site frontend.

I think you're missing this change:

https://github.com/WordPress/gutenberg/pull/61271/files#diff-b49a48d455acd888a968ee6facf9fb00d9c3de6f91d2818bbe9ddaa4b812dd55

@ramonjd
Copy link
Member Author

ramonjd commented May 28, 2024

I created a local branch containing this PR and #6482 and, with the Gutenberg plugin disabled, I tested creating a new block theme that uses a local file:./ image. The relative URL was not being turned into an absolute URL in the site frontend.

Ah, nice spotting. Thanks! 🙇🏻

I'll update and push quickly.

TBH I'd stopped working on this in anticipation of #6482 getting in first.

@ramonjd ramonjd force-pushed the add/background-images-relative-paths-theme-json branch from 80c5082 to 4c974ce Compare May 28, 2024 04:34
@noisysocks
Copy link
Member

noisysocks commented May 28, 2024

You asked me to test these two PRs so, ready or not, I'm testing these two PRs 😛

@ramonjd
Copy link
Member Author

ramonjd commented May 28, 2024

You asked me to test these two PRs so, ready or not, I'm testing these two PRs

🍺 You got me. 😄

@ramonjd ramonjd force-pushed the add/background-images-relative-paths-theme-json branch from 5c94e13 to 6189bff Compare May 28, 2024 08:17
@ramonjd ramonjd force-pushed the add/background-images-relative-paths-theme-json branch from 6189bff to edbc18d Compare May 29, 2024 02:29
@ramonjd ramonjd marked this pull request as ready for review May 29, 2024 05:12
Copy link

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, noisysocks.

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

Copy link
Member

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

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

Code looks correctly ported to me—just a few small things to fix.

I tested this by creating a new empty theme and using a relative URL in background.backgroundImage.url and all was well.

ramonjd and others added 4 commits May 30, 2024 15:08
Co-authored-by: Robert Anderson <robert@noisysocks.com>
Co-authored-by: Robert Anderson <robert@noisysocks.com>
Simplifying links array building in revisions controller
@noisysocks
Copy link
Member

Looking good. Would you mind adding step-by-step instructions for how to test the changes to class-wp-rest-global-styles-controller.php and class-wp-rest-global-styles-revisions-controller.php? I couldn't quite figure it out from hitting API endpoints locally.

@noisysocks
Copy link
Member

Thanks! LGTM 👍

@noisysocks
Copy link
Member

Committed in r58262.

@noisysocks noisysocks closed this May 31, 2024
*
* @since 6.6.0
*
* @param WP_Theme_JSON $theme_json A theme json instance.
Copy link
Member

Choose a reason for hiding this comment

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

Remove space before variable


/*
* The same file convention when registering web fonts.
* See: WP_Font_Face_Resolver:: to_theme_file_uri.
Copy link
Member

Choose a reason for hiding this comment

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

Remove space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants