class-wp-cli.php
2.84 KB
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
<?php
/**
* Class for WP-CLI commands
*/
class BackWPup_WP_CLI extends WP_CLI_Command {
/**
* Start a BackWPup job
*
* # EXAMPLES
*
* backwpup start 13
* backwpup start --jobid=13 (deprecated)
*
* @param $args
* @param $assoc_args
*/
public function start( $args, $assoc_args ) {
$jobid = 0;
if ( file_exists( BackWPup::get_plugin_data( 'running_file' ) ) ) {
WP_CLI::error( __( 'A job is already running.', 'backwpup' ) );
}
if ( isset( $jobid ) ) {
$jobid = (int) $assoc_args[ 'jobid' ];
}
if ( ! empty( $args[ 0 ] ) ) {
$jobid = (int) $args[ 0 ];
}
if ( empty( $jobid ) ) {
WP_CLI::error( __( 'No job ID specified!', 'backwpup' ) );
}
$jobids = BackWPup_Option::get_job_ids();
if ( ! in_array( $jobid, $jobids, true ) ) {
WP_CLI::error( __( 'Job ID does not exist!', 'backwpup' ) );
}
BackWPup_Job::start_cli( $jobid );
}
/**
* Abort a working BackWPup Job
*
*/
public function abort( $args, $assoc_args ) {
if ( ! file_exists( BackWPup::get_plugin_data( 'running_file' ) ) ) {
WP_CLI::error( __( 'Nothing to abort!', 'backwpup' ) );
}
//abort
BackWPup_Job::user_abort();
WP_CLI::success( __( 'Job will be terminated.', 'backwpup' ) ) ;
}
/**
* Display a List of Jobs
*
*/
public function jobs( $args, $assoc_args ) {
$formatter_args = array(
'format' => 'table',
'fields' => array(
'Job ID',
'Name'
),
'field' => NULL
);
$items = array();
$formatter = new WP_CLI\Formatter( $formatter_args );
$jobids = BackWPup_Option::get_job_ids();
foreach ($jobids as $jobid ) {
$items[] = array(
'Job ID' => $jobid,
'Name' => BackWPup_Option::get( $jobid, 'name' )
);
}
$formatter->display_items( $items );
}
/**
* See Status of a working job
*
* @param $args
* @param $assoc_args
*/
public function working( $args, $assoc_args ) {
$job_object = BackWPup_Job::get_working_data();
if ( ! is_object( $job_object ) ) {
WP_CLI::error( __( 'No job running', 'backwpup' ) );
}
$formatter_args = array(
'format' => 'table',
'fields' => array(
'JobID',
'Name',
'Warnings',
'Errors',
'On Step',
'Done',
),
'field' => NULL
);
$formatter = new WP_CLI\Formatter( $formatter_args );
$items = array();
$items[] = array(
'JobID' => $job_object->job[ 'jobid' ],
'Name' => $job_object->job[ 'name' ],
'Warnings' => $job_object->warnings,
'Errors' => $job_object->errors,
'On Step' => $job_object->steps_data[ $job_object->step_working ][ 'NAME' ],
'Done' => $job_object->step_percent . ' / ' . $job_object->substep_percent,
'Last message' => str_replace( '…', '...', strip_tags( $job_object->lastmsg ) )
);
$formatter->display_items( $items );
WP_CLI::log( 'Last Message: ' . str_replace( '…', '...', strip_tags( $job_object->lastmsg ) ) );
}
}