class-wc-cli-com-command.php
2 years ago
class-wc-cli-com-extension-command.php
2 years ago
class-wc-cli-rest-command.php
3 years ago
class-wc-cli-runner.php
3 years ago
class-wc-cli-tool-command.php
6 years ago
class-wc-cli-tracker-command.php
4 years ago
class-wc-cli-update-command.php
3 years ago
class-wc-cli-tool-command.php
108 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WC_CLI_Tool_Command class file. |
| 4 | * |
| 5 | * @package WooCommerce\CLI |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Hooks up our system status tools to the CLI. |
| 14 | * |
| 15 | * Forked from wp-cli/restful (by Daniel Bachhuber, released under the MIT license https://opensource.org/licenses/MIT). |
| 16 | * https://github.com/wp-cli/restful |
| 17 | * |
| 18 | * @version 3.0.0 |
| 19 | * @package WooCommerce |
| 20 | */ |
| 21 | class WC_CLI_Tool_Command { |
| 22 | |
| 23 | /** |
| 24 | * Registers just a 'list' and 'run' command to the WC CLI |
| 25 | * since we only want to enable certain actions on the system status |
| 26 | * tools endpoints. |
| 27 | */ |
| 28 | public static function register_commands() { |
| 29 | global $wp_rest_server; |
| 30 | |
| 31 | $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status/tools' ); |
| 32 | $response = $wp_rest_server->dispatch( $request ); |
| 33 | $response_data = $response->get_data(); |
| 34 | if ( empty( $response_data ) ) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | $parent = 'wc tool'; |
| 39 | $supported_commands = array( 'list', 'run' ); |
| 40 | foreach ( $supported_commands as $command ) { |
| 41 | $synopsis = array(); |
| 42 | if ( 'run' === $command ) { |
| 43 | $synopsis[] = array( |
| 44 | 'name' => 'id', |
| 45 | 'type' => 'positional', |
| 46 | 'description' => __( 'The id for the resource.', 'woocommerce' ), |
| 47 | 'optional' => false, |
| 48 | ); |
| 49 | $method = 'update_item'; |
| 50 | $route = '/wc/v2/system_status/tools/(?P<id>[\w-]+)'; |
| 51 | } elseif ( 'list' === $command ) { |
| 52 | $synopsis[] = array( |
| 53 | 'name' => 'fields', |
| 54 | 'type' => 'assoc', |
| 55 | 'description' => __( 'Limit response to specific fields. Defaults to all fields.', 'woocommerce' ), |
| 56 | 'optional' => true, |
| 57 | ); |
| 58 | $synopsis[] = array( |
| 59 | 'name' => 'field', |
| 60 | 'type' => 'assoc', |
| 61 | 'description' => __( 'Get the value of an individual field.', 'woocommerce' ), |
| 62 | 'optional' => true, |
| 63 | ); |
| 64 | $synopsis[] = array( |
| 65 | 'name' => 'format', |
| 66 | 'type' => 'assoc', |
| 67 | 'description' => __( 'Render response in a particular format.', 'woocommerce' ), |
| 68 | 'optional' => true, |
| 69 | 'default' => 'table', |
| 70 | 'options' => array( |
| 71 | 'table', |
| 72 | 'json', |
| 73 | 'csv', |
| 74 | 'ids', |
| 75 | 'yaml', |
| 76 | 'count', |
| 77 | 'headers', |
| 78 | 'body', |
| 79 | 'envelope', |
| 80 | ), |
| 81 | ); |
| 82 | $method = 'list_items'; |
| 83 | $route = '/wc/v2/system_status/tools'; |
| 84 | } |
| 85 | |
| 86 | $before_invoke = null; |
| 87 | if ( empty( $command_args['when'] ) && WP_CLI::get_config( 'debug' ) ) { |
| 88 | $before_invoke = function() { |
| 89 | wc_maybe_define_constant( 'SAVEQUERIES', true ); |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | $rest_command = new WC_CLI_REST_Command( 'system_status_tool', $route, $response_data['schema'] ); |
| 94 | |
| 95 | WP_CLI::add_command( |
| 96 | "{$parent} {$command}", |
| 97 | array( $rest_command, $method ), |
| 98 | array( |
| 99 | 'synopsis' => $synopsis, |
| 100 | 'when' => ! empty( $command_args['when'] ) ? $command_args['when'] : '', |
| 101 | 'before_invoke' => $before_invoke, |
| 102 | ) |
| 103 | ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } |
| 108 |