Skip to content

Fix font 404s and global-styles 403 in pattern creator editor#742

Merged
bor0 merged 6 commits intotrunkfrom
fix/741-cors-errors-font-404s
Apr 16, 2026
Merged

Fix font 404s and global-styles 403 in pattern creator editor#742
bor0 merged 6 commits intotrunkfrom
fix/741-cors-errors-font-404s

Conversation

@bor0
Copy link
Copy Markdown
Member

@bor0 bor0 commented Apr 14, 2026

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 via wp_remote_get() but omits baseURL from the returned object (local files receive one; remote URLs do not). Gutenberg's transformStyles rewrites url() property values using baseURL but silently ignores @import at-rules, so relative paths like @import "./NotoSerif/NotoSerifJP/style.css" from global-fonts/style.css resolve against the iframe page URL (/new-pattern/) instead of the CSS file location, producing 404s.

Fix: a block_editor_settings_all filter that detects styles lacking baseURL with relative @import paths, infers the correct base URL from the absolute url() 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 @import paths to absolute.

global-styles 403

The block editor fetches GET /wp/v2/global-styles/{id} to load the active theme's customisations. wp_global_styles is a non-public post type authored by an admin, so WordPress's read_post capability 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_cap filter that intercepts read_post checks on wp_global_styles posts 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:

  1. Log in to wordpress.org/patterns as a regular (non-admin) user.
  2. Open the pattern creator at https://wordpress.org/patterns/new-pattern/.
  3. Open DevTools → Network, filter by NotoSerif — before this fix you see 3 × 404 requests to /patterns/new-pattern/NotoSerif/.... After this fix those requests are absent.
  4. In Network, filter by global-styles — before this fix GET /wp-json/wp/v2/global-styles/{id}?context=view returns 403. After this fix it returns 200.
  5. Open DevTools → Console — confirm zero errors (previously 3–4, including CORS-related ones).
  6. Insert a paragraph block and set its font family to a Noto Serif variant — verify the font renders correctly in the editor canvas.
  7. Confirm the editor loads and functions normally with no JS errors or regressions.

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>
@bor0 bor0 self-assigned this Apr 14, 2026
@bor0 bor0 requested a review from Copilot April 14, 2026 13:33
@bor0
Copy link
Copy Markdown
Member Author

bor0 commented Apr 14, 2026

cc @thilinah for review

@bor0 bor0 marked this pull request as ready for review April 14, 2026 13:35
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_all filter to rewrite relative @import "./..." paths to absolute URLs (and sets baseURL) when editor styles are remote-fetched without a baseURL.
  • Adds a map_meta_cap filter to allow logged-in users to read_post for wp_global_styles during REST requests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread public_html/wp-content/plugins/pattern-creator/pattern-creator.php
Comment thread public_html/wp-content/plugins/pattern-creator/pattern-creator.php
Comment thread public_html/wp-content/plugins/pattern-creator/pattern-creator.php Outdated
bor0 and others added 5 commits April 14, 2026 15:47
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>
@bor0 bor0 requested a review from thilinah April 15, 2026 13:28
@bor0 bor0 merged commit 416e65f into trunk Apr 16, 2026
3 checks passed
@bor0 bor0 deleted the fix/741-cors-errors-font-404s branch April 16, 2026 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CORS errors prevent some styles from loading in editor

2 participants