-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 flash when clicking on template name when a plugin registered template matches a default WP theme template #7676
base: trunk
Are you sure you want to change the base?
Fix flash when clicking on template name when a plugin registered template matches a default WP theme template #7676
Conversation
…plate matches a default WP theme template
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
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.
Looks good to me
…lable for all post types
…st type slugs aren't leaked by get_block_templates()
db4ea9b
to
8ab1d7a
Compare
Thanks for the review, @apermo! Would you mind taking another look? I added a couple of commits that fix some edge cases and add unit tests. |
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.
Looking good to me besides that one suggestion, that would make things easier to read imho.
Co-authored-by: Christoph Daum <[email protected]>
I can't merge either, this happens on the subversion by those with commit right(core commiters), I just reviewed. |
70f64a1
to
8ef61a6
Compare
if ( isset( $query['post_type'] ) && ! isset( $template_file['postTypes'] ) ) { // The custom templates with no associated post types are available for all post types. | ||
$candidate = _build_block_template_result_from_file( $template_file, $template_type ); | ||
$default_template_types = get_default_block_template_types(); | ||
if ( ! isset( $default_template_types[ $candidate->slug ] ) ) { | ||
$query_result[] = $candidate; | ||
} | ||
} elseif ( | ||
! isset( $query['post_type'] ) || | ||
( isset( $query['post_type'] ) && isset( $template_file['postTypes'] ) && in_array( $query['post_type'], $template_file['postTypes'], true ) ) | ||
) { | ||
$query_result[] = _build_block_template_result_from_file( $template_file, $template_type ); | ||
} |
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 is definitely better than the one you posted minutes ago.
After checking, I agree that this is equivalent, but I'm unsure wether this is really an improvement. Because the readability in my eyes was better before with single ifs.
Adding a continue;
to the first if would likely have the same result. But I'm not convinced about that either.
Maybe moving the comments, and having those better in sight will do the trick here? This would also ask for a comment at the elseif
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.
Thanks for the feedback! Yeah, I don't have a strong opinion, I prefer the elseif
as it makes it clear there is no overlap between the two paths. I added an extra comment in 0cfab7c as you suggested. Please let me know how it looks.
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 readability in my eyes was better before with single ifs
I know this is probably getting into a "personal preference" territory, but I tend to agree with @Aljullu that if(){...} elseif(){...}
makes it clearer that the two conditionals don't overlap. Also makes this code a very tiny bit faster as the elseif()
may not need to be evaluated :)
Co-authored-by: Andrew Ozz <[email protected]>
foreach ( $template_files as $template_file ) { | ||
$query_result[] = _build_block_template_result_from_file( $template_file, $template_type ); | ||
// The custom templates with no associated post types are available for all post types. | ||
if ( isset( $query['post_type'] ) && ! isset( $template_file['postTypes'] ) ) { |
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.
Are these changes not needed in GB? They aren't in the original GB PR.
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 main difference is that in GB we are hooking into the get_block_templates
filter, in other words, we are hooking into the end of this function.
In the other hand, in WP core, we can introduce the changes inside the function. While I could have simply called _get_block_templates_files()
again with post_type
removed (as it's done in the GB PR), it made more sense to me to reuse the existing call to _get_block_templates_files()
. It requires some extra logic (basically, the new if ... else if ...
, but this way we call _get_block_templates_files()
only once, which I guess is better for performance.
I'm happy to change this approach if you think overcomplicates things.
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.
Thanks for explaining. Overall this makes sense to me, though one question: Why is the isset( $query['post_type'] )
check needed in this line here specifically?
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 was to avoid entering the if
clause unnecessarily (as it would go through the elseif
one, which is more performant). In 5cea08f I removed this isset( $query['post_type'])
and switched the order of the clauses, which hopefully makes the code easier to read.
@@ -1163,14 +1163,40 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) | |||
} | |||
|
|||
if ( ! isset( $query['wp_id'] ) ) { | |||
$template_files_query = $query; |
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.
Is this not important for the get_block_templates
filter below? Before we were updating the same instance.
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.
Good call. I added an extra commit (cd07c08) to make sure $query['slug__not_in']
stays consistent with what we had until now.
This looks good, but I'd appreciate some more eyes maybe from @anton-vlasenko or @felixarntz ? |
Thanks for the review, @ntsekouras! I answered/applied the feedback. |
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.
Mostly looks good to me, though one thing I'm unsure about.
foreach ( $template_files as $template_file ) { | ||
$query_result[] = _build_block_template_result_from_file( $template_file, $template_type ); | ||
// The custom templates with no associated post types are available for all post types. | ||
if ( isset( $query['post_type'] ) && ! isset( $template_file['postTypes'] ) ) { |
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.
Thanks for explaining. Overall this makes sense to me, though one question: Why is the isset( $query['post_type'] )
check needed in this line here specifically?
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.
Looks good to me (I only have some minor notes, please see below).
isset( $query['post_type'] ) && | ||
isset( $template_file['postTypes'] ) && | ||
in_array( $query['post_type'], $template_file['postTypes'], 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.
Nitpick: It seems the isset( $query['post_type'] )
check is redundant here, as $query['post_type']
should already be defined at this point.
isset( $query['post_type'] ) && | |
isset( $template_file['postTypes'] ) && | |
in_array( $query['post_type'], $template_file['postTypes'], true ) | |
isset( $template_file['postTypes'] ) && | |
in_array( $query['post_type'], $template_file['postTypes'], 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.
Good catch! I removed it in 5cea08f, after switching the order of the if ... else ...
clauses, which hopefully makes the code easier to read.
Co-authored-by: Anton Vlasenko <[email protected]>
This PR includes the changes that need to be backported from WordPress/gutenberg#66359 into WC core.
Trac ticket: https://core.trac.wordpress.org/ticket/62319
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.