-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathclass-command.php
143 lines (118 loc) · 3.84 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace WP_Parser;
use WP_CLI;
use WP_CLI_Command;
/**
* Converts PHPDoc markup into a template ready for import to a WordPress blog.
*/
class Command extends WP_CLI_Command {
/**
* Generate a JSON file containing the PHPDoc markup, and save to filesystem.
*
* @synopsis <directory> [<output_file>]
*
* @param array $args
*/
public function export( $args ) {
$directory = realpath( $args[0] );
$output_file = empty( $args[1] ) ? 'phpdoc.json' : $args[1];
$json = $this->_get_phpdoc_data( $directory );
$result = file_put_contents( $output_file, $json );
WP_CLI::line();
if ( false === $result ) {
WP_CLI::error( sprintf( 'Problem writing %1$s bytes of data to %2$s', strlen( $json ), $output_file ) );
exit;
}
WP_CLI::success( sprintf( 'Data exported to %1$s', $output_file ) );
WP_CLI::line();
}
/**
* Read a JSON file containing the PHPDoc markup, convert it into WordPress posts, and insert into DB.
*
* @synopsis <file> [--quick] [--import-internal]
*
* @param array $args
* @param array $assoc_args
*/
public function import( $args, $assoc_args ) {
list( $file ) = $args;
WP_CLI::line();
// Get the data from the <file>, and check it's valid.
$phpdoc = false;
if ( is_readable( $file ) ) {
$phpdoc = file_get_contents( $file );
}
if ( ! $phpdoc ) {
WP_CLI::error( sprintf( "Can't read %1\$s. Does the file exist?", $file ) );
exit;
}
$phpdoc = json_decode( $phpdoc, true );
if ( is_null( $phpdoc ) ) {
WP_CLI::error( sprintf( "JSON in %1\$s can't be decoded :(", $file ) );
exit;
}
// Import data
$this->_do_import( $phpdoc, isset( $assoc_args['quick'] ), isset( $assoc_args['import-internal'] ) );
}
/**
* Generate JSON containing the PHPDoc markup, convert it into WordPress posts, and insert into DB.
*
* @subcommand create
* @synopsis <directory> [--quick] [--import-internal] [--user]
*
* @param array $args
* @param array $assoc_args
*/
public function create( $args, $assoc_args ) {
list( $directory ) = $args;
$directory = realpath( $directory );
if ( empty( $directory ) ) {
WP_CLI::error( sprintf( "Can't read %1\$s. Does the file exist?", $directory ) );
exit;
}
WP_CLI::line();
// Import data
$this->_do_import( $this->_get_phpdoc_data( $directory, 'array' ), isset( $assoc_args['quick'] ), isset( $assoc_args['import-internal'] ) );
}
/**
* Generate the data from the PHPDoc markup.
*
* @param string $path Directory or file to scan for PHPDoc
* @param string $format What format the data is returned in: [json|array].
*
* @return string|array
*/
protected function _get_phpdoc_data( $path, $format = 'json' ) {
WP_CLI::line( sprintf( 'Extracting PHPDoc from %1$s. This may take a few minutes...', $path ) );
$is_file = is_file( $path );
$files = $is_file ? array( $path ) : get_wp_files( $path );
$path = $is_file ? dirname( $path ) : $path;
if ( $files instanceof \WP_Error ) {
WP_CLI::error( sprintf( 'Problem with %1$s: %2$s', $path, $files->get_error_message() ) );
exit;
}
$output = parse_files( $files, $path );
if ( 'json' == $format ) {
return json_encode( $output, JSON_PRETTY_PRINT );
}
return $output;
}
/**
* Import the PHPDoc $data into WordPress posts and taxonomies
*
* @param array $data
* @param bool $skip_sleep If true, the sleep() calls are skipped.
* @param bool $import_ignored If true, functions marked `@ignore` will be imported.
*/
protected function _do_import( array $data, $skip_sleep = true, $import_ignored = false ) {
if ( ! wp_get_current_user()->exists() ) {
WP_CLI::error( 'Please specify a valid user: --user=<id|login>' );
exit;
}
// Run the importer
$importer = new Importer;
$importer->setLogger( new WP_CLI_Logger() );
$importer->import( $data, $skip_sleep, $import_ignored );
WP_CLI::line();
}
}