-
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
[REST] Restore the missing double slash in the ID received by /templates #36881
Conversation
Will this work for subdirectory themes like |
/templates
/templates
The double slash is there for themes inside subfolders, the theme names for this kind of themes contain a "/" so that's why we needed the double slash. So you need to test with these themes as well. |
Good points @youknowriad and @TimothyBJacobs. I think there are two options here:
What do you think? |
Does each check require a database query? |
And to clarify, the double slash is between the theme slug and the template slug. So you'd only ever have one double slash, and subdirectory themes aren't allowed at an arbitrary depth, only one level. |
It does @TimothyBJacobs, with the REQUEST_URI way there would be less database queries. |
Yeah I think we could look at |
… all slashes with a double slash.
@adamziel, what if we try to normalize ID before making a DB call? Something like: https://3v4l.org/JU4LI $bad_id = 'twentytwentytwo/index';
$good_id = 'twentytwentytwo//index';
$theme = 'twentytwentytwo';
var_dump( preg_replace( "|$theme/+|", "$theme//", $bad_id ) );
var_dump( preg_replace( "|$theme/+|", "$theme//", $good_id ) ); I think P.S. Sorry about the bad regex. |
@TimothyBJacobs I think the culprit is what @noisysocks mentioned in the original ticket:
We would have to infer that information from REQUEST_URI. I fear there will be a lot of tricky corner cases, but it's worth a shot.
@Mamaduka I'd love that, but I don't think we can just assume that |
We're going to need some additional checks here that the |
I went for a different, simpler approach. Given that template ID is generated like Actually we could even sanitize it so that the last slash is always a double one – it is not optional to have it there. |
I think this is in a pretty good place. The only thing that is missing is a test with template name like |
I went for a unit test instead. I think this one is ready for feedback now. cc @mcsf @spacedmonkey @TimothyBJacobs @Mamaduka @draganescu @getdave |
Oh, that changes everything. I thought part of the difficulty stemmed from there being no tighter rules in how users can name slugs, particularly around possible slashes. |
New approach renders my feedback no longer applicable
Let's confirm that with @youknowriad – from my explorations it seems like the template |
That's correct. We just combine |
I think this ended up as a cleaner fix compared to previous explorations. We first default to what just works and only branch to a special case if we need to. Also the @TimothyBJacobs do you have any further comments? Also I see the testing instructions is for seeing the WSOD is fixed, but are there any other cases in themes in subfolders than could be affected? |
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.
LGTM with the caveat that I don't know if anything else could be broken by autodoubling we do here, other than fixing our problem. However the resulting changeset is quite lean so if it does break things it's easy to track.
I don't think E2E covers having a theme in a subdirectory, it would be nice to add that (as I mentioned above). Otherwise, I think all the bases are covered. Plus reverting this one should be easy as you said. |
…tes (#36881) * Fall back to a double-slashed template name if a single-slashed one is missing * Add a unit test * Use $_SERVER['REQUEST_URI'] as a fallback instead of simply replacing all slashes with a double slash. * Fallback to parsing the query string instead of doing it by default * Code style * Update docstring * Try doubling the last slash instead of parsing REQUEST_URI * Use sanitize_callback * Lint * Test for both single and double slash * Add unit tests for _sanitize_template_id * Update phpunit/class-gutenberg-rest-template-controller-test.php * Update phpunit/class-gutenberg-rest-template-controller-test.php
Description
Solves https://core.trac.wordpress.org/ticket/54507
When the permalink structure is set to
/index.php/%year%/%monthnum%/%day%/%postname%/
, requests to/wp/v2/templates/twentytwentytwo//home
fail, breaking the full site editor. The problem is double slash in the request path. WordPress resolves$request['id']
astwentytwentytwo/home
instead oftwentytwentytwo//home
. The endpoint has already been shipped in WordPress 5.8, so changing the URLs structure isn't really an option. This PR proposes a way of processing the received ID so the slashes are always correct regardless of how they came in.Test plan
/index.php/%year%/%monthnum%/%day%/%postname%/
into the text field.If this PR gets merged, it needs to also be backported into WP Beta.