lzb/import/block
Runs after one block has been imported from a JSON file, which is where an imported block gets a term, a namespace of its own or a note about where it came from.
It fires at the end of import_block(), after wp_insert_post() has created the lazyblocks post and after every lazyblocks_ meta has been written. The post exists and is published, so update_post_meta(), wp_set_object_terms() and wp_update_post() all work on $post_id straight away. Importing a file with ten blocks fires the action ten times.
Attributes
| Name | Type | Description |
|---|---|---|
$post_id | Int | post ID of the block just created |
$data | Array | the block as it appeared in the file |
$data is the exported block, not the stored meta. It has id, title, icon, keywords, slug, description, category, category_label, supports, controls, code, style, script, styles and condition. Two of those do not match what was saved. id is the post ID the block had on the site it was exported from, and slug carries the full name with its namespace while the stored lazyblocks_slug meta holds only the part after the slash.
Usage
function my_lzb_import_block( $post_id, $data ) {
// Move imported blocks into the agency's own namespace, so they cannot
// collide with blocks the client builds later.
$name = substr( $data['slug'], strpos( $data['slug'], '/' ) + 1 );
update_post_meta( $post_id, 'lazyblocks_slug', 'agency/' . $name );
update_post_meta( $post_id, 'imported_from_id', $data['id'] );
}
add_action( 'lzb/import/block', 'my_lzb_import_block', 10, 2 );Write the slug back through lazyblocks_slug without a namespace if you want the default lazyblock/ one, because Lazy Blocks prepends it when the block data is assembled. Three meta keys cannot be written from here at all. lazyblocks_code_editor_html, lazyblocks_code_frontend_html and lazyblocks_script_view are guarded against direct meta writes and silently rejected for anyone without the unfiltered_html capability, which on a multisite install is everyone below super admin. lzb/allow_unfiltered_html is the override.
To rewrite the payload before any post is inserted, use lzb/import_json. The template equivalent of this action is lzb/import/template.