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 subdirectory support in site editor template parts #49100

Closed
jeremyfelt opened this issue Mar 15, 2023 · 2 comments
Closed

Fix subdirectory support in site editor template parts #49100

jeremyfelt opened this issue Mar 15, 2023 · 2 comments
Labels
[Block] Template Part Affects the Template Parts Block [Type] Bug An existing feature does not function as intended

Comments

@jeremyfelt
Copy link
Member

Description

I have a series of template parts in subdirectories of my theme's parts/:

parts/
  subdir1/
    - part-name.html
  subdir2/
    - part-name.html

This works fine at first glance. Everything loads as expected in the site editor and on the front-end with the default markup that I've entered into each of those parts.

When I try to save one of these template parts from the site editor, a POST request is made to the wp/v2/template-parts/ endpoint like:

https://site.test/wp-json/wp/v2/template-parts/theme-name//parts/subdir1/part-name?_locale=user

With body JSON like:

{
  content: "...."
  id: "theme-name//parts/subdir1/part-name"
}

The response says that it saves, but no changes are reflected when I refresh the page or attempt to view the changes on the front-end.

When I look in the site's template parts interface, I see a new part added for: theme-name//parts/subdir//part-name.

When I look at the response to the POST request, I see an id of theme-name//parts/subdir//part-name, different from that of the ID that was submitted.

This appears to be caused by the REST controller's _sanitize_template_id() method, which "relies on the template ID format {theme_name}//{template_slug} and the fact that slugs cannot contain slashes".

From poking around at other issues, it seems like the intent of something like #25063 was to support subdirectories in parts/, but then #36881 may have reversed that through the introduction of _sanitize_template_id().

I would propose that supporting subdirectories in parts/ is necessary (and mostly supported already?), as staring at a wall of filenames without organization is no fun. :)

If it is not supported, then things like get_template_part() should be adjusted to match expectations.

Step-by-step reproduction instructions

  1. Add a subdirectory to a theme's parts directory
  2. Add a template file to that subdirectory
  3. Open the full site editor and attempt to save changes to the template file

OR

Pass theme-name//parts/subdir/footer to _sanitize_template_id():

  • Expect: theme-name//parts/subdir/footer
  • Get: theme-name//parts/subdir//footer

Screenshots, screen recording, code snippet

No response

Environment info

WordPress 6.1, Gutenberg latest, active or not

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@jeremyfelt jeremyfelt added the [Type] Bug An existing feature does not function as intended label Mar 15, 2023
@jeremyfelt
Copy link
Member Author

I spent too much time trying to hack around this, but one more thought: 😄

We use post_name as the unique identifier in $wpdb->posts for template parts in the database. sanitize_title() is enforced on this field for new posts of any type, but is it an actual requirement for wp_template_part?

The database will store dir/path/name just fine. Can we adjust wp_insert_post() to handle wp_template_part differently from other post types and then introduce a sanitize_template_title that clarifies expectations?

I'm not sure if there are real security implications with that kind of change or if this is just a leftover of attempting to lookup public-facing URL paths as part of a rewrite rule consistently.

@jeremyfelt
Copy link
Member Author

Closing in favor of #54279.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Template Part Affects the Template Parts Block [Type] Bug An existing feature does not function as intended
2 participants