forked from humanmade/WordPress-Importer
-
Notifications
You must be signed in to change notification settings - Fork 5
/
class-command.php
58 lines (53 loc) · 1.59 KB
/
class-command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
class WXR_Import_Command extends WP_CLI_Command {
/**
* Import content from a WXR file.
*
* ## OPTIONS
*
* <file>...
* : Path to one or more valid WXR files for importing. Directories are also accepted.
*
* [--verbose[=<level>]]
* : Should we print verbose statements?
* (No value for 'info'; or one of 'emergency', 'alert', 'critical',
* 'error', 'warning', 'notice', 'info', 'debug')
*
* [--default-author=<id>]
* : Default author ID to use if invalid user is found in the import data.
*/
public function import( $args, $assoc_args ) {
$logger = new WP_Importer_Logger_CLI();
if ( ! empty( $assoc_args['verbose'] ) ) {
if ( $assoc_args['verbose'] === true ) {
$logger->min_level = 'info';
} else {
$valid = $logger->level_to_numeric( $assoc_args['verbose'] );
if ( ! $valid ) {
WP_CLI::error( 'Invalid verbosity level' );
return;
}
$logger->min_level = $assoc_args['verbose'];
}
}
$path = realpath( $args[0] );
if ( ! $path ) {
WP_CLI::error( sprintf( 'Specified file %s does not exist', $args[0] ) );
}
$options = array(
'fetch_attachments' => true,
);
if ( isset( $assoc_args['default-author'] ) ) {
$options['default_author'] = absint( $assoc_args['default-author'] );
if ( ! get_user_by( 'ID', $options['default_author'] ) ) {
WP_CLI::error( 'Invalid default author ID specified.' );
}
}
$importer = new WXR_Importer( $options );
$importer->set_logger( $logger );
$result = $importer->import( $path );
if ( is_wp_error( $result ) ) {
WP_CLI::error( $result->get_error_message() );
}
}
}