add get single tax class, support cli tax class delete

This commit is contained in:
Ron Rennick 2020-08-11 17:34:46 -03:00
parent cbfe4bd595
commit c605b93737
3 changed files with 41 additions and 5 deletions

View File

@ -144,13 +144,18 @@ class WC_CLI_REST_Command {
*/
public function delete_item( $args, $assoc_args ) {
list( $status, $body ) = $this->do_request( 'DELETE', $this->get_filled_route( $args ), $assoc_args );
$object_id = isset( $body['id'] ) ? $body['id'] : '';
if ( ! $object_id && isset( $body['slug'] ) ) {
$object_id = $body['slug'];
}
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
WP_CLI::line( $body['id'] );
WP_CLI::line( $object_id );
} else {
if ( empty( $assoc_args['force'] ) ) {
WP_CLI::success( __( 'Trashed', 'woocommerce' ) . " {$this->name} {$body['id']}" );
WP_CLI::success( __( 'Trashed', 'woocommerce' ) . " {$this->name} {$object_id}" );
} else {
WP_CLI::success( __( 'Deleted', 'woocommerce' ) . " {$this->name} {$body['id']}." );
WP_CLI::success( __( 'Deleted', 'woocommerce' ) . " {$this->name} {$object_id}." );
}
}
}
@ -404,7 +409,7 @@ EOT;
foreach ( $this->get_supported_ids() as $id_name => $id_desc ) {
if ( 'id' !== $id_name && strpos( $route, '<' . $id_name . '>' ) !== false && ! empty( $args ) ) {
$route = str_replace( '(?P<' . $id_name . '>[\d]+)', $args[0], $route );
$route = str_replace( array( '(?P<' . $id_name . '>[\d]+)', '(?P<' . $id_name . '>\w[\w\s\-]*)' ), $args[0], $route );
$supported_id_matched = true;
}
}

View File

@ -115,10 +115,10 @@ class WC_CLI_Runner {
'zone_id' => __( 'Zone ID.', 'woocommerce' ),
'instance_id' => __( 'Instance ID.', 'woocommerce' ),
'id' => __( 'The ID for the resource.', 'woocommerce' ),
'slug' => __( 'The slug for the resource.', 'woocommerce' ),
);
$rest_command->set_supported_ids( $supported_ids );
$positional_args = array_keys( $supported_ids );
$parent = "wc {$route_data['schema']['title']}";
$supported_commands = array();

View File

@ -63,6 +63,11 @@ class WC_REST_Tax_Classes_V1_Controller extends WC_REST_Controller {
'type' => 'string',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
@ -157,6 +162,32 @@ class WC_REST_Tax_Classes_V1_Controller extends WC_REST_Controller {
return rest_ensure_response( $data );
}
/**
* Get one tax class.
*
* @param WP_REST_Request $request
* @return array
*/
public function get_item( $request ) {
if ( 'standard' === $request['slug'] ) {
$tax_class = array(
'slug' => 'standard',
'name' => __( 'Standard rate', 'woocommerce' ),
);
} else {
$tax_class = WC_Tax::get_tax_class_by( 'slug', sanitize_title( $request['slug'] ) );
}
$data = array();
if ( $tax_class ) {
$class = $this->prepare_item_for_response( $tax_class, $request );
$class = $this->prepare_response_for_collection( $class );
$data[] = $class;
}
return rest_ensure_response( $data );
}
/**
* Create a single tax class.
*