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