Repository navigation
WIP: Explore mediaRoles in block config
#72792
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
Draft
ntsekouras
wants to merge
1
commit into
trunk
Choose a base branch
from
explore-media-roles
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+392
−121
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
| /** | ||
| * Block compatibility functions for WordPress 7.0 | ||
| * | ||
| * Adds support for the `mediaRoles` property in block.json metadata. | ||
| * This ensures that the mediaRoles property is passed to JavaScript | ||
| * when blocks are registered via register_block_type_from_metadata(). | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Filters the settings determined from the block type metadata. | ||
| * | ||
| * WordPress core doesn't yet support the mediaRoles property in block.json. | ||
| * This filter adds it to the registered block type so it gets passed to JavaScript. | ||
| * | ||
| * @param WP_Block_Type $block_type Block type object. | ||
| * @param array $metadata Block metadata from block.json. | ||
| * @return WP_Block_Type Modified block type with mediaRoles if present. | ||
| */ | ||
| function gutenberg_add_media_roles_to_block_type( $block_type, $metadata ) { | ||
| // Check if mediaRoles exists in the metadata. | ||
| if ( isset( $metadata['mediaRoles'] ) && is_array( $metadata['mediaRoles'] ) ) { | ||
| // Add mediaRoles as a public property on the block type. | ||
| // This will be included when the block type is passed to JavaScript. | ||
| $block_type->media_roles = $metadata['mediaRoles']; | ||
| } | ||
|
|
||
| return $block_type; | ||
| } | ||
|
|
||
| add_filter( 'register_block_type_args', 'gutenberg_add_media_roles_to_block_type', 10, 2 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
| /** | ||
| * PHP and WordPress configuration compatibility functions for the Gutenberg | ||
| * editor plugin changes related to REST API. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Adds mediaRoles to the block type REST API response. | ||
| * @since 7.0.0 | ||
| * | ||
| * @param WP_REST_Response $response The response object. | ||
| * @param WP_Block_Type $block_type Block type object. | ||
| * @return WP_REST_Response Modified response with mediaRoles if present. | ||
| */ | ||
| function gutenberg_add_media_roles_to_rest_response( $response, $block_type ) { | ||
| if ( isset( $block_type->media_roles ) ) { | ||
| $response->data['media_roles'] = $block_type->media_roles; | ||
| } | ||
| return $response; | ||
| } | ||
| add_filter( 'rest_prepare_block_type', 'gutenberg_add_media_roles_to_rest_response', 10, 2 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand this correctly, the
typecan have a hardcodedvalueor derive it from a specific attribute. Is that correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. This is needed to handle both blocks without a
typeattribute likeImageand the ones that have a type.What I haven't thought much right now is what happens if blocks have already a similar type attribute, but its values are not strictly among
image|video|audio. This is not the case for the core blocks, but something that we might need to consider for 3rd party development.