Site icon Hip-Hop Website Design and Development

Are block templates incompatible with serialize_blocks?

I need to dynamically create posts from arbitrary content using wp_insert_post() and wp_update_post(). The post type I’m working with has a simple block template associated with it, so my goal is to get the block template attached to the post type object, insert my post content into the template using innerContent, and then use serialize_blocks to turn the template into renderable post_content markup (e.g. with markup).

My problem is that block templates are defined in PHP as indexed arrays:

$post_object->template = array(
   array( 'core/image', array(
        'align' => 'left',
   ) ),
   array( 'core/heading', array(
        'placeholder' => 'Add Author...',
   ) ),
   array( 'core/paragraph', array(
        'placeholder' => 'Add Description...',
    ) ),
);

While serialize_blocks() appears to require associative arrays similar to what parse_blocks returns:

$blocks = parse_blocks( $post->post_content );
var_dump( $blocks );
// array(
//   array( 'blockName' => 'core/image', 'attrs' => array(
//      'align' => 'left',
// ) ),
//  array( 'blockName' => 'core/heading', 'attrs' => array(
//       'placeholder' => 'Add Author...',
// ) ),
// array( 'blockName' => 'core/paragraph', 'attrs' => array(
//      'placeholder' => 'Add Description...',
//  ) ),
//);

Attempting to define a block template as an associative array breaks the editor, and attempting to serialize blocks using an indexed array results in undefined index and invalid argument warnings from wp-includes/blocks.php with nothing being returned by the function.

Is there something I’m missing here? Some other intermediary function to transpose an indexed block template array into the associative array format that parse_blocks returns?