Make WordPress Core

Opened 3 weeks ago

Last modified 12 days ago

#61642 assigned defect (bug)

Block Bindings: Add context needed by sources during its processing

Reported by: santosguillamot's profile santosguillamot Owned by: gziolo's profile gziolo
Milestone: 6.7 Priority: normal
Severity: normal Version: 6.6
Component: Editor Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description

Block bindings sources sometimes need to access some context that is not initially available in the block. Right now, we are extending it when the source is registered. The idea is to move the logic so it is added when the block bindings are processed. This means:

  • Before: Extend context during block bindings source registration. This implies that the blocks extend the context even when they don't use bindings.
  • After: Extend context during block bindings processing. This implies that the context is extended ONLY for the blocks where bindings are defined.

Change History (4)

This ticket was mentioned in PR #6456 on WordPress/wordpress-develop by @santosguillamot.


3 weeks ago
#1

  • Keywords has-unit-tests added

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

## What?
Block bindings sources sometimes need to access some context that is not initially available in the block. Right now, we are extending it when the source is registered. This pull request moves the logic so it is added when the block bindings are processed. This means:

  • Before: Extend context during block bindings source registration. This implies that the blocks extend the context even when they don't use bindings.
  • After: Extend context during block bindings processing. This implies that the context is extended ONLY for the blocks where bindings are defined.

## How?
Instead of using the get_block_type_uses_context created back then for this use case, use the available_context in the block class during bindings processing.

I assume it is not possible to remove that filter because of backward-compatibility.

### Testing instructions

  1. Register a custom field in your site to test it. You can use a code snippet like this:
    register_meta(
    	'post',
    	'url_custom_field',
    	array(
    		'show_in_rest' => true,
    		'single'       => true,
    		'type'         => 'string',
    		'default'	   => 'https://wpmovies.dev/wp-content/uploads/2023/04/goncharov-poster-original-1-682x1024.jpeg',
    	)
    );
    
  1. Go to a page and insert an image block.
  2. Go to the Code editor and connect the image to the custom field by adding the metadata bindings property. It should look something like this:
    <figure class="wp-block-image size-large">[[Image(https://wpmovies.dev/wp-content/uploads/2023/04/goncharov-poster-original-1-682x1024.jpeg)]]</figure>
    
    
  3. Save the page and check that in the frontend, the URL defined in the custom field is used.
  4. Repeat the process for paragraph, heading, and button blocks.

#2 @gziolo
12 days ago

  • Component changed from General to Editor
  • Keywords commit added
  • Milestone changed from Awaiting Review to 6.7
  • Owner set to gziolo
  • Status changed from new to assigned

This patch is ready to go. It is compatible with Block Binding sources registered in the core. I'm not entirely sure whether it will work with all custom sources registered by 3rd party code if they define uses_context without the related changes that were applied in the Gutenberg plugin. Should we wait until the WordPress packages get synced to WordPress core?

#3 @santosguillamot
12 days ago

Should we wait until the WordPress packages get synced to WordPress core?

Yes, I think you are right, and it requires the code added to the editor to bootstrap the sources defined in the server. The server rendering will work as expected, but not the editor. I am not sure how the process of syncing with WordPress packages works.

#4 @gziolo
12 days ago

@noisysocks, when do you plan to run the first sync of WordPress packages in the 6.7 release cycle?

Note: See TracTickets for help on using tickets.