-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.php
537 lines (471 loc) · 12.5 KB
/
base.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
namespace Example_WP_CLI\CLI;
use WP_CLI;
use WP_CLI\Utils as Utils;
/**
* CLI Framework Base class.
*/
class Base {
/**
* Arguments passed to command.
*
* @var array
*/
public $args = array();
/**
* Parameters passed to command.
*
* @var array
*/
public $assoc_args = array();
/**
* Whether command is requestiong verbose output.
* Requires passing --verbose along with the command.
*
* @var boolean
*/
public $verbose = false;
/**
* Whether command is requestiong silent output.
* Requires passing --silent along with the command.
*
* @var boolean
*/
public $silent = false;
/**
* The directory for the CLI log files.
*
* @var string
*/
protected static $log_dir = '';
/**
* The file name pattern for the log files.
*
* @var string
*/
protected static $log_file_name = 'cli-log.log';
/**
* Full log file path/filename.
*
* @var string
*/
protected static $log_file = '';
/**
* Whether to store CLI logs to the file system.
*
* @var string
*/
protected static $store_logs = true;
/**
* Constructor
*
* @since 0.1.0
*
* @param array $args Arguments passed to command.
* @param array $assoc_args Parameters passed to command.
* @param string $log_dir The directory for the CLI log files.
* @param string $log_file_name The file name pattern for the log files.
*/
public function __construct( $args, $assoc_args, $log_dir = null, $log_file_name = null ) {
$this->set_args( $args, $assoc_args );
self::set_log_dir( $log_dir );
self::set_log_file( $log_file_name );
}
/**
* Message Methods
*/
/**
* Outputs a WP_CLI success message as well as logs the message.
*
* @since 0.1.0
*
* @param string $msg The message to relay.
* @param mixed $data Optional data to print out.
*
* @return Base for chaining.
*/
public function success_message_log( $msg, $data = null ) {
return $this->success_message( $msg, $data )->write_log( $msg, $data );
}
/**
* Outputs a WP_CLI success message.
*
* @since 0.1.0
*
* @param string $msg The message to relay.
* @param mixed $data Optional data to print out.
*
* @return Base for chaining.
*/
public function success_message( $msg, $data = null ) {
WP_CLI::success( $data ? $msg . ': ' . print_r( $data, true ) : $msg );
return $this;
}
/**
* Outputs a WP_CLI message if the --verbose flag is set, as well as logs the message.
*
* @since 0.1.0
*
* @param string $msg The message to relay.
* @param mixed $data Optional data to print out.
*
* @return Base for chaining.
*/
public function verbose_message_log( $msg, $data = null ) {
return $this->verbose_message( $msg, $data )->write_log( $msg, $data );
}
/**
* Outputs a WP_CLI message if the --verbose flag is set.
*
* @since 0.1.0
*
* @param string $msg The message to relay.
* @param mixed $data Optional data to print out.
* @param string $method Which WP_CLI method to use for messaging.
*
* @return Base for chaining.
*/
public function verbose_message( $msg, $data = null, $method = 'line' ) {
if ( $this->verbose ) {
WP_CLI::{$method}( $data ? $msg . ': ' . print_r( $data, true ) : $msg );
}
return $this;
}
/**
* Outputs a WP_CLI error message if the --silent flag is set, and logs the message either way.
*
* @since 0.1.0
*
* @param string $msg The message to relay.
* @param mixed $data Optional data to print out.
*
* @return Base for chaining.
*/
public function silent_error_log( $msg, $data = null ) {
return $this->silent_log( $msg, $data, 'error' );
}
/**
* Outputs a WP_CLI message if the --silent flag is set, and logs the message either way.
*
* @since 0.1.0
*
* @param string $msg The message to relay.
* @param mixed $data Optional data to print out.
* @param string $method Which WP_CLI method to use for messaging.
*
* @return Base for chaining.
*/
public function silent_log( $msg, $data = null, $method = 'line' ) {
if ( $this->silent ) {
$this->write_log( $msg, $data );
} else {
$this->message_log( $msg, $data, $method );
}
return $this;
}
/**
* Outputs a WP_CLI error message as well as logs the error.
*
* @since 0.1.0
*
* @param string $msg The message to relay.
* @param mixed $data Optional data to print out.
*
* @return Base for chaining.
*/
public function error_message_log( $msg, $data = null ) {
return $this->error_message( $msg, $data, $method )->write_log( $msg, $data );
}
/**
* Outputs a WP_CLI error message.
*
* @since 0.1.0
*
* @param string $msg The message to relay.
* @param mixed $data Optional data to print out.
*
* @return Base for chaining.
*/
public function error_message( $msg, $data = null ) {
WP_CLI::error( $data ? $msg . ': ' . print_r( $data, true ) : $msg );
return $this;
}
/**
* Outputs a WP_CLI message as well as logs the message.
*
* @since 0.1.0
*
* @param string $msg The message to relay.
* @param mixed $data Optional data to print out.
* @param string $method Which WP_CLI method to use for messaging.
*
* @return Base for chaining.
*/
public function message_log( $msg, $data = null, $method = 'line' ) {
WP_CLI::{$method}( $data ? $msg . ': ' . print_r( $data, true ) : $msg );
return $this->write_log( $msg, $data );
}
/**
* Logging Methods
*/
/**
* Sends a message to the log file.
*
* @since 0.1.0
*
* @param string $msg The message to relay.
* @param mixed $data Optional data to print out.
*
* @return Base for chaining.
*/
public function write_log( $msg, $data = null ) {
if ( self::$store_logs ) {
$this->check_log();
$msg .= $data ? ': ' . print_r( $data, true ) : '';
// Cannot use $wp_filesystem->put_contents() as it does not provide a way to APPEND content to a file.
file_put_contents( self::$log_file, '[' . date( 'd-M-Y h:i:s A', current_time( 'timestamp' ) ) . '] ' . $msg . "\n", FILE_APPEND );
}
return $this;
}
/**
* Empties the wpcli log file.
*
* @since 0.1.0
*
* @return Base for chaining.
*/
public function empty_log() {
if ( self::$store_logs ) {
self::file_system()->put_contents( self::$log_file, "\n" );
$this->success_message( 'Log emptied.' );
} else {
$this->error_message( 'Log storage disabled.' );
}
return $this;
}
/**
* Deletes all the wpcli log files.
*
* @since 0.1.0
*
* @return Base for chaining.
*/
public function delete_logs() {
if ( self::$store_logs ) {
$this->confirm( 'Are you sure you want to delete the logs?', $this->assoc_args );
foreach ( glob( self::$log_dir . '*.log' ) as $log_file ) {
self::file_system()->delete( $log_file );
}
$this->success_message( 'Logs deleted.' );
} else {
$this->error_message( 'Log storage disabled.' );
}
return $this;
}
/**
* Creates a renamed backup of the wpcli log file.
*
* @since 0.1.0
*
* @return Base for chaining.
*/
public function backup_log() {
if ( self::$store_logs ) {
$this->check_log();
$file = self::$log_file;
$count = 1;
while ( file_exists( str_replace( '.log', '-' . $count . '.log', $file ) ) ) {
$count++;
}
$new_file = str_replace( '.log', '-' . $count . '.log', $file );
if ( ! copy( $file, $new_file ) ) {
WP_CLI::error( "Failed to copy {$new_file}..." );
}
$this->success_message( "Log backed up: {$new_file}" );
}
return $this;
}
/**
* Checks if the log directory/file exists and creates them.
*
* @since 0.1.0
*
* @return Base for chaining.
*/
public function check_log() {
if ( self::$store_logs ) {
if ( ! self::file_system()->exists( self::$log_dir ) ) {
self::file_system()->mkdir( self::$log_dir );
}
if ( ! self::file_system()->exists( self::$log_file ) ) {
self::file_system()->touch( self::$log_file );
}
}
return $this;
}
/**
* WPCLI Proxy Methods
*/
/**
* Progress Bar for WP_CLI
*
* @param int $param Either the count to initiate, or a progress event (tick or finish)
* @param string $action Action being performed
*
* @return \cli\progress\Bar Progress bar object.
*/
public function progress_bar( $param = 0, $action = 'Migrating', $items = 'Rows' ) {
if ( $param && is_numeric( $param ) ) {
$this->progress_bar = Utils\make_progress_bar( "$action $param $items", $param );
} elseif ( 'tick' === $param ) {
$this->progress_bar->tick();
} elseif ( 'finish' === $param ) {
$this->progress_bar->finish();
}
return $this->progress_bar;
}
/**
* Pauses execution temporarily, and clears WordPress internal object caches.
*
* In long-running scripts, the internal caches on `$wp_object_cache` and `$wpdb`
* can grow to consume gigabytes of memory. Periodically calling this utility
* can help with memory management.
*
* See WP_CLI\Utils\wp_clear_object_cache() (https://github.com/wp-cli/wp-cli/blob/master/php/utils-wp.php)
*
* @since 0.1.0
*
* @param integer $sleep_time Amount of time to pause execution. Potentially help things cool down.
*
* @return void
*/
public static function stop_the_insanity( $sleep_time = 0 ) {
if ( $sleep_time ) {
sleep( $sleep_time );
}
Utils\wp_clear_object_cache();
}
/**
* Proxy method for WP_CLI::confirm(). Passes in the $assoc_args property.
*
* @since 0.1.0
*
* @param string $question The confirmation question.
*
* @return mixed Exits excection if "no" is passed.
*/
public function confirm( $question ) {
return WP_CLI::confirm( $question, $this->assoc_args );
}
/**
* Checks the $assoc_args array for given flag.
*
* @since 0.1.0
*
* @param mixed $flag The flag to check.
* @param mixed $default The default/fallback value for the flag.
*
* @return mixed The value for that flag, or the fallback default.
*/
public function get_flag_value( $flag, $default = null ) {
return Utils\get_flag_value( $this->assoc_args, $flag, $default );
}
/**
* Utilities
*/
/**
* Sets the $args, $assoc_args, $verbose, and $silent properties.
*
* @since 0.1.0
*
* @param array $args Arguments passed to command.
* @param array $assoc_args Parameters passed to command.
*
* @return Base for chaining.
*/
public function set_args( $args, $assoc_args = array() ) {
$this->args = $args;
$this->assoc_args = $assoc_args;
$this->verbose = $this->get_flag_value( 'verbose', false );
$this->silent = $this->get_flag_value( 'silent', true );
return $this;
}
/**
* Sets the wpcli log directory
*
* @since 0.1.0
*
* @param string $log_dir The log directory. Defaults to creating a "logs" directory in the current.
*
* @return $log_dir The set log directory.
*/
public static function set_log_dir( $log_dir = null ) {
self::$log_dir = trailingslashit( null === $log_dir ? __DIR__ . '/logs' : $log_dir );
return self::$log_dir;
}
/**
* Sets the wpcli log file name and location.
*
* @since 0.1.0
*
* @param string $log_file_name The requested log file name. Defaults to 'cli-log.log'
* @param string $log_dir The requested log directory.
*
* @return $log_file The log file location.
*/
public static function set_log_file( $log_file_name = null, $log_dir = null ) {
self::$log_file_name = ! empty( $log_file_name ) ? $log_file_name : 'cli-log.log';
if ( null !== $log_dir || empty( self::$log_dir ) ) {
self::set_log_dir( $log_dir );
}
self::$log_file = self::$log_dir . self::$log_file_name;
return self::$log_file;
}
/**
* Toggles whether to store CLI logs to the file system.
*
* @since 0.1.1
*
* @param boolean $enabled Whether storing logs is enabled.
*
* @return void
*/
public static function stored_logs_enabled( $enabled = true ) {
self::$store_logs = (bool) $enabled;
}
/**
* Get the WordPress filesystem handler.
*
* @since 0.1.0
*
* @return WP_Filesystem_Base
*/
public static function file_system() {
global $wp_filesystem;
if ( ! is_object( $wp_filesystem ) ) {
WP_Filesystem();
}
return $wp_filesystem;
}
/**
* Make sure emails are not sent out during CLI commands.
*
* @since 0.1.0
*
* @return Base for chaining.
*/
protected function disable_emails() {
add_filter( 'wp_mail', function( $atts ) {
$atts['to'] = '';
$atts['subject'] = '';
$atts['message'] = '';
$atts['headers'] = 'From: admin@localhost.dev';
$atts['attachments'] = array();
return $atts;
} );
add_filter( 'wp_mail_from', function( $from_email ) {
return 'admin@localhost.dev';
} );
return $this;
}
}