-
Notifications
You must be signed in to change notification settings - Fork 7
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
Default attributes (e.g., "anchor") not available in server-side context #69
Comments
Did some research on this today: Where are attributes defined?From @Zamfi99's report here on the
This definition comes from const ANCHOR_SCHEMA = {
type: 'string',
source: 'attribute',
attribute: 'id',
selector: '*',
}; As discussed above and in linked issues, this definition is only available client-side. Although we can pull the What other block support attributes exist?I tried making a block that had the full set of block supports enabled: Toggle
|
Block with no supports | Block with every support |
{
"lock": {
"type": "object"
},
"metadata": {
"type": "object"
},
"className": {
"type": "string"
}
} |
{
"lock": {
"type": "object"
},
"metadata": {
"type": "object"
},
"align": {
"type": "string",
"enum": [
"left",
"center",
"right",
"wide",
"full",
""
]
},
"className": {
"type": "string"
},
"style": {
"type": "object"
},
"backgroundColor": {
"type": "string"
},
"textColor": {
"type": "string"
},
"gradient": {
"type": "string"
},
"fontSize": {
"type": "string"
},
"layout": {
"type": "object"
},
"anchor": {
"type": "string",
"source": "attribute",
"attribute": "id",
"selector": "*"
},
"ariaLabel": {
"type": "string",
"source": "attribute",
"attribute": "aria-label",
"selector": "*"
}
} |
There's a big difference between the two lists. However, we can ignore any properties without a source
. These properties are passed directly in the block header, which we get from parse_blocks() and forward to block output. Unsourced attributes will be represented in the Block Data API correctly.
Ignoring those, there are two remaining sourced attributes:
{
"anchor": {
"type": "string",
"source": "attribute",
"attribute": "id",
"selector": "*"
},
"ariaLabel": {
"type": "string",
"source": "attribute",
"attribute": "aria-label",
"selector": "*"
}
}
The anchor
attribute can be tested in the Gutenberg editor under block settings sidebar -> "Advanced" dropdown -> "HTML ANCHOR" section. I wasn't able to find an ariaLabel
UI, but manually adding a aria-label="some text"
attribute to the root node of the block persisted correctly without errors.
Conclusion
As far as I can tell, there are only two block supports that use client-side only defined attributes. Here are some possible options for us:
-
Add manual support for
anchor
andariaLabel
on the server-side parser. We could check to see if a block has those supports enabled, and then add these attribute definitions to a block's properties. The upside is that this would solve the problem and there are only two known missing properties. The downside is that this would require manual updates to keep in sync with Gutenberg, and those definitions will likely change. Due to Gutenberg's development velocity this will almost certainly break support for some attributes in the future or overlook support for new attributes. -
Add a way to enable support for these attributes via a filter. We could provide a code example that provides support for these attributes, but it would be on the plugin's users to fix if the example code goes out of sync. The upside is this would give customers an easy way to address this issue, but the downside would be the same as those above, just with the maintenance burden shifted to our customers instead of us.
-
Add Gutenberg-level support for block supports in PHP. Ideally, when we call
WP_Block_Type_Registry::get_instance()
, it should also know which supports are provided for that block and fill in the corresponding attribute information server-side. This would completely solve the issue for us and our customers, but would require some buy-in and changes to Gutenberg / WordPress core before the issue would be fixed.
@smithjw1, @ingeniumed Any thoughts here or other ideas? Thank you!
@Zamfi99 to help with your original issue in #68, here is some filter code that can be used to "officially" register add_action( 'init', function () {
$block_registry = \WP_Block_Type_Registry::get_instance();
$registered_blocks = $block_registry->get_all_registered();
foreach ( $registered_blocks as $block_name => $block_type ) {
$additional_block_attributes = [];
if ( isset( $block_type->supports['anchor'] ) && $block_type->supports['anchor'] ) {
$additional_block_attributes['anchor'] = [
'type' => 'string',
'source' => 'attribute',
'attribute' => 'id',
'selector' => '*',
];
}
if ( isset( $block_type->supports['ariaLabel'] ) && $block_type->supports['ariaLabel'] ) {
$additional_block_attributes['ariaLabel'] = [
'type' => 'string',
'source' => 'attribute',
'attribute' => 'aria-label',
'selector' => '*',
];
}
if ( ! empty( $additional_block_attributes ) ) {
// Re-register block with support attributes
$block_registry->unregister( $block_name );
$block_type->attributes = array_merge( $block_type->attributes, $additional_block_attributes );
$block_registry->register( $block_type );
}
}
} ); This will loop through registered blocks, find add_filter( 'vip_block_data_api__before_parse_post_content', function ( $post_content ) {
$block_registry = \WP_Block_Type_Registry::get_instance();
$registered_blocks = $block_registry->get_all_registered();
foreach ( $registered_blocks as $block_name => $block_type ) {
/* ... */
}
return $post_content;
} ); I've tested this code with an image: <!-- wp:image {"id":39,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large" id="custom-anchor"><img src="http://my.site/wp-content/uploads/image.jpg" alt="" class="wp-image-39"/></figure>
<!-- /wp:image --> and the result correctly holds the anchor attribute: {
"name": "core/image",
"attributes": {
"id": 39,
"sizeSlug": "large",
"linkDestination": "none",
"url": "http://my.site/wp-content/uploads/image.jpg",
"alt": "",
"anchor": "custom-anchor",
"width": 1024,
"height": 683
}
} I also tested that As discussed above, this may or may not be added to this plugin or WordPress core in the feature. Hopefully this code can be helpful for the present while we work that out. |
I'm closing this issue for the present. Please see the above comment for instructions on how to add |
Describe the bug
Currently, there is a significant issue with default attributes, such as the anchor attribute. These attributes are not available in the server-side context, causing them to be missing in the API response. This discrepancy can lead to functionality problems and inconsistencies when handling blocks, particularly those relying on default attributes for proper rendering and behavior.
To Reproduce
Expected behavior
Default attributes, such as anchor should be consistently included in the API response.
Actual behavior
Specific default attributes, such as ancho are currently missing from the API response.
Client-side registered attributes may be included in the API response if their values differ from the default settings.
The text was updated successfully, but these errors were encountered: