From 309d118287e10dd2d4b137ed832383c8d30eac78 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Thu, 9 Feb 2017 10:29:24 -0800 Subject: [PATCH] Restructure ID route code for the CLI & make the attribute term command work. --- includes/cli/class-wc-cli-rest-command.php | 41 ++++++++++++------ includes/cli/class-wc-cli-runner.php | 49 +++++++++------------- 2 files changed, 48 insertions(+), 42 deletions(-) diff --git a/includes/cli/class-wc-cli-rest-command.php b/includes/cli/class-wc-cli-rest-command.php index 13e19bc78fa..ffc64cff879 100644 --- a/includes/cli/class-wc-cli-rest-command.php +++ b/includes/cli/class-wc-cli-rest-command.php @@ -49,6 +49,11 @@ class WC_CLI_REST_Command { */ private $output_nesting_level = 0; + /** + * List of supported IDs and their description (name => desc). + */ + private $supported_ids = array(); + /** * Sets up REST Command. * @@ -73,6 +78,24 @@ class WC_CLI_REST_Command { } } + /** + * Passes supported ID arguments (things like product_id, order_id, etc) that we should look for in addition to id. + * + * @param array $supported_ids + */ + public function set_supported_ids( $supported_ids = array() ) { + $this->supported_ids = $supported_ids; + } + + /** + * Peturns an ID of supported ID arguments (things like product_id, order_id, etc) that we should look for in addition to id. + * + * @return array + */ + public function get_supported_ids() { + return $this->supported_ids; + } + /** * Create a new item. * @@ -312,18 +335,12 @@ EOT; private function get_filled_route( $args = array() ) { $parent_id_matched = false; $route = $this->route; - if ( strpos( $route, '' ) !== false && ! empty( $args ) ) { - $route = str_replace( '(?P[\d]+)', $args[0], $route ); - $parent_id_matched = true; - } elseif ( strpos( $this->route, '' ) !== false && ! empty( $args ) ) { - $route = str_replace( '(?P[\d]+)', $args[0], $route ); - $parent_id_matched = true; - } elseif ( strpos( $this->route, '' ) !== false && ! empty( $args ) ) { - $route = str_replace( '(?P[\d]+)', $args[0], $route ); - $parent_id_matched = true; - } elseif ( strpos( $this->route, '' ) !== false && ! empty( $args ) ) { - $route = str_replace( '(?P[\d]+)', $args[0], $route ); - $parent_id_matched = true; + + foreach ( $this->get_supported_ids() as $id_name => $id_desc ) { + if ( strpos( $route, '<' . $id_name . '>' ) !== false && ! empty( $args ) ) { + $route = str_replace( '(?P<' . $id_name . '>[\d]+)', $args[0], $route ); + $parent_id_matched = true; + } } $route = str_replace( array( '(?P[\d]+)', '(?P[\w-]+)' ), ( $parent_id_matched && ! empty( $args[1] ) ? $args[1] : $args[0] ), $route ); diff --git a/includes/cli/class-wc-cli-runner.php b/includes/cli/class-wc-cli-runner.php index a293f0c7891..eb358510cea 100644 --- a/includes/cli/class-wc-cli-runner.php +++ b/includes/cli/class-wc-cli-runner.php @@ -82,6 +82,17 @@ class WC_CLI_Runner { * @param array $command_args */ private static function register_route_commands( $rest_command, $route, $route_data, $command_args = array() ) { + // Define IDs that we are looking for in the routes (in addition to id) + // so that we can pass it to the rest command, and use it here to generate documentation. + $supported_ids = array( + 'product_id' => __( 'Product ID.', 'woocommerce' ), + 'customer_id' => __( 'Customer ID.', 'woocommerce' ), + 'order_id' => __( 'Order ID.', 'woocommerce' ), + 'refund_id' => __( 'Refund ID.', 'woocommerce' ), + 'attribute_id' => __( 'Attribute ID.', 'woocommerce' ), + ); + $rest_command->set_supported_ids( $supported_ids ); + $parent = "wc {$route_data['schema']['title']}"; $supported_commands = array(); @@ -123,36 +134,14 @@ class WC_CLI_Runner { $synopsis = array(); $arg_regs = array(); - if ( strpos( $route, '' ) !== false ) { - $synopsis[] = array( - 'name' => 'product_id', - 'type' => 'positional', - 'description' => __( 'Product ID.', 'woocommerce' ), - ); - } - - if ( strpos( $route, '' ) !== false ) { - $synopsis[] = array( - 'name' => 'customer_id', - 'type' => 'positional', - 'description' => __( 'Customer ID.', 'woocommerce' ), - ); - } - - if ( strpos( $route, '' ) !== false ) { - $synopsis[] = array( - 'name' => 'order_id', - 'type' => 'positional', - 'description' => __( 'Order ID.', 'woocommerce' ), - ); - } - - if ( strpos( $route, '' ) !== false ) { - $synopsis[] = array( - 'name' => 'refund_id', - 'type' => 'positional', - 'description' => __( 'Refund ID.', 'woocommerce' ), - ); + foreach ( $supported_ids as $id_name => $id_desc ) { + if ( strpos( $route, '<' . $id_name . '>' ) !== false ) { + $synopsis[] = array( + 'name' => $id_name, + 'type' => 'positional', + 'description' => $id_desc, + ); + } } if ( in_array( $command, array( 'delete', 'get', 'update' ) ) ) {