admin管理员组文章数量:1389924
I am writing a migration script which has to read the post_content of posts and then dynamically change some attributes of some custom Gutenberg blocks.
I was able to read the post_content and then convert them into block objects by using the parse_blocks
function. I was also able to dynamically change the attributes of the custom blocks by manipulating the block objects.
But I am not able to convert these block objects into the special HTML comments that Gutenberg uses to serialize them so that I can update the post_content.
I found that the PHP part of WordPress core only has parse_blocks
function to parse the special HTML comments into block objects and render_block
function to render the blocks, but there is no serialize_block
function.
I found that in JavaScript there is a function called serializeBlock
which does this. But is there an equivalent of it in PHP which I can call from my migration scripts?
I am writing a migration script which has to read the post_content of posts and then dynamically change some attributes of some custom Gutenberg blocks.
I was able to read the post_content and then convert them into block objects by using the parse_blocks
function. I was also able to dynamically change the attributes of the custom blocks by manipulating the block objects.
But I am not able to convert these block objects into the special HTML comments that Gutenberg uses to serialize them so that I can update the post_content.
I found that the PHP part of WordPress core only has parse_blocks
function to parse the special HTML comments into block objects and render_block
function to render the blocks, but there is no serialize_block
function.
I found that in JavaScript there is a function called serializeBlock
which does this. But is there an equivalent of it in PHP which I can call from my migration scripts?
2 Answers
Reset to default 5This markup is generated on the js side of things and saved in the content of the block editor, which is why there doesn't seem to be a native PHP function for this.
However, I found a PHP method that does exactly this in an "experimental" class in the Gutenberg plugin. You can see this here: https://github/WordPress/gutenberg/blob/master/lib/class-experimental-wp-widget-blocks-manager.php#L265
You could add it as a method in your own class or convert to a standard function like so:
/**
* Serializes a block.
*
* @param array $block Block object.
* @return string String representing the block.
*/
function serialize_block( $block ) {
if ( ! isset( $block['blockName'] ) ) {
return false;
}
$name = $block['blockName'];
if ( 0 === strpos( $name, 'core/' ) ) {
$name = substr( $name, strlen( 'core/' ) );
}
if ( empty( $block['attrs'] ) ) {
$opening_tag_suffix = '';
} else {
$opening_tag_suffix = ' ' . json_encode( $block['attrs'] );
}
if ( empty( $block['innerHTML'] ) ) {
return sprintf(
'<!-- wp:%s%s /-->',
$name,
$opening_tag_suffix
);
} else {
return sprintf(
'<!-- wp:%1$s%2$s -->%3$s<!-- /wp:%1$s -->',
$name,
$opening_tag_suffix,
$block['innerHTML']
);
}
}
March 2020 update: Looks like serialize_block()
is included with WP since 5.3.1, though i think it's undocumented right now. Here's the source on Trac. The docstring says:
/* [...]
*
* Returns the content of a block, including comment delimiters, serializing all
* attributes from the given parsed block.
*
* This should be used when preparing a block to be saved to post content.
* Prefer `render_block` when preparing a block for display. Unlike
* `render_block`, this does not evaluate a block's `render_callback`, and will
* instead preserve the markup as parsed.
*/
It seems to work fine from a few simple tests I did, but I'm not sure if it's intended for public usage yet, because there's also this Trac ticket with a different implementation (marked as "awaiting review" as of 2020.03.20): #47375 - Blocks API: Add server-side serialize_block()
本文标签: How to serialize a Gutenberg block from block attributes in PHP
版权声明:本文标题:How to serialize a Gutenberg block from block attributes in PHP? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744653841a2617845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论