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

Block Hooks: Fix context in update_ignored_hooked_blocks_postmeta #7941

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Dec 3, 2024

Discovered while working on #7898.

Testing Instructions

Also see the screencasts further below.

Preparatory steps:

  • Make sure you're using a Block Theme that uses a Navigation block in its home page template (e.g. in its header or footer). TT4 should work.
  • Upload the following code as a new plugin (i.e. copy to a new PHP file, zip, and upload to your WP install).
<?php
/**
 * Plugin Name:       Add Login/out block to Header Navigation
 * Description:       Block Hooks API demonstration
 * Version:           0.1.0
 * Requires at least: 6.7
 */

function add_loginout_block_to_header_navigation( $hooked_block_types, $relative_position, $anchor_block_type, $context ) {

	// Is $context a Navigation menu?
	if ( ! $context instanceof WP_Post || 'wp_navigation' !== $context->post_type ) {
		return $hooked_block_types;
	}
	
	if ( 'last_child' === $relative_position && 'core/navigation' === $anchor_block_type ) {
		$hooked_block_types[] = 'core/loginout';
	}

	return $hooked_block_types;
}
add_filter( 'hooked_block_types', 'add_loginout_block_to_header_navigation', 10, 4 );

To verify the issue (on trunk):

  • Activate the plugin.
  • On the frontend, verify that a new "Log in" link has been added as the last item in your Navigation block.
  • Click on "Edit site", and move that Login/out block to a different position within the Nav block.
  • Save your changes, and view them on the frontend once again.
  • Note that while the block is present in your chosen position, it is also still present as the Nav block's last child ❌
  • Click on "Edit site" once again. The editor should now reflect what you just saw on the frontend.
  • Deactivate the plugin.
  • Reload the Site Editor. Only the instance of the Login/out block that you moved should still be present.
  • Delete that instance, and save the template. View it on the frontend to verify that the Navigation block is now in its original state (i.e. no "Log in" links).

To verify the fix (on this branch):

  • Switch to this branch.
  • Re-activate the plugin.
  • On the frontend, verify that a new "Log in" link has been added as the last item in your Navigation block.
  • Click on "Edit site", and move that Login/out block to a different position within the Nav block.
  • Save your changes, and view them on the frontend once again.
  • Note that the block was successfully moved to your chosen position, and is no longer present in its previous place (as the Nav block's last child) ✅
  • Click on "Edit site" once again. The editor should reflect what you just saw on the frontend (Login/out block in desired position).

Screencast

Before

login-out-block-before

After

login-out-block-after

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


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.

@ockham ockham self-assigned this Dec 3, 2024
Copy link

github-actions bot commented Dec 3, 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.

@ockham ockham requested review from gziolo and cbravobernal December 3, 2024 17:23
@ockham ockham marked this pull request as ready for review December 3, 2024 17:23
Copy link

github-actions bot commented Dec 3, 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 bernhard-reiter.

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

@ockham
Copy link
Contributor Author

ockham commented Dec 4, 2024

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

@ockham ockham closed this Dec 4, 2024
@ockham ockham deleted the fix/update-ignored-hooked-blocks-postmeta-context branch December 4, 2024 12:06
Copy link
Contributor

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

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

LGTM!

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