class-wc-cli-command.php
11.3 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
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
<?php
/**
* WooCommerce CLI Command.
*
* Base class that must be extended by any WooCommerce sub commands.
*
* @class WC_CLI_Command
* @version 2.5.0
* @package WooCommerce/CLI
* @category CLI
* @author WooThemes
*/
class WC_CLI_Command extends WP_CLI_Command {
/**
* Add common cli arguments to argument list before WP_Query is run.
*
* @since 2.5.0
* @param array $base_args Required arguments for the query (e.g. `post_type`, etc)
* @param array $assoc_args Arguments provided in when invoking the command
* @return array
*/
protected function merge_wp_query_args( $base_args, $assoc_args ) {
$args = array();
// date
if ( ! empty( $assoc_args['created_at_min'] ) || ! empty( $assoc_args['created_at_max'] ) || ! empty( $assoc_args['updated_at_min'] ) || ! empty( $assoc_args['updated_at_max'] ) ) {
$args['date_query'] = array();
// resources created after specified date
if ( ! empty( $assoc_args['created_at_min'] ) ) {
$args['date_query'][] = array( 'column' => 'post_date_gmt', 'after' => $this->parse_datetime( $assoc_args['created_at_min'] ), 'inclusive' => true );
}
// resources created before specified date
if ( ! empty( $assoc_args['created_at_max'] ) ) {
$args['date_query'][] = array( 'column' => 'post_date_gmt', 'before' => $this->parse_datetime( $assoc_args['created_at_max'] ), 'inclusive' => true );
}
// resources updated after specified date
if ( ! empty( $assoc_args['updated_at_min'] ) ) {
$args['date_query'][] = array( 'column' => 'post_modified_gmt', 'after' => $this->parse_datetime( $assoc_args['updated_at_min'] ), 'inclusive' => true );
}
// resources updated before specified date
if ( ! empty( $assoc_args['updated_at_max'] ) ) {
$args['date_query'][] = array( 'column' => 'post_modified_gmt', 'before' => $this->parse_datetime( $assoc_args['updated_at_max'] ), 'inclusive' => true );
}
}
// Search.
if ( ! empty( $assoc_args['q'] ) ) {
$args['s'] = $assoc_args['q'];
}
// Number of post to show per page.
if ( ! empty( $assoc_args['limit'] ) ) {
$args['posts_per_page'] = $assoc_args['limit'];
}
// Number of post to displace or pass over.
if ( ! empty( $assoc_args['offset'] ) ) {
$args['offset'] = $assoc_args['offset'];
}
// order (ASC or DESC, DESC by default).
if ( ! empty( $assoc_args['order'] ) ) {
$args['order'] = $assoc_args['order'];
}
// orderby.
if ( ! empty( $assoc_args['orderby'] ) ) {
$args['orderby'] = $assoc_args['orderby'];
// allow sorting by meta value
if ( ! empty( $assoc_args['orderby_meta_key'] ) ) {
$args['meta_key'] = $assoc_args['orderby_meta_key'];
}
}
// allow post status change
if ( ! empty( $assoc_args['post_status'] ) ) {
$args['post_status'] = $assoc_args['post_status'];
unset( $assoc_args['post_status'] );
}
// filter by a list of post ids
if ( ! empty( $assoc_args['in'] ) ) {
$args['post__in'] = explode( ',', $assoc_args['in'] );
unset( $assoc_args['in'] );
}
// exclude by a list of post ids
if ( ! empty( $assoc_args['not_in'] ) ) {
$args['post__not_in'] = explode( ',', $assoc_args['not_in'] );
unset( $assoc_args['not_in'] );
}
// posts page.
$args['paged'] = ( isset( $assoc_args['page'] ) ) ? absint( $assoc_args['page'] ) : 1;
$args = apply_filters( 'woocommerce_cli_query_args', $args, $assoc_args );
return array_merge( $base_args, $args );
}
/**
* Add common cli arguments to argument list before WP_User_Query is run.
*
* @since 2.5.0
* @param array $base_args required arguments for the query (e.g. `post_type`, etc)
* @param array $assoc_args arguments provided in when invoking the command
* @return array
*/
protected function merge_wp_user_query_args( $base_args, $assoc_args ) {
$args = array();
// Custom Role
if ( ! empty( $assoc_args['role'] ) ) {
$args['role'] = $assoc_args['role'];
}
// Search
if ( ! empty( $assoc_args['q'] ) ) {
$args['search'] = $assoc_args['q'];
}
// Limit number of users returned.
if ( ! empty( $assoc_args['limit'] ) ) {
$args['number'] = absint( $assoc_args['limit'] );
}
// Offset
if ( ! empty( $assoc_args['offset'] ) ) {
$args['offset'] = absint( $assoc_args['offset'] );
}
// date
if ( ! empty( $assoc_args['created_at_min'] ) || ! empty( $assoc_args['created_at_max'] ) ) {
$args['date_query'] = array();
// resources created after specified date
if ( ! empty( $assoc_args['created_at_min'] ) ) {
$args['date_query'][] = array( 'after' => $this->parse_datetime( $assoc_args['created_at_min'] ), 'inclusive' => true );
}
// resources created before specified date
if ( ! empty( $assoc_args['created_at_max'] ) ) {
$args['date_query'][] = array( 'before' => $this->parse_datetime( $assoc_args['created_at_max'] ), 'inclusive' => true );
}
}
// Order (ASC or DESC, ASC by default).
if ( ! empty( $assoc_args['order'] ) ) {
$args['order'] = $assoc_args['order'];
}
// Orderby.
if ( ! empty( $assoc_args['orderby'] ) ) {
$args['orderby'] = $assoc_args['orderby'];
}
$args = apply_filters( 'woocommerce_cli_user_query_args', $args, $assoc_args );
return array_merge( $base_args, $args );
}
/**
* Parse an RFC3339 datetime into a MySQl datetime.
*
* Invalid dates default to unix epoch.
*
* @since 2.5.0
* @param string $datetime RFC3339 datetime
* @return string MySQl datetime (YYYY-MM-DD HH:MM:SS)
*/
protected function parse_datetime( $datetime ) {
// Strip millisecond precision (a full stop followed by one or more digits)
if ( strpos( $datetime, '.' ) !== false ) {
$datetime = preg_replace( '/\.\d+/', '', $datetime );
}
// default timezone to UTC
$datetime = preg_replace( '/[+-]\d+:+\d+$/', '+00:00', $datetime );
try {
$datetime = new DateTime( $datetime, new DateTimeZone( 'UTC' ) );
} catch ( Exception $e ) {
$datetime = new DateTime( '@0' );
}
return $datetime->format( 'Y-m-d H:i:s' );
}
/**
* Format a unix timestamp or MySQL datetime into an RFC3339 datetime.
*
* @since 2.5.0
* @param int|string $timestamp unix timestamp or MySQL datetime
* @param bool $convert_to_utc
* @return string RFC3339 datetime
*/
protected function format_datetime( $timestamp, $convert_to_utc = false ) {
if ( $convert_to_utc ) {
$timezone = new DateTimeZone( wc_timezone_string() );
} else {
$timezone = new DateTimeZone( 'UTC' );
}
try {
if ( is_numeric( $timestamp ) ) {
$date = new DateTime( "@{$timestamp}" );
} else {
$date = new DateTime( $timestamp, $timezone );
}
// convert to UTC by adjusting the time based on the offset of the site's timezone
if ( $convert_to_utc ) {
$date->modify( -1 * $date->getOffset() . ' seconds' );
}
} catch ( Exception $e ) {
$date = new DateTime( '@0' );
}
return $date->format( 'Y-m-d\TH:i:s\Z' );
}
/**
* Get formatter object based on supplied arguments.
*
* @since 2.5.0
* @param array $assoc_args Associative args from CLI to determine formatting
* @return \WP_CLI\Formatter
*/
protected function get_formatter( $assoc_args ) {
$args = $this->get_format_args( $assoc_args );
return new \WP_CLI\Formatter( $args );
}
/**
* Get default fields for formatter.
*
* Class that extends WC_CLI_Command should override this method.
*
* @since 2.5.0
* @return null|string|array
*/
protected function get_default_format_fields() {
return null;
}
/**
* Get format args that will be passed into CLI Formatter.
*
* @since 2.5.0
* @param array $assoc_args Associative args from CLI
* @return array Formatter args
*/
protected function get_format_args( $assoc_args ) {
$format_args = array(
'fields' => $this->get_default_format_fields(),
'field' => null,
'format' => 'table',
);
if ( isset( $assoc_args['fields'] ) ) {
$format_args['fields'] = $assoc_args['fields'];
}
if ( isset( $assoc_args['field'] ) ) {
$format_args['field'] = $assoc_args['field'];
}
if ( ! empty( $assoc_args['format'] ) && in_array( $assoc_args['format'], array( 'count', 'ids', 'table', 'csv', 'json' ) ) ) {
$format_args['format'] = $assoc_args['format'];
}
return $format_args;
}
/**
* Flatten multidimensional array in which nested array will be prefixed with
* parent keys separated with dot char, e.g. given an array:
*
* array(
* 'a' => array(
* 'b' => array(
* 'c' => ...
* )
* )
* )
*
* a flatten array would contain key 'a.b.c' => ...
*
* @since 2.5.0
* @param array $arr Array that may contains nested array
* @param string $prefix Prefix
*
* @return array Flattened array
*/
protected function flatten_array( $arr, $prefix = '' ) {
$flattened = array();
foreach ( $arr as $key => $value ) {
if ( is_array( $value ) ) {
if ( sizeof( $value ) > 0 ) {
// Full access to whole elements if indices are numerical.
$flattened[ $prefix . $key ] = $value;
// This is naive assumption that if element with index zero
// exists then array indices are numberical.
if ( ! empty( $value[0] ) ) {
// Allow size of array to be accessed, i.e., a.b.arr.size
$flattened[ $prefix . $key . '.size' ] = sizeof( $value );
}
$flattened = array_merge( $flattened, $this->flatten_array( $value, $prefix . $key . '.' ) );
} else {
$flattened[ $prefix . $key ] = '';
// Tells user that size of this array is zero.
$flattened[ $prefix . $key . '.size' ] = 0;
}
} else {
$flattened[ $prefix . $key ] = $value;
}
}
return $flattened;
}
/**
* Unflatten array will make key 'a.b.c' becomes nested array:
*
* array(
* 'a' => array(
* 'b' => array(
* 'c' => ...
* )
* )
* )
*
* @since 2.5.0
* @param array $arr Flattened array
* @return array
*/
protected function unflatten_array( $arr ) {
$unflatten = array();
foreach ( $arr as $key => $value ) {
$key_list = explode( '.', $key );
$first_key = array_shift( $key_list );
$first_key = $this->get_normalized_array_key( $first_key );
if ( sizeof( $key_list ) > 0 ) {
$remaining_keys = implode( '.', $key_list );
$subarray = $this->unflatten_array( array( $remaining_keys => $value ) );
foreach ( $subarray as $sub_key => $sub_value ) {
$sub_key = $this->get_normalized_array_key( $sub_key );
if ( ! empty( $unflatten[ $first_key ][ $sub_key ] ) ) {
$unflatten[ $first_key ][ $sub_key ] = array_merge_recursive( $unflatten[ $first_key ][ $sub_key ], $sub_value );
} else {
$unflatten[ $first_key ][ $sub_key ] = $sub_value;
}
}
} else {
$unflatten[ $first_key ] = $value;
}
}
return $unflatten;
}
/**
* Get normalized array key. If key is a numeric one it will be converted
* as absolute integer.
*
* @since 2.5.0
* @param string $key Array key
* @return string|int
*/
protected function get_normalized_array_key( $key ) {
if ( is_numeric( $key ) ) {
$key = absint( $key );
}
return $key;
}
/**
* Check if the value is equal to 'yes', 'true' or '1'
*
* @since 2.5.4
* @param string $value
* @return boolean
*/
protected function is_true( $value ) {
return ( 'yes' === $value || 'true' === $value || '1' === $value ) ? true : false;
}
}