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3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / cli / class-wc-cli-rest-command.php
woocommerce / includes / cli Last commit date
class-wc-cli-rest-command.php 6 years ago class-wc-cli-runner.php 5 years ago class-wc-cli-tool-command.php 6 years ago class-wc-cli-update-command.php 6 years ago
class-wc-cli-rest-command.php
462 lines
1 <?php
2 /**
3 * WP_CLI_Rest_Command class file.
4 *
5 * @package WooCommerce\Cli
6 */
7
8 use Automattic\Jetpack\Constants;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Main Command for WooCommere CLI.
16 *
17 * Since a lot of WC operations can be handled via the REST API, we base our CLI
18 * off of Restful to generate commands for each WooCommerce REST API endpoint
19 * so most of the logic is shared.
20 *
21 * Forked from wp-cli/restful (by Daniel Bachhuber, released under the MIT license https://opensource.org/licenses/MIT).
22 * https://github.com/wp-cli/restful
23 *
24 * @version 3.0.0
25 * @package WooCommerce
26 */
27 class WC_CLI_REST_Command {
28 /**
29 * Endpoints that have a parent ID.
30 * Ex: Product reviews, which has a product ID and a review ID.
31 *
32 * @var array
33 */
34 protected $routes_with_parent_id = array(
35 'customer_download',
36 'product_review',
37 'order_note',
38 'shop_order_refund',
39 );
40
41 /**
42 * Name of command/endpoint object.
43 *
44 * @var string
45 */
46 private $name;
47
48 /**
49 * Endpoint route.
50 *
51 * @var string
52 */
53 private $route;
54
55 /**
56 * Main resource ID.
57 *
58 * @var int
59 */
60 private $resource_identifier;
61
62 /**
63 * Schema for command.
64 *
65 * @var array
66 */
67 private $schema;
68
69 /**
70 * List of supported IDs and their description (name => desc).
71 *
72 * @var array
73 */
74 private $supported_ids = array();
75
76 /**
77 * Sets up REST Command.
78 *
79 * @param string $name Name of endpoint object (comes from schema).
80 * @param string $route Path to route of this endpoint.
81 * @param array $schema Schema object.
82 */
83 public function __construct( $name, $route, $schema ) {
84 $this->name = $name;
85
86 preg_match_all( '#\([^\)]+\)#', $route, $matches );
87 $first_match = $matches[0];
88 $resource_id = ! empty( $matches[0] ) ? array_pop( $matches[0] ) : null;
89 $this->route = rtrim( $route );
90 $this->schema = $schema;
91
92 $this->resource_identifier = $resource_id;
93 if ( in_array( $name, $this->routes_with_parent_id, true ) ) {
94 $is_singular = substr( $this->route, - strlen( $resource_id ) ) === $resource_id;
95 if ( ! $is_singular ) {
96 $this->resource_identifier = $first_match[0];
97 }
98 }
99 }
100
101 /**
102 * Passes supported ID arguments (things like product_id, order_id, etc) that we should look for in addition to id.
103 *
104 * @param array $supported_ids List of supported IDs.
105 */
106 public function set_supported_ids( $supported_ids = array() ) {
107 $this->supported_ids = $supported_ids;
108 }
109
110 /**
111 * Returns an ID of supported ID arguments (things like product_id, order_id, etc) that we should look for in addition to id.
112 *
113 * @return array
114 */
115 public function get_supported_ids() {
116 return $this->supported_ids;
117 }
118
119 /**
120 * Create a new item.
121 *
122 * @subcommand create
123 *
124 * @param array $args WP-CLI positional arguments.
125 * @param array $assoc_args WP-CLI associative arguments.
126 */
127 public function create_item( $args, $assoc_args ) {
128 $assoc_args = self::decode_json( $assoc_args );
129 list( $status, $body ) = $this->do_request( 'POST', $this->get_filled_route( $args ), $assoc_args );
130 if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
131 WP_CLI::line( $body['id'] );
132 } else {
133 WP_CLI::success( "Created {$this->name} {$body['id']}." );
134 }
135 }
136
137 /**
138 * Delete an existing item.
139 *
140 * @subcommand delete
141 *
142 * @param array $args WP-CLI positional arguments.
143 * @param array $assoc_args WP-CLI associative arguments.
144 */
145 public function delete_item( $args, $assoc_args ) {
146 list( $status, $body ) = $this->do_request( 'DELETE', $this->get_filled_route( $args ), $assoc_args );
147 if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
148 WP_CLI::line( $body['id'] );
149 } else {
150 if ( empty( $assoc_args['force'] ) ) {
151 WP_CLI::success( __( 'Trashed', 'woocommerce' ) . " {$this->name} {$body['id']}" );
152 } else {
153 WP_CLI::success( __( 'Deleted', 'woocommerce' ) . " {$this->name} {$body['id']}." );
154 }
155 }
156 }
157
158 /**
159 * Get a single item.
160 *
161 * @subcommand get
162 *
163 * @param array $args WP-CLI positional arguments.
164 * @param array $assoc_args WP-CLI associative arguments.
165 */
166 public function get_item( $args, $assoc_args ) {
167 $route = $this->get_filled_route( $args );
168 list( $status, $body, $headers ) = $this->do_request( 'GET', $route, $assoc_args );
169
170 if ( ! empty( $assoc_args['fields'] ) ) {
171 $body = self::limit_item_to_fields( $body, $assoc_args['fields'] );
172 }
173
174 if ( empty( $assoc_args['format'] ) ) {
175 $assoc_args['format'] = 'table';
176 }
177
178 if ( 'headers' === $assoc_args['format'] ) {
179 echo wp_json_encode( $headers );
180 } elseif ( 'body' === $assoc_args['format'] ) {
181 echo wp_json_encode( $body );
182 } elseif ( 'envelope' === $assoc_args['format'] ) {
183 echo wp_json_encode(
184 array(
185 'body' => $body,
186 'headers' => $headers,
187 'status' => $status,
188 )
189 );
190 } else {
191 $formatter = $this->get_formatter( $assoc_args );
192 $formatter->display_item( $body );
193 }
194 }
195
196 /**
197 * List all items.
198 *
199 * @subcommand list
200 *
201 * @param array $args WP-CLI positional arguments.
202 * @param array $assoc_args WP-CLI associative arguments.
203 */
204 public function list_items( $args, $assoc_args ) {
205 if ( ! empty( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) {
206 $method = 'HEAD';
207 } else {
208 $method = 'GET';
209 }
210
211 if ( ! isset( $assoc_args['per_page'] ) || empty( $assoc_args['per_page'] ) ) {
212 $assoc_args['per_page'] = '100';
213 }
214
215 list( $status, $body, $headers ) = $this->do_request( $method, $this->get_filled_route( $args ), $assoc_args );
216 if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
217 $items = array_column( $body, 'id' );
218 } else {
219 $items = $body;
220 }
221
222 if ( ! empty( $assoc_args['fields'] ) ) {
223 foreach ( $items as $key => $item ) {
224 $items[ $key ] = self::limit_item_to_fields( $item, $assoc_args['fields'] );
225 }
226 }
227
228 if ( empty( $assoc_args['format'] ) ) {
229 $assoc_args['format'] = 'table';
230 }
231
232 if ( ! empty( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) {
233 echo (int) $headers['X-WP-Total'];
234 } elseif ( 'headers' === $assoc_args['format'] ) {
235 echo wp_json_encode( $headers );
236 } elseif ( 'body' === $assoc_args['format'] ) {
237 echo wp_json_encode( $body );
238 } elseif ( 'envelope' === $assoc_args['format'] ) {
239 echo wp_json_encode(
240 array(
241 'body' => $body,
242 'headers' => $headers,
243 'status' => $status,
244 'api_url' => $this->api_url,
245 )
246 );
247 } else {
248 $formatter = $this->get_formatter( $assoc_args );
249 $formatter->display_items( $items );
250 }
251 }
252
253 /**
254 * Update an existing item.
255 *
256 * @subcommand update
257 *
258 * @param array $args WP-CLI positional arguments.
259 * @param array $assoc_args WP-CLI associative arguments.
260 */
261 public function update_item( $args, $assoc_args ) {
262 $assoc_args = self::decode_json( $assoc_args );
263 list( $status, $body ) = $this->do_request( 'POST', $this->get_filled_route( $args ), $assoc_args );
264 if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
265 WP_CLI::line( $body['id'] );
266 } else {
267 WP_CLI::success( __( 'Updated', 'woocommerce' ) . " {$this->name} {$body['id']}." );
268 }
269 }
270
271 /**
272 * Do a REST Request
273 *
274 * @param string $method Request method. Examples: 'POST', 'PUT', 'DELETE' or 'GET'.
275 * @param string $route Resource route.
276 * @param array $assoc_args Associative arguments passed to the originating WP-CLI command.
277 *
278 * @return array
279 */
280 private function do_request( $method, $route, $assoc_args ) {
281 wc_maybe_define_constant( 'REST_REQUEST', true );
282
283 $request = new WP_REST_Request( $method, $route );
284 if ( in_array( $method, array( 'POST', 'PUT' ), true ) ) {
285 $request->set_body_params( $assoc_args );
286 } else {
287 foreach ( $assoc_args as $key => $value ) {
288 $request->set_param( $key, $value );
289 }
290 }
291 if ( Constants::is_true( 'SAVEQUERIES' ) ) {
292 $original_queries = is_array( $GLOBALS['wpdb']->queries ) ? array_keys( $GLOBALS['wpdb']->queries ) : array();
293 }
294 $response = rest_do_request( $request );
295 if ( Constants::is_true( 'SAVEQUERIES' ) ) {
296 $performed_queries = array();
297 foreach ( (array) $GLOBALS['wpdb']->queries as $key => $query ) {
298 if ( in_array( $key, $original_queries, true ) ) {
299 continue;
300 }
301 $performed_queries[] = $query;
302 }
303 usort(
304 $performed_queries,
305 function( $a, $b ) {
306 if ( $a[1] === $b[1] ) {
307 return 0;
308 }
309 return ( $a[1] > $b[1] ) ? -1 : 1;
310 }
311 );
312
313 $query_count = count( $performed_queries );
314 $query_total_time = 0;
315 foreach ( $performed_queries as $query ) {
316 $query_total_time += $query[1];
317 }
318 $slow_query_message = '';
319 if ( $performed_queries && 'wc' === WP_CLI::get_config( 'debug' ) ) {
320 $slow_query_message .= '. Ordered by slowness, the queries are:' . PHP_EOL;
321 foreach ( $performed_queries as $i => $query ) {
322 $i++;
323 $bits = explode( ', ', $query[2] );
324 $backtrace = implode( ', ', array_slice( $bits, 13 ) );
325 $seconds = round( $query[1], 6 );
326 $slow_query_message .= <<<EOT
327 {$i}:
328 - {$seconds} seconds
329 - {$backtrace}
330 - {$query[0]}
331 EOT;
332 $slow_query_message .= PHP_EOL;
333 }
334 } elseif ( 'wc' !== WP_CLI::get_config( 'debug' ) ) {
335 $slow_query_message = '. Use --debug=wc to see all queries.';
336 }
337 $query_total_time = round( $query_total_time, 6 );
338 WP_CLI::debug( "wc command executed {$query_count} queries in {$query_total_time} seconds{$slow_query_message}", 'wc' );
339 }
340
341 $error = $response->as_error();
342
343 if ( $error ) {
344 // For authentication errors (status 401), include a reminder to set the --user flag.
345 // WP_CLI::error will only return the first message from WP_Error, so we will pass a string containing both instead.
346 if ( 401 === $response->get_status() ) {
347 $errors = $error->get_error_messages();
348 $errors[] = __( 'Make sure to include the --user flag with an account that has permissions for this action.', 'woocommerce' ) . ' {"status":401}';
349 $error = implode( "\n", $errors );
350 }
351 WP_CLI::error( $error );
352 }
353 return array( $response->get_status(), $response->get_data(), $response->get_headers() );
354 }
355
356 /**
357 * Get Formatter object based on supplied parameters.
358 *
359 * @param array $assoc_args Parameters passed to command. Determines formatting.
360 * @return \WP_CLI\Formatter
361 */
362 protected function get_formatter( &$assoc_args ) {
363 if ( ! empty( $assoc_args['fields'] ) ) {
364 if ( is_string( $assoc_args['fields'] ) ) {
365 $fields = explode( ',', $assoc_args['fields'] );
366 } else {
367 $fields = $assoc_args['fields'];
368 }
369 } else {
370 if ( ! empty( $assoc_args['context'] ) ) {
371 $fields = $this->get_context_fields( $assoc_args['context'] );
372 } else {
373 $fields = $this->get_context_fields( 'view' );
374 }
375 }
376 return new \WP_CLI\Formatter( $assoc_args, $fields );
377 }
378
379 /**
380 * Get a list of fields present in a given context
381 *
382 * @param string $context Scope under which the request is made. Determines fields present in response.
383 * @return array
384 */
385 private function get_context_fields( $context ) {
386 $fields = array();
387 foreach ( $this->schema['properties'] as $key => $args ) {
388 if ( empty( $args['context'] ) || in_array( $context, $args['context'], true ) ) {
389 $fields[] = $key;
390 }
391 }
392 return $fields;
393 }
394
395 /**
396 * Get the route for this resource
397 *
398 * @param array $args Positional arguments passed to the originating WP-CLI command.
399 * @return string
400 */
401 private function get_filled_route( $args = array() ) {
402 $supported_id_matched = false;
403 $route = $this->route;
404
405 foreach ( $this->get_supported_ids() as $id_name => $id_desc ) {
406 if ( 'id' !== $id_name && strpos( $route, '<' . $id_name . '>' ) !== false && ! empty( $args ) ) {
407 $route = str_replace( '(?P<' . $id_name . '>[\d]+)', $args[0], $route );
408 $supported_id_matched = true;
409 }
410 }
411
412 if ( ! empty( $args ) ) {
413 $id_replacement = $supported_id_matched && ! empty( $args[1] ) ? $args[1] : $args[0];
414 $route = str_replace( array( '(?P<id>[\d]+)', '(?P<id>[\w-]+)' ), $id_replacement, $route );
415 }
416
417 return rtrim( $route );
418 }
419
420 /**
421 * Reduce an item to specific fields.
422 *
423 * @param array $item Item to reduce.
424 * @param array $fields Fields to keep.
425 * @return array
426 */
427 private static function limit_item_to_fields( $item, $fields ) {
428 if ( empty( $fields ) ) {
429 return $item;
430 }
431 if ( is_string( $fields ) ) {
432 $fields = explode( ',', $fields );
433 }
434 foreach ( $item as $i => $field ) {
435 if ( ! in_array( $i, $fields, true ) ) {
436 unset( $item[ $i ] );
437 }
438 }
439 return $item;
440 }
441
442 /**
443 * JSON can be passed in some more complicated objects, like the payment gateway settings array.
444 * This function decodes the json (if present) and tries to get it's value.
445 *
446 * @param array $arr Array that will be scanned for JSON encoded values.
447 *
448 * @return array
449 */
450 protected function decode_json( $arr ) {
451 foreach ( $arr as $key => $value ) {
452 if ( '[' === substr( $value, 0, 1 ) || '{' === substr( $value, 0, 1 ) ) {
453 $arr[ $key ] = json_decode( $value, true );
454 } else {
455 continue;
456 }
457 }
458 return $arr;
459 }
460
461 }
462