Inventory_Presser_Blocks::create_block_description( WP_Post $post )
Adds a Description Block to the post when a vehicle is opened in the block editor.
On This Page
Parameters Parameters
- $post
-
(WP_Post) (Required) The post loaded into the block editor.
Return Return
(void)
Source Source
File: includes/class-blocks.php
public function create_block_description( $post ) {
global $pagenow;
// Is the user adding or editing a vehicle?
if ( ! is_admin()
|| get_post_type() !== INVP::POST_TYPE
|| ( 'post-new.php' !== $pagenow && 'post.php' !== $pagenow ) ) {
// No.
return;
}
// Does the post content contain a Description block?
$blocks = parse_blocks( $post->post_content );
foreach ( $blocks as $block ) {
// Is this a Description block?
if ( ! empty( $block['blockName'] ) && 'inventory-presser/description' === $block['blockName'] ) {
// Yes. The post already has a Description block.
return;
}
}
// The post does not have a Description block. Add one.
$blocks[] = array(
'blockName' => 'inventory-presser/description',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
);
$post->post_content = serialize_blocks( $blocks );
wp_update_post( $post );
}
Expand full source codeCollapse full source codeView on Github