Fix font 404s and global-styles 403 in pattern creator editor#742
Merged
Fix font 404s and global-styles 403 in pattern creator editor#742
Conversation
Two issues prevented styles from loading correctly in the editor canvas: 1. External editor styles (added via add_editor_style() with an https:// URL) are fetched by get_block_editor_theme_styles() without a baseURL. Gutenberg's transformStyles rewrites url() references using baseURL but ignores @import at-rules, so relative paths like @import "./NotoSerif/NotoSerifJP/style.css" from global-fonts/style.css resolved against the iframe page URL instead of the CSS file location, producing 404s. Fix: block_editor_settings_all filter that infers the base URL from absolute url() references already in the CSS and rewrites the relative @import paths to absolute. 2. The block editor fetches GET /wp/v2/global-styles/{id} to load theme customisations. wp_global_styles is a non-public post type authored by an admin, so the read_post capability check fails for regular users, returning 403 (surfaced as a CORS error in some browsers). Global styles are CSS configuration already served publicly via the site's front-end <style> tag, so allowing authenticated users to read them via REST is safe. Fix: map_meta_cap filter that returns ['read'] for read_post checks on wp_global_styles posts during REST requests. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Member
Author
|
cc @thilinah for review |
There was a problem hiding this comment.
Pull request overview
This PR addresses missing styles in the pattern creator editor canvas by (1) fixing relative @import resolution for remote editor styles lacking baseURL, and (2) allowing authenticated users to fetch wp_global_styles via the REST API to prevent 403/CORS errors.
Changes:
- Adds a
block_editor_settings_allfilter to rewrite relative@import "./..."paths to absolute URLs (and setsbaseURL) when editor styles are remote-fetched without abaseURL. - Adds a
map_meta_capfilter to allow logged-in users toread_postforwp_global_stylesduring REST requests.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…requests Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes two issues that prevented styles from loading correctly in the pattern creator editor canvas, reported in #741.
Font 404s (NotoSerif)
WordPress core's
get_block_editor_theme_styles()fetches external editor styles viawp_remote_get()but omitsbaseURLfrom the returned object (local files receive one; remote URLs do not). Gutenberg'stransformStylesrewritesurl()property values usingbaseURLbut silently ignores@importat-rules, so relative paths like@import "./NotoSerif/NotoSerifJP/style.css"fromglobal-fonts/style.cssresolve against the iframe page URL (/new-pattern/) instead of the CSS file location, producing 404s.Fix: a
block_editor_settings_allfilter that detects styles lackingbaseURLwith relative@importpaths, infers the correct base URL from the absoluteurl()font references already present in the same CSS (those point to sibling directories — removing the last two path segments gives the shared parent), then rewrites the@importpaths to absolute.global-styles 403
The block editor fetches
GET /wp/v2/global-styles/{id}to load the active theme's customisations.wp_global_stylesis a non-public post type authored by an admin, so WordPress'sread_postcapability check fails for regular users, returning 403. Browsers surface this as a CORS error in the console.Global styles are CSS configuration already compiled and served publicly via the site's front-end
<style>tag, so allowing authenticated users to read them through the REST API is safe.Fix: a
map_meta_capfilter that interceptsread_postchecks onwp_global_stylesposts during REST requests and returns['read']— a capability all logged-in users have.Fixes #741
Props @supernovia
Screenshots
Before: 3 console errors (404s + 403), NotoSerif fonts absent from editor canvas.
After: 0 console errors, fonts load correctly.
How to test the changes in this Pull Request:
NotoSerif— before this fix you see 3 × 404 requests to/patterns/new-pattern/NotoSerif/.... After this fix those requests are absent.global-styles— before this fixGET /wp-json/wp/v2/global-styles/{id}?context=viewreturns 403. After this fix it returns 200.