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 wp_template renaming when hooked_block_types filter exists #6955

Closed
wants to merge 7 commits into from

Conversation

alshakero
Copy link

The ticket explains the problem.

Trac ticket: https://core.trac.wordpress.org/ticket/61550


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 Jul 2, 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 alshakero, bernhard-reiter.

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

Copy link

github-actions bot commented Jul 2, 2024

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
Contributor

@ockham ockham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you very much for the fix!

@ockham ockham self-requested a review July 18, 2024 14:59
Copy link
Contributor

@ockham ockham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, I might've spoken too soon 😅

I remembered to look at the comment for this PR that fixed the same issue for wp_navigation post types.

Note this comment on using isset vs empty:

empty() is bad, because it would miss the cases where the post_content is legitimately changed to an empty string. isset() is better.

Specifically, per #6867, we could hit an edge case where a hooked block would be injected as the first or last child of an (otherwise empty) Template Part block. If we use empty(), this isn't going to work.

We should probably add some test coverage in tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php to cover the case where $changes->post_content isn't set (to make sure that https://core.trac.wordpress.org/ticket/61550 is fixed), and another test to cover the case I described in the previous paragraph.

(I can take care of those things next week if you don't have time. Unfortunately, this will mean the fix won't make it into 6.6.1 and will have to wait for 6.6.2 instead.)

@alshakero
Copy link
Author

alshakero commented Jul 18, 2024

Oooh great catch! How about array_key_exists property_exists which seems even more careful?

@ockham
Copy link
Contributor

ockham commented Jul 18, 2024

Oooh great catch! How about array_key_exists property_exists which seems even more careful?

So the only difference between property_exists() and isset() is that the latter returns false if the property exists but is null, right? In that case, I'd stick with if ( ! isset( $changes->post_content ) ) { return $changes; }, since it'll bail if $changes->post_content === null. This is fine IMO, since it's unclear how we'd otherwise apply the Block Hooks algorithm to a post content that's null; we'd effectively have to cast it to '' after all.

It's also more consistent with what we're doing with wp_navigation 🙂 Finally, it's hopefully a rather academic scenario only that $changes->post_content === null.

@alshakero
Copy link
Author

Hi @ockham! Thanks for addressing your feedback. I was working on it at the same time and only realized because I couldn't push. Here's my take on the test in case it helps

/**
	 * @ticket 61550
	 */
	public function test_hooked_block_types_filter_with_renamed_template_part() {
		$action = new MockAction();
		add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 );

		$changes             = new stdClass();
		$changes->ID         = 1;
		$changes->post_type  = 'wp_template_part';
		$changes->post_title = 'new title';
		$cloned_changes      = unserialize( serialize( $changes ) );

		$result = inject_ignored_hooked_blocks_metadata_attributes( $changes );

		// Ensure same reference.
		$this->assertSame(
			$changes,
			$result,
			'Should leave the changes object untouched if `post_content` is not set.'
		);

		// Ensure same values.
		$this->assertTrue(
			$result == $cloned_changes, // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- == compares all props and their values.
			'Should leave the changes object untouched if `post_content` is not set.'
		);

		$changes               = new stdClass();
		$changes->ID           = 1;
		$changes->post_type    = 'wp_template_part';
		$changes->post_title   = 'new title';
		$changes->post_content = 'new content';
		$cloned_changes        = unserialize( serialize( $changes ) );

		$result = inject_ignored_hooked_blocks_metadata_attributes( $changes );

		$this->assertNotSame(
			$changes,
			$result,
			'Should process the changes object if `post_content` is set.'
		);

		$changes               = new stdClass();
		$changes->ID           = 1;
		$changes->post_type    = 'wp_template_part';
		$changes->post_title   = 'new title';
		$changes->post_content = '';
		$cloned_changes        = unserialize( serialize( $changes ) );

		$result = inject_ignored_hooked_blocks_metadata_attributes( $changes );

		$this->assertNotSame(
			$changes,
			$result,
			'Should process the changes object if `post_content` is set to an empty string.'
		);
	}

@ockham
Copy link
Contributor

ockham commented Jul 23, 2024

Hi @ockham! Thanks for addressing your feedback. I was working on it at the same time and only realized because I couldn't push.

Oh no, I should've posted a comment to let you know! Apologies, I didn't mean to make you duplicate work.

Anyway, thank you for sharing your patch! I'll have a look if there's anything to incorporate into the tests and will probably land the PR later today 😊

@ockham
Copy link
Contributor

ockham commented Jul 23, 2024

Committed to Core in https://core.trac.wordpress.org/changeset/58785.

@ockham ockham closed this Jul 23, 2024
@ockham
Copy link
Contributor

ockham commented Jul 24, 2024

Backported to the 6.6 branch in https://core.trac.wordpress.org/changeset/58802.

@ockham ockham reopened this Jul 24, 2024
@ockham ockham closed this Jul 24, 2024
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.

2 participants