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

Fix blank site editor when theme name contains a period #37167

Merged
merged 7 commits into from
Dec 16, 2021

Conversation

mkaz
Copy link
Member

@mkaz mkaz commented Dec 6, 2021

Description

The rest routes do not include a period in the regex so if the theme name includes a period the API requests will fail.

There is still an issue when the route matches and the callback get_item() is passed the theme name as the id. The get_item() method attempts look up global styles using this id but as a string it fails.

@oandregal How should that look up work? Checking for numeric seems to work but feels like it is skipping an important part. I think I figured out the lookup, it got oversimplied but I think the regex might of been from template routes, and didn't need to be as complicated.

I've confirmed via test instructions below and this now seems to work with no errors.

Fixes #37156

How has this been tested?

  1. Rename a block theme to include a period, for example twentytwentytwo -> twentytwentytwo-1.0
  2. Activate block theme with period in the name.
  3. Load site editor confirm error in console.log (404 route not found)
  4. Apply patch.
  5. Confirm site editor loads and global style changes are saved.

@mkaz mkaz added the Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json label Dec 6, 2021
@mkaz mkaz requested a review from oandregal December 6, 2021 20:55
@noisysocks noisysocks added this to 👀 Needs review in WordPress 5.9 Must-Haves via automation Dec 7, 2021
@noisysocks noisysocks added the [Type] Bug An existing feature does not function as intended label Dec 7, 2021
@youknowriad
Copy link
Contributor

Will this change break the endpoint for themes inside subfolders? Did we test that use-case?

@youknowriad youknowriad added Core REST API Task Task for Core REST API efforts Backport to WP 6.6 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Dec 7, 2021
@mkaz
Copy link
Member Author

mkaz commented Dec 7, 2021

Thanks @youknowriad that explains the regex complexity, I couldn't figure out why.
It almost works 😁

With themes in a sub-directory, the template-parts API doesn't seem to load properly but the other APIs do. I'll troubleshoot some more.

@mkaz
Copy link
Member Author

mkaz commented Dec 7, 2021

ok, I added the \/ to the regex to support subdirs and it seems to be working for themes setup in the subdir.

It considers the theme name: subdir/theme so doesn't work for template parts in the theme.
So I think we want to discard the first subdir for it to load properly.

@mkaz
Copy link
Member Author

mkaz commented Dec 7, 2021

Should the theme slug include the subdir? There appears to be a conflict.

When looking at the template list it includes the subdir in the slug?
For example: http:https://localhost:8001/wp-admin/themes.php?page=gutenberg-edit-site&postType=wp_template

However, when including from template parts the subdir is not included - since a theme author likely did not create in the same subdirectory.

@mkaz
Copy link
Member Author

mkaz commented Dec 7, 2021

There are minor differences between how Core and Gutenberg register API routes, I created a core ticket with a patch to trunk that seems to fix this problem: https://core.trac.wordpress.org/ticket/54596

The same change applied to the Gutenberg plugin still has some minor issues. My environment is a bit of a mess between multiple core versions, themes, modified templates, and other discrepancies. I'll circle back to this one likely tomorrow and test with a fresh setup.

@mkaz
Copy link
Member Author

mkaz commented Dec 8, 2021

I've updated this PR to match the core trac ticket and now testing properly against 5.9 source, there are more errors when testing against WP 5.8.2, so we'll need more testing around backwards compatibility if the new plugin version is supposed to work with older WP installs.

Right now it is working functionally as expected. There is a 404 error on the API call when browsing to the listing of templates or template parts page: /wp-admin/themes.php?page=gutenberg-edit-site&postType=wp_template

However, clicking into the templates (or template parts) updating and saving works. It appears the issue on that page is the shortening of the theme name from the directory to the actual name itself, ie. from twentytwentytwo-1.0 -> "Twenty Twenty Two"

@youknowriad
Copy link
Contributor

I'm adding @adamziel as a reviewer here as he worked on similar issues in the endpoint recently.

@mkaz
Copy link
Member Author

mkaz commented Dec 13, 2021

This also needs to take into account additional characters.
See: WordPress/wordpress-develop#2034

@Mamaduka
Copy link
Member

Hi, @mkaz

It looks like the fix was committed in core - https://core.trac.wordpress.org/changeset/52376.

@youknowriad
Copy link
Contributor

Can we bring the changes to Gutenberg as well (as we need to support older WP versions)

@adamziel
Copy link
Contributor

Maybe we don't even need a separate PR, updating this one should do

@mkaz
Copy link
Member Author

mkaz commented Dec 15, 2021

👍 I migrated the changes made in the trac ticket 52376 and everything is now testing well. Please take a look and confirm. Same testing instructions as above but you can also try for additional special characters such as ( ) _ [ ] see trac ticket for additional details

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

There's a failing unit test but otherwise looks good.

@mkaz
Copy link
Member Author

mkaz commented Dec 15, 2021

There's a failing unit test but otherwise looks good.

My fault, bad grepping. I saw the test in the trac ticket but searched in the wrong directory for it. There are some tests in core that aren't in the plugin.

I updated the failing test with changes made to core. 👍

The core ticket added additional fixtures and tests that are not in the
plugin. So those are not being copied over. Just updating the existing
test and adding one for /themes endpoint.
@mkaz mkaz merged commit 0407251 into trunk Dec 16, 2021
WordPress 5.9 Must-Haves automation moved this from 👀 Needs review to ✅ Done Dec 16, 2021
@mkaz mkaz deleted the fix/37156-theme-period branch December 16, 2021 15:21
@github-actions github-actions bot added this to the Gutenberg 12.3 milestone Dec 16, 2021
@mkaz
Copy link
Member Author

mkaz commented Dec 16, 2021

NOTE: Even though the label is there, this does not need to be backported to core, it is an update from core, so the code is already synced.

@tellthemachines tellthemachines removed the Backport to WP 6.6 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Dec 21, 2021
tellthemachines pushed a commit that referenced this pull request Dec 21, 2021
* Add \. as valid character in rest route regex

* Fix regex for stylesheet vs. id

* Add slash to regex to support subdir themes

* Include subdir in regex

* Update regex to match core trac update

* Sync route regex for theme directory characters

Sync the updates in core from Trac https://core.trac.wordpress.org/changeset/52376
that support theme directories with special characters. Confirm site
editor loads properly.

* Update global styles phpunit test from trac ticket.

The core ticket added additional fixtures and tests that are not in the
plugin. So those are not being copied over. Just updating the existing
test and adding one for /themes endpoint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core REST API Task Task for Core REST API efforts Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Site editor fails to load when theme directory contains a period
6 participants