Restructure ID route code for the CLI & make the attribute term command work.

This commit is contained in:
Justin Shreve 2017-02-09 10:29:24 -08:00
parent 988175b746
commit 309d118287
2 changed files with 48 additions and 42 deletions

View File

@ -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, '<customer_id>' ) !== false && ! empty( $args ) ) {
$route = str_replace( '(?P<customer_id>[\d]+)', $args[0], $route );
$parent_id_matched = true;
} elseif ( strpos( $this->route, '<product_id>' ) !== false && ! empty( $args ) ) {
$route = str_replace( '(?P<product_id>[\d]+)', $args[0], $route );
$parent_id_matched = true;
} elseif ( strpos( $this->route, '<order_id>' ) !== false && ! empty( $args ) ) {
$route = str_replace( '(?P<order_id>[\d]+)', $args[0], $route );
$parent_id_matched = true;
} elseif ( strpos( $this->route, '<refund_id>' ) !== false && ! empty( $args ) ) {
$route = str_replace( '(?P<refund_id>[\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<id>[\d]+)', '(?P<id>[\w-]+)' ), ( $parent_id_matched && ! empty( $args[1] ) ? $args[1] : $args[0] ), $route );

View File

@ -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, '<product_id>' ) !== false ) {
$synopsis[] = array(
'name' => 'product_id',
'type' => 'positional',
'description' => __( 'Product ID.', 'woocommerce' ),
);
}
if ( strpos( $route, '<customer_id>' ) !== false ) {
$synopsis[] = array(
'name' => 'customer_id',
'type' => 'positional',
'description' => __( 'Customer ID.', 'woocommerce' ),
);
}
if ( strpos( $route, '<order_id>' ) !== false ) {
$synopsis[] = array(
'name' => 'order_id',
'type' => 'positional',
'description' => __( 'Order ID.', 'woocommerce' ),
);
}
if ( strpos( $route, '<refund_id>' ) !== 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' ) ) ) {