-
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
Full Site Editing: Avoid using the templates and template parts CPTs for theme provided templates #27321
Comments
This sounds like a good solution, actually. I was pondering endlessly on this yesterday, and I think the two key parts (aside from the resolution, which is an obvious need) for this to work would be:
By converting files into "fake posts" (e.g. by constructing new WP_Post objects with the file's slug and content) and returning them in the list we might be good, for as far as the Gutenberg entities are concerned. I might give this a try to see if my hypothesis actually make sense, but given how finicky this whole thing is, I strongly encourage others to try as well. |
Sounds like the way to go. |
Love to see this coming together, and to get rid of those pesky auto-generated CPTs ❤️ Carrying over some conceptual ideas from #27284 (comment) that might be useful for implementing this:
|
Right now! this does require a post object which is I believe the main reason FSE started with auto-drafts. |
Thanks for describing the main aspects. This is an important infrastructure step that should simplify the system quite a bit. The initial conveniences of relying on the CPT for all loading was useful during prototyping but is now starting to create many reconciliation issues and increasing the code complexity (sync logic, discriminating customized and not customized work, excluding auto-drafts on navigator, theme updates, etc). |
I'm all for this. With regards to:
Just recently I've been investigating a solution for internationalization & localization that will work directly against the template and template parts without any need for special mark up in the HTML. The basic premise is that the system should be able to automatically generate locale specific html files.
At run time, the locale specific templates and template parts will be loaded instead of the US English defaults. There's no further need for translation. No But I have some nagging questions about FSE
|
Continuing some of my thought from #27284:
I do wonder how performance compares if we are reading from the filesystem on every request. For example, is rendering a template by reading from the database significantly faster than rendering a template by reading from the filesystem? If it is much faster, maybe we would prefer to keep the CPT approach for rendering? Currently, we just compare file stats, but if we stopped using CPTs, we would also be reading the whole file directly which is more expensive. I also wonder if this prevents us from using caching mechanisms as easily. I also think we can iterate the sync experience so that it does less work. It doesn't need to sync everything on every load. We could make it smarter and therefore more performant :) Also, regarding developer experience:
Perhaps I misunderstand, but in my opinion, I think the sync mechanism we have right now (which just happens on wp_load) is really not that complex. (Though it certainly used to be!) I worry that creating some alternative method dealing with some non-post entity would be more complicated just because it is different from how everything else works 🤔 |
Loading templates from files is how WordPress has worked since themes were introduced, so there's a strong precedent in how things work. The database should be generally relied upon for user content and configuration; storing every theme file on theme switch as post types just seems convoluted, increasing the weight of the database with no mechanisms for freeing it later on (say, if you "deactivate" or "remove" a theme). These are things that could be accounted for in code, for sure, but it raises the question: what drives the need for copying files into posts in the first place? |
I think it should be pointed out that these behaviours were already implemented for templates until they were changed in version 9.4, and I believe they still are happening into 9.4 for template parts. |
Those are great points matias, thanks for the comment :)
In my mind, beyond the need for editing, an answer might be to have a single source of truth for templates. That makes it easier to do things like "get a list of all the templates" (perhaps for displaying in wp-admin). With an alternate approach, we don't have a source of truth -- it could be the filesystem, or it could be the database. We'd need to create new API endpoints or new functions for compiling the data, without relying on the post query stuff. And that's not just for Gutenberg, but also for plugin/theme developers. sThat said, if we do need dynamic templates, we probably can't use the database for them, so we might need to build all of that out anyways. 🤔 |
In my mind, for the client, this can remain by potentially having a dedicated endpoint for templates (and not the normal CPT one) and potentially similar APIs in the backend. |
I don't think source of truth is a very compelling argument in this case given interaction with it happens through abstracted APIs anyways. The single source of truth is also a bit of a mirage since theme files already exist separately, requiring a synchronization and reconciliation step to feed the custom post type, plus extra logic to discriminate user content, and likely some involved setup to handle translations. |
Today I started just spitballing ideas in a branch, mostly experimenting with ideas and checking which areas of the code will be affected.
I didn't get things to work in my branch, but from my brief exploration, it's definitely possible. |
@noahtallen I think we can mitigate a lot of those developer experience/single-source-of-truth concerns by providing abstractions at the right level -- such as a REST API endpoint, as suggested by @youknowriad. At an even lower level, we can have PHP functions that encapsulate the CPT/file lookup that can be used for frontend rendering, in combination with template resolution. An approach like that would allow us to encapsulate the 'hybrid' CPT/file lookup into a more abstract template lookup function where possible, and to expose the different mechanisms to the developer where needed (e.g. for the Site Editor). I believe that an approach like this would represent the underlying constraints quite well. |
Yeah, getting the frontend to work was relatively easy in my brief experiment (see this code for an ugly - but working - example). |
As an experiment, I tested using a REST endpoint like this: https://github.com/WordPress/gutenberg/blob/11fd10cb4ef97021c914a035b5a79ad494b7c125/lib/class-wp-rest-theme-templates.php Before we attempt removing drafts, I think the first step would be to go through all instances of |
I think maybe we need a way to create "new entity records" in "core-data" with "temporary ids" and replace these on save. |
I published a Feature request yesterday. See #27402 @aristath I'd like to be able to understand how you intend to achieve your solution and how this might fit in with i18n/l10n. It would be nice to have an outline of your proposed changes. I assume the primary requirement is to improve server response when the theme is not customised in any way. But, assuming some form of customisation is necessary - menus and widgets being the first things to be changed - then how will the solution work?
|
It doesn't affect i18n in any way. Whether the strings exist in the db or a file should not affect i18n.
There is no outline because there are no proposed changes. At this stage I'm simply experimenting, trying to identify the pain points and getting an idea of what we'll need to do so that a plan can be formulated and implemented. The idea here is that only customized templates will be saved in the db. If nothing was customized in the
Same as now, in posts with the
Multisite or single site should be the same, there is no need to have anything different for that.
autosaves no because only modified templates will be stored in the db. For revisions I don't know, I don't think that has anything to do with this issue 🤔
Customized parts cannot - and should not - be automatically reconciled. If the user wants to update their customized templates they can do it manually. Non-customized templates won't have an issue and there won't be a need to reconcile anything (so we can probably trash the
Of course. It's just a matter of checking both the parent and child themes when retrieving the list of template files.
Grand-child themes were never officially supported in WordPress. They work without a plugin in general, but that is mostly a happy accident and not a conscious decision or implementation. I don't see why we couldn't do it, but I don't believe it will be a priority. |
Thanks. I'd very much appreciate it if you could read my proposal in which locale specific templates and template parts are built statically. With this solution the Site Editor would need to know which locale the user is working in and load the templates and template parts from the appropriate folder. If the user's locale is not available, then the default ( I'm also wondering what the process might be for a theme developer.
How would the end user access the changed templates? |
If they delete their customized template the default will be used. Maybe there will be a button to revert a template to its default state. Whatever the solution, it wouldn't be related to this issue which is about db-vs-file templates when they are not customized.
The current process is built having in mind that the user will be able to export their theme, get a file, and then they'll be able to simply upload that file to another site.
I don't see how a security issue would even be possible in what will basically be just blocks content saved in the editor. Even if PHP was allowed in the theme template files, the moment the user customizes a template it gets saved in the db as a custom post, so any logic is lost and the only thing saved is the actual content. So there is no security concern. |
I'm afraid I don't know how to do that... Would someone else be able to work on this so it can move forward faster and not wait for me to familiarize myself with that part of the codebase? I feel this issue is too important to delay as a lot of other tickets can move forward once this is done. |
So, I've been experimenting on this issue for a while, and I've finally managed to come up with something that could be workable. 🤔 You can check out the draft PR: #27624 The core concepts are:
There are quirks and kinks to iron out, some are visible (titles are a mess right now), some will be trickier to find. I'd appreciate if someone would give it a spin. 🙇♂️ |
Good work here
I didn't take a look at the PR, the only thing that stands out to me in your explanation is this item. For me, ideally, this should be transparent and the "core-data" level. Meaning when we "save" an entity and the returned ID is different from the saved one, I imagine we could do a "mapping" to replace the fake id in state. Wouldn't something like that work? It seems also a good thing to have regardless, for instance, we could decide that we don't create auto-drafts in the post editor when loading the page, but we just create a local post object with a temporary id. |
Most of the React code goes over my head, but why not use 0 as the fake post ID? ID's are not meant to be strings. Also, why 'draft' as the post status? |
@carlomanf The ID is definitely debateable. As for the |
Thanks for the work on this one! |
Right now, full site editing UI and frontend rendering fetch templates from the
wp_template
andwp_template_part
CPT, whether it's for the templates that are edited by the user or the ones that are provided by the theme. This means a "synchronization" step is needed between the theme files and the CPT.This synchronization simplifies the fetching by allowing us to only use the CPT endpoints and WPQuery to retrieve the desired templates but it has its drawbacks:
Proposal
We should consider checking what would it take to update the framework to rely on the files directly for unmodified templates. Some potential challenges here will be:
cc @vindl @Copons @david-szabo97 @Addison-Stavlo @noahtallen @jeyip @mattwiebe @mtias @aristath @ockham @carlomanf
The text was updated successfully, but these errors were encountered: