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
Editing Toolkit / Patterns: Support skipping registering patterns with a requires-align-wide tag #47571
Editing Toolkit / Patterns: Support skipping registering patterns with a requires-align-wide tag #47571
Conversation
Caution: This PR affects files in the Editing Toolkit Plugin on WordPress.com D53001-code has been created so you can easily test it on your sandbox. See this FieldGuide page about developing the Editing Toolkit Plugin for more info: PCYsg-ly5-p2 |
This PR does not affect the size of JS and CSS bundles shipped to the user's browser. Generated by performance advisor bot at iscalypsofastyet.com. |
apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-from-api.php
Outdated
Show resolved
Hide resolved
* @return bool | ||
*/ | ||
private function can_register_pattern( $pattern ) { | ||
if ( $this->skip_full_width_patterns && in_array( 'requires_align_wide', array_keys( $pattern['tags'] ), true ) ) { |
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.
Question: if we're expecting a specific tag anyway, might a future optimization be (assuming they'll be more of such theme exceptions) something like requires_{ $feature }
, and then
private function can_register_pattern( $pattern ) {
foreach( $pattern['tags'] as $tag ) {
$exploded_tag = explode( 'requires_', $tag );
if ( $exploded_tag[1] ) {
return false !== get_theme_support( $exploded_tag[1] )
}
}
return true;
}
Very willing to be accused of over-thinking things! :D
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.
@ramonjd I actually quite like that. I'll have a play with it now, because it seems fairly safe and will also future proof it for any similar situations (the issue can be resolved by adding / removing tags, rather than requiring further code changes). Initially I thought it'd be better to cache the get_theme_support
feature rather than call it on every pattern (since we'll have > 100 patterns), but from looking at how get_theme_support
is implemented, it's an associative array lookup rather than looping over anything, so seems like it shouldn't be too expensive.
It just means we'd use the tag requires-align-wide
instead of requires_align_wide
so that we're hyphenating correctly.
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.
Initially I thought it'd be better to cache the get_theme_support feature rather than call it on every pattern (since we'll have > 100 patterns),
Ah yeah, good point. That totally escaped me.
Anyway, it's just an idea seeing as we're expecting a specific string regardless.
Sorry if I threw an implementation pepper into the soup!
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.
No, no, it's a good idea, thank you! Better to spend a little bit longer getting it right now, so that we can save some time further down the track 😀
Thanks for the feedback @ramonjd! I've updated this to use the simplified approach you suggested that also supports arbitrary features based on the tag's slug. I went with I've also updated the patterns to use a |
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.
This PR is designed to skip registering patterns that require full width support, when the site's currently active theme doesn't support full width. It will help avoid the issues described in #47565 and #46215.
Changes proposed in this Pull Request
requires-
, use the remainder of the tag to look up theme features. If the theme does not support the feature, then skip registering the pattern.requires-align-wide
on themes that don't supportalign-wide
.Screenshots
In a theme that supports
align-wide
we should still see theimages-2
pattern under the Gallery category in the inserter:The following screenshot is from a test site using the Maywood theme, which has support for
align-wide
:In a theme that does not support
align-wide
we should not be able to see theimages-2
pattern under the Gallery category in the inserter.The following screenshot is from a test site using the Hemingway Rewritten theme, which does not have support for
align-wide
. Note that in the screenshot I'm highlighting the pattern below the one in the above screenshot, showing that it no longer appears:Testing instructions
requires-align-wide
tag to the main pattern from the original issue (images-2
).How to test
apps/editing-toolkit
runyarn dev --sync
to sync to your sandbox0-sandbox.php
file add the following to ensure you're using fresh results from the patterns API that include the appropriate tags:With your test site sandboxed, and using a theme with
align-wide
support (e.g. any of the suggested themes / Varia child themes such as Maywood), go to edit a post or page, click the inserter and in the Patterns tab, under the Gallery category you should see the pattern with the title "Images" and the text within the pattern will include "5am, the beach". It should look like the top-most screenshot in this PR.Switch your theme to one that does not support
align-wide
(e.g. Hemingway Rewritten) and open up a post. In the inserter, under the Gallery category, you should not be able to see the pattern in question ("Images")Fixes #47565