-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[Pattern Directory]: Allow pattern registration from directory with theme.json #38323
[Pattern Directory]: Allow pattern registration from directory with theme.json #38323
Conversation
Are the slugs already surfaced in the pattern directory? This assumes that you know the slug to place it in theme.json. |
They can be extracted by the url in single pattern view at the Pattern Directory: example |
I think it needs to be more obvious. |
I'm not sure it does need to be. After all, this is a technical feature geared towards developers who are generally familiar with what a slug is already. To me, using slugs feels like a pretty reasonable solution, and one that doesn't require any new functionality or UI in the patterns directory. I'm curious to hear other folks' reactions too. |
This feels easy enough for theme developers to use. I'm curious what a more obvious alternative would be. |
5810d58
to
f3210b0
Compare
I think this could be a good start. As with other features that block themes enable, there are many opportunities for nicer, more integrated experiences; for instance, a UI similar to the Patterns Explorer with which a themer/user can cherry pick patterns from the directory to "attach" to the current theme. |
Neat! This is working well in my testing, and creating Should these patterns be highlighted as being featured by the theme? Right now they're dropped in with the rest of the patterns, so there's no indication to the user that the theme author specifically picked out "Short text surrounded by round images," for example.
We could change that - initially there was an assumption that categories would be loaded from the directory, we even have an endpoint stubbed out for it: https://api.wordpress.org/patterns/1.0/?categories
I think for now this is fine, but it would be pretty neat to have a UI like the google fonts selection while browsing wp.org/patterns, that would let you collect a few patterns & then output the |
I'm not really sure, but we can explore this in follow ups for sure. Do you think this adds some value in the form of some
That could work then. I can get the categories and register what's needed, but I think we should first try to find the best path forward with current and future pattern categories. Should there be parity between the directory and core? Are there are plans to add more categories in the directory? |
I think for the first version of this, we should just ensure that these patterns show up at the top of the list, and that's fine. |
There was a little conversation about that on the original issue #35364 (comment), and I still think having a "Theme Name" category would be useful, but don't feel strongly about it.
Yep, WordPress/pattern-directory#190 — we need to decide what they'll be, but there will be more. Eventually the source of truth for categories should be the Pattern Directory.
Absolutely, none of what I mentioned was a blocker — let me know if you want me to make issues for anything 🙂 |
Unfortunately there is no specific way to make this happen, because the patterns order depends on pattern registration time, which can happen in different times(actions, etc...) and we do not have any indication of that with the current API. In addition an API like that for ordering wouldn't work well in practice as it would give a way for everyone to register patterns and try to make them always appear on top. If we need to differentiate them from the rest, I guess we should create a new pattern category. Having said that, I think we can land this first iteration as is not something user facing and we can follow up with enhancements on category handling and explore sorting options. What do you think? |
lib/compat/wordpress-6.0/class-gutenberg-rest-pattern-directory-controller.php
Outdated
Show resolved
Hide resolved
if ( isset( $this->theme_json['patterns'] ) && is_array( $this->theme_json['patterns'] ) ) { | ||
return $this->theme_json['patterns']; | ||
} | ||
} |
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.
Instead of copying the entire file to lib/compat/wordpress-6.0, should we instead keep the original file under lib/compact/wordpress-5.9
but rename the class to match core classname and only declare it if we're on 5.8 (the class doesn't exist) and in lib/compat/wordpress-6.0
extend that class (like you did above for the rest controller and I believe I did that on another controller).
The advantage of this is that we know exactly what's on 5.9 and what was added on 6.0
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.
It seems in many cases including this one, this is quite hard due to private functions and properties. In this case for making it work we need to override the VALID_TOP_LEVEL_KEYS
const in child class, but this const is referenced inside private functions in the parent class, so it uses the existing old value from the parent.
I then tried to move more functions to the new child class but we have so many private functions/properties there used, that we would need to move so much code there, changing the names(prefix gutenberg_
) in order to be called by the child class without being private, but the content would be the same.
I think we cannot avoid this easily - at least from what I could see, and maybe we should copy the whole class as is here in 6.0
and in followups check what we should change to protected
instead of private
. That would help as from that point on, to handle this more gracefully.
What do you think? Am I missing something php related?
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.
Seems like the problem here is the change in VALID_TOP_LEVEL_KEYS
right? Would be good if we could switch at some point to "schema" based validation and avoid the adhoc code. That said, it's subject for another time.
For now, I guess we don't have any other option than copying everything 🤷
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.
In this case VALID_TOP_LEVEL_KEYS
is affected, but in the future we will probably need to make more changes in other private functions. That goes to other classes as well, so that's why I'm suggesting making some things protected
or static
for 6.0
and then we could make adjustments easier for future releases.
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.
Personally, I think each PR should add its own needed changes, if we can land this without copying everything, I'd do that. If we need to make another change later, we'll need to figure how to do it at that point.
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.
What if we add a couple of filters to the JSON parser in WP-Core for 5.9.1?
That would unblock this PR - as well as some others that currently have no way to hook in there
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.
If we need to make another change later, we'll need to figure how to do it at that point.
That's the thing though, the way I see it it as I explained above, we cannot avoid copying the class
. But if we do not change some private
things we will end up copying the class forever(when we have changes of course). Do you find something bad with making some things protected
or are there strong reasons for keeping them private
?
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.
@youknowriad @ntsekouras I haven't yet dug into an alternative way to extend this. I'll do it later.
I wanted you to be aware that in https://github.com/WordPress/gutenberg/pull/37140/files#r801650187 we'll need to change from private
to protected
some things in the resolver. I've prepared #38625 that could be part of 5.9.1.
Perhaps we can do similar modifications to WP_Theme_JSON
so this doesn't require copying the entire class.
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.
The required changes in core for this to work with a child class that changes VALID_TOP_LEVEL_KEYS
and just adds the new get_patterns
function would be:
- Change $theme_json to
protected
, because we need to access this from the child class - Update the line that uses
VALID_TOP_LEVEL_KEYS
fromself::VALID_TOP_LEVEL_KEYS
tostatic::VALID_TOP_LEVEL_KEYS
, asstatic::
is inheritance-aware.
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.
f6a6dd7
to
1499c1e
Compare
I've just merged #38625 so this needs a rebase to get those changes in. |
Will do in a jiffy 😄 |
1499c1e
to
9cca636
Compare
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.
It works as expected, the approach with slugs sounds easy enough, and the code looks fine. 🚢
Thanks all for the feedback/reviews! I'll land and let's iterate if needed about the prominence of the added patterns (specific new category, etc...). |
Thanks for your work here, should we add some documentation? |
Definitely. Sorry for missing this - I'll create a PR right away! |
Addressing #38620 would create docs automatically for this new key as well as other ones that are undocumented (customTemplates, templateParts, and any future one). |
I started adding some documentation here: https://github.com/WordPress/gutenberg/blob/trunk/docs/how-to-guides/themes/theme-json.md. That's a different thing @oandregal , no? |
That's more of a "how to" guide, it's aimed at presenting examples (although it needs more work to point to the reference doc, remove things, and update others). |
Resolves: #35364
Depends on #38625
This PR enables themes to register patterns from Pattern Directory through
theme.json
. It checks for apatterns
top level key that is an array ofpattern slugs
from the directory.Testing instructions
git merge --squash update/extend-theme-json
.empty
theme and go to the patterns tab in the inserter.Alternatively, you can add in your theme.json some patterns in the
patterns
property.Notes
second pattern
that is underuncategorized
. That is because there is no parity between pattern directory categories and core categories(specificallyimages
category in Pattern Directory.