Merge pull request #4185 from maxrice/rest-api-fixes-issue-4055
Various fixes for REST API
This commit is contained in:
commit
8054316fa6
|
@ -69,6 +69,7 @@ class WC_Admin {
|
|||
case 'users' :
|
||||
case 'user' :
|
||||
case 'profile' :
|
||||
case 'user-edit' :
|
||||
include( 'class-wc-admin-profile.php' );
|
||||
break;
|
||||
}
|
||||
|
@ -119,4 +120,4 @@ class WC_Admin {
|
|||
|
||||
endif;
|
||||
|
||||
return new WC_Admin();
|
||||
return new WC_Admin();
|
||||
|
|
|
@ -20,8 +20,8 @@ class WC_API_Authentication {
|
|||
*/
|
||||
public function __construct() {
|
||||
|
||||
// this filter can be removed in order to provide unauthenticated access to the API for testing, etc
|
||||
add_filter( 'woocommerce_api_check_authentication', array( $this, 'authenticate' ) );
|
||||
// to disable authentication, hook into this filter at a later priority and return a valid WP_User
|
||||
add_filter( 'woocommerce_api_check_authentication', array( $this, 'authenticate' ), 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,7 +109,7 @@ class WC_API_Authentication {
|
|||
foreach ( $param_names as $param_name ) {
|
||||
|
||||
if ( empty( $params ) )
|
||||
throw new Exception( sprintf( __( '%s parameter is missing', 'woocommerce' ), $param_name ) );
|
||||
throw new Exception( sprintf( __( '%s parameter is missing', 'woocommerce' ), $param_name ), 404 );
|
||||
}
|
||||
|
||||
// fetch WP user by consumer key
|
||||
|
|
|
@ -21,9 +21,9 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
/**
|
||||
* Register the routes for this class
|
||||
*
|
||||
* GET|POST /coupons
|
||||
* GET /coupons
|
||||
* GET /coupons/count
|
||||
* GET|PUT|DELETE /coupons/<id>
|
||||
* GET /coupons/<id>
|
||||
*
|
||||
* @since 2.1
|
||||
* @param array $routes
|
||||
|
@ -31,10 +31,9 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
*/
|
||||
public function register_routes( $routes ) {
|
||||
|
||||
# GET|POST /coupons
|
||||
# GET /coupons
|
||||
$routes[ $this->base ] = array(
|
||||
array( array( $this, 'get_coupons' ), WC_API_Server::READABLE ),
|
||||
array( array( $this, 'create_coupon' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ),
|
||||
);
|
||||
|
||||
# GET /coupons/count
|
||||
|
@ -42,11 +41,9 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
array( array( $this, 'get_coupons_count' ), WC_API_Server::READABLE ),
|
||||
);
|
||||
|
||||
# GET|PUT|DELETE /coupons/<id>
|
||||
# GET /coupons/<id>
|
||||
$routes[ $this->base . '/(?P<id>\d+)' ] = array(
|
||||
array( array( $this, 'get_coupon' ), WC_API_Server::READABLE ),
|
||||
array( array( $this, 'edit_coupon' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
|
||||
array( array( $this, 'delete_coupon' ), WC_API_Server::DELETABLE ),
|
||||
);
|
||||
|
||||
# GET /coupons/code/<code>, note that coupon codes can contain spaces, dashes and underscores
|
||||
|
@ -114,27 +111,27 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
$coupon_post = get_post( $coupon->id );
|
||||
|
||||
$coupon_data = array(
|
||||
'id' => $coupon->id,
|
||||
'code' => $coupon->code,
|
||||
'type' => $coupon->type,
|
||||
'created_at' => $this->server->format_datetime( $coupon_post->post_date_gmt ),
|
||||
'updated_at' => $this->server->format_datetime( $coupon_post->post_modified_gmt ),
|
||||
'amount' => woocommerce_format_decimal( $coupon->amount ),
|
||||
'individual_use' => $coupon->individual_use,
|
||||
'product_ids' => $coupon->product_ids,
|
||||
'exclude_product_ids' => $coupon->exclude_product_ids,
|
||||
'usage_limit' => $coupon->usage_limit,
|
||||
'usage_limit_per_user' => $coupon->usage_limit_per_user,
|
||||
'limit_usage_to_x_items' => $coupon->limit_usage_to_x_items,
|
||||
'usage_count' => $coupon->usage_count,
|
||||
'expiry_date' => $this->server->format_datetime( $coupon->expiry_date ),
|
||||
'apply_before_tax' => $coupon->apply_before_tax(),
|
||||
'enable_free_shipping' => $coupon->enable_free_shipping(),
|
||||
'product_categories' => $coupon->product_categories,
|
||||
'exclude_product_categories' => $coupon->exclude_product_categories,
|
||||
'exclude_sale_items' => $coupon->exclude_sale_items(),
|
||||
'minimum_amount' => $coupon->minimum_amount,
|
||||
'customer_email' => $coupon->customer_email,
|
||||
'id' => $coupon->id,
|
||||
'code' => $coupon->code,
|
||||
'type' => $coupon->type,
|
||||
'created_at' => $this->server->format_datetime( $coupon_post->post_date_gmt ),
|
||||
'updated_at' => $this->server->format_datetime( $coupon_post->post_modified_gmt ),
|
||||
'amount' => woocommerce_format_decimal( $coupon->amount, 2 ),
|
||||
'individual_use' => ( 'yes' === $coupon->individual_use ),
|
||||
'product_ids' => array_map( 'absint', $coupon->product_ids ),
|
||||
'exclude_product_ids' => array_map( 'absint', $coupon->exclude_product_ids ),
|
||||
'usage_limit' => ( ! empty( $coupon->usage_limit ) ) ? $coupon->usage_limit : null,
|
||||
'usage_limit_per_user' => ( ! empty( $coupon->usage_limit_per_user ) ) ? $coupon->usage_limit_per_user : null,
|
||||
'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items,
|
||||
'usage_count' => (int) $coupon->usage_count,
|
||||
'expiry_date' => $this->server->format_datetime( $coupon->expiry_date ),
|
||||
'apply_before_tax' => $coupon->apply_before_tax(),
|
||||
'enable_free_shipping' => $coupon->enable_free_shipping(),
|
||||
'product_category_ids' => array_map( 'absint', $coupon->product_categories ),
|
||||
'exclude_product_category_ids' => array_map( 'absint', $coupon->exclude_product_categories ),
|
||||
'exclude_sale_items' => $coupon->exclude_sale_items(),
|
||||
'minimum_amount' => woocommerce_format_decimal( $coupon->minimum_amount, 2 ),
|
||||
'customer_emails' => $coupon->customer_email,
|
||||
);
|
||||
|
||||
return array( 'coupon' => apply_filters( 'woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server ) );
|
||||
|
@ -151,9 +148,10 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
|
||||
$query = $this->query_coupons( $filter );
|
||||
|
||||
// TODO: permissions?
|
||||
if ( ! current_user_can( 'read_private_shop_coupons' ) )
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_read_coupons_count', __( 'You do not have permission to read the coupons count', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
|
||||
return array( 'count' => $query->found_posts );
|
||||
return array( 'count' => (int) $query->found_posts );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -178,23 +176,19 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
/**
|
||||
* Create a coupon
|
||||
*
|
||||
* @since 2.1
|
||||
* @TODO implement in 2.2
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function create_coupon( $data ) {
|
||||
|
||||
// TODO: permissions check
|
||||
|
||||
// TODO: implement - what's the minimum set of data required?
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a coupon
|
||||
*
|
||||
* @since 2.1
|
||||
* @TODO implement in 2.2
|
||||
* @param int $id the coupon ID
|
||||
* @param array $data
|
||||
* @return array
|
||||
|
@ -206,15 +200,13 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
if ( is_wp_error( $id ) )
|
||||
return $id;
|
||||
|
||||
// TODO: implement
|
||||
|
||||
return $this->get_coupon( $id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a coupon
|
||||
*
|
||||
* @since 2.1
|
||||
* @TODO enable along with PUT/POST in 2.2
|
||||
* @param int $id the coupon ID
|
||||
* @param bool $force true to permanently delete coupon, false to move to trash
|
||||
* @return array
|
||||
|
|
|
@ -44,9 +44,9 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
/**
|
||||
* Register the routes for this class
|
||||
*
|
||||
* GET|POST /customers
|
||||
* GET /customers
|
||||
* GET /customers/count
|
||||
* GET|PUT|DELETE /customers/<id>
|
||||
* GET /customers/<id>
|
||||
* GET /customers/<id>/orders
|
||||
*
|
||||
* @since 2.1
|
||||
|
@ -55,10 +55,9 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
*/
|
||||
public function register_routes( $routes ) {
|
||||
|
||||
# GET|POST /customers
|
||||
# GET /customers
|
||||
$routes[ $this->base ] = array(
|
||||
array( array( $this, 'get_customers' ), WC_API_SERVER::READABLE ),
|
||||
array( array( $this, 'create_customer' ), WC_API_SERVER::CREATABLE | WC_API_SERVER::ACCEPT_DATA ),
|
||||
);
|
||||
|
||||
# GET /customers/count
|
||||
|
@ -66,11 +65,9 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
array( array( $this, 'get_customers_count' ), WC_API_SERVER::READABLE ),
|
||||
);
|
||||
|
||||
# GET|PUT|DELETE /customers/<id>
|
||||
# GET /customers/<id>
|
||||
$routes[ $this->base . '/(?P<id>\d+)' ] = array(
|
||||
array( array( $this, 'get_customer' ), WC_API_SERVER::READABLE ),
|
||||
array( array( $this, 'edit_customer' ), WC_API_SERVER::EDITABLE | WC_API_SERVER::ACCEPT_DATA ),
|
||||
array( array( $this, 'delete_customer' ), WC_API_SERVER::DELETABLE ),
|
||||
);
|
||||
|
||||
# GET /customers/<id>/orders
|
||||
|
@ -149,7 +146,7 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
'last_order_id' => is_object( $last_order ) ? $last_order->id : null,
|
||||
'last_order_date' => is_object( $last_order ) ? $this->server->format_datetime( $last_order->post_date_gmt ) : null,
|
||||
'orders_count' => (int) $customer->_order_count,
|
||||
'total_spent' => woocommerce_format_decimal( $customer->_money_spent ),
|
||||
'total_spent' => woocommerce_format_decimal( $customer->_money_spent, 2 ),
|
||||
'avatar_url' => $this->get_avatar_url( $customer->customer_email ),
|
||||
'billing_address' => array(
|
||||
'first_name' => $customer->billing_first_name,
|
||||
|
@ -192,7 +189,7 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
$query = $this->query_customers( $filter );
|
||||
|
||||
if ( ! current_user_can( 'list_users' ) )
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_read_customer', __( 'You do not have permission to read customers', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_read_customers_count', __( 'You do not have permission to read the customers count', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
|
||||
return array( 'count' => count( $query->get_results() ) );
|
||||
}
|
||||
|
@ -201,7 +198,7 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
/**
|
||||
* Create a customer
|
||||
*
|
||||
* @since 2.1
|
||||
* @TODO implement in 2.2 with woocommerce_create_new_customer()
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
|
@ -210,15 +207,13 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
if ( ! current_user_can( 'create_users' ) )
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_create_customer', __( 'You do not have permission to create this customer', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
|
||||
// TODO: implement - woocommerce_create_new_customer()
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a customer
|
||||
*
|
||||
* @since 2.1
|
||||
* @TODO implement in 2.2
|
||||
* @param int $id the customer ID
|
||||
* @param array $data
|
||||
* @return array
|
||||
|
@ -230,15 +225,13 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
if ( ! is_wp_error( $id ) )
|
||||
return $id;
|
||||
|
||||
// TODO: implement
|
||||
|
||||
return $this->get_customer( $id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a customer
|
||||
*
|
||||
* @since 2.1
|
||||
* @TODO enable along with PUT/POST in 2.2
|
||||
* @param int $id the customer ID
|
||||
* @return array
|
||||
*/
|
||||
|
|
|
@ -22,7 +22,7 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
*
|
||||
* GET /orders
|
||||
* GET /orders/count
|
||||
* GET|PUT|DELETE /orders/<id>
|
||||
* GET|PUT /orders/<id>
|
||||
* GET /orders/<id>/notes
|
||||
*
|
||||
* @since 2.1
|
||||
|
@ -41,11 +41,10 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
array( array( $this, 'get_orders_count' ), WC_API_Server::READABLE ),
|
||||
);
|
||||
|
||||
# GET|PUT|DELETE /orders/<id>
|
||||
# GET|PUT /orders/<id>
|
||||
$routes[ $this->base . '/(?P<id>\d+)' ] = array(
|
||||
array( array( $this, 'get_order' ), WC_API_Server::READABLE ),
|
||||
array( array( $this, 'edit_order' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
|
||||
array( array( $this, 'delete_order' ), WC_API_Server::DELETABLE ),
|
||||
);
|
||||
|
||||
# GET /orders/<id>/notes
|
||||
|
@ -119,15 +118,15 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
'completed_at' => $this->server->format_datetime( $order->completed_date, true ),
|
||||
'status' => $order->status,
|
||||
'currency' => $order->order_currency,
|
||||
'total' => woocommerce_format_decimal( $order->get_total() ),
|
||||
'total' => woocommerce_format_decimal( $order->get_total(), 2 ),
|
||||
'total_line_items_quantity' => $order->get_item_count(),
|
||||
'total_tax' => woocommerce_format_decimal( $order->get_total_tax() ),
|
||||
'total_shipping' => woocommerce_format_decimal( $order->get_total_shipping() ),
|
||||
'cart_tax' => woocommerce_format_decimal( $order->get_cart_tax() ),
|
||||
'shipping_tax' => woocommerce_format_decimal( $order->get_shipping_tax() ),
|
||||
'total_discount' => woocommerce_format_decimal( $order->get_total_discount() ),
|
||||
'cart_discount' => woocommerce_format_decimal( $order->get_cart_discount() ),
|
||||
'order_discount' => woocommerce_format_decimal( $order->get_order_discount() ),
|
||||
'total_tax' => woocommerce_format_decimal( $order->get_total_tax(), 2 ),
|
||||
'total_shipping' => woocommerce_format_decimal( $order->get_total_shipping(), 2 ),
|
||||
'cart_tax' => woocommerce_format_decimal( $order->get_cart_tax(), 2 ),
|
||||
'shipping_tax' => woocommerce_format_decimal( $order->get_shipping_tax(), 2 ),
|
||||
'total_discount' => woocommerce_format_decimal( $order->get_total_discount(), 2 ),
|
||||
'cart_discount' => woocommerce_format_decimal( $order->get_cart_discount(), 2 ),
|
||||
'order_discount' => woocommerce_format_decimal( $order->get_order_discount(), 2 ),
|
||||
'shipping_methods' => $order->get_shipping_method(),
|
||||
'payment_details' => array(
|
||||
'method_id' => $order->payment_method,
|
||||
|
@ -177,9 +176,9 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
|
||||
$order_data['line_items'][] = array(
|
||||
'id' => $item_id,
|
||||
'subtotal' => woocommerce_format_decimal( $order->get_line_subtotal( $item ) ),
|
||||
'total' => woocommerce_format_decimal( $order->get_line_total( $item ) ),
|
||||
'total_tax' => woocommerce_format_decimal( $order->get_line_tax( $item ) ),
|
||||
'subtotal' => woocommerce_format_decimal( $order->get_line_subtotal( $item ), 2 ),
|
||||
'total' => woocommerce_format_decimal( $order->get_line_total( $item ), 2 ),
|
||||
'total_tax' => woocommerce_format_decimal( $order->get_line_tax( $item ), 2 ),
|
||||
'quantity' => (int) $item['qty'],
|
||||
'tax_class' => ( ! empty( $item['tax_class'] ) ) ? $item['tax_class'] : null,
|
||||
'name' => $item['name'],
|
||||
|
@ -195,7 +194,7 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
'id' => $shipping_item_id,
|
||||
'method_id' => $shipping_item['method_id'],
|
||||
'method_title' => $shipping_item['name'],
|
||||
'total' => woocommerce_format_decimal( $shipping_item['cost'] ),
|
||||
'total' => woocommerce_format_decimal( $shipping_item['cost'], 2 ),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -205,7 +204,7 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
$order_data['tax_lines'][] = array(
|
||||
'code' => $tax_code,
|
||||
'title' => $tax->label,
|
||||
'total' => woocommerce_format_decimal( $tax->amount ),
|
||||
'total' => woocommerce_format_decimal( $tax->amount, 2 ),
|
||||
'compound' => (bool) $tax->is_compound,
|
||||
);
|
||||
}
|
||||
|
@ -217,8 +216,8 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
'id' => $fee_item_id,
|
||||
'title' => $fee_item['name'],
|
||||
'tax_class' => ( ! empty( $fee_item['tax_class'] ) ) ? $fee_item['tax_class'] : null,
|
||||
'total' => woocommerce_format_decimal( $order->get_line_total( $fee_item ) ),
|
||||
'total_tax' => woocommerce_format_decimal( $order->get_line_tax( $fee_item ) ),
|
||||
'total' => woocommerce_format_decimal( $order->get_line_total( $fee_item ), 2 ),
|
||||
'total_tax' => woocommerce_format_decimal( $order->get_line_tax( $fee_item ), 2 ),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -228,7 +227,7 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
$order_data['coupon_lines'] = array(
|
||||
'id' => $coupon_item_id,
|
||||
'code' => $coupon_item['name'],
|
||||
'amount' => woocommerce_format_decimal( $coupon_item['discount_amount'] ),
|
||||
'amount' => woocommerce_format_decimal( $coupon_item['discount_amount'], 2 ),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -250,12 +249,12 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
|
||||
$query = $this->query_orders( $filter );
|
||||
|
||||
// TODO: permissions?
|
||||
if ( ! current_user_can( 'read_private_shop_orders' ) )
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_read_orders_count', __( 'You do not have permission to read the orders count', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
|
||||
return array( 'count' => $query->found_posts );
|
||||
return array( 'count' => (int) $query->found_posts );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Edit an order
|
||||
*
|
||||
|
@ -268,7 +267,7 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
*/
|
||||
public function edit_order( $id, $data ) {
|
||||
|
||||
$id = $this->validate_request( $id, 'shop_order', 'write' );
|
||||
$id = $this->validate_request( $id, 'shop_order', 'edit' );
|
||||
|
||||
if ( is_wp_error( $id ) )
|
||||
return $id;
|
||||
|
@ -286,7 +285,7 @@ class WC_API_Orders extends WC_API_Resource {
|
|||
/**
|
||||
* Delete an order
|
||||
*
|
||||
* @since 2.1
|
||||
* @TODO enable along with POST in 2.2
|
||||
* @param int $id the order ID
|
||||
* @param bool $force true to permanently delete order, false to move to trash
|
||||
* @return array
|
||||
|
|
|
@ -22,7 +22,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
*
|
||||
* GET /products
|
||||
* GET /products/count
|
||||
* GET|PUT|DELETE /products/<id>
|
||||
* GET /products/<id>
|
||||
* GET /products/<id>/reviews
|
||||
*
|
||||
* @since 2.1
|
||||
|
@ -41,11 +41,9 @@ class WC_API_Products extends WC_API_Resource {
|
|||
array( array( $this, 'get_products_count' ), WC_API_Server::READABLE ),
|
||||
);
|
||||
|
||||
# GET|PUT|DELETE /products/<id>
|
||||
# GET /products/<id>
|
||||
$routes[ $this->base . '/(?P<id>\d+)' ] = array(
|
||||
array( array( $this, 'get_product' ), WC_API_Server::READABLE ),
|
||||
array( array( $this, 'edit_product' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
|
||||
array( array( $this, 'delete_product' ), WC_API_Server::DELETABLE ),
|
||||
);
|
||||
|
||||
# GET /products/<id>/reviews
|
||||
|
@ -138,17 +136,18 @@ class WC_API_Products extends WC_API_Resource {
|
|||
if ( ! empty( $type ) )
|
||||
$filter['type'] = $type;
|
||||
|
||||
// TODO: permissions?
|
||||
if ( ! current_user_can( 'read_private_products' ) )
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_read_products_count', __( 'You do not have permission to read the products count', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
|
||||
$query = $this->query_products( $filter );
|
||||
|
||||
return array( 'count' => $query->found_posts );
|
||||
return array( 'count' => (int) $query->found_posts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a product
|
||||
*
|
||||
* @since 2.1
|
||||
* @TODO implement in 2.2
|
||||
* @param int $id the product ID
|
||||
* @param array $data
|
||||
* @return array
|
||||
|
@ -160,15 +159,13 @@ class WC_API_Products extends WC_API_Resource {
|
|||
if ( is_wp_error( $id ) )
|
||||
return $id;
|
||||
|
||||
// TODO: implement
|
||||
|
||||
return $this->get_product( $id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a product
|
||||
*
|
||||
* @since 2.1
|
||||
* @TODO enable along with PUT/POST in 2.2
|
||||
* @param int $id the product ID
|
||||
* @param bool $force true to permanently delete order, false to move to trash
|
||||
* @return array
|
||||
|
@ -281,9 +278,9 @@ class WC_API_Products extends WC_API_Resource {
|
|||
'virtual' => $product->is_virtual(),
|
||||
'permalink' => $product->get_permalink(),
|
||||
'sku' => $product->get_sku(),
|
||||
'price' => woocommerce_format_decimal( $product->get_price() ),
|
||||
'regular_price' => woocommerce_format_decimal( $product->get_regular_price() ),
|
||||
'sale_price' => $product->get_sale_price() ? woocommerce_format_decimal( $product->get_sale_price() ) : null,
|
||||
'price' => woocommerce_format_decimal( $product->get_price(), 2 ),
|
||||
'regular_price' => woocommerce_format_decimal( $product->get_regular_price(), 2 ),
|
||||
'sale_price' => $product->get_sale_price() ? woocommerce_format_decimal( $product->get_sale_price(), 2 ) : null,
|
||||
'price_html' => $product->get_price_html(),
|
||||
'taxable' => $product->is_taxable(),
|
||||
'tax_status' => $product->get_tax_status(),
|
||||
|
@ -299,7 +296,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
'visible' => $product->is_visible(),
|
||||
'catalog_visibility' => $product->visibility,
|
||||
'on_sale' => $product->is_on_sale(),
|
||||
'weight' => $product->get_weight() ? woocommerce_format_decimal( $product->get_weight() ) : null,
|
||||
'weight' => $product->get_weight() ? woocommerce_format_decimal( $product->get_weight(), 2 ) : null,
|
||||
'dimensions' => array(
|
||||
'length' => $product->length,
|
||||
'width' => $product->width,
|
||||
|
@ -313,7 +310,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
'description' => apply_filters( 'the_content', $product->get_post_data()->post_content ),
|
||||
'short_description' => apply_filters( 'woocommerce_short_description', $product->get_post_data()->post_excerpt ),
|
||||
'reviews_allowed' => ( 'open' === $product->get_post_data()->comment_status ),
|
||||
'average_rating' => woocommerce_format_decimal( $product->get_average_rating() ),
|
||||
'average_rating' => woocommerce_format_decimal( $product->get_average_rating(), 2 ),
|
||||
'rating_count' => (int) $product->get_rating_count(),
|
||||
'related_ids' => array_map( 'absint', array_values( $product->get_related() ) ),
|
||||
'upsell_ids' => array_map( 'absint', $product->get_upsells() ),
|
||||
|
@ -327,6 +324,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
'download_expiry' => (int) $product->download_expiry,
|
||||
'download_type' => $product->download_type,
|
||||
'purchase_note' => apply_filters( 'the_content', $product->purchase_note ),
|
||||
'total_sales' => metadata_exists( 'post', $product->id, 'total_sales' ) ? (int) get_post_meta( $product->id, 'total_sales', true ) : 0,
|
||||
'variations' => array(),
|
||||
'parent' => array(),
|
||||
);
|
||||
|
@ -358,9 +356,9 @@ class WC_API_Products extends WC_API_Resource {
|
|||
'virtual' => $variation->is_virtual(),
|
||||
'permalink' => $variation->get_permalink(),
|
||||
'sku' => $variation->get_sku(),
|
||||
'price' => woocommerce_format_decimal( $variation->get_price() ),
|
||||
'regular_price' => woocommerce_format_decimal( $variation->get_regular_price() ),
|
||||
'sale_price' => $variation->get_sale_price() ? woocommerce_format_decimal( $variation->get_sale_price() ) : null,
|
||||
'price' => woocommerce_format_decimal( $variation->get_price(), 2 ),
|
||||
'regular_price' => woocommerce_format_decimal( $variation->get_regular_price(), 2 ),
|
||||
'sale_price' => $variation->get_sale_price() ? woocommerce_format_decimal( $variation->get_sale_price(), 2 ) : null,
|
||||
'taxable' => $variation->is_taxable(),
|
||||
'tax_status' => $variation->get_tax_status(),
|
||||
'tax_class' => $variation->get_tax_class(),
|
||||
|
@ -370,7 +368,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
'purchaseable' => $variation->is_purchasable(),
|
||||
'visible' => $variation->variation_is_visible(),
|
||||
'on_sale' => $variation->is_on_sale(),
|
||||
'weight' => $variation->get_weight() ? woocommerce_format_decimal( $variation->get_weight() ) : null,
|
||||
'weight' => $variation->get_weight() ? woocommerce_format_decimal( $variation->get_weight(), 2 ) : null,
|
||||
'dimensions' => array(
|
||||
'length' => $variation->length,
|
||||
'width' => $variation->width,
|
||||
|
|
|
@ -143,6 +143,23 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
'filter_range' => true,
|
||||
) );
|
||||
|
||||
// new customers
|
||||
$users_query = new WP_User_Query(
|
||||
array(
|
||||
'fields' => array( 'user_registered' ),
|
||||
'role' => 'customer',
|
||||
)
|
||||
);
|
||||
|
||||
$customers = $users_query->get_results();
|
||||
|
||||
foreach ( $customers as $key => $customer ) {
|
||||
if ( strtotime( $customer->user_registered ) < $this->report->start_date || strtotime( $customer->user_registered ) > $this->report->end_date )
|
||||
unset( $customers[ $key ] );
|
||||
}
|
||||
|
||||
$total_customers = count( $customers );
|
||||
|
||||
// get order totals grouped by period
|
||||
$orders = $this->report->get_order_report_data( array(
|
||||
'data' => array(
|
||||
|
@ -255,12 +272,12 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
}
|
||||
|
||||
$period_totals[ $time ] = array(
|
||||
'sales' => woocommerce_format_decimal( 0.00 ),
|
||||
'sales' => woocommerce_format_decimal( 0.00, 2 ),
|
||||
'orders' => 0,
|
||||
'items' => 0,
|
||||
'tax' => woocommerce_format_decimal( 0.00 ),
|
||||
'shipping' => woocommerce_format_decimal( 0.00 ),
|
||||
'discount' => woocommerce_format_decimal( 0.00 ),
|
||||
'tax' => woocommerce_format_decimal( 0.00, 2 ),
|
||||
'shipping' => woocommerce_format_decimal( 0.00, 2 ),
|
||||
'discount' => woocommerce_format_decimal( 0.00, 2 ),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -272,10 +289,10 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
if ( ! isset( $period_totals[ $time ] ) )
|
||||
continue;
|
||||
|
||||
$period_totals[ $time ]['sales'] = woocommerce_format_decimal( $order->total_sales );
|
||||
$period_totals[ $time ]['sales'] = woocommerce_format_decimal( $order->total_sales, 2 );
|
||||
$period_totals[ $time ]['orders'] = (int) $order->total_orders;
|
||||
$period_totals[ $time ]['tax'] = woocommerce_format_decimal( $order->total_tax + $order->total_shipping_tax );
|
||||
$period_totals[ $time ]['shipping'] = woocommerce_format_decimal( $order->total_shipping );
|
||||
$period_totals[ $time ]['tax'] = woocommerce_format_decimal( $order->total_tax + $order->total_shipping_tax, 2 );
|
||||
$period_totals[ $time ]['shipping'] = woocommerce_format_decimal( $order->total_shipping, 2 );
|
||||
}
|
||||
|
||||
// add total order items for each period
|
||||
|
@ -297,19 +314,20 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
if ( ! isset( $period_totals[ $time ] ) )
|
||||
continue;
|
||||
|
||||
$period_totals[ $time ]['discount'] = woocommerce_format_decimal( $discount->discount_amount );
|
||||
$period_totals[ $time ]['discount'] = woocommerce_format_decimal( $discount->discount_amount, 2 );
|
||||
}
|
||||
|
||||
$sales_data = array(
|
||||
'sales' => woocommerce_format_decimal( $totals->sales ),
|
||||
'average' => woocommerce_format_decimal( $totals->sales / ( $this->report->chart_interval + 1 ) ),
|
||||
'orders' => (int) $totals->order_count,
|
||||
'items' => $total_items,
|
||||
'tax' => woocommerce_format_decimal( $totals->tax + $totals->shipping_tax ),
|
||||
'shipping' => woocommerce_format_decimal( $totals->shipping ),
|
||||
'discount' => is_null( $total_discount ) ? woocommerce_format_decimal( 0.00 ) : woocommerce_format_decimal( $total_discount ),
|
||||
'total_sales' => woocommerce_format_decimal( $totals->sales, 2 ),
|
||||
'average_sales' => woocommerce_format_decimal( $totals->sales / ( $this->report->chart_interval + 1 ), 2 ),
|
||||
'total_orders' => (int) $totals->order_count,
|
||||
'total_items' => $total_items,
|
||||
'total_tax' => woocommerce_format_decimal( $totals->tax + $totals->shipping_tax, 2 ),
|
||||
'total_shipping' => woocommerce_format_decimal( $totals->shipping, 2 ),
|
||||
'total_discount' => is_null( $total_discount ) ? woocommerce_format_decimal( 0.00, 2 ) : woocommerce_format_decimal( $total_discount, 2 ),
|
||||
'totals_grouped_by' => $this->report->chart_groupby,
|
||||
'totals' => $period_totals,
|
||||
'total_customers' => $total_customers,
|
||||
);
|
||||
|
||||
return array( 'sales' => apply_filters( 'woocommerce_api_report_response', $sales_data, $this->report, $fields, $this->server ) );
|
||||
|
|
|
@ -73,10 +73,10 @@ class WC_API_Resource {
|
|||
// only custom post types have per-post type/permission checks
|
||||
if ( 'customer' !== $type ) {
|
||||
|
||||
$post = get_post( $id, ARRAY_A );
|
||||
$post = get_post( $id );
|
||||
|
||||
// for checking permissions, product variations are the same as the product post type
|
||||
$post_type = ( 'product_variation' === $post['post_type'] ) ? 'product' : $post['post_type'];
|
||||
$post_type = ( 'product_variation' === $post->post_type ) ? 'product' : $post->post_type;
|
||||
|
||||
// validate post type
|
||||
if ( $type !== $post_type )
|
||||
|
@ -376,21 +376,21 @@ class WC_API_Resource {
|
|||
private function check_permission( $post, $context ) {
|
||||
|
||||
if ( ! is_a( $post, 'WP_Post' ) )
|
||||
$post = get_post( $post, ARRAY_A );
|
||||
$post = get_post( $post );
|
||||
|
||||
if ( is_null( $post ) )
|
||||
return false;
|
||||
|
||||
$post_type = get_post_type_object( $post['post_type'] );
|
||||
$post_type = get_post_type_object( $post->post_type );
|
||||
|
||||
if ( 'read' === $context )
|
||||
return current_user_can( $post_type->cap->read_post, $post['ID'] );
|
||||
return current_user_can( $post_type->cap->read_private_posts, $post->ID );
|
||||
|
||||
elseif ( 'edit' === $context )
|
||||
return current_user_can( $post_type->cap->edit_post, $post['ID'] );
|
||||
return current_user_can( $post_type->cap->edit_post, $post->ID );
|
||||
|
||||
elseif ( 'delete' === $context )
|
||||
return current_user_can( $post_type->cap->delete_post, $post['ID'] );
|
||||
return current_user_can( $post_type->cap->delete_post, $post->ID );
|
||||
|
||||
else
|
||||
return false;
|
||||
|
|
|
@ -433,7 +433,6 @@ class WC_API_Server {
|
|||
'ssl_enabled' => ( 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) ),
|
||||
'links' => array(
|
||||
'help' => 'http://docs.woothemes.com/document/woocommerce-rest-api/',
|
||||
'profile' => 'https://raw.github.com/rmccue/WP-API/master/docs/schema.json', // TODO: update this
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
@ -535,7 +534,7 @@ class WC_API_Server {
|
|||
|
||||
$page = $query->get( 'paged' );
|
||||
$single = $query->is_single();
|
||||
$total = $query->found_posts * $query->max_num_pages;
|
||||
$total = $query->found_posts;
|
||||
$total_pages = $query->max_num_pages;
|
||||
}
|
||||
|
||||
|
|
|
@ -138,15 +138,13 @@ class WC_API {
|
|||
*/
|
||||
private function includes() {
|
||||
|
||||
// TODO: are all these required?
|
||||
include_once( ABSPATH . WPINC . '/class-IXR.php' );
|
||||
include_once( ABSPATH . WPINC . '/class-wp-xmlrpc-server.php' );
|
||||
|
||||
// API server / response handlers
|
||||
include_once( 'api/class-wc-api-server.php' );
|
||||
include_once( 'api/interface-wc-api-handler.php' );
|
||||
include_once( 'api/class-wc-api-json-handler.php' );
|
||||
include_once( 'api/class-wc-api-xml-handler.php' );
|
||||
|
||||
// authentication
|
||||
include_once( 'api/class-wc-api-authentication.php' );
|
||||
$this->authentication = new WC_API_Authentication();
|
||||
|
||||
|
@ -157,24 +155,27 @@ class WC_API {
|
|||
include_once( 'api/class-wc-api-customers.php' );
|
||||
include_once( 'api/class-wc-api-reports.php' );
|
||||
|
||||
// TODO: some action to allow actors to load additional resource types or handlers
|
||||
// allow plugins to load other response handlers or resource classes
|
||||
do_action( 'woocommerce_api_loaded' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register API resources available
|
||||
* Register available API resources
|
||||
*
|
||||
* @since 2.1
|
||||
* @param object $server the REST server
|
||||
*/
|
||||
public function register_resources( $server ) {
|
||||
|
||||
$api_classes = apply_filters( 'woocommerce_api_classes', array(
|
||||
'WC_API_Customers',
|
||||
'WC_API_Orders',
|
||||
'WC_API_Products',
|
||||
'WC_API_Coupons',
|
||||
'WC_API_Reports',
|
||||
) );
|
||||
$api_classes = apply_filters( 'woocommerce_api_classes',
|
||||
array(
|
||||
'WC_API_Customers',
|
||||
'WC_API_Orders',
|
||||
'WC_API_Products',
|
||||
'WC_API_Coupons',
|
||||
'WC_API_Reports',
|
||||
)
|
||||
);
|
||||
|
||||
foreach ( $api_classes as $api_class ) {
|
||||
$this->$api_class = new $api_class( $server );
|
||||
|
|
Loading…
Reference in New Issue