Automattic\WooCommerce\Internal\CLI\Migrator\Lib
ImportSession::create
Creates a new import session.
Method of the class: ImportSession{}
No Hooks.
Returns
ImportSession. The created ImportSession instance.
Usage
$result = ImportSession::create( $args );
- $args(array) (required)
.
-
data_source(string)
The data source (e.g. 'wxr_file', 'wxr_url', 'markdown_zip') -
source_url(string)
Optional. URL of the source file for remote imports -
attachment_id(int)
Optional. ID of the uploaded file attachment - file_name(string)
Optional. Original name of the uploaded file
-
ImportSession::create() ImportSession::create code WC 10.7.0
public static function create( $args ) {
// Validate the required arguments for each data source.
// @TODO: Leave it up to filters to make it extendable.
switch ( $args['data_source'] ) {
case 'wxr_file':
if ( empty( $args['file_name'] ) ) {
throw new \Exception( 'File name is required for WXR file imports' );
}
break;
case 'wxr_url':
if ( empty( $args['source_url'] ) ) {
throw new \Exception( 'Source URL is required for remote imports' );
}
break;
case 'markdown_zip':
if ( empty( $args['file_name'] ) ) {
throw new \Exception( 'File name is required for Markdown ZIP imports' );
}
break;
case 'local_directory':
if ( empty( $args['file_name'] ) ) {
throw new \Exception( 'Directory path is required for local directory imports' );
}
break;
}
$post_id = wp_insert_post(
array(
'post_type' => self::POST_TYPE,
'post_status' => 'publish',
'post_title' => sprintf(
'Import from %s - %s',
$args['data_source'],
$args['file_name'] ?? $args['source_url'] ?? 'Unknown source'
),
'meta_input' => array(
'data_source' => $args['data_source'],
'started_at' => time(),
'file_name' => $args['file_name'] ?? null,
'source_url' => $args['source_url'] ?? null,
'attachment_id' => $args['attachment_id'] ?? null,
),
),
true
);
if ( is_wp_error( $post_id ) ) {
throw new \Exception( 'Error creating an import session: ' . $post_id->get_error_message() );
}
if ( ! empty( $args['attachment_id'] ) ) {
wp_update_post(
array(
'ID' => $post_id,
'post_parent' => $args['attachment_id'],
)
);
}
return new self( $post_id );
}