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 flash when clicking on template name when a plugin registered template matches a default WP theme template #7676

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from

Conversation

Aljullu
Copy link

@Aljullu Aljullu commented Oct 29, 2024

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.

Copy link

github-actions bot commented Oct 29, 2024

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props aljullu, apermo, azaozz, ntsekouras, flixos90, antonvlasenko.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link

@apermo apermo left a 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

@Aljullu Aljullu force-pushed the fix/flash-clicking-template-name branch from db4ea9b to 8ab1d7a Compare November 3, 2024 11:55
@Aljullu
Copy link
Author

Aljullu commented Nov 3, 2024

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.

Copy link

@apermo apermo left a 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.

src/wp-includes/block-template-utils.php Outdated Show resolved Hide resolved
@Aljullu
Copy link
Author

Aljullu commented Nov 8, 2024

Thanks for the approval, @apermo! I don't have merging rights, is that something you can take care of?

Otherwise, @azaozz, wondering if this is something you can do? Thanks in advance!

@apermo
Copy link

apermo commented Nov 8, 2024

Thanks for the approval, @apermo! I don't have merging rights, is that something you can take care of?

I can't merge either, this happens on the subversion by those with commit right(core commiters), I just reviewed.

@Aljullu Aljullu force-pushed the fix/flash-clicking-template-name branch from 70f64a1 to 8ef61a6 Compare November 26, 2024 13:29
Comment on lines 1182 to 1193
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 );
}
Copy link

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

Copy link
Author

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.

Copy link
Contributor

@azaozz azaozz Nov 26, 2024

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 :)

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'] ) ) {
Copy link

@ntsekouras ntsekouras Dec 4, 2024

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.

Copy link
Author

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.

Copy link
Member

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?

Copy link
Author

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;

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.

Copy link
Author

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.

@ntsekouras
Copy link

This looks good, but I'd appreciate some more eyes maybe from @anton-vlasenko or @felixarntz ?

@Aljullu
Copy link
Author

Aljullu commented Dec 4, 2024

Thanks for the review, @ntsekouras! I answered/applied the feedback.

Copy link
Member

@felixarntz felixarntz left a 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'] ) ) {
Copy link
Member

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?

Copy link

@anton-vlasenko anton-vlasenko left a 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).

Comment on lines 1193 to 1195
isset( $query['post_type'] ) &&
isset( $template_file['postTypes'] ) &&
in_array( $query['post_type'], $template_file['postTypes'], true )

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.

Suggested change
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 )

Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants