class-wc-cli-rest-command.php
5 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-runner.php
255 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WP_CLI_Runner class file. |
| 4 | * |
| 5 | * @package WooCommerce\CLI |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * WC API to WC CLI Bridge. |
| 14 | * |
| 15 | * Hooks into the REST API, figures out which endpoints come from WC, |
| 16 | * and registers them as CLI commands. |
| 17 | * |
| 18 | * Forked from wp-cli/restful (by Daniel Bachhuber, released under the MIT license https://opensource.org/licenses/MIT). |
| 19 | * https://github.com/wp-cli/restful |
| 20 | * |
| 21 | * @version 3.0.0 |
| 22 | * @package WooCommerce |
| 23 | */ |
| 24 | class WC_CLI_Runner { |
| 25 | /** |
| 26 | * Endpoints to disable (meaning they will not be available as CLI commands). |
| 27 | * Some of these can either be done via WP already, or are offered with |
| 28 | * some other changes (like tools). |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | private static $disabled_endpoints = array( |
| 33 | 'settings', |
| 34 | 'settings/(?P<group_id>[\w-]+)', |
| 35 | 'settings/(?P<group_id>[\w-]+)/batch', |
| 36 | 'settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)', |
| 37 | 'system_status', |
| 38 | 'system_status/tools', |
| 39 | 'system_status/tools/(?P<id>[\w-]+)', |
| 40 | 'reports', |
| 41 | 'reports/sales', |
| 42 | 'reports/top_sellers', |
| 43 | ); |
| 44 | |
| 45 | /** |
| 46 | * The version of the REST API we should target to |
| 47 | * generate commands. |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
| 51 | private static $target_rest_version = 'v2'; |
| 52 | |
| 53 | /** |
| 54 | * Register's all endpoints as commands once WP and WC have all loaded. |
| 55 | */ |
| 56 | public static function after_wp_load() { |
| 57 | global $wp_rest_server; |
| 58 | $wp_rest_server = new WP_REST_Server(); |
| 59 | do_action( 'rest_api_init', $wp_rest_server ); |
| 60 | |
| 61 | $request = new WP_REST_Request( 'GET', '/' ); |
| 62 | $request->set_param( 'context', 'help' ); |
| 63 | $response = $wp_rest_server->dispatch( $request ); |
| 64 | $response_data = $response->get_data(); |
| 65 | if ( empty( $response_data ) ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // Loop through all of our endpoints and register any valid WC endpoints. |
| 70 | foreach ( $response_data['routes'] as $route => $route_data ) { |
| 71 | // Only register endpoints for WC and our target version. |
| 72 | if ( substr( $route, 0, 4 + strlen( self::$target_rest_version ) ) !== '/wc/' . self::$target_rest_version ) { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | // Only register endpoints with schemas. |
| 77 | if ( empty( $route_data['schema']['title'] ) ) { |
| 78 | /* translators: %s: Route to a given WC-API endpoint */ |
| 79 | WP_CLI::debug( sprintf( __( 'No schema title found for %s, skipping REST command registration.', 'woocommerce' ), $route ), 'wc' ); |
| 80 | continue; |
| 81 | } |
| 82 | // Ignore batch endpoints. |
| 83 | if ( 'batch' === $route_data['schema']['title'] ) { |
| 84 | continue; |
| 85 | } |
| 86 | // Disable specific endpoints. |
| 87 | $route_pieces = explode( '/', $route ); |
| 88 | $endpoint_piece = str_replace( '/wc/' . $route_pieces[2] . '/', '', $route ); |
| 89 | if ( in_array( $endpoint_piece, self::$disabled_endpoints, true ) ) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | self::register_route_commands( new WC_CLI_REST_Command( $route_data['schema']['title'], $route, $route_data['schema'] ), $route, $route_data ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Generates command information and tells WP CLI about all |
| 99 | * commands available from a route. |
| 100 | * |
| 101 | * @param string $rest_command WC-API command. |
| 102 | * @param string $route Path to route endpoint. |
| 103 | * @param array $route_data Command data. |
| 104 | * @param array $command_args WP-CLI command arguments. |
| 105 | */ |
| 106 | private static function register_route_commands( $rest_command, $route, $route_data, $command_args = array() ) { |
| 107 | // Define IDs that we are looking for in the routes (in addition to id) |
| 108 | // so that we can pass it to the rest command, and use it here to generate documentation. |
| 109 | $supported_ids = array( |
| 110 | 'product_id' => __( 'Product ID.', 'woocommerce' ), |
| 111 | 'customer_id' => __( 'Customer ID.', 'woocommerce' ), |
| 112 | 'order_id' => __( 'Order ID.', 'woocommerce' ), |
| 113 | 'refund_id' => __( 'Refund ID.', 'woocommerce' ), |
| 114 | 'attribute_id' => __( 'Attribute ID.', 'woocommerce' ), |
| 115 | 'zone_id' => __( 'Zone ID.', 'woocommerce' ), |
| 116 | 'instance_id' => __( 'Instance ID.', 'woocommerce' ), |
| 117 | 'id' => __( 'The ID for the resource.', 'woocommerce' ), |
| 118 | 'slug' => __( 'The slug for the resource.', 'woocommerce' ), |
| 119 | ); |
| 120 | $rest_command->set_supported_ids( $supported_ids ); |
| 121 | $positional_args = array_keys( $supported_ids ); |
| 122 | $parent = "wc {$route_data['schema']['title']}"; |
| 123 | $supported_commands = array(); |
| 124 | |
| 125 | // Get a list of supported commands for each route. |
| 126 | foreach ( $route_data['endpoints'] as $endpoint ) { |
| 127 | preg_match_all( '#\([^\)]+\)#', $route, $matches ); |
| 128 | $resource_id = ! empty( $matches[0] ) ? array_pop( $matches[0] ) : null; |
| 129 | $trimmed_route = rtrim( $route ); |
| 130 | $is_singular = substr( $trimmed_route, - strlen( $resource_id ) ) === $resource_id; |
| 131 | |
| 132 | // List a collection. |
| 133 | if ( array( 'GET' ) === $endpoint['methods'] && ! $is_singular ) { |
| 134 | $supported_commands['list'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array(); |
| 135 | } |
| 136 | // Create a specific resource. |
| 137 | if ( array( 'POST' ) === $endpoint['methods'] && ! $is_singular ) { |
| 138 | $supported_commands['create'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array(); |
| 139 | } |
| 140 | // Get a specific resource. |
| 141 | if ( array( 'GET' ) === $endpoint['methods'] && $is_singular ) { |
| 142 | $supported_commands['get'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array(); |
| 143 | } |
| 144 | // Update a specific resource. |
| 145 | if ( in_array( 'POST', $endpoint['methods'], true ) && $is_singular ) { |
| 146 | $supported_commands['update'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array(); |
| 147 | } |
| 148 | // Delete a specific resource. |
| 149 | if ( array( 'DELETE' ) === $endpoint['methods'] && $is_singular ) { |
| 150 | $supported_commands['delete'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | foreach ( $supported_commands as $command => $endpoint_args ) { |
| 155 | $synopsis = array(); |
| 156 | $arg_regs = array(); |
| 157 | $ids = array(); |
| 158 | |
| 159 | foreach ( $supported_ids as $id_name => $id_desc ) { |
| 160 | if ( strpos( $route, '<' . $id_name . '>' ) !== false ) { |
| 161 | $synopsis[] = array( |
| 162 | 'name' => $id_name, |
| 163 | 'type' => 'positional', |
| 164 | 'description' => $id_desc, |
| 165 | 'optional' => false, |
| 166 | ); |
| 167 | $ids[] = $id_name; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | foreach ( $endpoint_args as $name => $args ) { |
| 172 | if ( ! in_array( $name, $positional_args, true ) || strpos( $route, '<' . $id_name . '>' ) === false ) { |
| 173 | $arg_regs[] = array( |
| 174 | 'name' => $name, |
| 175 | 'type' => 'assoc', |
| 176 | 'description' => ! empty( $args['description'] ) ? $args['description'] : '', |
| 177 | 'optional' => empty( $args['required'] ), |
| 178 | ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | foreach ( $arg_regs as $arg_reg ) { |
| 183 | $synopsis[] = $arg_reg; |
| 184 | } |
| 185 | |
| 186 | if ( in_array( $command, array( 'list', 'get' ), true ) ) { |
| 187 | $synopsis[] = array( |
| 188 | 'name' => 'fields', |
| 189 | 'type' => 'assoc', |
| 190 | 'description' => __( 'Limit response to specific fields. Defaults to all fields.', 'woocommerce' ), |
| 191 | 'optional' => true, |
| 192 | ); |
| 193 | $synopsis[] = array( |
| 194 | 'name' => 'field', |
| 195 | 'type' => 'assoc', |
| 196 | 'description' => __( 'Get the value of an individual field.', 'woocommerce' ), |
| 197 | 'optional' => true, |
| 198 | ); |
| 199 | $synopsis[] = array( |
| 200 | 'name' => 'format', |
| 201 | 'type' => 'assoc', |
| 202 | 'description' => __( 'Render response in a particular format.', 'woocommerce' ), |
| 203 | 'optional' => true, |
| 204 | 'default' => 'table', |
| 205 | 'options' => array( |
| 206 | 'table', |
| 207 | 'json', |
| 208 | 'csv', |
| 209 | 'ids', |
| 210 | 'yaml', |
| 211 | 'count', |
| 212 | 'headers', |
| 213 | 'body', |
| 214 | 'envelope', |
| 215 | ), |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | if ( in_array( $command, array( 'create', 'update', 'delete' ), true ) ) { |
| 220 | $synopsis[] = array( |
| 221 | 'name' => 'porcelain', |
| 222 | 'type' => 'flag', |
| 223 | 'description' => __( 'Output just the id when the operation is successful.', 'woocommerce' ), |
| 224 | 'optional' => true, |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | $methods = array( |
| 229 | 'list' => 'list_items', |
| 230 | 'create' => 'create_item', |
| 231 | 'delete' => 'delete_item', |
| 232 | 'get' => 'get_item', |
| 233 | 'update' => 'update_item', |
| 234 | ); |
| 235 | |
| 236 | $before_invoke = null; |
| 237 | if ( empty( $command_args['when'] ) && \WP_CLI::get_config( 'debug' ) ) { |
| 238 | $before_invoke = function() { |
| 239 | wc_maybe_define_constant( 'SAVEQUERIES', true ); |
| 240 | }; |
| 241 | } |
| 242 | |
| 243 | WP_CLI::add_command( |
| 244 | "{$parent} {$command}", |
| 245 | array( $rest_command, $methods[ $command ] ), |
| 246 | array( |
| 247 | 'synopsis' => $synopsis, |
| 248 | 'when' => ! empty( $command_args['when'] ) ? $command_args['when'] : '', |
| 249 | 'before_invoke' => $before_invoke, |
| 250 | ) |
| 251 | ); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 |