forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wp-cli.php
240 lines (206 loc) · 4.99 KB
/
class-wp-cli.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Wrapper class for WP-CLI
*
* @package wp-cli
* @author Andreas Creten
*/
class WP_CLI {
static $commands = array();
/**
* Add a command to the wp-cli list of commands
*
* @param string $name The name of the command that will be used in the cli
* @param string $class The class to manage the command
* @return void
*/
public function addCommand($name, $class) {
self::$commands[$name] = $class;
}
/**
* Display a message in the cli
*
* @param string $message
* @return void
*/
static function out($message) {
\cli\out($message);
}
/**
* Display a message in the CLI and end with a newline
*
* @param string $message
* @return void
*/
static function line($message = '') {
\cli\line($message);
}
/**
* Display an error in the CLI and end with a newline
*
* @param string $message
* @param string $label
* @return void
*/
static function error($message, $label = 'Error') {
\cli\line('%R'.$label.': %n'.self::errorToString($message));
}
/**
* Display a success in the CLI and end with a newline
*
* @param string $message
* @param string $label
* @return void
*/
static function success($message, $label = 'Success') {
\cli\line('%G'.$label.': %n'.$message);
}
/**
* Display a warning in the CLI and end with a newline
*
* @param string $message
* @param string $label
* @return void
*/
static function warning($message, $label = 'Warning') {
\cli\line('%C'.$label.': %n'.$message);
}
/**
* Convert a wp_error into a String
*
* @param mixed $errors
* @return string
*/
static function errorToString($errors) {
if(is_string($errors)){
return $errors;
}
elseif(is_wp_error($errors) && $errors->get_error_code()){
foreach($errors->get_error_messages() as $message){
if($errors->get_error_data() )
return $message . ' ' . $errors->get_error_data();
else
return $message;
}
}
}
/**
* Display the help function for the wp-cli
*
* @return void
*/
static function generalHelp() {
self::line('Example usage:');
foreach(self::$commands as $name => $command) {
self::out(' wp '.$name);
$methods = WP_CLI_Command::getMethods($command);
if(!empty($methods)) {
self::out(' ['.implode('|', $methods).']');
}
self::line(' ...');
}
self::block( <<<EOB
Built-in commands:
core Update the WordPress core
home Open the wp-cli project on Github
option Manipulate the WordPress options
plugin Do cool things with the installed plugins
theme Do cool things with the installed themes
See 'wp <command> help' for more information on a specific command.
EOB
);
}
/**
* Display multiple lines of content
*
* @param string $content
* @return void
*/
function block( $content ) {
foreach ( explode( "\n", $content ) as $line )
self::line( $line );
}
/**
* Display a legend
*
* @param array( code => title ) $legend
* @return void
*/
static function legend($legend) {
$legend['%yU'] = 'Update Available';
$legend_line = array();
foreach ( $legend as $key => $title )
$legend_line[] = "$key = $title%n";
WP_CLI::line( 'Legend: ' . implode( ', ', $legend_line ) );
}
/**
* Return the beginning of the status line for a certain plugin or theme
*
* @param string $item The plugin or theme name
* @param string $key The transient key
*
* @return string
*/
static function get_update_status( $item, $key ) {
$update_list = get_site_transient( $key );
if ( isset( $update_list->response[ $item ] ) )
return ' %yU%n';
return ' ';
}
}
/**
* A Upgrader Skin for WordPress that only generates plain-text
*
* @package wp-cli
* @author Andreas Creten
*/
class CLI_Upgrader_Skin {
var $upgrader;
var $done_header = false;
var $result = false;
function __construct($args = array()) {
$defaults = array('url' => '', 'nonce' => '', 'title' => '', 'context' => false);
$this->options = wp_parse_args($args, $defaults);
}
function set_upgrader(&$upgrader) {
if(is_object($upgrader)) {
$this->upgrader =& $upgrader;
}
$this->add_strings();
}
function add_strings() {}
function set_result($result) {
$this->result = $result;
}
function request_filesystem_credentials($error = false) {
$url = $this->options['url'];
$context = $this->options['context'];
if(!empty($this->options['nonce'])) {
$url = wp_nonce_url($url, $this->options['nonce']);
}
// Possible to bring inline, Leaving as is for now.
return request_filesystem_credentials($url, '', $error, $context);
}
function header() {}
function footer() {}
function error($errors) {
$this->feedback(WP_CLI::errorToString($errors));
}
function feedback($string) {
if(isset( $this->upgrader->strings[$string]))
$string = $this->upgrader->strings[$string];
if(strpos($string, '%') !== false) {
$args = func_get_args();
$args = array_splice($args, 1);
if(!empty($args)) {
$string = vsprintf($string, $args);
}
}
if(empty($string)) {
return;
}
echo $string;
}
function before() {}
function after() {}
}