Repository navigation
Expand file tree
/
Copy pathCommandWithDBObject.php
More file actions
187 lines (158 loc) · 4.55 KB
/
Copy pathCommandWithDBObject.php
File metadata and controls
187 lines (158 loc) · 4.55 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
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
<?php
namespace WP_CLI;
use WP_CLI;
use WP_CLI_Command;
use WP_CLI\Utils;
use WP_Error;
/**
* Base class for WP-CLI commands that deal with database objects.
*
* @package wp-cli
*/
abstract class CommandWithDBObject extends WP_CLI_Command {
/**
* @var string $object_type WordPress' expected name for the object.
*/
protected $obj_type;
/**
* @var string $obj_id_key Key representing object's PK field in db.
*/
protected $obj_id_key = 'ID';
/**
* @var array $obj_fields Default fields to display for each object.
*/
protected $obj_fields;
/**
* Create a given database object.
* Exits with status.
*
* @param array $args Arguments passed to command. Generally unused.
* @param array $assoc_args Parameters passed to command to be passed to callback.
* @param callable $callback Function used to create object.
*/
protected function _create( $args, $assoc_args, $callback ) {
unset( $assoc_args[ $this->obj_id_key ] );
$obj_id = $callback( $assoc_args );
if ( is_wp_error( $obj_id ) ) {
WP_CLI::error( $obj_id );
}
if ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
WP_CLI::line( $obj_id );
} else {
WP_CLI::success( "Created {$this->obj_type} {$obj_id}." );
}
}
/**
* Update a given database object.
* Exits with status.
*
* @param array $args Collection of one or more object ids to update.
* @param array $assoc_args Fields => values to update on each object.
* @param callable $callback Function used to update object.
*/
protected function _update( $args, $assoc_args, $callback ) {
$status = 0;
if ( empty( $assoc_args ) ) {
WP_CLI::error( 'Need some fields to update.' );
}
if ( Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
wp_defer_term_counting( true );
}
foreach ( $args as $obj_id ) {
$params = array_merge( $assoc_args, [ $this->obj_id_key => $obj_id ] );
$status = $this->success_or_failure(
$this->wp_error_to_resp(
$callback( $params ),
"Updated {$this->obj_type} {$obj_id}."
)
);
}
if ( Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
wp_defer_term_counting( false );
}
exit( $status );
}
/**
* Transforms arguments with '__' from CSV into expected arrays
*
* @param array $assoc_args
* @return array
*/
protected static function process_csv_arguments_to_arrays( $assoc_args ) {
foreach ( $assoc_args as $k => $v ) {
if ( false !== strpos( $k, '__' ) ) {
$assoc_args[ $k ] = explode( ',', $v );
}
}
return $assoc_args;
}
/**
* Delete a given database object.
* Exits with status.
*
* @param array $args Collection of one or more object ids to delete.
* @param array $assoc_args Any arguments needed for the callback function.
* @param callable $callback Function used to delete object.
*/
protected function _delete( $args, $assoc_args, $callback ) {
$status = 0;
if ( Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
wp_defer_term_counting( true );
}
foreach ( $args as $obj_id ) {
$result = $callback( $obj_id, $assoc_args );
$status = $this->success_or_failure( $result );
}
if ( Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
wp_defer_term_counting( false );
}
exit( $status );
}
/**
* Format callback response to consistent format.
*
* @param WP_Error|true $response Response from CRUD callback.
* @param string $success_msg
* @return array
*/
protected function wp_error_to_resp( $response, $success_msg ) {
return is_wp_error( $response )
? [ 'error', $response->get_error_message() ]
: [ 'success', $success_msg ];
}
/**
* Display success or warning based on response; return proper exit code.
*
* @param array $response Formatted from a CRUD callback.
* @return int $status
*/
protected function success_or_failure( $response ) {
list( $type, $msg ) = $response;
if ( 'success' === $type ) {
WP_CLI::success( $msg );
$status = 0;
} else {
WP_CLI::warning( $msg );
$status = 1;
}
return $status;
}
/**
* Get Formatter object based on supplied parameters.
*
* @param array $assoc_args Parameters passed to command. Determines formatting.
* @return Formatter
*/
protected function get_formatter( &$assoc_args ) {
if ( ! empty( $assoc_args['fields'] ) ) {
if ( is_string( $assoc_args['fields'] ) ) {
$fields = explode( ',', $assoc_args['fields'] );
} else {
$fields = $assoc_args['fields'];
}
} else {
$fields = $this->obj_fields;
}
return new Formatter( $assoc_args, $fields, $this->obj_type );
}
}