Automattic\WooCommerce\Admin\Features\ProductBlockEditor
ProductFormsController::migrate_product_form_posts()
Create or update a product_form post for each product form template. If the post already exists, it will be updated. If the post does not exist, it will be created even if the action is update.
Method of the class: ProductFormsController{}
Hooks from the method
Return
null
. Nothing (null).
Usage
$ProductFormsController = new ProductFormsController(); $ProductFormsController->migrate_product_form_posts( $action );
- $action(string) (required)
- - The action to perform. insert | update.
ProductFormsController::migrate_product_form_posts() ProductFormsController::migrate product form posts code WC 9.7.1
public function migrate_product_form_posts( $action ) { /** * Allow extend the list of templates that should be auto-generated. * * @since 9.1.0 * @param array $templates List of templates to auto-generate. */ $templates = apply_filters( 'woocommerce_product_form_templates', $this->product_form_templates ); foreach ( $templates as $slug ) { $file_path = BlockTemplateUtils::get_block_template_path( $slug ); if ( ! $file_path ) { continue; } $file_data = BlockTemplateUtils::get_template_file_data( $file_path ); $posts = get_posts( array( 'name' => $slug, 'post_type' => 'product_form', 'post_status' => 'any', 'posts_per_page' => 1, ) ); /* * Update the the CPT post if it already exists, * and the action is `update`. */ if ( 'update' === $action ) { $post = $posts[0] ?? null; if ( ! empty( $post ) ) { wp_update_post( array( 'ID' => $post->ID, 'post_title' => $file_data['title'], 'post_content' => BlockTemplateUtils::get_template_content( $file_path ), 'post_excerpt' => $file_data['description'], ) ); } } /* * Skip the post creation if the post already exists. */ if ( ! empty( $posts ) ) { continue; } $post = wp_insert_post( array( 'post_title' => $file_data['title'], 'post_name' => $slug, 'post_status' => 'publish', 'post_type' => 'product_form', 'post_content' => BlockTemplateUtils::get_template_content( $file_path ), 'post_excerpt' => $file_data['description'], ) ); } }