mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Closed
ramonjd
wants to merge
13
commits into
WordPress:trunk
from
ramonjd:add/background-images-relative-paths-theme-json
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fed68b1
First commit - sync global styles controller changes from https://git…
ramonjd 4aecb9e
Adding changes to revisions controller and wp_get_global_stylesheet
ramonjd 8a0c364
Starting on unit tests
ramonjd f3e6754
Resolving theme file URIs in the global styles stylesheet
ramonjd cc77f01
El LINTO DEL DIABLO
ramonjd edbc18d
Add resolver tests.
ramonjd 2e0f58a
Adding the filters to the tests
ramonjd f507e66
_Gutenberg removed :)
ramonjd 4f2b71c
Remove image, which is unnecessary for the tests
ramonjd 2c22921
Update src/wp-includes/class-wp-theme-json-resolver.php
ramonjd ce97301
Apply suggestions from code review
ramonjd ffc1305
Removing unrelated since annotation
ramonjd e91746d
Set default var value of $theme_json to null to indicate that it need…
ramonjd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -744,4 +744,82 @@ public static function get_style_variations() { | |
} | ||
return $variations; | ||
} | ||
|
||
/** | ||
* Resolves relative paths in theme.json styles to theme absolute paths | ||
* and returns them in an array that can be embedded | ||
* as the value of `_link` object in REST API responses. | ||
* | ||
* @since 6.6.0 | ||
* | ||
* @param WP_Theme_JSON $theme_json A theme json instance. | ||
* @return array An array of resolved paths. | ||
*/ | ||
public static function get_resolved_theme_uris( $theme_json ) { | ||
$resolved_theme_uris = array(); | ||
|
||
if ( ! $theme_json instanceof WP_Theme_JSON ) { | ||
return $resolved_theme_uris; | ||
} | ||
|
||
$theme_json_data = $theme_json->get_raw_data(); | ||
|
||
// Top level styles. | ||
$background_image_url = isset( $theme_json_data['styles']['background']['backgroundImage']['url'] ) ? $theme_json_data['styles']['background']['backgroundImage']['url'] : null; | ||
|
||
/* | ||
* The same file convention when registering web fonts. | ||
* See: WP_Font_Face_Resolver:: to_theme_file_uri. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove space. |
||
*/ | ||
$placeholder = 'file:./'; | ||
if ( | ||
isset( $background_image_url ) && | ||
is_string( $background_image_url ) && | ||
// Skip if the src doesn't start with the placeholder, as there's nothing to replace. | ||
str_starts_with( $background_image_url, $placeholder ) | ||
) { | ||
$file_type = wp_check_filetype( $background_image_url ); | ||
$src_url = str_replace( $placeholder, '', $background_image_url ); | ||
$resolved_theme_uri = array( | ||
'name' => $background_image_url, | ||
'href' => sanitize_url( get_theme_file_uri( $src_url ) ), | ||
'target' => 'styles.background.backgroundImage.url', | ||
); | ||
if ( isset( $file_type['type'] ) ) { | ||
$resolved_theme_uri['type'] = $file_type['type']; | ||
} | ||
$resolved_theme_uris[] = $resolved_theme_uri; | ||
} | ||
|
||
return $resolved_theme_uris; | ||
} | ||
|
||
/** | ||
* Resolves relative paths in theme.json styles to theme absolute paths | ||
* and merges them with incoming theme JSON. | ||
* | ||
* @since 6.6.0 | ||
* | ||
* @param WP_Theme_JSON $theme_json A theme json instance. | ||
* @return WP_Theme_JSON Theme merged with resolved paths, if any found. | ||
*/ | ||
public static function resolve_theme_file_uris( $theme_json ) { | ||
$resolved_urls = static::get_resolved_theme_uris( $theme_json ); | ||
if ( empty( $resolved_urls ) ) { | ||
return $theme_json; | ||
} | ||
|
||
$resolved_theme_json_data = array( | ||
'version' => WP_Theme_JSON::LATEST_SCHEMA, | ||
); | ||
|
||
foreach ( $resolved_urls as $resolved_url ) { | ||
$path = explode( '.', $resolved_url['target'] ); | ||
_wp_array_set( $resolved_theme_json_data, $path, $resolved_url['href'] ); | ||
} | ||
|
||
$theme_json->merge( new WP_Theme_JSON( $resolved_theme_json_data ) ); | ||
|
||
return $theme_json; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Remove space before variable