-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Theme export: convert uploaded URLs to relative path assets #66407
base: trunk
Are you sure you want to change the base?
Theme export: convert uploaded URLs to relative path assets #66407
Conversation
Flaky tests detected in 8646cce. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11512116126
|
lib/global-styles-and-settings.php
Outdated
@@ -30,6 +30,8 @@ function gutenberg_get_global_stylesheet( $types = array() ) { | |||
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); | |||
$tree = WP_Theme_JSON_Resolver_Gutenberg::resolve_theme_file_uris( $tree ); | |||
|
|||
var_dump( WP_Theme_JSON_Resolver_Gutenberg::get_migrated_relative_theme_uris( $tree ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to test the output
*/ | ||
public static function get_resolved_theme_uris( $theme_json ) { | ||
$resolved_theme_uris = array(); | ||
private static function process_theme_uris( $theme_json, $options = array() ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything here and below is a major refactor. Just testing things out for now.
Basically so we have a single source of truth about where these types of URIs live.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a drive-by comment to say I love this idea, feels like a quality of life improvement that'll make exporting feel that much smoother 👍
In terms of the directory where we save the file, at first I saw "uploads" as a directory and wondered if "assets" would be better. Now that I see it's been updated to "assets", I'm wondering if that could conflict with how a theme has structured its other... assets?
Naming and defaults are tricky to figure out, but I was wondering what the trade-offs might be between storing the uploaded files somewhere explicit so that they don't conflict (or aren't mixed up with) files that live with the theme (uploads
) OR attempting to place them somewhere that makes sense as a permanent home for the files (assets
) 🤔
I was half wondering if we might need an export modal at some point for configuration of the output zip — with this PR there'd only be a single field, though, so maybe not very useful just yet 😄
Anyway, mostly just wanted to say thanks for working on this!
Thank you @andrewserong! I'm a bit obsessed with it now - I want to get it working 🤣 With fonts working, it's a pretty good quality of life upgrade!
I'm dumping the files under assets in the zip, but I guess there could be a slight risk of overwriting existing files where assets already exists. Then again, there's that risk with any name we choose. Maybe
Good idea for down the road! |
I like "assets", I think it's good to set a convention there. I don't understand much how we'll be overwriting anything though? We're exporting a new theme right and we control all its content no? |
Might have been paranoia. Just in case an uploaded file has the same name as a theme file already under |
Thank you for working on this.
I am not sure I understood the question correctly. |
I see I didn't know about this. Personally, I'm not entirely sure this is a good idea. I'd rather "compute" the full theme. |
Thanks for the feedback!
For fonts, this PR does something very similar to "styles": {
"background": {
"backgroundImage": {
"id": 7,
"source": "file",
"title": "default_man_sitting_alone_eating_lunch_thinking_about_WordPressXRhXpu",
"url": "http://localhost:8888/wp-content/uploads/2024/10/default_man_sitting_alone_eating_lunch_thinking_about_WordPressXRhXpu.png"
}
}, The goal of this PR is for the exported theme.json to look more like this: "styles": {
"background": {
"backgroundImage": {
"url": "file:./assets/default_man_sitting_alone_eating_lunch_thinking_about_WordPressXRhXpu.png"
}
}, The image file is fetched and dumped in
What would that look like do you think? In this PR, I don't think it's much of a stretch to check for duplicate names in the destination directory via |
Ensure to strip background image properties before setting relative path
$href = array( | ||
'url' => $href, | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can test this in the Core patch since Core tests have access to ZipArchive.
The issue for this PR is: #41798 |
🚧
Warning
Experimental code - please don't treat it as ready yet. Thank you!
What? How?
This is a first pass, so it's pretty rough and tumble. No tests or defensive coding yet.
This PR checks a theme tree for URIs containing the current site's upload path. If found:
'http://domain/wp-content/uploads/1/2/test.gif' >' file:./uploads/test.gif'
/uploads
The outcome is that the uploaded files become part of the new theme's asset library, and can therefore be installed anywhere.
Why?
A theme can use uploaded files for:
The paths to these assets are absolute URIs to the uploaded files.
Currently, when exporting themes these URIs are preserved in the theme.json, which means that the theme isn't "portable" in that relies on those hosted assets.
Testing Instructions
file:./
paths/uploads
in the zip.Exported theme.json [BEFORE]
Exported theme.json [AFTER]
Screenshots or screencast