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: Allow passing block definitions to hooked_block_types filter (and thus, hooking patterns) #5837

Closed

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Jan 3, 2024

Another alternative to #5811 and #5835. Uses some code from #5609.

Implements an idea originally conceived of in https://core.trac.wordpress.org/ticket/59572:

We might want to allow providing a full, parsed-block style, block definition, at least for the filter:

array( 'mycommerce/mini-cart', array(
        'isPreview'    => true,
        'miniCartIcon' => 'bag',
) )

In addition, this PR seeks to also explore a possible solution for https://core.trac.wordpress.org/ticket/60126: With the ability to pass attributes for hooked blocks, we can now hook patterns (by giving core/pattern as the block type, and setting the slug attribute to the desired pattern).

To demonstrate the latter, apply the following patch to the Like Button block code:

diff --git a/like-button.php b/like-button.php
index 65acbc3..08a54c1 100644
--- a/like-button.php
+++ b/like-button.php
@@ -22,5 +22,35 @@
  */
 function create_block_like_button_block_init() {
 	register_block_type( __DIR__ . '/build' );
+
+	register_block_pattern(
+		'ockham/like-button-wrapper',
+		array(
+			'title'       => __( 'Like Button', 'like-button' ),
+			'description' => _x( 'A button to like content.', 'Block pattern description', 'like-button' ),
+			'content'     => '<!-- wp:group {"layout":{"type":"constrained"}} --><div class="wp-block-group"><!-- wp:ockham/like-button /--></div><!-- /wp:group -->',
+			'inserter'    => false
+		)
+	);
 }
 add_action( 'init', 'create_block_like_button_block_init' );
+
+function insert_like_button_pattern_after_post_content( $hooked_blocks, $position, $anchor_block ) {
+	if ( 'after' !== $position ) {
+		return $hooked_blocks;
+	}
+
+	if ( 'core/post-content' !== $anchor_block ) {
+		return $hooked_blocks;
+	}
+
+	$hooked_blocks[] = array(
+		'blockName' => 'core/pattern',
+		'attrs'     => array(
+			'slug' => 'ockham/like-button-wrapper',
+		),
+	);
+
+	return $hooked_blocks;
+}
+add_filter( 'hooked_block_types', 'insert_like_button_pattern_after_post_content', 10, 3 );
diff --git a/src/block.json b/src/block.json
index 99660d4..1cd5685 100644
--- a/src/block.json
+++ b/src/block.json
@@ -19,7 +19,6 @@
 	"viewScript": "file:./view.js",
 	"usesContext": [ "postType", "postId", "commentId" ],
 	"blockHooks": {
-		"core/comment-template": "lastChild",
-		"core/post-content": "after"
+		"core/comment-template": "lastChild"
 	}
 }

TODO:

  • Think about downsides: Still doesn't allow flexible setting of the layout type depending on adjacent blocks (only hard-wired). Or do patterns support anything of the sort?
  • Allow specifying attributes for hooked blocks in block.json after all?

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


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 Jan 3, 2024
@ockham ockham changed the title Block Hooks: Allow passing block definitions to hooked_block_types filter Block Hooks: Allow passing block definitions to hooked_block_types filter (and thus, hooking patterns) Jan 3, 2024
@ockham
Copy link
Contributor Author

ockham commented Jan 3, 2024

FYI @andrewserong @tellthemachines @yansern @TimBroddin
(Note that this is an early-stage exploration.)

Copy link

github-actions bot commented Jan 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.

* @param string $anchor_block_type The anchor block type.
* @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
*/
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gziolo had another suggestion:

Suggested change
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
$hooked_block_types = apply_filters( "hooked_blocks_{$anchor_block_type}", $hooked_blocks, $relative_position, $anchor_block, $context );

or even

Suggested change
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
$hooked_block_types = apply_filters( "hooked_blocks_{$relative_position}_{$anchor_block_type}", $hooked_blocks, $anchor_block, $context );

which would allow a syntax like

add_filter( 'hooked_blocks_before_ockham/like-button', ... );

@ockham
Copy link
Contributor Author

ockham commented Jan 4, 2024

Advantages of this approach

Downsides of this approach

  • By allowing parsed block arrays as entries of the array in the first arg, we're bending the notion of hooked block types quite a bit 😅
  • While it might be appealing to be able to use a pattern (and specify its slug) as a hooked block, that's not quite enough to solve https://core.trac.wordpress.org/ticket/60126, as we cannot dynamically set the containing group block's layout attribute.
  • What's more is that even if we don't use a pattern but e.g. use a Group block wrapper (and set its inner block to be a Like button), we don't know wha the adjacent anchor block's layout attribute is set to (if any) as we only have access to anchor_block_type (but not the actual anchor block instance that includes attributes etc).
    • To remediate, we would either need to add yet another argument to the filter (the full $anchor_block instance), which would be very ugly; or alternatively, to change the existing $anchor_block_type to implement PHP's ArrayAccess trait, and __toString. But that's a bit too much magic _on top of allowing $hooked_block_types's entries to arrays of parsed block arrays.
  • If a filter wants to remove a hooked block, lookup in the $hooked_block_types array becomes harder; rather than just searching a string, it now also needs to account for the possibility that an array entry could be an parsed block array, and would need to compare the search string to that array's blockName field.

@leor347

This comment was marked as spam.

@aijaz227

This comment was marked as spam.

@tellthemachines
Copy link
Contributor

I just reported the spam comments above to GH, not sure if I should delete them now or leave them here as evidence 🤔

@andrewserong
Copy link
Contributor

I just did the same 😄
Do you have an option to hide the comments in the ellipsis menu on each comment? (I don't have access to in this repo)

@tellthemachines
Copy link
Contributor

Great idea @andrewserong! Done.

@ockham
Copy link
Contributor Author

ockham commented Jan 15, 2024

Thanks, folks!

I wonder what keeps attracting spambots to this PR; I already reported another one about a week ago 😕

@ockham
Copy link
Contributor Author

ockham commented Jan 16, 2024

Closing this in favor of #5835. Per discussion on #5811, #5835 (comment), and #5837 (comment), I believe that #5835 is the better design and has fewer drawbacks.

@ockham ockham closed this Jan 16, 2024
@ockham ockham deleted the try/block-hooks-allow-block-definitions branch January 16, 2024 14:09
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.

5 participants