commit
9c15ca936b
|
@ -27,7 +27,7 @@ abstract class WC_REST_Shipping_Zones_Controller_Base extends WC_REST_Controller
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
|
|
@ -18,128 +18,16 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Coupons controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Posts_Controller
|
||||
* @extends WC_REST_Coupons_V1_Controller
|
||||
*/
|
||||
class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
||||
class WC_REST_Coupons_Controller extends WC_REST_Coupons_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'coupons';
|
||||
|
||||
/**
|
||||
* Post type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $post_type = 'shop_coupon';
|
||||
|
||||
/**
|
||||
* Order refunds actions.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the routes for coupons.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'code' => array(
|
||||
'description' => __( 'Coupon code.', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Query args.
|
||||
*
|
||||
* @param array $args
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function query_args( $args, $request ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( ! empty( $request['code'] ) ) {
|
||||
$id = wc_get_coupon_id_by_code( $request['code'] );
|
||||
$args['post__in'] = array( $id );
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Prepare a single coupon output for response.
|
||||
|
@ -149,16 +37,11 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
* @return WP_REST_Response $data
|
||||
*/
|
||||
public function prepare_item_for_response( $post, $request ) {
|
||||
$coupon = new WC_Coupon( (int) $post->ID );
|
||||
$data = $coupon->get_data();
|
||||
|
||||
// The API returns 'expiry_date' and 'exclude_product_ids' instead of date_expires and 'excluded_product_ids'.
|
||||
$data['expiry_date'] = $data['date_expires'];
|
||||
$data['exclude_product_ids'] = $data['excluded_product_ids'];
|
||||
unset( $data['excluded_product_ids'], $data['date_expires'] );
|
||||
$coupon = new WC_Coupon( (int) $post->ID );
|
||||
$data = $coupon->get_data();
|
||||
|
||||
$format_decimal = array( 'amount', 'minimum_amount', 'maximum_amount' );
|
||||
$format_date = array( 'date_created', 'date_modified', 'expiry_date' );
|
||||
$format_date = array( 'date_created', 'date_modified', 'date_expires' );
|
||||
$format_null = array( 'usage_limit', 'usage_limit_per_user', 'limit_usage_to_x_items' );
|
||||
|
||||
// Format decimal values.
|
||||
|
@ -195,15 +78,6 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Only reutrn writeable props from schema.
|
||||
* @param array $schema
|
||||
* @return bool
|
||||
*/
|
||||
protected function filter_writable_props( $schema ) {
|
||||
return empty( $schema['readonly'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single coupon for create or update.
|
||||
*
|
||||
|
@ -218,14 +92,6 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
$schema = $this->get_item_schema();
|
||||
$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
|
||||
|
||||
// BW compat
|
||||
if ( $request['exclude_product_ids'] ) {
|
||||
$request['excluded_product_ids'] = $request['exclude_product_ids'];
|
||||
}
|
||||
if ( $request['expiry_date'] ) {
|
||||
$request['date_expires'] = $request['expiry_date'];
|
||||
}
|
||||
|
||||
// Validate required POST fields.
|
||||
if ( 'POST' === $request->get_method() && 0 === $coupon->get_id() ) {
|
||||
if ( empty( $request['code'] ) ) {
|
||||
|
@ -233,7 +99,7 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
}
|
||||
}
|
||||
|
||||
// Handle all writable props
|
||||
// Handle all writable props.
|
||||
foreach ( $data_keys as $key ) {
|
||||
$value = $request[ $key ];
|
||||
|
||||
|
@ -253,7 +119,7 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
case 'meta_data' :
|
||||
if ( is_array( $value ) ) {
|
||||
foreach ( $value as $meta ) {
|
||||
$coupon->update_meta_data( $meta['key'], $meta['value'], $meta['id'] );
|
||||
$coupon->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -281,104 +147,6 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $coupon, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single item.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
/* translators: %s: post type */
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$coupon_id = $this->save_coupon( $request );
|
||||
if ( is_wp_error( $coupon_id ) ) {
|
||||
return $coupon_id;
|
||||
}
|
||||
|
||||
$post = get_post( $coupon_id );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
$this->add_post_meta_fields( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param object $post Inserted object (not a WP_Post object).
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single coupon.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
try {
|
||||
$post_id = (int) $request['id'];
|
||||
|
||||
if ( empty( $post_id ) || get_post_type( $post_id ) !== $this->post_type ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$coupon_id = $this->save_coupon( $request );
|
||||
if ( is_wp_error( $coupon_id ) ) {
|
||||
return $coupon_id;
|
||||
}
|
||||
|
||||
$post = get_post( $coupon_id );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param object $post Inserted object (not a WP_Post object).
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
return rest_ensure_response( $response );
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a coupon to the database.
|
||||
*/
|
||||
public function save_coupon( $request ) {
|
||||
try {
|
||||
$coupon = $this->prepare_item_for_database( $request );
|
||||
|
||||
if ( is_wp_error( $coupon ) ) {
|
||||
return $coupon;
|
||||
}
|
||||
|
||||
$coupon->save();
|
||||
return $coupon->get_id();
|
||||
} catch ( WC_Data_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
|
||||
} catch ( WC_REST_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Coupon's schema, conforming to JSON Schema.
|
||||
*
|
||||
|
@ -401,6 +169,11 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'amount' => array(
|
||||
'description' => __( 'The amount of discount.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the coupon was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
|
@ -413,11 +186,6 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'Coupon description.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'discount_type' => array(
|
||||
'description' => __( 'Determines the type of discount that will be applied.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
|
@ -425,12 +193,12 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
'enum' => array_keys( wc_get_coupon_types() ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'amount' => array(
|
||||
'description' => __( 'The amount of discount.', 'woocommerce' ),
|
||||
'description' => array(
|
||||
'description' => __( 'Coupon description.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'expiry_date' => array(
|
||||
'date_expires' => array(
|
||||
'description' => __( 'UTC DateTime when the coupon expires.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
|
@ -455,7 +223,7 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'exclude_product_ids' => array(
|
||||
'excluded_product_ids' => array(
|
||||
'description' => __( "List of product ID's the coupon cannot be used on.", 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
|
@ -534,7 +302,7 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
'readonly' => true,
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Order meta data.', 'woocommerce' ),
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
|
@ -563,20 +331,4 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|||
);
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections of attachments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
$params['code'] = array(
|
||||
'description' => __( 'Limit result set to resources with a specific code.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,232 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Customers controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
* @extends WC_REST_Customer_Downloads_V1_Controller
|
||||
*/
|
||||
class WC_REST_Customer_Downloads_Controller extends WC_REST_Controller {
|
||||
class WC_REST_Customer_Downloads_Controller extends WC_REST_Customer_Downloads_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'customers/(?P<customer_id>[\d]+)/downloads';
|
||||
|
||||
/**
|
||||
* Register the routes for customers.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'customer_id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read customers.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
$customer = get_user_by( 'id', (int) $request['customer_id'] );
|
||||
|
||||
if ( ! $customer ) {
|
||||
return new WP_Error( 'woocommerce_rest_customer_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
if ( ! wc_rest_check_user_permissions( 'read', $customer->id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all customer downloads.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$downloads = wc_get_customer_available_downloads( (int) $request['customer_id'] );
|
||||
|
||||
$data = array();
|
||||
foreach ( $downloads as $download_data ) {
|
||||
$download = $this->prepare_item_for_response( (object) $download_data, $request );
|
||||
$download = $this->prepare_response_for_collection( $download );
|
||||
$data[] = $download;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single download output for response.
|
||||
*
|
||||
* @param stdObject $download Download object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $download, $request ) {
|
||||
$data = (array) $download;
|
||||
$data['access_expires'] = $data['access_expires'] ? wc_rest_prepare_date_response( $data['access_expires'] ) : 'never';
|
||||
$data['downloads_remaining'] = '' === $data['downloads_remaining'] ? 'unlimited' : $data['downloads_remaining'];
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $download, $request ) );
|
||||
|
||||
/**
|
||||
* Filter customer download data returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdObject $download Download object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_customer_download', $response, $download, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param stdClass $download Download object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return array Links for the given customer download.
|
||||
*/
|
||||
protected function prepare_links( $download, $request ) {
|
||||
$base = str_replace( '(?P<customer_id>[\d]+)', $request['customer_id'], $this->rest_base );
|
||||
$links = array(
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||
),
|
||||
'product' => array(
|
||||
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $download->product_id ) ),
|
||||
),
|
||||
'order' => array(
|
||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $download->order_id ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Customer Download's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'customer_download',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'download_url' => array(
|
||||
'description' => __( 'Download file URL.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'download_id' => array(
|
||||
'description' => __( 'Download ID (MD5).', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'product_id' => array(
|
||||
'description' => __( 'Downloadable product ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'download_name' => array(
|
||||
'description' => __( 'Downloadable file name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'order_id' => array(
|
||||
'description' => __( 'Order ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'order_key' => array(
|
||||
'description' => __( 'Order key.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'downloads_remaining' => array(
|
||||
'description' => __( 'Amount of downloads remaining.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'access_expires' => array(
|
||||
'description' => __( "The date when the download access expires, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'file' => array(
|
||||
'description' => __( 'File details.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
'properties' => array(
|
||||
'name' => array(
|
||||
'description' => __( 'File name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'file' => array(
|
||||
'description' => __( 'File URL.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -18,498 +18,30 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Customers controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
* @extends WC_REST_Customers_V1_Controller
|
||||
*/
|
||||
class WC_REST_Customers_Controller extends WC_REST_Controller {
|
||||
class WC_REST_Customers_Controller extends WC_REST_Customers_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'customers';
|
||||
|
||||
/**
|
||||
* Register the routes for customers.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'email' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'New user email address.', 'woocommerce' ),
|
||||
),
|
||||
'username' => array(
|
||||
'required' => 'no' === get_option( 'woocommerce_registration_generate_username', 'yes' ),
|
||||
'description' => __( 'New user username.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
'password' => array(
|
||||
'required' => 'no' === get_option( 'woocommerce_registration_generate_password', 'no' ),
|
||||
'description' => __( 'New user password.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
'reassign' => array(
|
||||
'default' => 0,
|
||||
'type' => 'integer',
|
||||
'description' => __( 'ID to reassign posts to.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read customers.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_user_permissions( 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access create customers.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_user_permissions( 'create' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
|
||||
if ( ! wc_rest_check_user_permissions( 'read', $id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access update a customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
|
||||
if ( ! wc_rest_check_user_permissions( 'edit', $id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access delete a customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
|
||||
if ( ! wc_rest_check_user_permissions( 'delete', $id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access batch create, update and delete items.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function batch_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_user_permissions( 'batch' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all customers.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$prepared_args = array();
|
||||
$prepared_args['exclude'] = $request['exclude'];
|
||||
$prepared_args['include'] = $request['include'];
|
||||
$prepared_args['order'] = $request['order'];
|
||||
$prepared_args['number'] = $request['per_page'];
|
||||
if ( ! empty( $request['offset'] ) ) {
|
||||
$prepared_args['offset'] = $request['offset'];
|
||||
} else {
|
||||
$prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number'];
|
||||
}
|
||||
$orderby_possibles = array(
|
||||
'id' => 'ID',
|
||||
'include' => 'include',
|
||||
'name' => 'display_name',
|
||||
'registered_date' => 'registered',
|
||||
);
|
||||
$prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ];
|
||||
$prepared_args['search'] = $request['search'];
|
||||
|
||||
if ( '' !== $prepared_args['search'] ) {
|
||||
$prepared_args['search'] = '*' . $prepared_args['search'] . '*';
|
||||
}
|
||||
|
||||
// Filter by email.
|
||||
if ( ! empty( $request['email'] ) ) {
|
||||
$prepared_args['search'] = $request['email'];
|
||||
$prepared_args['search_columns'] = array( 'user_email' );
|
||||
}
|
||||
|
||||
// Filter by role.
|
||||
if ( 'all' !== $request['role'] ) {
|
||||
$prepared_args['role'] = $request['role'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter arguments, before passing to WP_User_Query, when querying users via the REST API.
|
||||
*
|
||||
* @see https://developer.wordpress.org/reference/classes/wp_user_query/
|
||||
*
|
||||
* @param array $prepared_args Array of arguments for WP_User_Query.
|
||||
* @param WP_REST_Request $request The current request.
|
||||
*/
|
||||
$prepared_args = apply_filters( 'woocommerce_rest_customer_query', $prepared_args, $request );
|
||||
|
||||
$query = new WP_User_Query( $prepared_args );
|
||||
|
||||
$users = array();
|
||||
foreach ( $query->results as $user ) {
|
||||
$data = $this->prepare_item_for_response( $user, $request );
|
||||
$users[] = $this->prepare_response_for_collection( $data );
|
||||
}
|
||||
|
||||
$response = rest_ensure_response( $users );
|
||||
|
||||
// Store pagation values for headers then unset for count query.
|
||||
$per_page = (int) $prepared_args['number'];
|
||||
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
|
||||
|
||||
$prepared_args['fields'] = 'ID';
|
||||
|
||||
$total_users = $query->get_total();
|
||||
if ( $total_users < 1 ) {
|
||||
// Out-of-bounds, run the query again without LIMIT for total count.
|
||||
unset( $prepared_args['number'] );
|
||||
unset( $prepared_args['offset'] );
|
||||
$count_query = new WP_User_Query( $prepared_args );
|
||||
$total_users = $count_query->get_total();
|
||||
}
|
||||
$response->header( 'X-WP-Total', (int) $total_users );
|
||||
$max_pages = ceil( $total_users / $per_page );
|
||||
$response->header( 'X-WP-TotalPages', (int) $max_pages );
|
||||
|
||||
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
|
||||
if ( $page > 1 ) {
|
||||
$prev_page = $page - 1;
|
||||
if ( $prev_page > $max_pages ) {
|
||||
$prev_page = $max_pages;
|
||||
}
|
||||
$prev_link = add_query_arg( 'page', $prev_page, $base );
|
||||
$response->link_header( 'prev', $prev_link );
|
||||
}
|
||||
if ( $max_pages > $page ) {
|
||||
$next_page = $page + 1;
|
||||
$next_link = add_query_arg( 'page', $next_page, $base );
|
||||
$response->link_header( 'next', $next_link );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
try {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_customer_exists', __( 'Cannot create existing resource.', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
// Sets the username.
|
||||
$request['username'] = ! empty( $request['username'] ) ? $request['username'] : '';
|
||||
|
||||
// Sets the password.
|
||||
$request['password'] = ! empty( $request['password'] ) ? $request['password'] : '';
|
||||
|
||||
// Create customer.
|
||||
$customer = new WC_Customer;
|
||||
$customer->set_username( $request['username'] );
|
||||
$customer->set_password( $request['password'] );
|
||||
$customer->set_email( $request['email'] );
|
||||
$customer->save();
|
||||
|
||||
if ( ! $customer->get_id() ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_cannot_create', __( 'This resource cannot be created.', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
$this->update_customer_meta_fields( $customer, $request );
|
||||
$customer->save();
|
||||
|
||||
$user_data = get_userdata( $customer->get_id() );
|
||||
$this->update_additional_fields_for_object( $user_data, $request );
|
||||
|
||||
/**
|
||||
* Fires after a customer is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_User $user_data Data used to create the customer.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating customer, false when updating customer.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_customer', $user_data, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $user_data, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $customer->get_id() ) ) );
|
||||
|
||||
return $response;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$user_data = get_userdata( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $user_data->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$customer = $this->prepare_item_for_response( $user_data, $request );
|
||||
$response = rest_ensure_response( $customer );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single user.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
try {
|
||||
$id = (int) $request['id'];
|
||||
$customer = new WC_Customer( $id );
|
||||
|
||||
if ( ! $customer->get_id() ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
if ( ! empty( $request['email'] ) && email_exists( $request['email'] ) && $request['email'] !== $customer->get_email() ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_customer_invalid_email', __( 'Email address is invalid.', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
if ( ! empty( $request['username'] ) && $request['username'] !== $customer->get_username() ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_customer_invalid_argument', __( "Username isn't editable.", 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
// Customer email.
|
||||
if ( isset( $request['email'] ) ) {
|
||||
$customer->set_email( sanitize_email( $request['email'] ) );
|
||||
}
|
||||
|
||||
// Customer password.
|
||||
if ( isset( $request['password'] ) ) {
|
||||
$customer->set_password( wc_clean( $request['password'] ) );
|
||||
}
|
||||
|
||||
$this->update_customer_meta_fields( $customer, $request );
|
||||
$customer->save();
|
||||
|
||||
$user_data = get_userdata( $customer->get_id() );
|
||||
$this->update_additional_fields_for_object( $user_data, $request );
|
||||
|
||||
if ( ! is_user_member_of_blog( $user_data->ID ) ) {
|
||||
$user_data->add_role( 'customer' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a customer is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_User $customer Data used to create the customer.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating customer, false when updating customer.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_customer', $user_data, $request, false );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $user_data, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
return $response;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$reassign = isset( $request['reassign'] ) ? absint( $request['reassign'] ) : null;
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Customers do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$user_data = get_userdata( $id );
|
||||
if ( ! $user_data ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
if ( ! empty( $reassign ) ) {
|
||||
if ( $reassign === $id || ! get_userdata( $reassign ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_customer_invalid_reassign', __( 'Invalid resource id for reassignment.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $user_data, $request );
|
||||
|
||||
/** Include admin customer functions to get access to wp_delete_user() */
|
||||
require_once ABSPATH . 'wp-admin/includes/user.php';
|
||||
|
||||
$customer = new WC_Customer( $id );
|
||||
|
||||
if ( ! is_null( $reassign ) ) {
|
||||
$result = $customer->delete_and_reassign( $reassign );
|
||||
} else {
|
||||
$result = $customer->delete();
|
||||
}
|
||||
|
||||
if ( ! $result ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a customer is deleted via the REST API.
|
||||
*
|
||||
* @param WP_User $user_data User data.
|
||||
* @param WP_REST_Response $response The response returned from the API.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_delete_customer', $user_data, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Prepare a single customer output for response.
|
||||
*
|
||||
* @param WP_User $user_data User object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param WP_User $user_data User object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $user_data, $request ) {
|
||||
$customer = new WC_Customer( $user_data->ID );
|
||||
$data = $customer->get_data();
|
||||
$format_date = array( 'date_created', 'date_modified' );
|
||||
$meta_data = $data['meta_data'];
|
||||
unset( $data['meta_data'] );
|
||||
|
||||
// Format date values.
|
||||
foreach ( $format_date as $key ) {
|
||||
|
@ -521,6 +53,9 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
|
|||
$data['total_spent'] = $customer->get_total_spent();
|
||||
$data['avatar_url'] = $customer->get_avatar_url();
|
||||
|
||||
// Includes meta_data as last item.
|
||||
$data['meta_data'] = $meta_data;
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
@ -544,63 +79,16 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
|
|||
* @param WP_REST_Request $request
|
||||
*/
|
||||
protected function update_customer_meta_fields( $customer, $request ) {
|
||||
$schema = $this->get_item_schema();
|
||||
parent::update_customer_meta_fields( $customer, $request );
|
||||
|
||||
// Meta data
|
||||
// Meta data.
|
||||
if ( isset( $request['meta_data'] ) ) {
|
||||
if ( is_array( $request['meta_data'] ) ) {
|
||||
foreach ( $request['meta_data'] as $meta ) {
|
||||
$customer->update_meta_data( $meta['key'], $meta['value'], $meta['id'] );
|
||||
$customer->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Customer first name.
|
||||
if ( isset( $request['first_name'] ) ) {
|
||||
$customer->set_first_name( wc_clean( $request['first_name'] ) );
|
||||
}
|
||||
|
||||
// Customer last name.
|
||||
if ( isset( $request['last_name'] ) ) {
|
||||
$customer->set_last_name( wc_clean( $request['last_name'] ) );
|
||||
}
|
||||
|
||||
// Customer billing address.
|
||||
if ( isset( $request['billing'] ) ) {
|
||||
foreach ( array_keys( $schema['properties']['billing']['properties'] ) as $field ) {
|
||||
if ( isset( $request['billing'][ $field ] ) && is_callable( array( $customer, "set_billing_{$field}" ) ) ) {
|
||||
$customer->{"set_billing_{$field}"}( $request['billing'][ $field ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Customer shipping address.
|
||||
if ( isset( $request['shipping'] ) ) {
|
||||
foreach ( array_keys( $schema['properties']['shipping']['properties'] ) as $field ) {
|
||||
if ( isset( $request['shipping'][ $field ] ) && is_callable( array( $customer, "set_shipping_{$field}" ) ) ) {
|
||||
$customer->{"set_shipping_{$field}"}( $request['shipping'][ $field ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param WP_User $customer Customer object.
|
||||
* @return array Links for the given customer.
|
||||
*/
|
||||
protected function prepare_links( $customer ) {
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $customer->ID ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -673,24 +161,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
|
|||
'type' => 'string',
|
||||
'context' => array( 'edit' ),
|
||||
),
|
||||
'orders_count' => array(
|
||||
'description' => __( 'Quantity of orders made by the customer.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_spent' => array(
|
||||
'description' => __( 'Total amount spent.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'avatar_url' => array(
|
||||
'description' => __( 'Avatar URL.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'billing' => array(
|
||||
'description' => __( 'List of billing address data.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
|
@ -812,8 +282,26 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
|
|||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'orders_count' => array(
|
||||
'description' => __( 'Quantity of orders made by the customer.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_spent' => array(
|
||||
'description' => __( 'Total amount spent.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'avatar_url' => array(
|
||||
'description' => __( 'Avatar URL.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Order meta data.', 'woocommerce' ),
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
|
@ -843,85 +331,4 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
|
|||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get role names.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_role_names() {
|
||||
global $wp_roles;
|
||||
return array_keys( $wp_roles->role_names );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['context']['default'] = 'view';
|
||||
|
||||
$params['exclude'] = array(
|
||||
'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
$params['include'] = array(
|
||||
'description' => __( 'Limit result set to specific IDs.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
$params['offset'] = array(
|
||||
'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['order'] = array(
|
||||
'default' => 'asc',
|
||||
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
|
||||
'enum' => array( 'asc', 'desc' ),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['orderby'] = array(
|
||||
'default' => 'name',
|
||||
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
|
||||
'enum' => array(
|
||||
'id',
|
||||
'include',
|
||||
'name',
|
||||
'registered_date',
|
||||
),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['email'] = array(
|
||||
'description' => __( 'Limit result set to resources with a specific email.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'email',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['role'] = array(
|
||||
'description' => __( 'Limit result set to resources with a specific role.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'customer',
|
||||
'enum' => array_merge( array( 'all' ), $this->get_role_names() ),
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,157 +18,16 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Order Notes controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
* @extends WC_REST_ControllerWC_REST_Order_Notes_V1_Controller
|
||||
*/
|
||||
class WC_REST_Order_Notes_Controller extends WC_REST_Controller {
|
||||
class WC_REST_Order_Notes_Controller extends WC_REST_Order_Notes_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'orders/(?P<order_id>[\d]+)/notes';
|
||||
|
||||
/**
|
||||
* Post type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $post_type = 'shop_order';
|
||||
|
||||
/**
|
||||
* Register the routes for order notes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'order_id' => array(
|
||||
'description' => __( 'The order ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'note' => array(
|
||||
'type' => 'string',
|
||||
'description' => __( 'Order note content.', 'woocommerce' ),
|
||||
'required' => true,
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'order_id' => array(
|
||||
'description' => __( 'The order ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read order notes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_post_permissions( $this->post_type, 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access create order notes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_post_permissions( $this->post_type, 'create' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a order note.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['order_id'] );
|
||||
|
||||
if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'read', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access delete a order note.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['order_id'] );
|
||||
|
||||
if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'delete', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Get order notes from an order.
|
||||
|
@ -177,14 +36,14 @@ class WC_REST_Order_Notes_Controller extends WC_REST_Controller {
|
|||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$order = get_post( (int) $request['order_id'] );
|
||||
$order = wc_get_order( (int) $request['order_id'] );
|
||||
|
||||
if ( empty( $order->post_type ) || $this->post_type !== $order->post_type ) {
|
||||
if ( ! $order || $this->post_type !== $order->get_type() ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'post_id' => $order->ID,
|
||||
'post_id' => $order->get_id(),
|
||||
'approve' => 'approve',
|
||||
'type' => 'order_note',
|
||||
);
|
||||
|
@ -223,226 +82,6 @@ class WC_REST_Order_Notes_Controller extends WC_REST_Controller {
|
|||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single order note.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
/* translators: %s: post type */
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$order = get_post( (int) $request['order_id'] );
|
||||
|
||||
if ( empty( $order->post_type ) || $this->post_type !== $order->post_type ) {
|
||||
return new WP_Error( 'woocommerce_rest_order_invalid_id', __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$order = wc_get_order( $order );
|
||||
|
||||
// Create the note.
|
||||
$note_id = $order->add_order_note( $request['note'], $request['customer_note'] );
|
||||
|
||||
if ( ! $note_id ) {
|
||||
return new WP_Error( 'woocommerce_api_cannot_create_order_note', __( 'Cannot create order note, please try again.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
$note = get_comment( $note_id );
|
||||
$this->update_additional_fields_for_object( $note, $request );
|
||||
|
||||
/**
|
||||
* Fires after a order note is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Comment $note New order note object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_order_note', $note, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $note, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, str_replace( '(?P<order_id>[\d]+)', $order->get_id(), $this->rest_base ), $note_id ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single order note.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$order = get_post( (int) $request['order_id'] );
|
||||
|
||||
if ( empty( $order->post_type ) || $this->post_type !== $order->post_type ) {
|
||||
return new WP_Error( 'woocommerce_rest_order_invalid_id', __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$note = get_comment( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $note ) || intval( $note->comment_post_ID ) !== intval( $order->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$order_note = $this->prepare_item_for_response( $note, $request );
|
||||
$response = rest_ensure_response( $order_note );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single order note.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response|WP_Error
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Webhooks do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$order = get_post( (int) $request['order_id'] );
|
||||
|
||||
if ( empty( $order->post_type ) || $this->post_type !== $order->post_type ) {
|
||||
return new WP_Error( 'woocommerce_rest_order_invalid_id', __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$note = get_comment( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $note ) || intval( $note->comment_post_ID ) !== intval( $order->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $note, $request );
|
||||
|
||||
$result = wp_delete_comment( $note->comment_ID, true );
|
||||
|
||||
if ( ! $result ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), 'order_note' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a order note is deleted or trashed via the REST API.
|
||||
*
|
||||
* @param WP_Comment $note The deleted or trashed order note.
|
||||
* @param WP_REST_Response $response The response data.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_delete_order_note', $note, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single order note output for response.
|
||||
*
|
||||
* @param WP_Comment $note Order note object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $note, $request ) {
|
||||
$data = array(
|
||||
'id' => (int) $note->comment_ID,
|
||||
'date_created' => wc_rest_prepare_date_response( $note->comment_date_gmt ),
|
||||
'note' => $note->comment_content,
|
||||
'customer_note' => (bool) get_comment_meta( $note->comment_ID, 'is_customer_note', true ),
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $note ) );
|
||||
|
||||
/**
|
||||
* Filter order note object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param WP_Comment $note Order note object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_order_note', $response, $note, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param WP_Comment $note Delivery order_note object.
|
||||
* @return array Links for the given order note.
|
||||
*/
|
||||
protected function prepare_links( $note ) {
|
||||
$order_id = (int) $note->comment_post_ID;
|
||||
$base = str_replace( '(?P<order_id>[\d]+)', $order_id, $this->rest_base );
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $note->comment_ID ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||
),
|
||||
'up' => array(
|
||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $order_id ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Order Notes schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'order_note',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the order note was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'note' => array(
|
||||
'description' => __( 'Order note.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'customer_note' => array(
|
||||
'description' => __( 'Shows/define if the note is only for reference or for the customer (the user will be notified).', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
|
|
|
@ -14,8 +14,6 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
exit;
|
||||
}
|
||||
|
||||
include( 'class-wc-rest-orders-controller.php' );
|
||||
|
||||
/**
|
||||
* REST API Order Refunds controller class.
|
||||
*
|
||||
|
@ -29,7 +27,7 @@ class WC_REST_Order_Refunds_Controller extends WC_REST_Orders_Controller {
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
@ -220,13 +218,14 @@ class WC_REST_Order_Refunds_Controller extends WC_REST_Orders_Controller {
|
|||
/**
|
||||
* Query args.
|
||||
*
|
||||
* @param array $args
|
||||
* @param WP_REST_Request $request
|
||||
* @param array $args Request args.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return array
|
||||
*/
|
||||
public function query_args( $args, $request ) {
|
||||
$args['post_status'] = array_keys( wc_get_order_statuses() );
|
||||
$args['post_parent__in'] = array( absint( $request['order_id'] ) );
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
|
@ -330,7 +329,7 @@ class WC_REST_Order_Refunds_Controller extends WC_REST_Orders_Controller {
|
|||
'context' => array( 'view' ),
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Order meta data.', 'woocommerce' ),
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
|
@ -442,7 +441,7 @@ class WC_REST_Order_Refunds_Controller extends WC_REST_Orders_Controller {
|
|||
),
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Order item meta data.', 'woocommerce' ),
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
|
@ -479,35 +478,6 @@ class WC_REST_Order_Refunds_Controller extends WC_REST_Orders_Controller {
|
|||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'meta' => array(
|
||||
'description' => __( 'Order item meta data (formatted).', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
'items' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'key' => array(
|
||||
'description' => __( 'Meta key.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'label' => array(
|
||||
'description' => __( 'Meta label.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'value' => array(
|
||||
'description' => __( 'Meta value.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
|
|
@ -18,30 +18,16 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Orders controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Posts_Controller
|
||||
* @extends WC_REST_Orders_V1_Controller
|
||||
*/
|
||||
class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
||||
class WC_REST_Orders_Controller extends WC_REST_Orders_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'orders';
|
||||
|
||||
/**
|
||||
* Post type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $post_type = 'shop_order';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Stores the request.
|
||||
|
@ -49,80 +35,6 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
*/
|
||||
protected $request = array();
|
||||
|
||||
/**
|
||||
* Initialize orders actions.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the routes for orders.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Expands an order item to get its data.
|
||||
* @param WC_Order_item $item
|
||||
|
@ -139,25 +51,10 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
}
|
||||
}
|
||||
|
||||
// Add meta, SKU and PRICE to products.
|
||||
// Add SKU and PRICE to products.
|
||||
if ( is_callable( array( $item, 'get_product' ) ) ) {
|
||||
$data['sku'] = $item->get_product() ? $item->get_product()->get_sku(): null;
|
||||
$data['price'] = $item->get_total() / max( 1, $item->get_quantity() );
|
||||
|
||||
// Format meta data.
|
||||
if ( isset( $data['meta_data'] ) ) {
|
||||
$hideprefix = 'true' === $this->request['all_item_meta'] ? null : '_';
|
||||
$item_meta = $item->get_formatted_meta_data( $hideprefix );
|
||||
|
||||
foreach ( $item_meta as $key => $values ) {
|
||||
// Label was used in previous version of API - set it here.
|
||||
$item_meta[ $key ]->label = $values->display_key;
|
||||
unset( $item_meta[ $key ]->display_key );
|
||||
unset( $item_meta[ $key ]->display_value );
|
||||
}
|
||||
|
||||
$data['meta'] = array_values( $item_meta );
|
||||
}
|
||||
}
|
||||
|
||||
// Format taxes.
|
||||
|
@ -176,6 +73,11 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
$data['taxes'] = array();
|
||||
}
|
||||
|
||||
// Remove names for coupons, taxes and shipping.
|
||||
if ( isset( $data['code'] ) || isset( $data['rate_code'] ) || isset( $data['method_title'] ) ) {
|
||||
unset( $data['name'] );
|
||||
}
|
||||
|
||||
// Remove props we don't want to expose.
|
||||
unset( $data['order_id'] );
|
||||
unset( $data['type'] );
|
||||
|
@ -246,92 +148,6 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param WC_Order $order Order object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return array Links for the given order.
|
||||
*/
|
||||
protected function prepare_links( $order, $request ) {
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $order->get_id() ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||
),
|
||||
);
|
||||
if ( 0 !== (int) $order->get_user_id() ) {
|
||||
$links['customer'] = array(
|
||||
'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $order->get_user_id() ) ),
|
||||
);
|
||||
}
|
||||
if ( 0 !== (int) $order->get_parent_id() ) {
|
||||
$links['up'] = array(
|
||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $order->get_parent_id() ) ),
|
||||
);
|
||||
}
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query args.
|
||||
*
|
||||
* @param array $args
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function query_args( $args, $request ) {
|
||||
global $wpdb;
|
||||
|
||||
// Set post_status.
|
||||
if ( 'any' !== $request['status'] ) {
|
||||
$args['post_status'] = 'wc-' . $request['status'];
|
||||
} else {
|
||||
$args['post_status'] = 'any';
|
||||
}
|
||||
|
||||
if ( ! empty( $request['customer'] ) ) {
|
||||
if ( ! empty( $args['meta_query'] ) ) {
|
||||
$args['meta_query'] = array();
|
||||
}
|
||||
|
||||
$args['meta_query'][] = array(
|
||||
'key' => '_customer_user',
|
||||
'value' => $request['customer'],
|
||||
'type' => 'NUMERIC',
|
||||
);
|
||||
}
|
||||
|
||||
// Search by product.
|
||||
if ( ! empty( $request['product'] ) ) {
|
||||
$order_ids = $wpdb->get_col( $wpdb->prepare( "
|
||||
SELECT order_id
|
||||
FROM {$wpdb->prefix}woocommerce_order_items
|
||||
WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
|
||||
AND order_item_type = 'line_item'
|
||||
", $request['product'] ) );
|
||||
|
||||
// Force WP_Query return empty if don't found any order.
|
||||
$order_ids = ! empty( $order_ids ) ? $order_ids : array( 0 );
|
||||
|
||||
$args['post__in'] = $order_ids;
|
||||
}
|
||||
|
||||
// Search.
|
||||
if ( ! empty( $args['s'] ) ) {
|
||||
$order_ids = wc_order_search( $args['s'] );
|
||||
|
||||
if ( ! empty( $order_ids ) ) {
|
||||
unset( $args['s'] );
|
||||
$args['post__in'] = array_merge( $order_ids, array( 0 ) );
|
||||
}
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single order for create.
|
||||
*
|
||||
|
@ -373,7 +189,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
case 'meta_data' :
|
||||
if ( is_array( $value ) ) {
|
||||
foreach ( $value as $meta ) {
|
||||
$order->update_meta_data( $meta['key'], $meta['value'], $meta['id'] );
|
||||
$order->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -398,143 +214,6 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $order, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create base WC Order object.
|
||||
* @deprecated 2.7.0
|
||||
* @param array $data
|
||||
* @return WC_Order
|
||||
*/
|
||||
protected function create_base_order( $data ) {
|
||||
return wc_create_order( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Only reutrn writeable props from schema.
|
||||
* @param array $schema
|
||||
* @return bool
|
||||
*/
|
||||
protected function filter_writable_props( $schema ) {
|
||||
return empty( $schema['readonly'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create order.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return int|WP_Error
|
||||
*/
|
||||
protected function create_order( $request ) {
|
||||
try {
|
||||
// Make sure customer exists.
|
||||
if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] && false === get_user_by( 'id', $request['customer_id'] ) ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id',__( 'Customer ID is invalid.', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
$order = $this->prepare_item_for_database( $request );
|
||||
$order->set_created_via( 'rest-api' );
|
||||
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
|
||||
$order->calculate_totals();
|
||||
$order->save();
|
||||
|
||||
// Handle set paid
|
||||
if ( true === $request['set_paid'] ) {
|
||||
$order->payment_complete( $request['transaction_id'] );
|
||||
}
|
||||
|
||||
return $order->get_id();
|
||||
} catch ( WC_Data_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
|
||||
} catch ( WC_REST_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update order.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return int|WP_Error
|
||||
*/
|
||||
protected function update_order( $request ) {
|
||||
try {
|
||||
$order = $this->prepare_item_for_database( $request );
|
||||
$order->save();
|
||||
|
||||
// Handle set paid
|
||||
if ( $order->needs_payment() && true === $request['set_paid'] ) {
|
||||
$order->payment_complete( $request['transaction_id'] );
|
||||
}
|
||||
|
||||
// If items have changed, recalculate order totals.
|
||||
if ( isset( $request['billing'], $request['shipping'], $request['line_items'], $request['shipping_lines'], $request['fee_lines'], $request['coupon_lines'] ) ) {
|
||||
$order->calculate_totals();
|
||||
}
|
||||
|
||||
return $order->get_id();
|
||||
} catch ( WC_Data_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
|
||||
} catch ( WC_REST_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update address.
|
||||
*
|
||||
* @param WC_Order $order
|
||||
* @param array $posted
|
||||
* @param string $type
|
||||
*/
|
||||
protected function update_address( $order, $posted, $type = 'billing' ) {
|
||||
foreach ( $posted as $key => $value ) {
|
||||
if ( is_callable( array( $order, "set_{$type}_{$key}" ) ) ) {
|
||||
$order->{"set_{$type}_{$key}"}( $value );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the product ID from the SKU or posted ID.
|
||||
* @param array $posted Request data
|
||||
* @return int
|
||||
*/
|
||||
protected function get_product_id( $posted ) {
|
||||
if ( ! empty( $posted['sku'] ) ) {
|
||||
$product_id = (int) wc_get_product_id_by_sku( $posted['sku'] );
|
||||
} elseif ( ! empty( $posted['product_id'] ) && empty( $posted['variation_id'] ) ) {
|
||||
$product_id = (int) $posted['product_id'];
|
||||
} elseif ( ! empty( $posted['variation_id'] ) ) {
|
||||
$product_id = (int) $posted['variation_id'];
|
||||
} else {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_required_product_reference', __( 'Product ID or SKU is required.', 'woocommerce' ), 400 );
|
||||
}
|
||||
return $product_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe set an item prop if the value was posted.
|
||||
* @param WC_Order_Item $item
|
||||
* @param string $prop
|
||||
* @param array $posted Request data.
|
||||
*/
|
||||
protected function maybe_set_item_prop( $item, $prop, $posted ) {
|
||||
if ( isset( $posted[ $prop ] ) ) {
|
||||
$item->{"set_$prop"}( $posted[ $prop ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe set item props if the values were posted.
|
||||
* @param WC_Order_Item $item
|
||||
* @param string[] $props
|
||||
* @param array $posted Request data.
|
||||
*/
|
||||
protected function maybe_set_item_props( $item, $props, $posted ) {
|
||||
foreach ( $props as $prop ) {
|
||||
$this->maybe_set_item_prop( $item, $prop, $posted );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe set item meta if posted.
|
||||
* @param WC_Order_Item $item
|
||||
|
@ -644,165 +323,6 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method to create/update order items.
|
||||
* When updating, the item ID provided is checked to ensure it is associated
|
||||
* with the order.
|
||||
*
|
||||
* @param WC_Order $order order
|
||||
* @param string $item_type
|
||||
* @param array $posted item provided in the request body
|
||||
* @throws WC_REST_Exception If item ID is not associated with order
|
||||
*/
|
||||
protected function set_item( $order, $item_type, $posted ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( ! empty( $posted['id'] ) ) {
|
||||
$action = 'update';
|
||||
} else {
|
||||
$action = 'create';
|
||||
}
|
||||
|
||||
$method = 'prepare_' . $item_type;
|
||||
|
||||
// Verify provided line item ID is associated with order.
|
||||
if ( 'update' === $action ) {
|
||||
$result = $wpdb->get_row(
|
||||
$wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
|
||||
absint( $posted['id'] ),
|
||||
absint( $order->get_id() )
|
||||
) );
|
||||
if ( is_null( $result ) ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 );
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare item data
|
||||
$item = $this->$method( $posted, $action );
|
||||
|
||||
/**
|
||||
* Action hook to adjust item before save.
|
||||
* @since 2.7.0
|
||||
*/
|
||||
do_action( 'woocommerce_rest_set_order_item', $item, $posted );
|
||||
|
||||
// Save or add to order
|
||||
if ( 'create' === $action ) {
|
||||
$order->add_item( $item );
|
||||
} else {
|
||||
$item->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to check if the resource ID associated with the provided item is null.
|
||||
* Items can be deleted by setting the resource ID to null.
|
||||
*
|
||||
* @param array $item Item provided in the request body.
|
||||
* @return bool True if the item resource ID is null, false otherwise.
|
||||
*/
|
||||
protected function item_is_null( $item ) {
|
||||
$keys = array( 'product_id', 'method_id', 'method_title', 'name', 'code' );
|
||||
|
||||
foreach ( $keys as $key ) {
|
||||
if ( array_key_exists( $key, $item ) && is_null( $item[ $key ] ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single item.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
/* translators: %s: post type */
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$order_id = $this->create_order( $request );
|
||||
if ( is_wp_error( $order_id ) ) {
|
||||
return $order_id;
|
||||
}
|
||||
|
||||
$post = get_post( $order_id );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param object $post Inserted object (not a WP_Post object).
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single order.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
try {
|
||||
$post_id = (int) $request['id'];
|
||||
|
||||
if ( empty( $post_id ) || get_post_type( $post_id ) !== $this->post_type ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$order_id = $this->update_order( $request );
|
||||
if ( is_wp_error( $order_id ) ) {
|
||||
return $order_id;
|
||||
}
|
||||
|
||||
$post = get_post( $order_id );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param object $post Inserted object (not a WP_Post object).
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
return rest_ensure_response( $response );
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get order statuses without prefixes.
|
||||
* @return array
|
||||
*/
|
||||
protected function get_order_statuses() {
|
||||
$order_statuses = array();
|
||||
|
||||
foreach ( array_keys( wc_get_order_statuses() ) as $status ) {
|
||||
$order_statuses[] = str_replace( 'wc-', '', $status );
|
||||
}
|
||||
|
||||
return $order_statuses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Order's schema, conforming to JSON Schema.
|
||||
*
|
||||
|
@ -832,12 +352,6 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
'enum' => $this->get_order_statuses(),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'order_key' => array(
|
||||
'description' => __( 'Order key.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'currency' => array(
|
||||
'description' => __( 'Currency the order was created with, in ISO format.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
|
@ -861,6 +375,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
'description' => __( "The date the order was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_modified' => array(
|
||||
'description' => __( "The date the order was last modified, in the site's timezone.", 'woocommerce' ),
|
||||
|
@ -868,12 +383,6 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'customer_id' => array(
|
||||
'description' => __( 'User ID who owns the order. 0 for guests.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'default' => 0,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'discount_total' => array(
|
||||
'description' => __( 'Total discount amount for the order.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
|
@ -916,6 +425,18 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'customer_id' => array(
|
||||
'description' => __( 'User ID who owns the order. 0 for guests.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'default' => 0,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'order_key' => array(
|
||||
'description' => __( 'Order key.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'billing' => array(
|
||||
'description' => __( 'Billing address.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
|
@ -1092,7 +613,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
'readonly' => true,
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Order meta data.', 'woocommerce' ),
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
|
@ -1204,7 +725,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
),
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Order item meta data.', 'woocommerce' ),
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
|
@ -1241,35 +762,6 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'meta' => array(
|
||||
'description' => __( 'Order item meta data (formatted).', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
'items' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'key' => array(
|
||||
'description' => __( 'Meta key.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'label' => array(
|
||||
'description' => __( 'Meta label.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'value' => array(
|
||||
'description' => __( 'Meta value.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -1324,7 +816,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
'readonly' => true,
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Order item meta data.', 'woocommerce' ),
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
|
@ -1410,7 +902,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
),
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Order item meta data.', 'woocommerce' ),
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
|
@ -1508,7 +1000,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
),
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Order item meta data.', 'woocommerce' ),
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
|
@ -1566,7 +1058,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
'readonly' => true,
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Order item meta data.', 'woocommerce' ),
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
|
@ -1634,43 +1126,4 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['status'] = array(
|
||||
'default' => 'any',
|
||||
'description' => __( 'Limit result set to orders assigned a specific status.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'enum' => array_merge( array( 'any' ), $this->get_order_statuses() ),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['customer'] = array(
|
||||
'description' => __( 'Limit result set to orders assigned a specific customer.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['product'] = array(
|
||||
'description' => __( 'Limit result set to orders assigned a specific product.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['dp'] = array(
|
||||
'default' => 2,
|
||||
'description' => __( 'Number of decimal points to use in each resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ class WC_REST_Payment_Gateways_Controller extends WC_REST_Controller {
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
|
|
@ -18,223 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Product Attribute Terms controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Terms_Controller
|
||||
* @extends WC_REST_Product_Attribute_Terms_V1_Controller
|
||||
*/
|
||||
class WC_REST_Product_Attribute_Terms_Controller extends WC_REST_Terms_Controller {
|
||||
class WC_REST_Product_Attribute_Terms_Controller extends WC_REST_Product_Attribute_Terms_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/attributes/(?P<attribute_id>[\d]+)/terms';
|
||||
|
||||
/**
|
||||
* Register the routes for terms.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'attribute_id' => array(
|
||||
'description' => __( 'Unique identifier for the attribute of the terms.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'name' => array(
|
||||
'type' => 'string',
|
||||
'description' => __( 'Name for the resource.', 'woocommerce' ),
|
||||
'required' => true,
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
));
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'attribute_id' => array(
|
||||
'description' => __( 'Unique identifier for the attribute of the terms.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
'args' => array(
|
||||
'attribute_id' => array(
|
||||
'description' => __( 'Unique identifier for the attribute of the terms.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single product attribute term output for response.
|
||||
*
|
||||
* @param WP_Term $item Term object.
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_REST_Response $response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
// Get term order.
|
||||
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order_' . $this->taxonomy );
|
||||
|
||||
$data = array(
|
||||
'id' => (int) $item->term_id,
|
||||
'name' => $item->name,
|
||||
'slug' => $item->slug,
|
||||
'description' => $item->description,
|
||||
'menu_order' => (int) $menu_order,
|
||||
'count' => (int) $item->count,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||
|
||||
/**
|
||||
* Filter a term item returned from the API.
|
||||
*
|
||||
* Allows modification of the term data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $item The original term object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update term meta fields.
|
||||
*
|
||||
* @param WP_Term $term
|
||||
* @param WP_REST_Request $request
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
protected function update_term_meta_fields( $term, $request ) {
|
||||
$id = (int) $term->term_id;
|
||||
|
||||
update_woocommerce_term_meta( $id, 'order_' . $this->taxonomy, $request['menu_order'] );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Attribute Term's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'product_attribute_term',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Term name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
),
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'HTML description of the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'wp_filter_post_kses',
|
||||
),
|
||||
),
|
||||
'menu_order' => array(
|
||||
'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'count' => array(
|
||||
'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -18,638 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Product Attributes controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
* @extends WC_REST_Product_Attributes_V1_Controller
|
||||
*/
|
||||
class WC_REST_Product_Attributes_Controller extends WC_REST_Controller {
|
||||
class WC_REST_Product_Attributes_Controller extends WC_REST_Product_Attributes_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/attributes';
|
||||
|
||||
/**
|
||||
* Attribute name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $attribute = '';
|
||||
|
||||
/**
|
||||
* Register the routes for product attributes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'name' => array(
|
||||
'description' => __( 'Name for the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
));
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => true,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read the attributes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to create a attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'create' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you cannot create new resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
if ( ! $this->get_taxonomy( $request ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to update a attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
if ( ! $this->get_taxonomy( $request ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'edit' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot update resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to delete a attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if ( ! $this->get_taxonomy( $request ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'delete' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access batch create, update and delete items.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function batch_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'batch' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all attributes.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$attributes = wc_get_attribute_taxonomies();
|
||||
$data = array();
|
||||
foreach ( $attributes as $attribute_obj ) {
|
||||
$attribute = $this->prepare_item_for_response( $attribute_obj, $request );
|
||||
$attribute = $this->prepare_response_for_collection( $attribute );
|
||||
$data[] = $attribute;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Request|WP_Error
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$args = array(
|
||||
'attribute_label' => $request['name'],
|
||||
'attribute_name' => wc_sanitize_taxonomy_name( stripslashes( $request['slug'] ) ),
|
||||
'attribute_type' => ! empty( $request['type'] ) ? $request['type'] : 'select',
|
||||
'attribute_orderby' => ! empty( $request['order_by'] ) ? $request['order_by'] : 'menu_order',
|
||||
'attribute_public' => true === $request['has_archives'],
|
||||
);
|
||||
|
||||
// Set the attribute slug.
|
||||
if ( empty( $args['attribute_name'] ) ) {
|
||||
$args['attribute_name'] = wc_sanitize_taxonomy_name( stripslashes( $args['attribute_label'] ) );
|
||||
} else {
|
||||
$args['attribute_name'] = preg_replace( '/^pa\_/', '', wc_sanitize_taxonomy_name( stripslashes( $args['attribute_name'] ) ) );
|
||||
}
|
||||
|
||||
$valid_slug = $this->validate_attribute_slug( $args['attribute_name'], true );
|
||||
if ( is_wp_error( $valid_slug ) ) {
|
||||
return $valid_slug;
|
||||
}
|
||||
|
||||
$insert = $wpdb->insert(
|
||||
$wpdb->prefix . 'woocommerce_attribute_taxonomies',
|
||||
$args,
|
||||
array( '%s', '%s', '%s', '%s', '%d' )
|
||||
);
|
||||
|
||||
// Checks for errors.
|
||||
if ( is_wp_error( $insert ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', $insert->get_error_message(), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$attribute = $this->get_attribute( $wpdb->insert_id );
|
||||
|
||||
if ( is_wp_error( $attribute ) ) {
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
$this->update_additional_fields_for_object( $attribute, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single product attribute is created or updated via the REST API.
|
||||
*
|
||||
* @param stdObject $attribute Inserted attribute object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating attribute, false when updating.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_product_attribute', $attribute, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $attribute, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( '/' . $this->namespace . '/' . $this->rest_base . '/' . $attribute->attribute_id ) );
|
||||
|
||||
// Clear transients.
|
||||
wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
|
||||
delete_transient( 'wc_attribute_taxonomies' );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Request|WP_Error
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$attribute = $this->get_attribute( (int) $request['id'] );
|
||||
|
||||
if ( is_wp_error( $attribute ) ) {
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
$response = $this->prepare_item_for_response( $attribute, $request );
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single term from a taxonomy.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Request|WP_Error
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$id = (int) $request['id'];
|
||||
$format = array( '%s', '%s', '%s', '%s', '%d' );
|
||||
$args = array(
|
||||
'attribute_label' => $request['name'],
|
||||
'attribute_name' => wc_sanitize_taxonomy_name( stripslashes( $request['slug'] ) ),
|
||||
'attribute_type' => $request['type'],
|
||||
'attribute_orderby' => $request['order_by'],
|
||||
'attribute_public' => $request['has_archives'],
|
||||
);
|
||||
|
||||
$i = 0;
|
||||
foreach ( $args as $key => $value ) {
|
||||
if ( empty( $value ) && ! is_bool( $value ) ) {
|
||||
unset( $args[ $key ] );
|
||||
unset( $format[ $i ] );
|
||||
}
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
// Set the attribute slug.
|
||||
if ( ! empty( $args['attribute_name'] ) ) {
|
||||
$args['attribute_name'] = preg_replace( '/^pa\_/', '', wc_sanitize_taxonomy_name( stripslashes( $args['attribute_name'] ) ) );
|
||||
|
||||
$valid_slug = $this->validate_attribute_slug( $args['attribute_name'], false );
|
||||
if ( is_wp_error( $valid_slug ) ) {
|
||||
return $valid_slug;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $args ) {
|
||||
$update = $wpdb->update(
|
||||
$wpdb->prefix . 'woocommerce_attribute_taxonomies',
|
||||
$args,
|
||||
array( 'attribute_id' => $id ),
|
||||
$format,
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
// Checks for errors.
|
||||
if ( false === $update ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Could not edit the attribute.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
$attribute = $this->get_attribute( $id );
|
||||
|
||||
if ( is_wp_error( $attribute ) ) {
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
$this->update_additional_fields_for_object( $attribute, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single product attribute is created or updated via the REST API.
|
||||
*
|
||||
* @param stdObject $attribute Inserted attribute object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating attribute, false when updating.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_product_attribute', $attribute, $request, false );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $attribute, $request );
|
||||
|
||||
// Clear transients.
|
||||
wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
|
||||
delete_transient( 'wc_attribute_taxonomies' );
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response|WP_Error
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Resource does not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$attribute = $this->get_attribute( (int) $request['id'] );
|
||||
|
||||
if ( is_wp_error( $attribute ) ) {
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $attribute, $request );
|
||||
|
||||
$deleted = $wpdb->delete(
|
||||
$wpdb->prefix . 'woocommerce_attribute_taxonomies',
|
||||
array( 'attribute_id' => $attribute->attribute_id ),
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
if ( false === $deleted ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
$taxonomy = wc_attribute_taxonomy_name( $attribute->attribute_name );
|
||||
|
||||
if ( taxonomy_exists( $taxonomy ) ) {
|
||||
$terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' );
|
||||
foreach ( $terms as $term ) {
|
||||
wp_delete_term( $term->term_id, $taxonomy );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a single attribute is deleted via the REST API.
|
||||
*
|
||||
* @param stdObject $attribute The deleted attribute.
|
||||
* @param WP_REST_Response $response The response data.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_delete_product_attribute', $attribute, $response, $request );
|
||||
|
||||
// Fires woocommerce_attribute_deleted hook.
|
||||
do_action( 'woocommerce_attribute_deleted', $attribute->attribute_id, $attribute->attribute_name, $taxonomy );
|
||||
|
||||
// Clear transients.
|
||||
wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
|
||||
delete_transient( 'wc_attribute_taxonomies' );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single product attribute output for response.
|
||||
*
|
||||
* @param obj $item Term object.
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_REST_Response $response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
$data = array(
|
||||
'id' => (int) $item->attribute_id,
|
||||
'name' => $item->attribute_label,
|
||||
'slug' => wc_attribute_taxonomy_name( $item->attribute_name ),
|
||||
'type' => $item->attribute_type,
|
||||
'order_by' => $item->attribute_orderby,
|
||||
'has_archives' => (bool) $item->attribute_public,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $item ) );
|
||||
|
||||
/**
|
||||
* Filter a attribute item returned from the API.
|
||||
*
|
||||
* Allows modification of the product attribute data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $item The original attribute object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_product_attribute', $response, $item, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param object $attribute Attribute object.
|
||||
* @return array Links for the given attribute.
|
||||
*/
|
||||
protected function prepare_links( $attribute ) {
|
||||
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( trailingslashit( $base ) . $attribute->attribute_id ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( $base ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Attribute's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'product_attribute',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Attribute name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
),
|
||||
),
|
||||
'type' => array(
|
||||
'description' => __( 'Type of attribute.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'select',
|
||||
'enum' => array_keys( wc_get_attribute_types() ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'order_by' => array(
|
||||
'description' => __( 'Default sort order.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'menu_order',
|
||||
'enum' => array( 'menu_order', 'name', 'name_num', 'id' ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'has_archives' => array(
|
||||
'description' => __( 'Enable/Disable attribute archives.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = array();
|
||||
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute name.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return string
|
||||
*/
|
||||
protected function get_taxonomy( $request ) {
|
||||
if ( '' !== $this->attribute ) {
|
||||
return $this->attribute;
|
||||
}
|
||||
|
||||
if ( $request['id'] ) {
|
||||
$name = wc_attribute_taxonomy_name_by_id( (int) $request['id'] );
|
||||
|
||||
$this->attribute = $name;
|
||||
}
|
||||
|
||||
return $this->attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute data.
|
||||
*
|
||||
* @param int $id Attribute ID.
|
||||
* @return stdClass|WP_Error
|
||||
*/
|
||||
protected function get_attribute( $id ) {
|
||||
global $wpdb;
|
||||
|
||||
$attribute = $wpdb->get_row( $wpdb->prepare( "
|
||||
SELECT *
|
||||
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
|
||||
WHERE attribute_id = %d
|
||||
", $id ) );
|
||||
|
||||
if ( is_wp_error( $attribute ) || is_null( $attribute ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_attribute_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate attribute slug.
|
||||
*
|
||||
* @param string $slug
|
||||
* @param bool $new_data
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
protected function validate_attribute_slug( $slug, $new_data = true ) {
|
||||
if ( strlen( $slug ) >= 28 ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_product_attribute_slug_too_long', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
|
||||
} elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_product_attribute_slug_reserved_name', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
|
||||
} elseif ( $new_data && taxonomy_exists( wc_attribute_taxonomy_name( $slug ) ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_product_attribute_slug_already_exists', sprintf( __( 'Slug "%s" is already in use. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -18,250 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Product Categories controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Terms_Controller
|
||||
* @extends WC_REST_Product_Categories_V1_Controller
|
||||
*/
|
||||
class WC_REST_Product_Categories_Controller extends WC_REST_Terms_Controller {
|
||||
class WC_REST_Product_Categories_Controller extends WC_REST_Product_Categories_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/categories';
|
||||
|
||||
/**
|
||||
* Taxonomy.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $taxonomy = 'product_cat';
|
||||
|
||||
/**
|
||||
* Prepare a single product category output for response.
|
||||
*
|
||||
* @param WP_Term $item Term object.
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_REST_Response $response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
// Get category display type.
|
||||
$display_type = get_woocommerce_term_meta( $item->term_id, 'display_type' );
|
||||
|
||||
// Get category order.
|
||||
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order' );
|
||||
|
||||
$data = array(
|
||||
'id' => (int) $item->term_id,
|
||||
'name' => $item->name,
|
||||
'slug' => $item->slug,
|
||||
'parent' => (int) $item->parent,
|
||||
'description' => $item->description,
|
||||
'display' => $display_type ? $display_type : 'default',
|
||||
'image' => array(),
|
||||
'menu_order' => (int) $menu_order,
|
||||
'count' => (int) $item->count,
|
||||
);
|
||||
|
||||
// Get category image.
|
||||
if ( $image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' ) ) {
|
||||
$attachment = get_post( $image_id );
|
||||
|
||||
$data['image'] = array(
|
||||
'id' => (int) $image_id,
|
||||
'date_created' => wc_rest_prepare_date_response( $attachment->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $attachment->post_modified_gmt ),
|
||||
'src' => wp_get_attachment_url( $image_id ),
|
||||
'title' => get_the_title( $attachment ),
|
||||
'alt' => get_post_meta( $image_id, '_wp_attachment_image_alt', true ),
|
||||
);
|
||||
}
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||
|
||||
/**
|
||||
* Filter a term item returned from the API.
|
||||
*
|
||||
* Allows modification of the term data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $item The original term object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update term meta fields.
|
||||
*
|
||||
* @param WP_Term $term
|
||||
* @param WP_REST_Request $request
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
protected function update_term_meta_fields( $term, $request ) {
|
||||
$id = (int) $term->term_id;
|
||||
|
||||
if ( isset( $request['display'] ) ) {
|
||||
update_woocommerce_term_meta( $id, 'display_type', 'default' === $request['display'] ? '' : $request['display'] );
|
||||
}
|
||||
|
||||
if ( isset( $request['menu_order'] ) ) {
|
||||
update_woocommerce_term_meta( $id, 'order', $request['menu_order'] );
|
||||
}
|
||||
|
||||
if ( isset( $request['image'] ) ) {
|
||||
if ( empty( $request['image']['id'] ) && ! empty( $request['image']['src'] ) ) {
|
||||
$upload = wc_rest_upload_image_from_url( esc_url_raw( $request['image']['src'] ) );
|
||||
|
||||
if ( is_wp_error( $upload ) ) {
|
||||
return $upload;
|
||||
}
|
||||
|
||||
$image_id = wc_rest_set_uploaded_image_as_attachment( $upload );
|
||||
} else {
|
||||
$image_id = isset( $request['image']['id'] ) ? absint( $request['image']['id'] ) : 0;
|
||||
}
|
||||
|
||||
// Check if image_id is a valid image attachment before updating the term meta.
|
||||
if ( $image_id && wp_attachment_is_image( $image_id ) ) {
|
||||
update_woocommerce_term_meta( $id, 'thumbnail_id', $image_id );
|
||||
|
||||
// Set the image alt.
|
||||
if ( ! empty( $request['image']['alt'] ) ) {
|
||||
update_post_meta( $image_id, '_wp_attachment_image_alt', wc_clean( $request['image']['alt'] ) );
|
||||
}
|
||||
|
||||
// Set the image title.
|
||||
if ( ! empty( $request['image']['title'] ) ) {
|
||||
wp_update_post( array( 'ID' => $image_id, 'post_title' => wc_clean( $request['image']['title'] ) ) );
|
||||
}
|
||||
} else {
|
||||
delete_woocommerce_term_meta( $id, 'thumbnail_id' );
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Category schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => $this->taxonomy,
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Category name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
),
|
||||
),
|
||||
'parent' => array(
|
||||
'description' => __( 'The ID for the parent of the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'HTML description of the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'wp_filter_post_kses',
|
||||
),
|
||||
),
|
||||
'display' => array(
|
||||
'description' => __( 'Category archive display type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'default',
|
||||
'enum' => array( 'default', 'products', 'subcategories', 'both' ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'image' => array(
|
||||
'description' => __( 'Image data.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Image ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_modified' => array(
|
||||
'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'src' => array(
|
||||
'description' => __( 'Image URL.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Image name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'alt' => array(
|
||||
'description' => __( 'Image alternative text.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'menu_order' => array(
|
||||
'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'count' => array(
|
||||
'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Product Reviews Controller.
|
||||
* REST API Product Reviews Controller
|
||||
*
|
||||
* Handles requests to /products/<product_id>/reviews.
|
||||
*
|
||||
|
@ -18,16 +18,16 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Product Reviews Controller Class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
* @extends WC_REST_Product_Reviews_V1_Controller
|
||||
*/
|
||||
class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
|
||||
class WC_REST_Product_Reviews_Controller extends WC_REST_Product_Reviews_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
@ -40,87 +40,7 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
|
|||
* Register the routes for product reviews.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'product_id' => array(
|
||||
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the variation.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'review' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Review content.', 'woocommerce' ),
|
||||
),
|
||||
'name' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Name of the reviewer.', 'woocommerce' ),
|
||||
),
|
||||
'email' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Email of the reviewer.', 'woocommerce' ),
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'product_id' => array(
|
||||
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
parent::register_routes();
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
'args' => array(
|
||||
|
@ -139,78 +59,6 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
|
|||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read webhook deliveries.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_post_permissions( 'product', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['product_id'] );
|
||||
|
||||
if ( $post && ! wc_rest_check_post_permissions( 'product', 'read', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to create a new product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['product_id'] );
|
||||
if ( $post && ! wc_rest_check_post_permissions( 'product', 'create', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to update a product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['product_id'] );
|
||||
if ( $post && ! wc_rest_check_post_permissions( 'product', 'edit', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to delete a product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['product_id'] );
|
||||
if ( $post && ! wc_rest_check_post_permissions( 'product', 'delete', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to batch manage product reviews.
|
||||
*
|
||||
|
@ -224,218 +72,6 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all reviews from a product.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$product_id = (int) $request['product_id'];
|
||||
|
||||
if ( 'product' !== get_post_type( $product_id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$reviews = get_approved_comments( $product_id );
|
||||
$data = array();
|
||||
foreach ( $reviews as $review_data ) {
|
||||
$review = $this->prepare_item_for_response( $review_data, $request );
|
||||
$review = $this->prepare_response_for_collection( $review );
|
||||
$data[] = $review;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$product_id = (int) $request['product_id'];
|
||||
|
||||
if ( 'product' !== get_post_type( $product_id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$review = get_comment( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $review ) || intval( $review->comment_post_ID ) !== $product_id ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$delivery = $this->prepare_item_for_response( $review, $request );
|
||||
$response = rest_ensure_response( $delivery );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
$product_id = (int) $request['product_id'];
|
||||
|
||||
if ( 'product' !== get_post_type( $product_id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$prepared_review = $this->prepare_item_for_database( $request );
|
||||
|
||||
/**
|
||||
* Filter a product review (comment) before it is inserted via the REST API.
|
||||
*
|
||||
* Allows modification of the comment right before it is inserted via `wp_insert_comment`.
|
||||
*
|
||||
* @param array $prepared_review The prepared comment data for `wp_insert_comment`.
|
||||
* @param WP_REST_Request $request Request used to insert the comment.
|
||||
*/
|
||||
$prepared_review = apply_filters( 'rest_pre_insert_product_review', $prepared_review, $request );
|
||||
|
||||
$product_review_id = wp_insert_comment( $prepared_review );
|
||||
if ( ! $product_review_id ) {
|
||||
return new WP_Error( 'rest_product_review_failed_create', __( 'Creating product review failed.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
update_comment_meta( $product_review_id, 'rating', ( ! empty( $request['rating'] ) ? $request['rating'] : '0' ) );
|
||||
|
||||
$product_review = get_comment( $product_review_id );
|
||||
$this->update_additional_fields_for_object( $product_review, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Comment $product_review Inserted object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_product_review", $product_review, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $product_review, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $product_review_id ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
$product_review_id = (int) $request['id'];
|
||||
$product_id = (int) $request['product_id'];
|
||||
|
||||
if ( 'product' !== get_post_type( $product_id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$review = get_comment( $product_review_id );
|
||||
|
||||
if ( empty( $product_review_id ) || empty( $review ) || intval( $review->comment_post_ID ) !== $product_id ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$prepared_review = $this->prepare_item_for_database( $request );
|
||||
|
||||
$updated = wp_update_comment( $prepared_review );
|
||||
if ( 0 === $updated ) {
|
||||
return new WP_Error( 'rest_product_review_failed_edit', __( 'Updating product review failed.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
if ( ! empty( $request['rating'] ) ) {
|
||||
update_comment_meta( $product_review_id, 'rating', $request['rating'] );
|
||||
}
|
||||
|
||||
$product_review = get_comment( $product_review_id );
|
||||
$this->update_additional_fields_for_object( $product_review, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Comment $comment Inserted object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_product_review", $product_review, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $product_review, $request );
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$product_review_id = absint( is_array( $request['id'] ) ? $request['id']['id'] : $request['id'] );
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
$product_review = get_comment( $product_review_id );
|
||||
if ( empty( $product_review_id ) || empty( $product_review->comment_ID ) || empty( $product_review->comment_post_ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid product review ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter whether a product review is trashable.
|
||||
*
|
||||
* Return false to disable trash support for the product review.
|
||||
*
|
||||
* @param boolean $supports_trash Whether the object supports trashing.
|
||||
* @param WP_Post $product_review The object being considered for trashing support.
|
||||
*/
|
||||
$supports_trash = apply_filters( 'rest_product_review_trashable', ( EMPTY_TRASH_DAYS > 0 ), $product_review );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $product_review, $request );
|
||||
|
||||
if ( $force ) {
|
||||
$result = wp_delete_comment( $product_review_id, true );
|
||||
} else {
|
||||
if ( ! $supports_trash ) {
|
||||
return new WP_Error( 'rest_trash_not_supported', __( 'The product review does not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
if ( 'trash' === $product_review->comment_approved ) {
|
||||
return new WP_Error( 'rest_already_trashed', __( 'The comment has already been trashed.', 'woocommerce' ), array( 'status' => 410 ) );
|
||||
}
|
||||
|
||||
$result = wp_trash_comment( $product_review->comment_ID );
|
||||
}
|
||||
|
||||
if ( ! $result ) {
|
||||
return new WP_Error( 'rest_cannot_delete', __( 'The product review cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a product review is deleted via the REST API.
|
||||
*
|
||||
* @param object $product_review The deleted item.
|
||||
* @param WP_REST_Response $response The response data.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'rest_delete_product_review', $product_review, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk create, update and delete items.
|
||||
*
|
||||
|
@ -464,163 +100,4 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
|
|||
|
||||
return parent::batch_items( $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single product review output for response.
|
||||
*
|
||||
* @param WP_Comment $review Product review object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $review, $request ) {
|
||||
$data = array(
|
||||
'id' => (int) $review->comment_ID,
|
||||
'date_created' => wc_rest_prepare_date_response( $review->comment_date_gmt ),
|
||||
'review' => $review->comment_content,
|
||||
'rating' => (int) get_comment_meta( $review->comment_ID, 'rating', true ),
|
||||
'name' => $review->comment_author,
|
||||
'email' => $review->comment_author_email,
|
||||
'verified' => wc_review_is_from_verified_owner( $review->comment_ID ),
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $review, $request ) );
|
||||
|
||||
/**
|
||||
* Filter product reviews object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param WP_Comment $review Product review object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_product_review', $response, $review, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single product review to be inserted into the database.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return array|WP_Error $prepared_review
|
||||
*/
|
||||
protected function prepare_item_for_database( $request ) {
|
||||
$prepared_review = array( 'comment_approved' => 1, 'comment_type' => 'review' );
|
||||
|
||||
if ( isset( $request['id'] ) ) {
|
||||
$prepared_review['comment_ID'] = (int) $request['id'];
|
||||
}
|
||||
|
||||
if ( isset( $request['review'] ) ) {
|
||||
$prepared_review['comment_content'] = $request['review'];
|
||||
}
|
||||
|
||||
if ( isset( $request['product_id'] ) ) {
|
||||
$prepared_review['comment_post_ID'] = (int) $request['product_id'];
|
||||
}
|
||||
|
||||
if ( isset( $request['name'] ) ) {
|
||||
$prepared_review['comment_author'] = $request['name'];
|
||||
}
|
||||
|
||||
if ( isset( $request['email'] ) ) {
|
||||
$prepared_review['comment_author_email'] = $request['email'];
|
||||
}
|
||||
|
||||
return apply_filters( 'rest_preprocess_product_review', $prepared_review, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param WP_Comment $review Product review object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return array Links for the given product review.
|
||||
*/
|
||||
protected function prepare_links( $review, $request ) {
|
||||
$product_id = (int) $request['product_id'];
|
||||
$base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $review->comment_ID ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||
),
|
||||
'up' => array(
|
||||
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $product_id ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Product Review's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'product_review',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'review' => array(
|
||||
'description' => __( 'The content of the review.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the review was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'rating' => array(
|
||||
'description' => __( 'Review rating (0 to 5).', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Reviewer name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'email' => array(
|
||||
'description' => __( 'Reviewer email.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'verified' => array(
|
||||
'description' => __( 'Shows if the reviewer bought the product or not.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,117 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Product Shipping Classes controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Terms_Controller
|
||||
* @extends WC_REST_Product_Shipping_Classes_V1_Controller
|
||||
*/
|
||||
class WC_REST_Product_Shipping_Classes_Controller extends WC_REST_Terms_Controller {
|
||||
class WC_REST_Product_Shipping_Classes_Controller extends WC_REST_Product_Shipping_Classes_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/shipping_classes';
|
||||
|
||||
/**
|
||||
* Taxonomy.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $taxonomy = 'product_shipping_class';
|
||||
|
||||
/**
|
||||
* Prepare a single product shipping class output for response.
|
||||
*
|
||||
* @param obj $item Term object.
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_REST_Response $response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
$data = array(
|
||||
'id' => (int) $item->term_id,
|
||||
'name' => $item->name,
|
||||
'slug' => $item->slug,
|
||||
'description' => $item->description,
|
||||
'count' => (int) $item->count,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||
|
||||
/**
|
||||
* Filter a term item returned from the API.
|
||||
*
|
||||
* Allows modification of the term data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $item The original term object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Shipping Class schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => $this->taxonomy,
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Shipping class name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
),
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'HTML description of the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'wp_filter_post_kses',
|
||||
),
|
||||
),
|
||||
'count' => array(
|
||||
'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -18,117 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Product Tags controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Terms_Controller
|
||||
* @extends WC_REST_Product_Tags_V1_Controller
|
||||
*/
|
||||
class WC_REST_Product_Tags_Controller extends WC_REST_Terms_Controller {
|
||||
class WC_REST_Product_Tags_Controller extends WC_REST_Product_Tags_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/tags';
|
||||
|
||||
/**
|
||||
* Taxonomy.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $taxonomy = 'product_tag';
|
||||
|
||||
/**
|
||||
* Prepare a single product tag output for response.
|
||||
*
|
||||
* @param obj $item Term object.
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_REST_Response $response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
$data = array(
|
||||
'id' => (int) $item->term_id,
|
||||
'name' => $item->name,
|
||||
'slug' => $item->slug,
|
||||
'description' => $item->description,
|
||||
'count' => (int) $item->count,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||
|
||||
/**
|
||||
* Filter a term item returned from the API.
|
||||
*
|
||||
* Allows modification of the term data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $item The original term object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Tag's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => $this->taxonomy,
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Tag name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
),
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'HTML description of the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'wp_filter_post_kses',
|
||||
),
|
||||
),
|
||||
'count' => array(
|
||||
'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
@ -149,7 +149,7 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller
|
|||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $data
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public function prepare_item_for_response( $post, $request ) {
|
||||
$variation = wc_get_product( $post );
|
||||
|
@ -191,6 +191,8 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller
|
|||
'shipping_class_id' => $variation->get_shipping_class_id(),
|
||||
'image' => $this->get_images( $variation ),
|
||||
'attributes' => $this->get_attributes( $variation ),
|
||||
'menu_order' => $variation->get_menu_order(),
|
||||
'meta_data' => $variation->get_meta_data(),
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
|
@ -218,7 +220,7 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller
|
|||
* Prepare a single variation for create or update.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_Error|stdClass $data Post object.
|
||||
* @return WP_Error|stdClass Post object.
|
||||
*/
|
||||
protected function prepare_item_for_database( $request ) {
|
||||
if ( isset( $request['id'] ) ) {
|
||||
|
@ -242,41 +244,214 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller
|
|||
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $variation, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update post meta fields.
|
||||
*
|
||||
* @param WP_Post $post
|
||||
* @param WP_REST_Request $request
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
protected function update_post_meta_fields( $post, $request ) {
|
||||
try {
|
||||
$variable_product = wc_get_product( $post );
|
||||
$product = wc_get_product( $variable_product->get_parent_id() );
|
||||
$this->save_variations_data( $product, $request, true );
|
||||
return true;
|
||||
} catch ( WC_REST_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add post meta fields.
|
||||
*
|
||||
* @param WP_Post $post
|
||||
* @param WP_REST_Request $request
|
||||
* @return bool|WP_Error
|
||||
* @param WP_REST_Request $request Request data.
|
||||
* @return bool
|
||||
*/
|
||||
protected function add_post_meta_fields( $post, $request ) {
|
||||
try {
|
||||
$variable_product = wc_get_product( $post->ID );
|
||||
$product = wc_get_product( $variable_product->get_parent_id() );
|
||||
$request['id'] = $post->ID;
|
||||
$this->save_variations_data( $product, $request, true );
|
||||
return true;
|
||||
} catch ( WC_REST_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
return $this->update_post_meta_fields( $post, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update post meta fields.
|
||||
*
|
||||
* @param WP_Post $post
|
||||
* @param WP_REST_Request $request Request data.
|
||||
* @return bool
|
||||
*/
|
||||
protected function update_post_meta_fields( $post, $request ) {
|
||||
$variation = wc_get_product( $post );
|
||||
|
||||
// Save variation meta fields.
|
||||
$variation = $this->set_variation_meta( $variation, $request );
|
||||
|
||||
// Save the variation data.
|
||||
$variation->save();
|
||||
|
||||
// Clear caches here so in sync with any new variations.
|
||||
wc_delete_product_transients( $variation->get_parent_id() );
|
||||
wp_cache_delete( 'product-' . $variation->get_parent_id(), 'products' );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set variation meta.
|
||||
*
|
||||
* @throws WC_REST_Exception REST API exceptions.
|
||||
* @param WC_Product $product Product instance.
|
||||
* @param WP_REST_Request $request Request data.
|
||||
* @return WC_Product_Variation
|
||||
*/
|
||||
protected function set_variation_meta( $variation, $request ) {
|
||||
// Status.
|
||||
if ( isset( $request['visible'] ) ) {
|
||||
$variation->set_status( false === $request['visible'] ? 'private' : 'publish' );
|
||||
}
|
||||
|
||||
// SKU.
|
||||
if ( isset( $request['sku'] ) ) {
|
||||
$variation->set_sku( wc_clean( $request['sku'] ) );
|
||||
}
|
||||
|
||||
// Thumbnail.
|
||||
if ( isset( $request['image'] ) && is_array( $request['image'] ) ) {
|
||||
$image = $request['image'];
|
||||
$image = current( $image );
|
||||
if ( is_array( $image ) ) {
|
||||
$image['position'] = 0;
|
||||
}
|
||||
|
||||
$variation = $this->set_product_images( $variation, array( $image ) );
|
||||
}
|
||||
|
||||
// Virtual variation.
|
||||
if ( isset( $request['virtual'] ) ) {
|
||||
$variation->set_virtual( $request['virtual'] );
|
||||
}
|
||||
|
||||
// Downloadable variation.
|
||||
if ( isset( $request['downloadable'] ) ) {
|
||||
$variation->set_downloadable( $request['downloadable'] );
|
||||
}
|
||||
|
||||
// Downloads.
|
||||
if ( $variation->get_downloadable() ) {
|
||||
// Downloadable files.
|
||||
if ( isset( $request['downloads'] ) && is_array( $request['downloads'] ) ) {
|
||||
$variation = $this->save_downloadable_files( $variation, $request['downloads'] );
|
||||
}
|
||||
|
||||
// Download limit.
|
||||
if ( isset( $request['download_limit'] ) ) {
|
||||
$variation->set_download_limit( $request['download_limit'] );
|
||||
}
|
||||
|
||||
// Download expiry.
|
||||
if ( isset( $request['download_expiry'] ) ) {
|
||||
$variation->set_download_expiry( $request['download_expiry'] );
|
||||
}
|
||||
}
|
||||
|
||||
// Shipping data.
|
||||
$variation = $this->save_product_shipping_data( $variation, $request );
|
||||
|
||||
// Stock handling.
|
||||
if ( isset( $request['manage_stock'] ) ) {
|
||||
$variation->set_manage_stock( $request['manage_stock'] );
|
||||
}
|
||||
|
||||
if ( isset( $request['in_stock'] ) ) {
|
||||
$variation->set_stock_status( true === $request['in_stock'] ? 'instock' : 'outofstock' );
|
||||
}
|
||||
|
||||
if ( isset( $request['backorders'] ) ) {
|
||||
$variation->set_backorders( $request['backorders'] );
|
||||
}
|
||||
|
||||
if ( $variation->get_manage_stock() ) {
|
||||
if ( isset( $request['stock_quantity'] ) ) {
|
||||
$variation->set_stock_quantity( $request['stock_quantity'] );
|
||||
} elseif ( isset( $request['inventory_delta'] ) ) {
|
||||
$stock_quantity = wc_stock_amount( $variation->get_stock_quantity() );
|
||||
$stock_quantity += wc_stock_amount( $request['inventory_delta'] );
|
||||
$variation->set_stock_quantity( $stock_quantity );
|
||||
}
|
||||
} else {
|
||||
$variation->set_backorders( 'no' );
|
||||
$variation->set_stock_quantity( '' );
|
||||
}
|
||||
|
||||
// Regular Price.
|
||||
if ( isset( $request['regular_price'] ) ) {
|
||||
$variation->set_regular_price( $request['regular_price'] );
|
||||
}
|
||||
|
||||
// Sale Price.
|
||||
if ( isset( $request['sale_price'] ) ) {
|
||||
$variation->set_sale_price( $request['sale_price'] );
|
||||
}
|
||||
|
||||
if ( isset( $request['date_on_sale_from'] ) ) {
|
||||
$variation->set_date_on_sale_from( $request['date_on_sale_from'] );
|
||||
}
|
||||
|
||||
if ( isset( $request['date_on_sale_to'] ) ) {
|
||||
$variation->set_date_on_sale_to( $request['date_on_sale_to'] );
|
||||
}
|
||||
|
||||
// Tax class.
|
||||
if ( isset( $request['tax_class'] ) ) {
|
||||
$variation->set_tax_class( $request['tax_class'] );
|
||||
}
|
||||
|
||||
// Description.
|
||||
if ( isset( $request['description'] ) ) {
|
||||
$variation->set_description( wp_kses_post( $request['description'] ) );
|
||||
}
|
||||
|
||||
// Update taxonomies.
|
||||
if ( isset( $request['attributes'] ) ) {
|
||||
$attributes = array();
|
||||
$parent = wc_get_product( $variation->get_parent_id() );
|
||||
$parent_attributes = $parent->get_attributes();
|
||||
|
||||
foreach ( $request['attributes'] as $attribute ) {
|
||||
$attribute_id = 0;
|
||||
$attribute_name = '';
|
||||
|
||||
// Check ID for global attributes or name for product attributes.
|
||||
if ( ! empty( $attribute['id'] ) ) {
|
||||
$attribute_id = absint( $attribute['id'] );
|
||||
$attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id );
|
||||
} elseif ( ! empty( $attribute['name'] ) ) {
|
||||
$attribute_name = sanitize_title( $attribute['name'] );
|
||||
}
|
||||
|
||||
if ( ! $attribute_id && ! $attribute_name ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! isset( $parent_attributes[ $attribute_name ] ) || ! $parent_attributes[ $attribute_name ]->get_variation() ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attribute_key = sanitize_title( $parent_attributes[ $attribute_name ]->get_name() );
|
||||
$attribute_value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : '';
|
||||
|
||||
if ( $parent_attributes[ $attribute_name ]->is_taxonomy() ) {
|
||||
// If dealing with a taxonomy, we need to get the slug from the name posted to the API.
|
||||
$term = get_term_by( 'name', $attribute_value, $attribute_name );
|
||||
|
||||
if ( $term && ! is_wp_error( $term ) ) {
|
||||
$attribute_value = $term->slug;
|
||||
} else {
|
||||
$attribute_value = sanitize_title( $attribute_value );
|
||||
}
|
||||
}
|
||||
|
||||
$attributes[ $attribute_key ] = $attribute_value;
|
||||
}
|
||||
|
||||
$variation->set_attributes( $attributes );
|
||||
}
|
||||
|
||||
// Menu order.
|
||||
if ( $request['menu_order'] ) {
|
||||
$variation->set_menu_order( $request['menu_order'] );
|
||||
}
|
||||
|
||||
// Meta data.
|
||||
if ( is_array( $request['meta_data'] ) ) {
|
||||
foreach ( $request['meta_data'] as $meta ) {
|
||||
$variation->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
|
||||
}
|
||||
}
|
||||
|
||||
return $variation;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -621,6 +796,37 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller
|
|||
),
|
||||
),
|
||||
),
|
||||
'menu_order' => array(
|
||||
'description' => __( 'Menu order, used to custom sort products.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'meta_data' => array(
|
||||
'description' => __( 'Meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Meta ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'key' => array(
|
||||
'description' => __( 'Meta key.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'value' => array(
|
||||
'description' => __( 'Meta value.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -18,382 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Report Sales controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
* @extends WC_REST_Report_Sales_V1_Controller
|
||||
*/
|
||||
class WC_REST_Report_Sales_Controller extends WC_REST_Controller {
|
||||
class WC_REST_Report_Sales_Controller extends WC_REST_Report_Sales_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'reports/sales';
|
||||
|
||||
/**
|
||||
* Report instance.
|
||||
*
|
||||
* @var WC_Admin_Report
|
||||
*/
|
||||
protected $report;
|
||||
|
||||
/**
|
||||
* Register the routes for sales reports.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read report.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'reports', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sales reports.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$data = array();
|
||||
$item = $this->prepare_item_for_response( null, $request );
|
||||
$data[] = $this->prepare_response_for_collection( $item );
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a report sales object for serialization.
|
||||
*
|
||||
* @param null $_
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $_, $request ) {
|
||||
// Set date filtering.
|
||||
$filter = array(
|
||||
'period' => $request['period'],
|
||||
'date_min' => $request['date_min'],
|
||||
'date_max' => $request['date_max'],
|
||||
);
|
||||
$this->setup_report( $filter );
|
||||
|
||||
// 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 );
|
||||
$report_data = $this->report->get_report_data();
|
||||
$period_totals = array();
|
||||
|
||||
// Setup period totals by ensuring each period in the interval has data.
|
||||
for ( $i = 0; $i <= $this->report->chart_interval; $i++ ) {
|
||||
|
||||
switch ( $this->report->chart_groupby ) {
|
||||
case 'day' :
|
||||
$time = date( 'Y-m-d', strtotime( "+{$i} DAY", $this->report->start_date ) );
|
||||
break;
|
||||
default :
|
||||
$time = date( 'Y-m', strtotime( "+{$i} MONTH", $this->report->start_date ) );
|
||||
break;
|
||||
}
|
||||
|
||||
// Set the customer signups for each period.
|
||||
$customer_count = 0;
|
||||
foreach ( $customers as $customer ) {
|
||||
if ( date( ( 'day' == $this->report->chart_groupby ) ? 'Y-m-d' : 'Y-m', strtotime( $customer->user_registered ) ) == $time ) {
|
||||
$customer_count++;
|
||||
}
|
||||
}
|
||||
|
||||
$period_totals[ $time ] = array(
|
||||
'sales' => wc_format_decimal( 0.00, 2 ),
|
||||
'orders' => 0,
|
||||
'items' => 0,
|
||||
'tax' => wc_format_decimal( 0.00, 2 ),
|
||||
'shipping' => wc_format_decimal( 0.00, 2 ),
|
||||
'discount' => wc_format_decimal( 0.00, 2 ),
|
||||
'customers' => $customer_count,
|
||||
);
|
||||
}
|
||||
|
||||
// add total sales, total order count, total tax and total shipping for each period
|
||||
foreach ( $report_data->orders as $order ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order->post_date ) ) : date( 'Y-m', strtotime( $order->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$period_totals[ $time ]['sales'] = wc_format_decimal( $order->total_sales, 2 );
|
||||
$period_totals[ $time ]['tax'] = wc_format_decimal( $order->total_tax + $order->total_shipping_tax, 2 );
|
||||
$period_totals[ $time ]['shipping'] = wc_format_decimal( $order->total_shipping, 2 );
|
||||
}
|
||||
|
||||
foreach ( $report_data->order_counts as $order ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order->post_date ) ) : date( 'Y-m', strtotime( $order->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$period_totals[ $time ]['orders'] = (int) $order->count;
|
||||
}
|
||||
|
||||
// Add total order items for each period.
|
||||
foreach ( $report_data->order_items as $order_item ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order_item->post_date ) ) : date( 'Y-m', strtotime( $order_item->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$period_totals[ $time ]['items'] = (int) $order_item->order_item_count;
|
||||
}
|
||||
|
||||
// Add total discount for each period.
|
||||
foreach ( $report_data->coupons as $discount ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $discount->post_date ) ) : date( 'Y-m', strtotime( $discount->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$period_totals[ $time ]['discount'] = wc_format_decimal( $discount->discount_amount, 2 );
|
||||
}
|
||||
|
||||
$sales_data = array(
|
||||
'total_sales' => $report_data->total_sales,
|
||||
'net_sales' => $report_data->net_sales,
|
||||
'average_sales' => $report_data->average_sales,
|
||||
'total_orders' => $report_data->total_orders,
|
||||
'total_items' => $report_data->total_items,
|
||||
'total_tax' => wc_format_decimal( $report_data->total_tax + $report_data->total_shipping_tax, 2 ),
|
||||
'total_shipping' => $report_data->total_shipping,
|
||||
'total_refunds' => $report_data->total_refunds,
|
||||
'total_discount' => $report_data->total_coupons,
|
||||
'totals_grouped_by' => $this->report->chart_groupby,
|
||||
'totals' => $period_totals,
|
||||
'total_customers' => $total_customers,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $sales_data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( array(
|
||||
'about' => array(
|
||||
'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ),
|
||||
),
|
||||
) );
|
||||
|
||||
/**
|
||||
* Filter a report sales returned from the API.
|
||||
*
|
||||
* Allows modification of the report sales data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdClass $data The original report object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_report_sales', $response, (object) $sales_data, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the report object and parse any date filtering.
|
||||
*
|
||||
* @param array $filter date filtering
|
||||
*/
|
||||
protected function setup_report( $filter ) {
|
||||
include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php' );
|
||||
include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-report-sales-by-date.php' );
|
||||
|
||||
$this->report = new WC_Report_Sales_By_Date();
|
||||
|
||||
if ( empty( $filter['period'] ) ) {
|
||||
|
||||
// Custom date range.
|
||||
$filter['period'] = 'custom';
|
||||
|
||||
if ( ! empty( $filter['date_min'] ) || ! empty( $filter['date_max'] ) ) {
|
||||
|
||||
// Overwrite _GET to make use of WC_Admin_Report::calculate_current_range() for custom date ranges.
|
||||
$_GET['start_date'] = $filter['date_min'];
|
||||
$_GET['end_date'] = isset( $filter['date_max'] ) ? $filter['date_max'] : null;
|
||||
|
||||
} else {
|
||||
|
||||
// Default custom range to today.
|
||||
$_GET['start_date'] = $_GET['end_date'] = date( 'Y-m-d', current_time( 'timestamp' ) );
|
||||
}
|
||||
} else {
|
||||
|
||||
$filter['period'] = empty( $filter['period'] ) ? 'week' : $filter['period'];
|
||||
|
||||
// Change "week" period to "7day".
|
||||
if ( 'week' === $filter['period'] ) {
|
||||
$filter['period'] = '7day';
|
||||
}
|
||||
}
|
||||
|
||||
$this->report->calculate_current_range( $filter['period'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Report's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'sales_report',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'total_sales' => array(
|
||||
'description' => __( 'Gross sales in the period.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'net_sales' => array(
|
||||
'description' => __( 'Net sales in the period.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'average_sales' => array(
|
||||
'description' => __( 'Average net daily sales.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_orders' => array(
|
||||
'description' => __( 'Total of orders placed.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_items' => array(
|
||||
'description' => __( 'Total of items purchased.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_tax' => array(
|
||||
'description' => __( 'Total charged for taxes.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_shipping' => array(
|
||||
'description' => __( 'Total charged for shipping.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_refunds' => array(
|
||||
'description' => __( 'Total of refunded orders.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_discount' => array(
|
||||
'description' => __( 'Total of coupons used.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'totals_grouped_by' => array(
|
||||
'description' => __( 'Group type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'totals' => array(
|
||||
'description' => __( 'Totals.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'array',
|
||||
),
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
'period' => array(
|
||||
'description' => __( 'Report period.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'enum' => array( 'week', 'month', 'last_month', 'year' ),
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
'date_min' => array(
|
||||
/* translators: %s: date format */
|
||||
'description' => sprintf( __( 'Return sales for a specific start date, the date need to be in the %s format.', 'woocommerce' ), 'YYYY-MM-AA' ),
|
||||
'type' => 'string',
|
||||
'format' => 'date',
|
||||
'validate_callback' => 'wc_rest_validate_reports_request_arg',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
'date_max' => array(
|
||||
/* translators: %s: date format */
|
||||
'description' => sprintf( __( 'Return sales for a specific end date, the date need to be in the %s format.', 'woocommerce' ), 'YYYY-MM-AA' ),
|
||||
'type' => 'string',
|
||||
'format' => 'date',
|
||||
'validate_callback' => 'wc_rest_validate_reports_request_arg',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
);
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -18,157 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Report Top Sellers controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Report_Sales_Controller
|
||||
* @extends WC_REST_Report_Top_Sellers_V1_Controller
|
||||
*/
|
||||
class WC_REST_Report_Top_Sellers_Controller extends WC_REST_Report_Sales_Controller {
|
||||
class WC_REST_Report_Top_Sellers_Controller extends WC_REST_Report_Top_Sellers_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'reports/top_sellers';
|
||||
|
||||
/**
|
||||
* Get sales reports.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
// Set date filtering.
|
||||
$filter = array(
|
||||
'period' => $request['period'],
|
||||
'date_min' => $request['date_min'],
|
||||
'date_max' => $request['date_max'],
|
||||
);
|
||||
$this->setup_report( $filter );
|
||||
|
||||
$report_data = $this->report->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'_product_id' => array(
|
||||
'type' => 'order_item_meta',
|
||||
'order_item_type' => 'line_item',
|
||||
'function' => '',
|
||||
'name' => 'product_id',
|
||||
),
|
||||
'_qty' => array(
|
||||
'type' => 'order_item_meta',
|
||||
'order_item_type' => 'line_item',
|
||||
'function' => 'SUM',
|
||||
'name' => 'order_item_qty',
|
||||
),
|
||||
),
|
||||
'order_by' => 'order_item_qty DESC',
|
||||
'group_by' => 'product_id',
|
||||
'limit' => isset( $filter['limit'] ) ? absint( $filter['limit'] ) : 12,
|
||||
'query_type' => 'get_results',
|
||||
'filter_range' => true,
|
||||
) );
|
||||
|
||||
$top_sellers = array();
|
||||
|
||||
foreach ( $report_data as $item ) {
|
||||
$product = wc_get_product( $item->product_id );
|
||||
|
||||
if ( $product ) {
|
||||
$top_sellers[] = array(
|
||||
'name' => $product->get_name(),
|
||||
'product_id' => (int) $item->product_id,
|
||||
'quantity' => wc_stock_amount( $item->order_item_qty ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$data = array();
|
||||
foreach ( $top_sellers as $top_seller ) {
|
||||
$item = $this->prepare_item_for_response( (object) $top_seller, $request );
|
||||
$data[] = $this->prepare_response_for_collection( $item );
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a report sales object for serialization.
|
||||
*
|
||||
* @param stdClass $top_seller
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $top_seller, $request ) {
|
||||
$data = array(
|
||||
'name' => $top_seller->name,
|
||||
'product_id' => $top_seller->product_id,
|
||||
'quantity' => $top_seller->quantity,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( array(
|
||||
'about' => array(
|
||||
'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ),
|
||||
),
|
||||
'product' => array(
|
||||
'href' => rest_url( sprintf( '/%s/products/%s', $this->namespace, $top_seller->product_id ) ),
|
||||
),
|
||||
) );
|
||||
|
||||
/**
|
||||
* Filter a report top sellers returned from the API.
|
||||
*
|
||||
* Allows modification of the report top sellers data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdClass $top_seller The original report object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_report_top_sellers', $response, $top_seller, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Report's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'top_sellers_report',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'name' => array(
|
||||
'description' => __( 'Product name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'product_id' => array(
|
||||
'description' => __( 'Product ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'quantity' => array(
|
||||
'description' => __( 'Total number of purchases.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -18,157 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Reports controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
* @extends WC_REST_Reports_V1_Controller
|
||||
*/
|
||||
class WC_REST_Reports_Controller extends WC_REST_Controller {
|
||||
class WC_REST_Reports_Controller extends WC_REST_Reports_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'reports';
|
||||
|
||||
/**
|
||||
* Register the routes for reports.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read reports.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'reports', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all reports.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$data = array();
|
||||
$reports = array(
|
||||
array(
|
||||
'slug' => 'sales',
|
||||
'description' => __( 'List of sales reports.', 'woocommerce' ),
|
||||
),
|
||||
array(
|
||||
'slug' => 'top_sellers',
|
||||
'description' => __( 'List of top sellers products.', 'woocommerce' ),
|
||||
),
|
||||
);
|
||||
|
||||
foreach ( $reports as $report ) {
|
||||
$item = $this->prepare_item_for_response( (object) $report, $request );
|
||||
$data[] = $this->prepare_response_for_collection( $item );
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a report object for serialization.
|
||||
*
|
||||
* @param stdClass $report Report data.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $report, $request ) {
|
||||
$data = array(
|
||||
'slug' => $report->slug,
|
||||
'description' => $report->description,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $report->slug ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
|
||||
),
|
||||
) );
|
||||
|
||||
/**
|
||||
* Filter a report returned from the API.
|
||||
*
|
||||
* Allows modification of the report data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $report The original report object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_report', $response, $report, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Report's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'report',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'A human-readable description of the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -1,24 +1,31 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Settings controller
|
||||
*
|
||||
* Handles requests to the /settings endpoints.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Settings Groups Controller.
|
||||
* Handles requests to the /settings and /settings/<group> endpoints.
|
||||
* REST API Settings controller class.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @version 2.7.0
|
||||
* @since 2.7.0
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Settings_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* WP REST API namespace/version.
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
|
|
@ -1,24 +1,31 @@
|
|||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Settings Controller.
|
||||
* REST API Settings Options controller
|
||||
*
|
||||
* Handles requests to the /settings/$group/$setting endpoints.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @version 2.7.0
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Settings Options controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Settings_Options_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* WP REST API namespace/version.
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
@ -242,7 +249,7 @@ class WC_REST_Settings_Options_Controller extends WC_REST_Controller {
|
|||
|
||||
/**
|
||||
* Update a single setting in a group.
|
||||
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_Error|WP_REST_Response
|
||||
|
@ -422,22 +429,22 @@ class WC_REST_Settings_Options_Controller extends WC_REST_Controller {
|
|||
* Boolean for if a setting type is a valid supported setting type.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param string $type
|
||||
* @return boolean
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function is_setting_type_valid( $type ) {
|
||||
return in_array( $type, array(
|
||||
'text', // validates with validate_setting_text_field
|
||||
'email', // validates with validate_setting_text_field
|
||||
'number', // validates with validate_setting_text_field
|
||||
'color', // validates with validate_setting_text_field
|
||||
'password', // validates with validate_setting_text_field
|
||||
'textarea', // validates with validate_setting_textarea_field
|
||||
'select', // validates with validate_setting_select_field
|
||||
'multiselect', // validates with validate_setting_multiselect_field
|
||||
'radio', // validates with validate_setting_radio_field (-> validate_setting_select_field)
|
||||
'checkbox', // validates with validate_setting_checkbox_field
|
||||
'image_width', // validates with validate_setting_image_width_field
|
||||
'text', // Validates with validate_setting_text_field.
|
||||
'email', // Validates with validate_setting_text_field.
|
||||
'number', // Validates with validate_setting_text_field.
|
||||
'color', // Validates with validate_setting_text_field.
|
||||
'password', // Validates with validate_setting_text_field.
|
||||
'textarea', // Validates with validate_setting_textarea_field.
|
||||
'select', // Validates with validate_setting_select_field.
|
||||
'multiselect', // Validates with validate_setting_multiselect_field.
|
||||
'radio', // Validates with validate_setting_radio_field (-> validate_setting_select_field).
|
||||
'checkbox', // Validates with validate_setting_checkbox_field.
|
||||
'image_width', // Validates with validate_setting_image_width_field.
|
||||
) );
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ class WC_REST_Shipping_Methods_Controller extends WC_REST_Controller {
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
|
|
@ -25,7 +25,7 @@ class WC_REST_System_Status_Controller extends WC_REST_Controller {
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
|
|
@ -25,7 +25,7 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller {
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
|
|
@ -18,345 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Tax Classes controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
* @extends WC_REST_Tax_Classes_V1_Controller
|
||||
*/
|
||||
class WC_REST_Tax_Classes_Controller extends WC_REST_Controller {
|
||||
class WC_REST_Tax_Classes_Controller extends WC_REST_Tax_Classes_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'taxes/classes';
|
||||
|
||||
/**
|
||||
* Register the routes for tax classes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<slug>\w[\w\s\-]*)', array(
|
||||
'args' => array(
|
||||
'slug' => array(
|
||||
'description' => __( 'Unique slug for the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read tax classes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access create tax classes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'create' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access delete a tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tax classes.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$tax_classes = array();
|
||||
|
||||
// Add standard class.
|
||||
$tax_classes[] = array(
|
||||
'slug' => 'standard',
|
||||
'name' => __( 'Standard rate', 'woocommerce' ),
|
||||
);
|
||||
|
||||
$classes = WC_Tax::get_tax_classes();
|
||||
|
||||
foreach ( $classes as $class ) {
|
||||
$tax_classes[] = array(
|
||||
'slug' => sanitize_title( $class ),
|
||||
'name' => $class,
|
||||
);
|
||||
}
|
||||
|
||||
$data = array();
|
||||
foreach ( $tax_classes as $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.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
$exists = false;
|
||||
$classes = WC_Tax::get_tax_classes();
|
||||
$tax_class = array(
|
||||
'slug' => sanitize_title( $request['name'] ),
|
||||
'name' => $request['name'],
|
||||
);
|
||||
|
||||
// Check if class exists.
|
||||
foreach ( $classes as $key => $class ) {
|
||||
if ( sanitize_title( $class ) === $tax_class['slug'] ) {
|
||||
$exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Return error if tax class already exists.
|
||||
if ( $exists ) {
|
||||
return new WP_Error( 'woocommerce_rest_tax_class_exists', __( 'Cannot create existing resource.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
// Add the new class.
|
||||
$classes[] = $tax_class['name'];
|
||||
|
||||
update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) );
|
||||
|
||||
$this->update_additional_fields_for_object( $tax_class, $request );
|
||||
|
||||
/**
|
||||
* Fires after a tax class is created or updated via the REST API.
|
||||
*
|
||||
* @param stdClass $tax_class Data used to create the tax class.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating tax class, false when updating tax class.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_tax_class', (object) $tax_class, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $tax_class, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $tax_class['slug'] ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single tax class.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Taxes do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$tax_class = array(
|
||||
'slug' => sanitize_title( $request['slug'] ),
|
||||
'name' => '',
|
||||
);
|
||||
$classes = WC_Tax::get_tax_classes();
|
||||
$deleted = false;
|
||||
|
||||
foreach ( $classes as $key => $class ) {
|
||||
if ( sanitize_title( $class ) === $tax_class['slug'] ) {
|
||||
$tax_class['name'] = $class;
|
||||
unset( $classes[ $key ] );
|
||||
$deleted = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $deleted ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) );
|
||||
|
||||
// Delete tax rate locations locations from the selected class.
|
||||
$wpdb->query( $wpdb->prepare( "
|
||||
DELETE locations.*
|
||||
FROM {$wpdb->prefix}woocommerce_tax_rate_locations AS locations
|
||||
INNER JOIN
|
||||
{$wpdb->prefix}woocommerce_tax_rates AS rates
|
||||
ON rates.tax_rate_id = locations.tax_rate_id
|
||||
WHERE rates.tax_rate_class = '%s'
|
||||
", $tax_class['slug'] ) );
|
||||
|
||||
// Delete tax rates in the selected class.
|
||||
$wpdb->delete( $wpdb->prefix . 'woocommerce_tax_rates', array( 'tax_rate_class' => $tax_class['slug'] ), array( '%s' ) );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $tax_class, $request );
|
||||
|
||||
/**
|
||||
* Fires after a tax class is deleted via the REST API.
|
||||
*
|
||||
* @param stdClass $tax_class The tax data.
|
||||
* @param WP_REST_Response $response The response returned from the API.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_delete_tax', (object) $tax_class, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single tax class output for response.
|
||||
*
|
||||
* @param array $tax_class Tax class data.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $tax_class, $request ) {
|
||||
$data = $tax_class;
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links() );
|
||||
|
||||
/**
|
||||
* Filter tax object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdClass $tax_class Tax object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_tax', $response, (object) $tax_class, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @return array Links for the given tax class.
|
||||
*/
|
||||
protected function prepare_links() {
|
||||
$links = array(
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Tax Classes schema, conforming to JSON Schema
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'tax_class',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'slug' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Tax class name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'required' => true,
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -18,691 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Taxes controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
* @extends WC_REST_Taxes_V1_Controller
|
||||
*/
|
||||
class WC_REST_Taxes_Controller extends WC_REST_Controller {
|
||||
class WC_REST_Taxes_Controller extends WC_REST_Taxes_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'taxes';
|
||||
|
||||
/**
|
||||
* Register the routes for taxes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read taxes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access create taxes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'create' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access update a tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access delete a tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access batch create, update and delete items.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function batch_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'batch' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all taxes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$prepared_args = array();
|
||||
$prepared_args['exclude'] = $request['exclude'];
|
||||
$prepared_args['include'] = $request['include'];
|
||||
$prepared_args['order'] = $request['order'];
|
||||
$prepared_args['number'] = $request['per_page'];
|
||||
if ( ! empty( $request['offset'] ) ) {
|
||||
$prepared_args['offset'] = $request['offset'];
|
||||
} else {
|
||||
$prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number'];
|
||||
}
|
||||
$orderby_possibles = array(
|
||||
'id' => 'tax_rate_id',
|
||||
'order' => 'tax_rate_order',
|
||||
);
|
||||
$prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ];
|
||||
$prepared_args['class'] = $request['class'];
|
||||
|
||||
/**
|
||||
* Filter arguments, before passing to $wpdb->get_results(), when querying taxes via the REST API.
|
||||
*
|
||||
* @param array $prepared_args Array of arguments for $wpdb->get_results().
|
||||
* @param WP_REST_Request $request The current request.
|
||||
*/
|
||||
$prepared_args = apply_filters( 'woocommerce_rest_tax_query', $prepared_args, $request );
|
||||
|
||||
$query = "
|
||||
SELECT *
|
||||
FROM {$wpdb->prefix}woocommerce_tax_rates
|
||||
WHERE 1 = 1
|
||||
";
|
||||
|
||||
// Filter by tax class.
|
||||
if ( ! empty( $prepared_args['class'] ) ) {
|
||||
$class = 'standard' !== $prepared_args['class'] ? sanitize_title( $prepared_args['class'] ) : '';
|
||||
$query .= " AND tax_rate_class = '$class'";
|
||||
}
|
||||
|
||||
// Order tax rates.
|
||||
$order_by = sprintf( ' ORDER BY %s', sanitize_key( $prepared_args['orderby'] ) );
|
||||
|
||||
// Pagination.
|
||||
$pagination = sprintf( ' LIMIT %d, %d', $prepared_args['offset'], $prepared_args['number'] );
|
||||
|
||||
// Query taxes.
|
||||
$results = $wpdb->get_results( $query . $order_by . $pagination );
|
||||
|
||||
$taxes = array();
|
||||
foreach ( $results as $tax ) {
|
||||
$data = $this->prepare_item_for_response( $tax, $request );
|
||||
$taxes[] = $this->prepare_response_for_collection( $data );
|
||||
}
|
||||
|
||||
$response = rest_ensure_response( $taxes );
|
||||
|
||||
// Store pagation values for headers then unset for count query.
|
||||
$per_page = (int) $prepared_args['number'];
|
||||
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
|
||||
|
||||
// Query only for ids.
|
||||
$wpdb->get_results( str_replace( 'SELECT *', 'SELECT tax_rate_id', $query ) );
|
||||
|
||||
// Calcule totals.
|
||||
$total_taxes = (int) $wpdb->num_rows;
|
||||
$response->header( 'X-WP-Total', (int) $total_taxes );
|
||||
$max_pages = ceil( $total_taxes / $per_page );
|
||||
$response->header( 'X-WP-TotalPages', (int) $max_pages );
|
||||
|
||||
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
|
||||
if ( $page > 1 ) {
|
||||
$prev_page = $page - 1;
|
||||
if ( $prev_page > $max_pages ) {
|
||||
$prev_page = $max_pages;
|
||||
}
|
||||
$prev_link = add_query_arg( 'page', $prev_page, $base );
|
||||
$response->link_header( 'prev', $prev_link );
|
||||
}
|
||||
if ( $max_pages > $page ) {
|
||||
$next_page = $page + 1;
|
||||
$next_link = add_query_arg( 'page', $next_page, $base );
|
||||
$response->link_header( 'next', $next_link );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Take tax data from the request and return the updated or newly created rate.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @param stdClass|null $current Existing tax object.
|
||||
* @return stdClass
|
||||
*/
|
||||
protected function create_or_update_tax( $request, $current = null ) {
|
||||
$id = absint( isset( $request['id'] ) ? $request['id'] : 0 );
|
||||
$data = array();
|
||||
$fields = array(
|
||||
'tax_rate_country',
|
||||
'tax_rate_state',
|
||||
'tax_rate',
|
||||
'tax_rate_name',
|
||||
'tax_rate_priority',
|
||||
'tax_rate_compound',
|
||||
'tax_rate_shipping',
|
||||
'tax_rate_order',
|
||||
'tax_rate_class',
|
||||
);
|
||||
|
||||
foreach ( $fields as $field ) {
|
||||
// Keys via API differ from the stored names returned by _get_tax_rate.
|
||||
$key = 'tax_rate' === $field ? 'rate' : str_replace( 'tax_rate_', '', $field );
|
||||
|
||||
// Remove data that was not posted.
|
||||
if ( ! isset( $request[ $key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Test new data against current data.
|
||||
if ( $current && $current->$field === $request[ $key ] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add to data array.
|
||||
switch ( $key ) {
|
||||
case 'tax_rate_priority' :
|
||||
case 'tax_rate_compound' :
|
||||
case 'tax_rate_shipping' :
|
||||
case 'tax_rate_order' :
|
||||
$data[ $field ] = absint( $request[ $key ] );
|
||||
break;
|
||||
case 'tax_rate_class' :
|
||||
$data[ $field ] = 'standard' !== $request['tax_rate_class'] ? $request['tax_rate_class'] : '';
|
||||
break;
|
||||
default :
|
||||
$data[ $field ] = wc_clean( $request[ $key ] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $id ) {
|
||||
WC_Tax::_update_tax_rate( $id, $data );
|
||||
} else {
|
||||
$id = WC_Tax::_insert_tax_rate( $data );
|
||||
}
|
||||
|
||||
// Add locales.
|
||||
if ( ! empty( $request['postcode'] ) ) {
|
||||
WC_Tax::_update_tax_rate_postcodes( $id, wc_clean( $request['postcode'] ) );
|
||||
}
|
||||
if ( ! empty( $request['city'] ) ) {
|
||||
WC_Tax::_update_tax_rate_cities( $id, wc_clean( $request['city'] ) );
|
||||
}
|
||||
|
||||
return WC_Tax::_get_tax_rate( $id, OBJECT );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_tax_exists', __( 'Cannot create existing resource.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$tax = $this->create_or_update_tax( $request );
|
||||
|
||||
$this->update_additional_fields_for_object( $tax, $request );
|
||||
|
||||
/**
|
||||
* Fires after a tax is created or updated via the REST API.
|
||||
*
|
||||
* @param stdClass $tax Data used to create the tax.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating tax, false when updating tax.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_tax', $tax, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $tax, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $tax->tax_rate_id ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$tax_obj = WC_Tax::_get_tax_rate( $id, OBJECT );
|
||||
|
||||
if ( empty( $id ) || empty( $tax_obj ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$tax = $this->prepare_item_for_response( $tax_obj, $request );
|
||||
$response = rest_ensure_response( $tax );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$tax_obj = WC_Tax::_get_tax_rate( $id, OBJECT );
|
||||
|
||||
if ( empty( $id ) || empty( $tax_obj ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$tax = $this->create_or_update_tax( $request, $tax_obj );
|
||||
|
||||
$this->update_additional_fields_for_object( $tax, $request );
|
||||
|
||||
/**
|
||||
* Fires after a tax is created or updated via the REST API.
|
||||
*
|
||||
* @param stdClass $tax Data used to create the tax.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating tax, false when updating tax.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_tax', $tax, $request, false );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $tax, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$id = (int) $request['id'];
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Taxes do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$tax = WC_Tax::_get_tax_rate( $id, OBJECT );
|
||||
|
||||
if ( empty( $id ) || empty( $tax ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $tax, $request );
|
||||
|
||||
WC_Tax::_delete_tax_rate( $id );
|
||||
|
||||
if ( 0 === $wpdb->rows_affected ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a tax is deleted via the REST API.
|
||||
*
|
||||
* @param stdClass $tax The tax data.
|
||||
* @param WP_REST_Response $response The response returned from the API.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_delete_tax', $tax, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single tax output for response.
|
||||
*
|
||||
* @param stdClass $tax Tax object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $tax, $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$id = (int) $tax->tax_rate_id;
|
||||
$data = array(
|
||||
'id' => $id,
|
||||
'country' => $tax->tax_rate_country,
|
||||
'state' => $tax->tax_rate_state,
|
||||
'postcode' => '',
|
||||
'city' => '',
|
||||
'rate' => $tax->tax_rate,
|
||||
'name' => $tax->tax_rate_name,
|
||||
'priority' => (int) $tax->tax_rate_priority,
|
||||
'compound' => (bool) $tax->tax_rate_compound,
|
||||
'shipping' => (bool) $tax->tax_rate_shipping,
|
||||
'order' => (int) $tax->tax_rate_order,
|
||||
'class' => $tax->tax_rate_class ? $tax->tax_rate_class : 'standard',
|
||||
);
|
||||
|
||||
// Get locales from a tax rate.
|
||||
$locales = $wpdb->get_results( $wpdb->prepare( "
|
||||
SELECT location_code, location_type
|
||||
FROM {$wpdb->prefix}woocommerce_tax_rate_locations
|
||||
WHERE tax_rate_id = %d
|
||||
", $id ) );
|
||||
|
||||
if ( ! is_wp_error( $tax ) && ! is_null( $tax ) ) {
|
||||
foreach ( $locales as $locale ) {
|
||||
$data[ $locale->location_type ] = $locale->location_code;
|
||||
}
|
||||
}
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $tax ) );
|
||||
|
||||
/**
|
||||
* Filter tax object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdClass $tax Tax object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_tax', $response, $tax, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param stdClass $tax Tax object.
|
||||
* @return array Links for the given tax.
|
||||
*/
|
||||
protected function prepare_links( $tax ) {
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $tax->tax_rate_id ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Taxes schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'tax',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'country' => array(
|
||||
'description' => __( 'Country ISO 3166 code.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'state' => array(
|
||||
'description' => __( 'State code.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'postcode' => array(
|
||||
'description' => __( 'Postcode / ZIP.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'city' => array(
|
||||
'description' => __( 'City name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'rate' => array(
|
||||
'description' => __( 'Tax rate.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Tax rate name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'priority' => array(
|
||||
'description' => __( 'Tax priority.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'default' => 1,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'compound' => array(
|
||||
'description' => __( 'Whether or not this is a compound rate.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'shipping' => array(
|
||||
'description' => __( 'Whether or not this tax rate also gets applied to shipping.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => true,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'order' => array(
|
||||
'description' => __( 'Indicates the order that will appear in queries.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'class' => array(
|
||||
'description' => __( 'Tax class.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'standard',
|
||||
'enum' => array_merge( array( 'standard' ), WC_Tax::get_tax_class_slugs() ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['context']['default'] = 'view';
|
||||
|
||||
$params['exclude'] = array(
|
||||
'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
$params['include'] = array(
|
||||
'description' => __( 'Limit result set to specific IDs.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
$params['offset'] = array(
|
||||
'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['order'] = array(
|
||||
'default' => 'asc',
|
||||
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
|
||||
'enum' => array( 'asc', 'desc' ),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['orderby'] = array(
|
||||
'default' => 'order',
|
||||
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
|
||||
'enum' => array(
|
||||
'id',
|
||||
'order',
|
||||
),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['class'] = array(
|
||||
'description' => __( 'Sort by tax class.', 'woocommerce' ),
|
||||
'enum' => array_merge( array( 'standard' ), WC_Tax::get_tax_class_slugs() ),
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -18,312 +18,14 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* REST API Webhook Deliveries controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
* @extends WC_REST_Webhook_Deliveries_V1_Controller
|
||||
*/
|
||||
class WC_REST_Webhook_Deliveries_Controller extends WC_REST_Controller {
|
||||
class WC_REST_Webhook_Deliveries_Controller extends WC_REST_Webhook_Deliveries_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'webhooks/(?P<webhook_id>[\d]+)/deliveries';
|
||||
|
||||
/**
|
||||
* Register the routes for webhook deliveries.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'webhook_id' => array(
|
||||
'description' => __( 'Unique identifier for the webhook.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'webhook_id' => array(
|
||||
'description' => __( 'Unique identifier for the webhook.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read webhook deliveries.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_post_permissions( 'shop_webhook', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a webhook develivery.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['webhook_id'] );
|
||||
|
||||
if ( $post && ! wc_rest_check_post_permissions( 'shop_webhook', 'read', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all webhook deliveries.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$webhook = new WC_Webhook( (int) $request['webhook_id'] );
|
||||
|
||||
if ( empty( $webhook->post_data->post_type ) || 'shop_webhook' !== $webhook->post_data->post_type ) {
|
||||
return new WP_Error( 'woocommerce_rest_webhook_invalid_id', __( 'Invalid webhook ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$logs = $webhook->get_delivery_logs();
|
||||
|
||||
$data = array();
|
||||
foreach ( $logs as $log ) {
|
||||
$delivery = $this->prepare_item_for_response( (object) $log, $request );
|
||||
$delivery = $this->prepare_response_for_collection( $delivery );
|
||||
$data[] = $delivery;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single webhook delivery.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$webhook = new WC_Webhook( (int) $request['webhook_id'] );
|
||||
|
||||
if ( empty( $webhook->post_data->post_type ) || 'shop_webhook' !== $webhook->post_data->post_type ) {
|
||||
return new WP_Error( 'woocommerce_rest_webhook_invalid_id', __( 'Invalid webhook ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$log = $webhook->get_delivery_log( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $log ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$delivery = $this->prepare_item_for_response( (object) $log, $request );
|
||||
$response = rest_ensure_response( $delivery );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single webhook delivery output for response.
|
||||
*
|
||||
* @param stdClass $log Delivery log object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $log, $request ) {
|
||||
$data = (array) $log;
|
||||
|
||||
// Add timestamp.
|
||||
$data['date_created'] = wc_rest_prepare_date_response( $log->comment->comment_date_gmt );
|
||||
|
||||
// Remove comment object.
|
||||
unset( $data['comment'] );
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $log ) );
|
||||
|
||||
/**
|
||||
* Filter webhook delivery object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdClass $log Delivery log object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_webhook_delivery', $response, $log, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param stdClass $log Delivery log object.
|
||||
* @return array Links for the given webhook delivery.
|
||||
*/
|
||||
protected function prepare_links( $log ) {
|
||||
$webhook_id = (int) $log->request_headers['X-WC-Webhook-ID'];
|
||||
$base = str_replace( '(?P<webhook_id>[\d]+)', $webhook_id, $this->rest_base );
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $log->id ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||
),
|
||||
'up' => array(
|
||||
'href' => rest_url( sprintf( '/%s/webhooks/%d', $this->namespace, $webhook_id ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Webhook's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'webhook_delivery',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'duration' => array(
|
||||
'description' => __( 'The delivery duration, in seconds.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'summary' => array(
|
||||
'description' => __( 'A friendly summary of the response including the HTTP response code, message, and body.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'request_url' => array(
|
||||
'description' => __( 'The URL where the webhook was delivered.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'request_headers' => array(
|
||||
'description' => __( 'The URL where the webhook was delivered.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'request_headers' => array(
|
||||
'description' => __( 'Request headers.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
'request_body' => array(
|
||||
'description' => __( 'Request body.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'response_code' => array(
|
||||
'description' => __( 'The HTTP response code from the receiving server.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'response_message' => array(
|
||||
'description' => __( 'The HTTP response message from the receiving server.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'response_headers' => array(
|
||||
'description' => __( 'Array of the response headers from the receiving server.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
'response_body' => array(
|
||||
'description' => __( 'The response body from the receiving server.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the webhook delivery was logged, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -20,564 +20,12 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Posts_Controller
|
||||
*/
|
||||
class WC_REST_Webhooks_Controller extends WC_REST_Posts_Controller {
|
||||
class WC_REST_Webhooks_Controller extends WC_REST_Webhooks_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'webhooks';
|
||||
|
||||
/**
|
||||
* Post type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $post_type = 'shop_webhook';
|
||||
|
||||
/**
|
||||
* Initialize Webhooks actions.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the routes for webhooks.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'topic' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Webhook topic.', 'woocommerce' ),
|
||||
),
|
||||
'delivery_url' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Webhook delivery URL.', 'woocommerce' ),
|
||||
),
|
||||
'secret' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Webhook secret.', 'woocommerce' ),
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single webhook.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
/* translators: %s: post type */
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
// Validate topic.
|
||||
if ( empty( $request['topic'] ) || ! wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic is required and must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
// Validate delivery URL.
|
||||
if ( empty( $request['delivery_url'] ) || ! wc_is_valid_url( $request['delivery_url'] ) ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_delivery_url", __( 'Webhook delivery URL must be a valid URL starting with http:// or https://.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$post = $this->prepare_item_for_database( $request );
|
||||
if ( is_wp_error( $post ) ) {
|
||||
return $post;
|
||||
}
|
||||
|
||||
$post->post_type = $this->post_type;
|
||||
$post_id = wp_insert_post( $post, true );
|
||||
|
||||
if ( is_wp_error( $post_id ) ) {
|
||||
|
||||
if ( in_array( $post_id->get_error_code(), array( 'db_insert_error' ) ) ) {
|
||||
$post_id->add_data( array( 'status' => 500 ) );
|
||||
} else {
|
||||
$post_id->add_data( array( 'status' => 400 ) );
|
||||
}
|
||||
return $post_id;
|
||||
}
|
||||
$post->ID = $post_id;
|
||||
|
||||
$webhook = new WC_Webhook( $post_id );
|
||||
|
||||
// Set topic.
|
||||
$webhook->set_topic( $request['topic'] );
|
||||
|
||||
// Set delivery URL.
|
||||
$webhook->set_delivery_url( $request['delivery_url'] );
|
||||
|
||||
// Set secret.
|
||||
$webhook->set_secret( ! empty( $request['secret'] ) ? $request['secret'] : '' );
|
||||
|
||||
// Set API version to WP API integration v1.
|
||||
$webhook->set_api_version( 'wp_api_v1' );
|
||||
|
||||
// Set status.
|
||||
if ( ! empty( $request['status'] ) ) {
|
||||
$webhook->update_status( $request['status'] );
|
||||
}
|
||||
|
||||
$post = get_post( $post_id );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Post $post Inserted object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post_id ) ) );
|
||||
|
||||
// Send ping.
|
||||
$webhook->deliver_ping();
|
||||
|
||||
// Clear cache.
|
||||
delete_transient( 'woocommerce_webhook_ids' );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single webhook.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$post = get_post( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$webhook = new WC_Webhook( $id );
|
||||
|
||||
// Update topic.
|
||||
if ( ! empty( $request['topic'] ) ) {
|
||||
if ( wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) {
|
||||
$webhook->set_topic( $request['topic'] );
|
||||
} else {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Update delivery URL.
|
||||
if ( ! empty( $request['delivery_url'] ) ) {
|
||||
if ( wc_is_valid_url( $request['delivery_url'] ) ) {
|
||||
$webhook->set_delivery_url( $request['delivery_url'] );
|
||||
} else {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_delivery_url", __( 'Webhook delivery URL must be a valid URL starting with http:// or https://.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Update secret.
|
||||
if ( ! empty( $request['secret'] ) ) {
|
||||
$webhook->set_secret( $request['secret'] );
|
||||
}
|
||||
|
||||
// Update status.
|
||||
if ( ! empty( $request['status'] ) ) {
|
||||
$webhook->update_status( $request['status'] );
|
||||
}
|
||||
|
||||
$post = $this->prepare_item_for_database( $request );
|
||||
if ( is_wp_error( $post ) ) {
|
||||
return $post;
|
||||
}
|
||||
|
||||
// Convert the post object to an array, otherwise wp_update_post will expect non-escaped input.
|
||||
$post_id = wp_update_post( (array) $post, true );
|
||||
if ( is_wp_error( $post_id ) ) {
|
||||
if ( in_array( $post_id->get_error_code(), array( 'db_update_error' ) ) ) {
|
||||
$post_id->add_data( array( 'status' => 500 ) );
|
||||
} else {
|
||||
$post_id->add_data( array( 'status' => 400 ) );
|
||||
}
|
||||
return $post_id;
|
||||
}
|
||||
|
||||
$post = get_post( $post_id );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Post $post Inserted object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
|
||||
// Clear cache.
|
||||
delete_transient( 'woocommerce_webhook_ids' );
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single webhook.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response|WP_Error
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Webhooks do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$post = get_post( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid post ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
|
||||
$result = wp_delete_post( $id, true );
|
||||
|
||||
if ( ! $result ) {
|
||||
/* translators: %s: post type */
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a single item is deleted or trashed via the REST API.
|
||||
*
|
||||
* @param object $post The deleted or trashed item.
|
||||
* @param WP_REST_Response $response The response data.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( "woocommerce_rest_delete_{$this->post_type}", $post, $response, $request );
|
||||
|
||||
// Clear cache.
|
||||
delete_transient( 'woocommerce_webhook_ids' );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single webhook for create or update.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_Error|stdClass $data Post object.
|
||||
*/
|
||||
protected function prepare_item_for_database( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$data = new stdClass;
|
||||
|
||||
// Post ID.
|
||||
if ( isset( $request['id'] ) ) {
|
||||
$data->ID = absint( $request['id'] );
|
||||
}
|
||||
|
||||
// Validate required POST fields.
|
||||
if ( 'POST' === $request->get_method() && empty( $data->ID ) ) {
|
||||
// @codingStandardsIgnoreStart
|
||||
$data->post_title = ! empty( $request['name'] ) ? $request['name'] : sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) );
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
// Post author.
|
||||
$data->post_author = get_current_user_id();
|
||||
|
||||
// Post password.
|
||||
$password = strlen( uniqid( 'webhook_' ) );
|
||||
$data->post_password = $password > 20 ? substr( $password, 0, 20 ) : $password;
|
||||
|
||||
// Post status.
|
||||
$data->post_status = 'publish';
|
||||
} else {
|
||||
|
||||
// Allow edit post title.
|
||||
if ( ! empty( $request['name'] ) ) {
|
||||
$data->post_title = $request['name'];
|
||||
}
|
||||
}
|
||||
|
||||
// Comment status.
|
||||
$data->comment_status = 'closed';
|
||||
|
||||
// Ping status.
|
||||
$data->ping_status = 'closed';
|
||||
|
||||
/**
|
||||
* Filter the query_vars used in `get_items` for the constructed query.
|
||||
*
|
||||
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
|
||||
* prepared for insertion.
|
||||
*
|
||||
* @param stdClass $data An object representing a single item prepared
|
||||
* for inserting or updating the database.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $data, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single webhook output for response.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $post, $request ) {
|
||||
$id = (int) $post->ID;
|
||||
$webhook = new WC_Webhook( $id );
|
||||
$data = array(
|
||||
'id' => $webhook->id,
|
||||
'name' => $webhook->get_name(),
|
||||
'status' => $webhook->get_status(),
|
||||
'topic' => $webhook->get_topic(),
|
||||
'resource' => $webhook->get_resource(),
|
||||
'event' => $webhook->get_event(),
|
||||
'hooks' => $webhook->get_hooks(),
|
||||
'delivery_url' => $webhook->get_delivery_url(),
|
||||
'date_created' => wc_rest_prepare_date_response( $webhook->get_post_data()->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $webhook->get_post_data()->post_modified_gmt ),
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $post, $request ) );
|
||||
|
||||
/**
|
||||
* Filter webhook object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param WC_Webhook $webhook Webhook object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $webhook, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Query args.
|
||||
*
|
||||
* @param array $args
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function query_args( $args, $request ) {
|
||||
// Set post_status.
|
||||
switch ( $request['status'] ) {
|
||||
case 'active' :
|
||||
$args['post_status'] = 'publish';
|
||||
break;
|
||||
case 'paused' :
|
||||
$args['post_status'] = 'draft';
|
||||
break;
|
||||
case 'disabled' :
|
||||
$args['post_status'] = 'pending';
|
||||
break;
|
||||
default :
|
||||
$args['post_status'] = 'any';
|
||||
break;
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Webhook's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'webhook',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'A friendly name for the webhook.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'status' => array(
|
||||
'description' => __( 'Webhook status.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'active',
|
||||
'enum' => array( 'active', 'paused', 'disabled' ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'wc_is_webhook_valid_topic',
|
||||
),
|
||||
),
|
||||
'topic' => array(
|
||||
'description' => __( 'Webhook topic.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'resource' => array(
|
||||
'description' => __( 'Webhook resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'event' => array(
|
||||
'description' => __( 'Webhook event.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'hooks' => array(
|
||||
'description' => __( 'WooCommerce action names associated with the webhook.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
'delivery_url' => array(
|
||||
'description' => __( 'The URL where the webhook payload is delivered.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'secret' => array(
|
||||
'description' => __( "Secret key used to generate a hash of the delivered webhook and provided in the request headers. This will default is a MD5 hash from the current user's ID|username if not provided.", 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'edit' ),
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the webhook was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_modified' => array(
|
||||
'description' => __( "The date the webhook was last modified, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections of attachments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['status'] = array(
|
||||
'default' => 'all',
|
||||
'description' => __( 'Limit result set to webhooks assigned a specific status.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'enum' => array( 'all', 'active', 'paused', 'disabled' ),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
protected $namespace = 'wc/v2';
|
||||
}
|
||||
|
|
|
@ -309,7 +309,7 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
if ( is_callable( array( $customer, "set_billing_{$field}" ) ) ) {
|
||||
$customer->{"set_billing_{$field}"}( $data['billing_address'][ $field ] );
|
||||
} else {
|
||||
$customer->update_meta_data( 'billing_' . $field, wc_clean( $data['billing_address'][ $field ] ), $meta['id'] );
|
||||
$customer->update_meta_data( 'billing_' . $field, wc_clean( $data['billing_address'][ $field ] ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -322,7 +322,7 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
if ( is_callable( array( $customer, "set_shipping_{$field}" ) ) ) {
|
||||
$customer->{"set_shipping_{$field}"}( $data['shipping_address'][ $field ] );
|
||||
} else {
|
||||
$customer->update_meta_data( 'shipping_' . $field, wc_clean( $data['shipping_address'][ $field ] ), $meta['id'] );
|
||||
$customer->update_meta_data( 'shipping_' . $field, wc_clean( $data['shipping_address'][ $field ] ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -310,7 +310,7 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
if ( is_callable( array( $customer, "set_billing_{$field}" ) ) ) {
|
||||
$customer->{"set_billing_{$field}"}( $data['billing_address'][ $field ] );
|
||||
} else {
|
||||
$customer->update_meta_data( 'billing_' . $field, wc_clean( $data['billing_address'][ $field ] ), $meta['id'] );
|
||||
$customer->update_meta_data( 'billing_' . $field, wc_clean( $data['billing_address'][ $field ] ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -323,7 +323,7 @@ class WC_API_Customers extends WC_API_Resource {
|
|||
if ( is_callable( array( $customer, "set_shipping_{$field}" ) ) ) {
|
||||
$customer->{"set_shipping_{$field}"}( $data['shipping_address'][ $field ] );
|
||||
} else {
|
||||
$customer->update_meta_data( 'shipping_' . $field, wc_clean( $data['shipping_address'][ $field ] ), $meta['id'] );
|
||||
$customer->update_meta_data( 'shipping_' . $field, wc_clean( $data['shipping_address'][ $field ] ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,577 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Coupons controller
|
||||
*
|
||||
* Handles requests to the /coupons endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Coupons controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Posts_Controller
|
||||
*/
|
||||
class WC_REST_Coupons_V1_Controller extends WC_REST_Posts_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'coupons';
|
||||
|
||||
/**
|
||||
* Post type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $post_type = 'shop_coupon';
|
||||
|
||||
/**
|
||||
* Order refunds actions.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the routes for coupons.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'code' => array(
|
||||
'description' => __( 'Coupon code.', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Query args.
|
||||
*
|
||||
* @param array $args Query args
|
||||
* @param WP_REST_Request $request Request data.
|
||||
* @return array
|
||||
*/
|
||||
public function query_args( $args, $request ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( ! empty( $request['code'] ) ) {
|
||||
$id = wc_get_coupon_id_by_code( $request['code'] );
|
||||
$args['post__in'] = array( $id );
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single coupon output for response.
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $data
|
||||
*/
|
||||
public function prepare_item_for_response( $post, $request ) {
|
||||
$coupon = new WC_Coupon( (int) $post->ID );
|
||||
$_data = $coupon->get_data();
|
||||
|
||||
$format_decimal = array( 'amount', 'minimum_amount', 'maximum_amount' );
|
||||
$format_date = array( 'date_created', 'date_modified', 'date_expires' );
|
||||
$format_null = array( 'usage_limit', 'usage_limit_per_user', 'limit_usage_to_x_items' );
|
||||
|
||||
// Format decimal values.
|
||||
foreach ( $format_decimal as $key ) {
|
||||
$_data[ $key ] = wc_format_decimal( $_data[ $key ], 2 );
|
||||
}
|
||||
|
||||
// Format date values.
|
||||
foreach ( $format_date as $key ) {
|
||||
$_data[ $key ] = $_data[ $key ] ? wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $_data[ $key ] ) ) ) : null;
|
||||
}
|
||||
|
||||
// Format null values.
|
||||
foreach ( $format_null as $key ) {
|
||||
$_data[ $key ] = $_data[ $key ] ? $_data[ $key ] : null;
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'id' => $_data['id'],
|
||||
'code' => $_data['code'],
|
||||
'date_created' => $_data['date_created'],
|
||||
'date_modified' => $_data['date_modified'],
|
||||
'discount_type' => $_data['discount_type'],
|
||||
'description' => $_data['description'],
|
||||
'amount' => $_data['amount'],
|
||||
'expiry_date' => $_data['date_expires'],
|
||||
'usage_count' => $_data['usage_count'],
|
||||
'individual_use' => $_data['individual_use'],
|
||||
'product_ids' => $_data['product_ids'],
|
||||
'exclude_product_ids' => $_data['excluded_product_ids'],
|
||||
'usage_limit' => $_data['usage_limit'],
|
||||
'usage_limit_per_user' => $_data['usage_limit_per_user'],
|
||||
'limit_usage_to_x_items' => $_data['limit_usage_to_x_items'],
|
||||
'free_shipping' => $_data['free_shipping'],
|
||||
'product_categories' => $_data['product_categories'],
|
||||
'excluded_product_categories' => $_data['excluded_product_categories'],
|
||||
'exclude_sale_items' => $_data['exclude_sale_items'],
|
||||
'minimum_amount' => $_data['minimum_amount'],
|
||||
'maximum_amount' => $_data['maximum_amount'],
|
||||
'email_restrictions' => $_data['email_restrictions'],
|
||||
'used_by' => $_data['used_by'],
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( $this->prepare_links( $post, $request ) );
|
||||
|
||||
/**
|
||||
* Filter the data for a response.
|
||||
*
|
||||
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
|
||||
* prepared for the response.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param WP_Post $post Post object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Only reutrn writeable props from schema.
|
||||
* @param array $schema
|
||||
* @return bool
|
||||
*/
|
||||
protected function filter_writable_props( $schema ) {
|
||||
return empty( $schema['readonly'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single coupon for create or update.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_Error|stdClass $data Post object.
|
||||
*/
|
||||
protected function prepare_item_for_database( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$id = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
|
||||
$coupon = new WC_Coupon( $id );
|
||||
$schema = $this->get_item_schema();
|
||||
$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
|
||||
|
||||
// Update to schema to make compatible with CRUD schema.
|
||||
if ( $request['exclude_product_ids'] ) {
|
||||
$request['excluded_product_ids'] = $request['exclude_product_ids'];
|
||||
}
|
||||
if ( $request['expiry_date'] ) {
|
||||
$request['date_expires'] = $request['expiry_date'];
|
||||
}
|
||||
|
||||
// Validate required POST fields.
|
||||
if ( 'POST' === $request->get_method() && 0 === $coupon->get_id() ) {
|
||||
if ( empty( $request['code'] ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_empty_coupon_code', sprintf( __( 'The coupon code cannot be empty.', 'woocommerce' ), 'code' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Handle all writable props.
|
||||
foreach ( $data_keys as $key ) {
|
||||
$value = $request[ $key ];
|
||||
|
||||
if ( ! is_null( $value ) ) {
|
||||
switch ( $key ) {
|
||||
case 'code' :
|
||||
$coupon_code = apply_filters( 'woocommerce_coupon_code', $value );
|
||||
$id = $coupon->get_id() ? $coupon->get_id() : 0;
|
||||
$id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id );
|
||||
|
||||
if ( $id_from_code ) {
|
||||
return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$coupon->set_code( $coupon_code );
|
||||
break;
|
||||
case 'description' :
|
||||
$coupon->set_description( wp_filter_post_kses( $value ) );
|
||||
break;
|
||||
default :
|
||||
if ( is_callable( array( $coupon, "set_{$key}" ) ) ) {
|
||||
$coupon->{"set_{$key}"}( $value );
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the query_vars used in `get_items` for the constructed query.
|
||||
*
|
||||
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
|
||||
* prepared for insertion.
|
||||
*
|
||||
* @param WC_Coupon $coupon The coupon object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $coupon, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single item.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
/* translators: %s: post type */
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$coupon_id = $this->save_coupon( $request );
|
||||
if ( is_wp_error( $coupon_id ) ) {
|
||||
return $coupon_id;
|
||||
}
|
||||
|
||||
$post = get_post( $coupon_id );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
$this->add_post_meta_fields( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param object $post Inserted object (not a WP_Post object).
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single coupon.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
try {
|
||||
$post_id = (int) $request['id'];
|
||||
|
||||
if ( empty( $post_id ) || get_post_type( $post_id ) !== $this->post_type ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$coupon_id = $this->save_coupon( $request );
|
||||
if ( is_wp_error( $coupon_id ) ) {
|
||||
return $coupon_id;
|
||||
}
|
||||
|
||||
$post = get_post( $coupon_id );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param object $post Inserted object (not a WP_Post object).
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
return rest_ensure_response( $response );
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a coupon to the database.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|int
|
||||
*/
|
||||
protected function save_coupon( $request ) {
|
||||
try {
|
||||
$coupon = $this->prepare_item_for_database( $request );
|
||||
|
||||
if ( is_wp_error( $coupon ) ) {
|
||||
return $coupon;
|
||||
}
|
||||
|
||||
$coupon->save();
|
||||
return $coupon->get_id();
|
||||
} catch ( WC_Data_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
|
||||
} catch ( WC_REST_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Coupon's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => $this->post_type,
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the object.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'code' => array(
|
||||
'description' => __( 'Coupon code.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the coupon was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_modified' => array(
|
||||
'description' => __( "The date the coupon was last modified, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'Coupon description.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'discount_type' => array(
|
||||
'description' => __( 'Determines the type of discount that will be applied.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'fixed_cart',
|
||||
'enum' => array_keys( wc_get_coupon_types() ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'amount' => array(
|
||||
'description' => __( 'The amount of discount.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'expiry_date' => array(
|
||||
'description' => __( 'UTC DateTime when the coupon expires.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'usage_count' => array(
|
||||
'description' => __( 'Number of times the coupon has been used already.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'individual_use' => array(
|
||||
'description' => __( 'Whether coupon can only be used individually.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'product_ids' => array(
|
||||
'description' => __( "List of product ID's the coupon can be used on.", 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'exclude_product_ids' => array(
|
||||
'description' => __( "List of product ID's the coupon cannot be used on.", 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'usage_limit' => array(
|
||||
'description' => __( 'How many times the coupon can be used.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'usage_limit_per_user' => array(
|
||||
'description' => __( 'How many times the coupon can be used per customer.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'limit_usage_to_x_items' => array(
|
||||
'description' => __( 'Max number of items in the cart the coupon can be applied to.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'free_shipping' => array(
|
||||
'description' => __( 'Define if can be applied for free shipping.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'product_categories' => array(
|
||||
'description' => __( "List of category ID's the coupon applies to.", 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'excluded_product_categories' => array(
|
||||
'description' => __( "List of category ID's the coupon does not apply to.", 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'exclude_sale_items' => array(
|
||||
'description' => __( 'Define if should not apply when have sale items.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'minimum_amount' => array(
|
||||
'description' => __( 'Minimum order amount that needs to be in the cart before coupon applies.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'maximum_amount' => array(
|
||||
'description' => __( 'Maximum order amount allowed when using the coupon.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'email_restrictions' => array(
|
||||
'description' => __( 'List of email addresses that can use this coupon.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'used_by' => array(
|
||||
'description' => __( 'List of user IDs who have used the coupon.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections of attachments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['code'] = array(
|
||||
'description' => __( 'Limit result set to resources with a specific code.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,249 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Customer Downloads controller
|
||||
*
|
||||
* Handles requests to the /customers/<customer_id>/downloads endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Customers controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Customer_Downloads_V1_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'customers/(?P<customer_id>[\d]+)/downloads';
|
||||
|
||||
/**
|
||||
* Register the routes for customers.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'customer_id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read customers.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
$customer = get_user_by( 'id', (int) $request['customer_id'] );
|
||||
|
||||
if ( ! $customer ) {
|
||||
return new WP_Error( 'woocommerce_rest_customer_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
if ( ! wc_rest_check_user_permissions( 'read', $customer->get_id() ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all customer downloads.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$downloads = wc_get_customer_available_downloads( (int) $request['customer_id'] );
|
||||
|
||||
$data = array();
|
||||
foreach ( $downloads as $download_data ) {
|
||||
$download = $this->prepare_item_for_response( (object) $download_data, $request );
|
||||
$download = $this->prepare_response_for_collection( $download );
|
||||
$data[] = $download;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single download output for response.
|
||||
*
|
||||
* @param stdObject $download Download object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $download, $request ) {
|
||||
$data = (array) $download;
|
||||
$data['access_expires'] = $data['access_expires'] ? wc_rest_prepare_date_response( $data['access_expires'] ) : 'never';
|
||||
$data['downloads_remaining'] = '' === $data['downloads_remaining'] ? 'unlimited' : $data['downloads_remaining'];
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $download, $request ) );
|
||||
|
||||
/**
|
||||
* Filter customer download data returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdObject $download Download object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_customer_download', $response, $download, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param stdClass $download Download object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return array Links for the given customer download.
|
||||
*/
|
||||
protected function prepare_links( $download, $request ) {
|
||||
$base = str_replace( '(?P<customer_id>[\d]+)', $request['customer_id'], $this->rest_base );
|
||||
$links = array(
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||
),
|
||||
'product' => array(
|
||||
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $download->product_id ) ),
|
||||
),
|
||||
'order' => array(
|
||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $download->order_id ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Customer Download's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'customer_download',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'download_url' => array(
|
||||
'description' => __( 'Download file URL.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'download_id' => array(
|
||||
'description' => __( 'Download ID (MD5).', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'product_id' => array(
|
||||
'description' => __( 'Downloadable product ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'download_name' => array(
|
||||
'description' => __( 'Downloadable file name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'order_id' => array(
|
||||
'description' => __( 'Order ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'order_key' => array(
|
||||
'description' => __( 'Order key.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'downloads_remaining' => array(
|
||||
'description' => __( 'Amount of downloads remaining.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'access_expires' => array(
|
||||
'description' => __( "The date when the download access expires, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'file' => array(
|
||||
'description' => __( 'File details.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
'properties' => array(
|
||||
'name' => array(
|
||||
'description' => __( 'File name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'file' => array(
|
||||
'description' => __( 'File URL.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,922 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Customers controller
|
||||
*
|
||||
* Handles requests to the /customers endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Customers controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Customers_V1_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'customers';
|
||||
|
||||
/**
|
||||
* Register the routes for customers.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'email' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'New user email address.', 'woocommerce' ),
|
||||
),
|
||||
'username' => array(
|
||||
'required' => 'no' === get_option( 'woocommerce_registration_generate_username', 'yes' ),
|
||||
'description' => __( 'New user username.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
'password' => array(
|
||||
'required' => 'no' === get_option( 'woocommerce_registration_generate_password', 'no' ),
|
||||
'description' => __( 'New user password.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
'reassign' => array(
|
||||
'default' => 0,
|
||||
'type' => 'integer',
|
||||
'description' => __( 'ID to reassign posts to.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read customers.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_user_permissions( 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access create customers.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_user_permissions( 'create' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
|
||||
if ( ! wc_rest_check_user_permissions( 'read', $id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access update a customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
|
||||
if ( ! wc_rest_check_user_permissions( 'edit', $id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access delete a customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
|
||||
if ( ! wc_rest_check_user_permissions( 'delete', $id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access batch create, update and delete items.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function batch_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_user_permissions( 'batch' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all customers.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$prepared_args = array();
|
||||
$prepared_args['exclude'] = $request['exclude'];
|
||||
$prepared_args['include'] = $request['include'];
|
||||
$prepared_args['order'] = $request['order'];
|
||||
$prepared_args['number'] = $request['per_page'];
|
||||
if ( ! empty( $request['offset'] ) ) {
|
||||
$prepared_args['offset'] = $request['offset'];
|
||||
} else {
|
||||
$prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number'];
|
||||
}
|
||||
$orderby_possibles = array(
|
||||
'id' => 'ID',
|
||||
'include' => 'include',
|
||||
'name' => 'display_name',
|
||||
'registered_date' => 'registered',
|
||||
);
|
||||
$prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ];
|
||||
$prepared_args['search'] = $request['search'];
|
||||
|
||||
if ( '' !== $prepared_args['search'] ) {
|
||||
$prepared_args['search'] = '*' . $prepared_args['search'] . '*';
|
||||
}
|
||||
|
||||
// Filter by email.
|
||||
if ( ! empty( $request['email'] ) ) {
|
||||
$prepared_args['search'] = $request['email'];
|
||||
$prepared_args['search_columns'] = array( 'user_email' );
|
||||
}
|
||||
|
||||
// Filter by role.
|
||||
if ( 'all' !== $request['role'] ) {
|
||||
$prepared_args['role'] = $request['role'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter arguments, before passing to WP_User_Query, when querying users via the REST API.
|
||||
*
|
||||
* @see https://developer.wordpress.org/reference/classes/wp_user_query/
|
||||
*
|
||||
* @param array $prepared_args Array of arguments for WP_User_Query.
|
||||
* @param WP_REST_Request $request The current request.
|
||||
*/
|
||||
$prepared_args = apply_filters( 'woocommerce_rest_customer_query', $prepared_args, $request );
|
||||
|
||||
$query = new WP_User_Query( $prepared_args );
|
||||
|
||||
$users = array();
|
||||
foreach ( $query->results as $user ) {
|
||||
$data = $this->prepare_item_for_response( $user, $request );
|
||||
$users[] = $this->prepare_response_for_collection( $data );
|
||||
}
|
||||
|
||||
$response = rest_ensure_response( $users );
|
||||
|
||||
// Store pagation values for headers then unset for count query.
|
||||
$per_page = (int) $prepared_args['number'];
|
||||
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
|
||||
|
||||
$prepared_args['fields'] = 'ID';
|
||||
|
||||
$total_users = $query->get_total();
|
||||
if ( $total_users < 1 ) {
|
||||
// Out-of-bounds, run the query again without LIMIT for total count.
|
||||
unset( $prepared_args['number'] );
|
||||
unset( $prepared_args['offset'] );
|
||||
$count_query = new WP_User_Query( $prepared_args );
|
||||
$total_users = $count_query->get_total();
|
||||
}
|
||||
$response->header( 'X-WP-Total', (int) $total_users );
|
||||
$max_pages = ceil( $total_users / $per_page );
|
||||
$response->header( 'X-WP-TotalPages', (int) $max_pages );
|
||||
|
||||
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
|
||||
if ( $page > 1 ) {
|
||||
$prev_page = $page - 1;
|
||||
if ( $prev_page > $max_pages ) {
|
||||
$prev_page = $max_pages;
|
||||
}
|
||||
$prev_link = add_query_arg( 'page', $prev_page, $base );
|
||||
$response->link_header( 'prev', $prev_link );
|
||||
}
|
||||
if ( $max_pages > $page ) {
|
||||
$next_page = $page + 1;
|
||||
$next_link = add_query_arg( 'page', $next_page, $base );
|
||||
$response->link_header( 'next', $next_link );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
try {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_customer_exists', __( 'Cannot create existing resource.', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
// Sets the username.
|
||||
$request['username'] = ! empty( $request['username'] ) ? $request['username'] : '';
|
||||
|
||||
// Sets the password.
|
||||
$request['password'] = ! empty( $request['password'] ) ? $request['password'] : '';
|
||||
|
||||
// Create customer.
|
||||
$customer = new WC_Customer;
|
||||
$customer->set_username( $request['username'] );
|
||||
$customer->set_password( $request['password'] );
|
||||
$customer->set_email( $request['email'] );
|
||||
$customer->save();
|
||||
|
||||
if ( ! $customer->get_id() ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_cannot_create', __( 'This resource cannot be created.', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
$this->update_customer_meta_fields( $customer, $request );
|
||||
$customer->save();
|
||||
|
||||
$user_data = get_userdata( $customer->get_id() );
|
||||
$this->update_additional_fields_for_object( $user_data, $request );
|
||||
|
||||
/**
|
||||
* Fires after a customer is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_User $user_data Data used to create the customer.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating customer, false when updating customer.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_customer', $user_data, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $user_data, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $customer->get_id() ) ) );
|
||||
|
||||
return $response;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$user_data = get_userdata( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $user_data->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$customer = $this->prepare_item_for_response( $user_data, $request );
|
||||
$response = rest_ensure_response( $customer );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single user.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
try {
|
||||
$id = (int) $request['id'];
|
||||
$customer = new WC_Customer( $id );
|
||||
|
||||
if ( ! $customer->get_id() ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
if ( ! empty( $request['email'] ) && email_exists( $request['email'] ) && $request['email'] !== $customer->get_email() ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_customer_invalid_email', __( 'Email address is invalid.', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
if ( ! empty( $request['username'] ) && $request['username'] !== $customer->get_username() ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_customer_invalid_argument', __( "Username isn't editable.", 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
// Customer email.
|
||||
if ( isset( $request['email'] ) ) {
|
||||
$customer->set_email( sanitize_email( $request['email'] ) );
|
||||
}
|
||||
|
||||
// Customer password.
|
||||
if ( isset( $request['password'] ) ) {
|
||||
$customer->set_password( wc_clean( $request['password'] ) );
|
||||
}
|
||||
|
||||
$this->update_customer_meta_fields( $customer, $request );
|
||||
$customer->save();
|
||||
|
||||
$user_data = get_userdata( $customer->get_id() );
|
||||
$this->update_additional_fields_for_object( $user_data, $request );
|
||||
|
||||
if ( ! is_user_member_of_blog( $user_data->ID ) ) {
|
||||
$user_data->add_role( 'customer' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a customer is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_User $customer Data used to create the customer.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating customer, false when updating customer.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_customer', $user_data, $request, false );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $user_data, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
return $response;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single customer.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$reassign = isset( $request['reassign'] ) ? absint( $request['reassign'] ) : null;
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Customers do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$user_data = get_userdata( $id );
|
||||
if ( ! $user_data ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
if ( ! empty( $reassign ) ) {
|
||||
if ( $reassign === $id || ! get_userdata( $reassign ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_customer_invalid_reassign', __( 'Invalid resource id for reassignment.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $user_data, $request );
|
||||
|
||||
/** Include admin customer functions to get access to wp_delete_user() */
|
||||
require_once ABSPATH . 'wp-admin/includes/user.php';
|
||||
|
||||
$customer = new WC_Customer( $id );
|
||||
|
||||
if ( ! is_null( $reassign ) ) {
|
||||
$result = $customer->delete_and_reassign( $reassign );
|
||||
} else {
|
||||
$result = $customer->delete();
|
||||
}
|
||||
|
||||
if ( ! $result ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a customer is deleted via the REST API.
|
||||
*
|
||||
* @param WP_User $user_data User data.
|
||||
* @param WP_REST_Response $response The response returned from the API.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_delete_customer', $user_data, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single customer output for response.
|
||||
*
|
||||
* @param WP_User $user_data User object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $user_data, $request ) {
|
||||
$customer = new WC_Customer( $user_data->ID );
|
||||
$_data = $customer->get_data();
|
||||
$last_order = wc_get_customer_last_order( $customer->get_id() );
|
||||
$format_date = array( 'date_created', 'date_modified' );
|
||||
|
||||
// Format date values.
|
||||
foreach ( $format_date as $key ) {
|
||||
$_data[ $key ] = $_data[ $key ] ? wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $_data[ $key ] ) ) ) : null;
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'id' => $_data['id'],
|
||||
'date_created' => $_data['date_created'],
|
||||
'date_modified' => $_data['date_modified'],
|
||||
'email' => $_data['email'],
|
||||
'first_name' => $_data['first_name'],
|
||||
'last_name' => $_data['last_name'],
|
||||
'username' => $_data['username'],
|
||||
'last_order' => array(
|
||||
'id' => is_object( $last_order ) ? $last_order->get_id() : null,
|
||||
'date' => is_object( $last_order ) ? wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $last_order->get_date_created() ) ) ) : null
|
||||
),
|
||||
'orders_count' => $customer->get_order_count(),
|
||||
'total_spent' => $customer->get_total_spent(),
|
||||
'avatar_url' => $customer->get_avatar_url(),
|
||||
'billing' => $_data['billing'],
|
||||
'shipping' => $_data['shipping'],
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( $this->prepare_links( $user_data ) );
|
||||
|
||||
/**
|
||||
* Filter customer data returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param WP_User $user_data User object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_customer', $response, $user_data, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update customer meta fields.
|
||||
*
|
||||
* @param WC_Customer $customer
|
||||
* @param WP_REST_Request $request
|
||||
*/
|
||||
protected function update_customer_meta_fields( $customer, $request ) {
|
||||
$schema = $this->get_item_schema();
|
||||
|
||||
// Customer first name.
|
||||
if ( isset( $request['first_name'] ) ) {
|
||||
$customer->set_first_name( wc_clean( $request['first_name'] ) );
|
||||
}
|
||||
|
||||
// Customer last name.
|
||||
if ( isset( $request['last_name'] ) ) {
|
||||
$customer->set_last_name( wc_clean( $request['last_name'] ) );
|
||||
}
|
||||
|
||||
// Customer billing address.
|
||||
if ( isset( $request['billing'] ) ) {
|
||||
foreach ( array_keys( $schema['properties']['billing']['properties'] ) as $field ) {
|
||||
if ( isset( $request['billing'][ $field ] ) && is_callable( array( $customer, "set_billing_{$field}" ) ) ) {
|
||||
$customer->{"set_billing_{$field}"}( $request['billing'][ $field ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Customer shipping address.
|
||||
if ( isset( $request['shipping'] ) ) {
|
||||
foreach ( array_keys( $schema['properties']['shipping']['properties'] ) as $field ) {
|
||||
if ( isset( $request['shipping'][ $field ] ) && is_callable( array( $customer, "set_shipping_{$field}" ) ) ) {
|
||||
$customer->{"set_shipping_{$field}"}( $request['shipping'][ $field ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param WP_User $customer Customer object.
|
||||
* @return array Links for the given customer.
|
||||
*/
|
||||
protected function prepare_links( $customer ) {
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $customer->ID ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Customer's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'customer',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the customer was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_modified' => array(
|
||||
'description' => __( "The date the customer was last modified, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'email' => array(
|
||||
'description' => __( 'The email address for the customer.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'email',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'first_name' => array(
|
||||
'description' => __( 'Customer first name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'last_name' => array(
|
||||
'description' => __( 'Customer last name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'username' => array(
|
||||
'description' => __( 'Customer login name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_user',
|
||||
),
|
||||
),
|
||||
'password' => array(
|
||||
'description' => __( 'Customer password.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'edit' ),
|
||||
),
|
||||
'last_order' => array(
|
||||
'description' => __( 'Last order data.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Last order ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date' => array(
|
||||
'description' => __( 'UTC DateTime of the customer last order.', 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
'orders_count' => array(
|
||||
'description' => __( 'Quantity of orders made by the customer.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_spent' => array(
|
||||
'description' => __( 'Total amount spent.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'avatar_url' => array(
|
||||
'description' => __( 'Avatar URL.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'billing' => array(
|
||||
'description' => __( 'List of billing address data.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'properties' => array(
|
||||
'first_name' => array(
|
||||
'description' => __( 'First name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'last_name' => array(
|
||||
'description' => __( 'Last name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'company' => array(
|
||||
'description' => __( 'Company name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'address_1' => array(
|
||||
'description' => __( 'Address line 1.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'address_2' => array(
|
||||
'description' => __( 'Address line 2.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'city' => array(
|
||||
'description' => __( 'City name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'state' => array(
|
||||
'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'postcode' => array(
|
||||
'description' => __( 'Postal code.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'country' => array(
|
||||
'description' => __( 'ISO code of the country.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'email' => array(
|
||||
'description' => __( 'Email address.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'email',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'phone' => array(
|
||||
'description' => __( 'Phone number.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'shipping' => array(
|
||||
'description' => __( 'List of shipping address data.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'properties' => array(
|
||||
'first_name' => array(
|
||||
'description' => __( 'First name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'last_name' => array(
|
||||
'description' => __( 'Last name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'company' => array(
|
||||
'description' => __( 'Company name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'address_1' => array(
|
||||
'description' => __( 'Address line 1.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'address_2' => array(
|
||||
'description' => __( 'Address line 2.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'city' => array(
|
||||
'description' => __( 'City name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'state' => array(
|
||||
'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'postcode' => array(
|
||||
'description' => __( 'Postal code.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'country' => array(
|
||||
'description' => __( 'ISO code of the country.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get role names.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_role_names() {
|
||||
global $wp_roles;
|
||||
|
||||
return array_keys( $wp_roles->role_names );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['context']['default'] = 'view';
|
||||
|
||||
$params['exclude'] = array(
|
||||
'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
$params['include'] = array(
|
||||
'description' => __( 'Limit result set to specific IDs.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
$params['offset'] = array(
|
||||
'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['order'] = array(
|
||||
'default' => 'asc',
|
||||
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
|
||||
'enum' => array( 'asc', 'desc' ),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['orderby'] = array(
|
||||
'default' => 'name',
|
||||
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
|
||||
'enum' => array(
|
||||
'id',
|
||||
'include',
|
||||
'name',
|
||||
'registered_date',
|
||||
),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['email'] = array(
|
||||
'description' => __( 'Limit result set to resources with a specific email.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'email',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['role'] = array(
|
||||
'description' => __( 'Limit result set to resources with a specific role.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'customer',
|
||||
'enum' => array_merge( array( 'all' ), $this->get_role_names() ),
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
return $params;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,436 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Order Notes controller
|
||||
*
|
||||
* Handles requests to the /orders/<order_id>/notes endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Order Notes controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Order_Notes_V1_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'orders/(?P<order_id>[\d]+)/notes';
|
||||
|
||||
/**
|
||||
* Post type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $post_type = 'shop_order';
|
||||
|
||||
/**
|
||||
* Register the routes for order notes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'order_id' => array(
|
||||
'description' => __( 'The order ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'note' => array(
|
||||
'type' => 'string',
|
||||
'description' => __( 'Order note content.', 'woocommerce' ),
|
||||
'required' => true,
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'order_id' => array(
|
||||
'description' => __( 'The order ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read order notes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_post_permissions( $this->post_type, 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access create order notes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_post_permissions( $this->post_type, 'create' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a order note.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
$order = wc_get_order( (int) $request['order_id'] );
|
||||
|
||||
if ( $order && ! wc_rest_check_post_permissions( $this->post_type, 'read', $order->get_id() ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access delete a order note.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
$order = wc_get_order( (int) $request['order_id'] );
|
||||
|
||||
if ( $order && ! wc_rest_check_post_permissions( $this->post_type, 'delete', $order->get_id() ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get order notes from an order.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$order = wc_get_order( (int) $request['order_id'] );
|
||||
|
||||
if ( ! $order || $this->post_type !== $order->get_type() ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'post_id' => $order->get_id(),
|
||||
'approve' => 'approve',
|
||||
'type' => 'order_note',
|
||||
);
|
||||
|
||||
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
|
||||
|
||||
$notes = get_comments( $args );
|
||||
|
||||
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
|
||||
|
||||
$data = array();
|
||||
foreach ( $notes as $note ) {
|
||||
$order_note = $this->prepare_item_for_response( $note, $request );
|
||||
$order_note = $this->prepare_response_for_collection( $order_note );
|
||||
$data[] = $order_note;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single order note.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
/* translators: %s: post type */
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$order = wc_get_order( (int) $request['order_id'] );
|
||||
|
||||
if ( ! $order || $this->post_type !== $order->get_type() ) {
|
||||
return new WP_Error( 'woocommerce_rest_order_invalid_id', __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
// Create the note.
|
||||
$note_id = $order->add_order_note( $request['note'], $request['customer_note'] );
|
||||
|
||||
if ( ! $note_id ) {
|
||||
return new WP_Error( 'woocommerce_api_cannot_create_order_note', __( 'Cannot create order note, please try again.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
$note = get_comment( $note_id );
|
||||
$this->update_additional_fields_for_object( $note, $request );
|
||||
|
||||
/**
|
||||
* Fires after a order note is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Comment $note New order note object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_order_note', $note, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $note, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, str_replace( '(?P<order_id>[\d]+)', $order->get_id(), $this->rest_base ), $note_id ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single order note.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$order = wc_get_order( (int) $request['order_id'] );
|
||||
|
||||
if ( ! $order || $this->post_type !== $order->get_type() ) {
|
||||
return new WP_Error( 'woocommerce_rest_order_invalid_id', __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$note = get_comment( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $note ) || intval( $note->comment_post_ID ) !== intval( $order->get_id() ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$order_note = $this->prepare_item_for_response( $note, $request );
|
||||
$response = rest_ensure_response( $order_note );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single order note.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response|WP_Error
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Webhooks do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$order = wc_get_order( (int) $request['order_id'] );
|
||||
|
||||
if ( ! $order || $this->post_type !== $order->get_type() ) {
|
||||
return new WP_Error( 'woocommerce_rest_order_invalid_id', __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$note = get_comment( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $note ) || intval( $note->comment_post_ID ) !== intval( $order->get_id() ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $note, $request );
|
||||
|
||||
$result = wp_delete_comment( $note->comment_ID, true );
|
||||
|
||||
if ( ! $result ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), 'order_note' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a order note is deleted or trashed via the REST API.
|
||||
*
|
||||
* @param WP_Comment $note The deleted or trashed order note.
|
||||
* @param WP_REST_Response $response The response data.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_delete_order_note', $note, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single order note output for response.
|
||||
*
|
||||
* @param WP_Comment $note Order note object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $note, $request ) {
|
||||
$data = array(
|
||||
'id' => (int) $note->comment_ID,
|
||||
'date_created' => wc_rest_prepare_date_response( $note->comment_date_gmt ),
|
||||
'note' => $note->comment_content,
|
||||
'customer_note' => (bool) get_comment_meta( $note->comment_ID, 'is_customer_note', true ),
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $note ) );
|
||||
|
||||
/**
|
||||
* Filter order note object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param WP_Comment $note Order note object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_order_note', $response, $note, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param WP_Comment $note Delivery order_note object.
|
||||
* @return array Links for the given order note.
|
||||
*/
|
||||
protected function prepare_links( $note ) {
|
||||
$order_id = (int) $note->comment_post_ID;
|
||||
$base = str_replace( '(?P<order_id>[\d]+)', $order_id, $this->rest_base );
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $note->comment_ID ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||
),
|
||||
'up' => array(
|
||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $order_id ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Order Notes schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'order_note',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the order note was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'note' => array(
|
||||
'description' => __( 'Order note.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'customer_note' => array(
|
||||
'description' => __( 'Shows/define if the note is only for reference or for the customer (the user will be notified).', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,524 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Order Refunds controller
|
||||
*
|
||||
* Handles requests to the /orders/<order_id>/refunds endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.6.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Order Refunds controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Posts_Controller
|
||||
*/
|
||||
class WC_REST_Order_Refunds_V1_Controller extends WC_REST_Orders_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'orders/(?P<order_id>[\d]+)/refunds';
|
||||
|
||||
/**
|
||||
* Post type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $post_type = 'shop_order_refund';
|
||||
|
||||
/**
|
||||
* Order refunds actions.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter( "woocommerce_rest_{$this->post_type}_trashable", '__return_false' );
|
||||
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the routes for order refunds.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'order_id' => array(
|
||||
'description' => __( 'The order ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'order_id' => array(
|
||||
'description' => __( 'The order ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => true,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single order refund output for response.
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $data
|
||||
*/
|
||||
public function prepare_item_for_response( $post, $request ) {
|
||||
$order = wc_get_order( (int) $request['order_id'] );
|
||||
|
||||
if ( ! $order ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 );
|
||||
}
|
||||
|
||||
$refund = wc_get_order( $post );
|
||||
|
||||
if ( ! $refund || $refund->get_parent_id() !== $order->get_id() ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 );
|
||||
}
|
||||
|
||||
$dp = $request['dp'];
|
||||
|
||||
$data = array(
|
||||
'id' => $refund->get_id(),
|
||||
'date_created' => wc_rest_prepare_date_response( $refund->get_date_created() ),
|
||||
'amount' => wc_format_decimal( $refund->get_amount(), $dp ),
|
||||
'reason' => $refund->get_reason(),
|
||||
'line_items' => array(),
|
||||
);
|
||||
|
||||
// Add line items.
|
||||
foreach ( $refund->get_items() as $item_id => $item ) {
|
||||
$product = $refund->get_product_from_item( $item );
|
||||
$product_id = 0;
|
||||
$variation_id = 0;
|
||||
$product_sku = null;
|
||||
|
||||
// Check if the product exists.
|
||||
if ( is_object( $product ) ) {
|
||||
$product_id = $item->get_product_id();
|
||||
$variation_id = $item->get_variation_id();
|
||||
$product_sku = $product->get_sku();
|
||||
}
|
||||
|
||||
$meta = new WC_Order_Item_Meta( $item, $product );
|
||||
|
||||
$item_meta = array();
|
||||
|
||||
$hideprefix = 'true' === $request['all_item_meta'] ? null : '_';
|
||||
|
||||
foreach ( $meta->get_formatted( $hideprefix ) as $meta_key => $formatted_meta ) {
|
||||
$item_meta[] = array(
|
||||
'key' => $formatted_meta['key'],
|
||||
'label' => $formatted_meta['label'],
|
||||
'value' => $formatted_meta['value'],
|
||||
);
|
||||
}
|
||||
|
||||
$line_item = array(
|
||||
'id' => $item_id,
|
||||
'name' => $item['name'],
|
||||
'sku' => $product_sku,
|
||||
'product_id' => (int) $product_id,
|
||||
'variation_id' => (int) $variation_id,
|
||||
'quantity' => wc_stock_amount( $item['qty'] ),
|
||||
'tax_class' => ! empty( $item['tax_class'] ) ? $item['tax_class'] : '',
|
||||
'price' => wc_format_decimal( $refund->get_item_total( $item, false, false ), $dp ),
|
||||
'subtotal' => wc_format_decimal( $refund->get_line_subtotal( $item, false, false ), $dp ),
|
||||
'subtotal_tax' => wc_format_decimal( $item['line_subtotal_tax'], $dp ),
|
||||
'total' => wc_format_decimal( $refund->get_line_total( $item, false, false ), $dp ),
|
||||
'total_tax' => wc_format_decimal( $item['line_tax'], $dp ),
|
||||
'taxes' => array(),
|
||||
'meta' => $item_meta,
|
||||
);
|
||||
|
||||
$item_line_taxes = maybe_unserialize( $item['line_tax_data'] );
|
||||
if ( isset( $item_line_taxes['total'] ) ) {
|
||||
$line_tax = array();
|
||||
|
||||
foreach ( $item_line_taxes['total'] as $tax_rate_id => $tax ) {
|
||||
$line_tax[ $tax_rate_id ] = array(
|
||||
'id' => $tax_rate_id,
|
||||
'total' => $tax,
|
||||
'subtotal' => '',
|
||||
);
|
||||
}
|
||||
|
||||
foreach ( $item_line_taxes['subtotal'] as $tax_rate_id => $tax ) {
|
||||
$line_tax[ $tax_rate_id ]['subtotal'] = $tax;
|
||||
}
|
||||
|
||||
$line_item['taxes'] = array_values( $line_tax );
|
||||
}
|
||||
|
||||
$data['line_items'][] = $line_item;
|
||||
}
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $refund, $request ) );
|
||||
|
||||
/**
|
||||
* Filter the data for a response.
|
||||
*
|
||||
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
|
||||
* prepared for the response.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param WP_Post $post Post object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param WC_Order_Refund $refund Comment object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return array Links for the given order refund.
|
||||
*/
|
||||
protected function prepare_links( $refund, $request ) {
|
||||
$order_id = $refund->get_parent_id();
|
||||
$base = str_replace( '(?P<order_id>[\d]+)', $order_id, $this->rest_base );
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $refund->get_id() ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||
),
|
||||
'up' => array(
|
||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $order_id ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query args.
|
||||
*
|
||||
* @param array $args Request args.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return array
|
||||
*/
|
||||
public function query_args( $args, $request ) {
|
||||
$args['post_status'] = array_keys( wc_get_order_statuses() );
|
||||
$args['post_parent__in'] = array( absint( $request['order_id'] ) );
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single item.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
/* translators: %s: post type */
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$order_data = get_post( (int) $request['order_id'] );
|
||||
|
||||
if ( empty( $order_data ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_order', __( 'Order is invalid', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
if ( 0 > $request['amount'] ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_order_refund', __( 'Refund amount must be greater than zero.', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
// Create the refund.
|
||||
$refund = wc_create_refund( array(
|
||||
'order_id' => $order_data->ID,
|
||||
'amount' => $request['amount'],
|
||||
'reason' => empty( $request['reason'] ) ? null : $request['reason'],
|
||||
'line_items' => $request['line_items'],
|
||||
'refund_payment' => is_bool( $request['api_refund'] ) ? $request['api_refund'] : true,
|
||||
'restock_items' => true,
|
||||
) );
|
||||
|
||||
if ( is_wp_error( $refund ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500 );
|
||||
}
|
||||
|
||||
if ( ! $refund ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', __( 'Cannot create order refund, please try again.', 'woocommerce' ), 500 );
|
||||
}
|
||||
|
||||
$post = get_post( $refund->get_id() );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param object $post Inserted object (not a WP_Post object).
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Order's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => $this->post_type,
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the order refund was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'amount' => array(
|
||||
'description' => __( 'Refund amount.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'reason' => array(
|
||||
'description' => __( 'Reason for refund.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'line_items' => array(
|
||||
'description' => __( 'Line items data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'items' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Item ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Product name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'sku' => array(
|
||||
'description' => __( 'Product SKU.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'product_id' => array(
|
||||
'description' => __( 'Product ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'variation_id' => array(
|
||||
'description' => __( 'Variation ID, if applicable.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'quantity' => array(
|
||||
'description' => __( 'Quantity ordered.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'tax_class' => array(
|
||||
'description' => __( 'Tax class of product.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'price' => array(
|
||||
'description' => __( 'Product price.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'subtotal' => array(
|
||||
'description' => __( 'Line subtotal (before discounts).', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'subtotal_tax' => array(
|
||||
'description' => __( 'Line subtotal tax (before discounts).', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'total' => array(
|
||||
'description' => __( 'Line total (after discounts).', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'total_tax' => array(
|
||||
'description' => __( 'Line total tax (after discounts).', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'taxes' => array(
|
||||
'description' => __( 'Line taxes.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
'items' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Tax rate ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total' => array(
|
||||
'description' => __( 'Tax total.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'subtotal' => array(
|
||||
'description' => __( 'Tax subtotal.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'meta' => array(
|
||||
'description' => __( 'Line item meta data.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
'items' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'key' => array(
|
||||
'description' => __( 'Meta key.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'label' => array(
|
||||
'description' => __( 'Meta label.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'value' => array(
|
||||
'description' => __( 'Meta value.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['dp'] = array(
|
||||
'default' => 2,
|
||||
'description' => __( 'Number of decimal points to use in each resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,240 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Product Attribute Terms controller
|
||||
*
|
||||
* Handles requests to the products/attributes/<attribute_id>/terms endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Product Attribute Terms controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Terms_Controller
|
||||
*/
|
||||
class WC_REST_Product_Attribute_Terms_V1_Controller extends WC_REST_Terms_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/attributes/(?P<attribute_id>[\d]+)/terms';
|
||||
|
||||
/**
|
||||
* Register the routes for terms.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'attribute_id' => array(
|
||||
'description' => __( 'Unique identifier for the attribute of the terms.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'name' => array(
|
||||
'type' => 'string',
|
||||
'description' => __( 'Name for the resource.', 'woocommerce' ),
|
||||
'required' => true,
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
));
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'attribute_id' => array(
|
||||
'description' => __( 'Unique identifier for the attribute of the terms.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
'args' => array(
|
||||
'attribute_id' => array(
|
||||
'description' => __( 'Unique identifier for the attribute of the terms.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single product attribute term output for response.
|
||||
*
|
||||
* @param WP_Term $item Term object.
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_REST_Response $response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
// Get term order.
|
||||
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order_' . $this->taxonomy );
|
||||
|
||||
$data = array(
|
||||
'id' => (int) $item->term_id,
|
||||
'name' => $item->name,
|
||||
'slug' => $item->slug,
|
||||
'description' => $item->description,
|
||||
'menu_order' => (int) $menu_order,
|
||||
'count' => (int) $item->count,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||
|
||||
/**
|
||||
* Filter a term item returned from the API.
|
||||
*
|
||||
* Allows modification of the term data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $item The original term object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update term meta fields.
|
||||
*
|
||||
* @param WP_Term $term
|
||||
* @param WP_REST_Request $request
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
protected function update_term_meta_fields( $term, $request ) {
|
||||
$id = (int) $term->term_id;
|
||||
|
||||
update_woocommerce_term_meta( $id, 'order_' . $this->taxonomy, $request['menu_order'] );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Attribute Term's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'product_attribute_term',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Term name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
),
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'HTML description of the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'wp_filter_post_kses',
|
||||
),
|
||||
),
|
||||
'menu_order' => array(
|
||||
'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'count' => array(
|
||||
'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,664 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Product Attributes controller
|
||||
*
|
||||
* Handles requests to the products/attributes endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Product Attributes controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Product_Attributes_V1_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/attributes';
|
||||
|
||||
/**
|
||||
* Attribute name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $attribute = '';
|
||||
|
||||
/**
|
||||
* Register the routes for product attributes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'name' => array(
|
||||
'description' => __( 'Name for the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
));
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => true,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read the attributes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to create a attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'create' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you cannot create new resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
if ( ! $this->get_taxonomy( $request ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to update a attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
if ( ! $this->get_taxonomy( $request ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'edit' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot update resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to delete a attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if ( ! $this->get_taxonomy( $request ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'delete' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access batch create, update and delete items.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function batch_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'attributes', 'batch' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all attributes.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$attributes = wc_get_attribute_taxonomies();
|
||||
$data = array();
|
||||
foreach ( $attributes as $attribute_obj ) {
|
||||
$attribute = $this->prepare_item_for_response( $attribute_obj, $request );
|
||||
$attribute = $this->prepare_response_for_collection( $attribute );
|
||||
$data[] = $attribute;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Request|WP_Error
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$args = array(
|
||||
'attribute_label' => $request['name'],
|
||||
'attribute_name' => wc_sanitize_taxonomy_name( stripslashes( $request['slug'] ) ),
|
||||
'attribute_type' => ! empty( $request['type'] ) ? $request['type'] : 'select',
|
||||
'attribute_orderby' => ! empty( $request['order_by'] ) ? $request['order_by'] : 'menu_order',
|
||||
'attribute_public' => true === $request['has_archives'],
|
||||
);
|
||||
|
||||
// Set the attribute slug.
|
||||
if ( empty( $args['attribute_name'] ) ) {
|
||||
$args['attribute_name'] = wc_sanitize_taxonomy_name( stripslashes( $args['attribute_label'] ) );
|
||||
} else {
|
||||
$args['attribute_name'] = preg_replace( '/^pa\_/', '', wc_sanitize_taxonomy_name( stripslashes( $args['attribute_name'] ) ) );
|
||||
}
|
||||
|
||||
$valid_slug = $this->validate_attribute_slug( $args['attribute_name'], true );
|
||||
if ( is_wp_error( $valid_slug ) ) {
|
||||
return $valid_slug;
|
||||
}
|
||||
|
||||
$insert = $wpdb->insert(
|
||||
$wpdb->prefix . 'woocommerce_attribute_taxonomies',
|
||||
$args,
|
||||
array( '%s', '%s', '%s', '%s', '%d' )
|
||||
);
|
||||
|
||||
// Checks for errors.
|
||||
if ( is_wp_error( $insert ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', $insert->get_error_message(), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$attribute = $this->get_attribute( $wpdb->insert_id );
|
||||
|
||||
if ( is_wp_error( $attribute ) ) {
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
$this->update_additional_fields_for_object( $attribute, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single product attribute is created or updated via the REST API.
|
||||
*
|
||||
* @param stdObject $attribute Inserted attribute object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating attribute, false when updating.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_product_attribute', $attribute, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $attribute, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( '/' . $this->namespace . '/' . $this->rest_base . '/' . $attribute->attribute_id ) );
|
||||
|
||||
// Clear transients.
|
||||
$this->flush_rewrite_rules();
|
||||
delete_transient( 'wc_attribute_taxonomies' );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Request|WP_Error
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$attribute = $this->get_attribute( (int) $request['id'] );
|
||||
|
||||
if ( is_wp_error( $attribute ) ) {
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
$response = $this->prepare_item_for_response( $attribute, $request );
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single term from a taxonomy.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Request|WP_Error
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$id = (int) $request['id'];
|
||||
$format = array( '%s', '%s', '%s', '%s', '%d' );
|
||||
$args = array(
|
||||
'attribute_label' => $request['name'],
|
||||
'attribute_name' => wc_sanitize_taxonomy_name( stripslashes( $request['slug'] ) ),
|
||||
'attribute_type' => $request['type'],
|
||||
'attribute_orderby' => $request['order_by'],
|
||||
'attribute_public' => $request['has_archives'],
|
||||
);
|
||||
|
||||
$i = 0;
|
||||
foreach ( $args as $key => $value ) {
|
||||
if ( empty( $value ) && ! is_bool( $value ) ) {
|
||||
unset( $args[ $key ] );
|
||||
unset( $format[ $i ] );
|
||||
}
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
// Set the attribute slug.
|
||||
if ( ! empty( $args['attribute_name'] ) ) {
|
||||
$args['attribute_name'] = preg_replace( '/^pa\_/', '', wc_sanitize_taxonomy_name( stripslashes( $args['attribute_name'] ) ) );
|
||||
|
||||
$valid_slug = $this->validate_attribute_slug( $args['attribute_name'], false );
|
||||
if ( is_wp_error( $valid_slug ) ) {
|
||||
return $valid_slug;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $args ) {
|
||||
$update = $wpdb->update(
|
||||
$wpdb->prefix . 'woocommerce_attribute_taxonomies',
|
||||
$args,
|
||||
array( 'attribute_id' => $id ),
|
||||
$format,
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
// Checks for errors.
|
||||
if ( false === $update ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Could not edit the attribute.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
$attribute = $this->get_attribute( $id );
|
||||
|
||||
if ( is_wp_error( $attribute ) ) {
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
$this->update_additional_fields_for_object( $attribute, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single product attribute is created or updated via the REST API.
|
||||
*
|
||||
* @param stdObject $attribute Inserted attribute object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating attribute, false when updating.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_product_attribute', $attribute, $request, false );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $attribute, $request );
|
||||
|
||||
// Clear transients.
|
||||
$this->flush_rewrite_rules();
|
||||
delete_transient( 'wc_attribute_taxonomies' );
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single attribute.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response|WP_Error
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Resource does not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$attribute = $this->get_attribute( (int) $request['id'] );
|
||||
|
||||
if ( is_wp_error( $attribute ) ) {
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $attribute, $request );
|
||||
|
||||
$deleted = $wpdb->delete(
|
||||
$wpdb->prefix . 'woocommerce_attribute_taxonomies',
|
||||
array( 'attribute_id' => $attribute->attribute_id ),
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
if ( false === $deleted ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
$taxonomy = wc_attribute_taxonomy_name( $attribute->attribute_name );
|
||||
|
||||
if ( taxonomy_exists( $taxonomy ) ) {
|
||||
$terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' );
|
||||
foreach ( $terms as $term ) {
|
||||
wp_delete_term( $term->term_id, $taxonomy );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a single attribute is deleted via the REST API.
|
||||
*
|
||||
* @param stdObject $attribute The deleted attribute.
|
||||
* @param WP_REST_Response $response The response data.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_delete_product_attribute', $attribute, $response, $request );
|
||||
|
||||
// Fires woocommerce_attribute_deleted hook.
|
||||
do_action( 'woocommerce_attribute_deleted', $attribute->attribute_id, $attribute->attribute_name, $taxonomy );
|
||||
|
||||
// Clear transients.
|
||||
$this->flush_rewrite_rules();
|
||||
delete_transient( 'wc_attribute_taxonomies' );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single product attribute output for response.
|
||||
*
|
||||
* @param obj $item Term object.
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_REST_Response $response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
$data = array(
|
||||
'id' => (int) $item->attribute_id,
|
||||
'name' => $item->attribute_label,
|
||||
'slug' => wc_attribute_taxonomy_name( $item->attribute_name ),
|
||||
'type' => $item->attribute_type,
|
||||
'order_by' => $item->attribute_orderby,
|
||||
'has_archives' => (bool) $item->attribute_public,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $item ) );
|
||||
|
||||
/**
|
||||
* Filter a attribute item returned from the API.
|
||||
*
|
||||
* Allows modification of the product attribute data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $item The original attribute object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_product_attribute', $response, $item, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param object $attribute Attribute object.
|
||||
* @return array Links for the given attribute.
|
||||
*/
|
||||
protected function prepare_links( $attribute ) {
|
||||
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( trailingslashit( $base ) . $attribute->attribute_id ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( $base ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Attribute's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'product_attribute',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Attribute name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
),
|
||||
),
|
||||
'type' => array(
|
||||
'description' => __( 'Type of attribute.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'select',
|
||||
'enum' => array_keys( wc_get_attribute_types() ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'order_by' => array(
|
||||
'description' => __( 'Default sort order.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'menu_order',
|
||||
'enum' => array( 'menu_order', 'name', 'name_num', 'id' ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'has_archives' => array(
|
||||
'description' => __( 'Enable/Disable attribute archives.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = array();
|
||||
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute name.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return string
|
||||
*/
|
||||
protected function get_taxonomy( $request ) {
|
||||
if ( '' !== $this->attribute ) {
|
||||
return $this->attribute;
|
||||
}
|
||||
|
||||
if ( $request['id'] ) {
|
||||
$name = wc_attribute_taxonomy_name_by_id( (int) $request['id'] );
|
||||
|
||||
$this->attribute = $name;
|
||||
}
|
||||
|
||||
return $this->attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute data.
|
||||
*
|
||||
* @param int $id Attribute ID.
|
||||
* @return stdClass|WP_Error
|
||||
*/
|
||||
protected function get_attribute( $id ) {
|
||||
global $wpdb;
|
||||
|
||||
$attribute = $wpdb->get_row( $wpdb->prepare( "
|
||||
SELECT *
|
||||
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
|
||||
WHERE attribute_id = %d
|
||||
", $id ) );
|
||||
|
||||
if ( is_wp_error( $attribute ) || is_null( $attribute ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_attribute_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate attribute slug.
|
||||
*
|
||||
* @param string $slug
|
||||
* @param bool $new_data
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
protected function validate_attribute_slug( $slug, $new_data = true ) {
|
||||
if ( strlen( $slug ) >= 28 ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_product_attribute_slug_too_long', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
|
||||
} elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_product_attribute_slug_reserved_name', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
|
||||
} elseif ( $new_data && taxonomy_exists( wc_attribute_taxonomy_name( $slug ) ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_product_attribute_slug_already_exists', sprintf( __( 'Slug "%s" is already in use. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule to flush rewrite rules.
|
||||
*
|
||||
* @since 2.7.0
|
||||
*/
|
||||
protected function flush_rewrite_rules() {
|
||||
wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,267 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Product Categories controller
|
||||
*
|
||||
* Handles requests to the products/categories endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Product Categories controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Terms_Controller
|
||||
*/
|
||||
class WC_REST_Product_Categories_V1_Controller extends WC_REST_Terms_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/categories';
|
||||
|
||||
/**
|
||||
* Taxonomy.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $taxonomy = 'product_cat';
|
||||
|
||||
/**
|
||||
* Prepare a single product category output for response.
|
||||
*
|
||||
* @param WP_Term $item Term object.
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_REST_Response $response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
// Get category display type.
|
||||
$display_type = get_woocommerce_term_meta( $item->term_id, 'display_type' );
|
||||
|
||||
// Get category order.
|
||||
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order' );
|
||||
|
||||
$data = array(
|
||||
'id' => (int) $item->term_id,
|
||||
'name' => $item->name,
|
||||
'slug' => $item->slug,
|
||||
'parent' => (int) $item->parent,
|
||||
'description' => $item->description,
|
||||
'display' => $display_type ? $display_type : 'default',
|
||||
'image' => array(),
|
||||
'menu_order' => (int) $menu_order,
|
||||
'count' => (int) $item->count,
|
||||
);
|
||||
|
||||
// Get category image.
|
||||
if ( $image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' ) ) {
|
||||
$attachment = get_post( $image_id );
|
||||
|
||||
$data['image'] = array(
|
||||
'id' => (int) $image_id,
|
||||
'date_created' => wc_rest_prepare_date_response( $attachment->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $attachment->post_modified_gmt ),
|
||||
'src' => wp_get_attachment_url( $image_id ),
|
||||
'title' => get_the_title( $attachment ),
|
||||
'alt' => get_post_meta( $image_id, '_wp_attachment_image_alt', true ),
|
||||
);
|
||||
}
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||
|
||||
/**
|
||||
* Filter a term item returned from the API.
|
||||
*
|
||||
* Allows modification of the term data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $item The original term object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update term meta fields.
|
||||
*
|
||||
* @param WP_Term $term
|
||||
* @param WP_REST_Request $request
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
protected function update_term_meta_fields( $term, $request ) {
|
||||
$id = (int) $term->term_id;
|
||||
|
||||
if ( isset( $request['display'] ) ) {
|
||||
update_woocommerce_term_meta( $id, 'display_type', 'default' === $request['display'] ? '' : $request['display'] );
|
||||
}
|
||||
|
||||
if ( isset( $request['menu_order'] ) ) {
|
||||
update_woocommerce_term_meta( $id, 'order', $request['menu_order'] );
|
||||
}
|
||||
|
||||
if ( isset( $request['image'] ) ) {
|
||||
if ( empty( $request['image']['id'] ) && ! empty( $request['image']['src'] ) ) {
|
||||
$upload = wc_rest_upload_image_from_url( esc_url_raw( $request['image']['src'] ) );
|
||||
|
||||
if ( is_wp_error( $upload ) ) {
|
||||
return $upload;
|
||||
}
|
||||
|
||||
$image_id = wc_rest_set_uploaded_image_as_attachment( $upload );
|
||||
} else {
|
||||
$image_id = isset( $request['image']['id'] ) ? absint( $request['image']['id'] ) : 0;
|
||||
}
|
||||
|
||||
// Check if image_id is a valid image attachment before updating the term meta.
|
||||
if ( $image_id && wp_attachment_is_image( $image_id ) ) {
|
||||
update_woocommerce_term_meta( $id, 'thumbnail_id', $image_id );
|
||||
|
||||
// Set the image alt.
|
||||
if ( ! empty( $request['image']['alt'] ) ) {
|
||||
update_post_meta( $image_id, '_wp_attachment_image_alt', wc_clean( $request['image']['alt'] ) );
|
||||
}
|
||||
|
||||
// Set the image title.
|
||||
if ( ! empty( $request['image']['title'] ) ) {
|
||||
wp_update_post( array( 'ID' => $image_id, 'post_title' => wc_clean( $request['image']['title'] ) ) );
|
||||
}
|
||||
} else {
|
||||
delete_woocommerce_term_meta( $id, 'thumbnail_id' );
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Category schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => $this->taxonomy,
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Category name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
),
|
||||
),
|
||||
'parent' => array(
|
||||
'description' => __( 'The ID for the parent of the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'HTML description of the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'wp_filter_post_kses',
|
||||
),
|
||||
),
|
||||
'display' => array(
|
||||
'description' => __( 'Category archive display type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'default',
|
||||
'enum' => array( 'default', 'products', 'subcategories', 'both' ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'image' => array(
|
||||
'description' => __( 'Image data.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Image ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_modified' => array(
|
||||
'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'src' => array(
|
||||
'description' => __( 'Image URL.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Image name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'alt' => array(
|
||||
'description' => __( 'Image alternative text.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'menu_order' => array(
|
||||
'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'count' => array(
|
||||
'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,568 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Product Reviews Controller
|
||||
*
|
||||
* Handles requests to /products/<product_id>/reviews.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Product Reviews Controller Class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Product_Reviews_V1_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/(?P<product_id>[\d]+)/reviews';
|
||||
|
||||
/**
|
||||
* Register the routes for product reviews.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'product_id' => array(
|
||||
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the variation.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'review' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Review content.', 'woocommerce' ),
|
||||
),
|
||||
'name' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Name of the reviewer.', 'woocommerce' ),
|
||||
),
|
||||
'email' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Email of the reviewer.', 'woocommerce' ),
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'product_id' => array(
|
||||
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read webhook deliveries.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_post_permissions( 'product', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['product_id'] );
|
||||
|
||||
if ( $post && ! wc_rest_check_post_permissions( 'product', 'read', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to create a new product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['product_id'] );
|
||||
if ( $post && ! wc_rest_check_post_permissions( 'product', 'create', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to update a product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['product_id'] );
|
||||
if ( $post && ! wc_rest_check_post_permissions( 'product', 'edit', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to delete a product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['product_id'] );
|
||||
if ( $post && ! wc_rest_check_post_permissions( 'product', 'delete', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all reviews from a product.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$product_id = (int) $request['product_id'];
|
||||
|
||||
if ( 'product' !== get_post_type( $product_id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$reviews = get_approved_comments( $product_id );
|
||||
$data = array();
|
||||
foreach ( $reviews as $review_data ) {
|
||||
$review = $this->prepare_item_for_response( $review_data, $request );
|
||||
$review = $this->prepare_response_for_collection( $review );
|
||||
$data[] = $review;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$product_id = (int) $request['product_id'];
|
||||
|
||||
if ( 'product' !== get_post_type( $product_id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$review = get_comment( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $review ) || intval( $review->comment_post_ID ) !== $product_id ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$delivery = $this->prepare_item_for_response( $review, $request );
|
||||
$response = rest_ensure_response( $delivery );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
$product_id = (int) $request['product_id'];
|
||||
|
||||
if ( 'product' !== get_post_type( $product_id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$prepared_review = $this->prepare_item_for_database( $request );
|
||||
|
||||
/**
|
||||
* Filter a product review (comment) before it is inserted via the REST API.
|
||||
*
|
||||
* Allows modification of the comment right before it is inserted via `wp_insert_comment`.
|
||||
*
|
||||
* @param array $prepared_review The prepared comment data for `wp_insert_comment`.
|
||||
* @param WP_REST_Request $request Request used to insert the comment.
|
||||
*/
|
||||
$prepared_review = apply_filters( 'rest_pre_insert_product_review', $prepared_review, $request );
|
||||
|
||||
$product_review_id = wp_insert_comment( $prepared_review );
|
||||
if ( ! $product_review_id ) {
|
||||
return new WP_Error( 'rest_product_review_failed_create', __( 'Creating product review failed.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
update_comment_meta( $product_review_id, 'rating', ( ! empty( $request['rating'] ) ? $request['rating'] : '0' ) );
|
||||
|
||||
$product_review = get_comment( $product_review_id );
|
||||
$this->update_additional_fields_for_object( $product_review, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Comment $product_review Inserted object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_product_review", $product_review, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $product_review, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $product_review_id ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
$product_review_id = (int) $request['id'];
|
||||
$product_id = (int) $request['product_id'];
|
||||
|
||||
if ( 'product' !== get_post_type( $product_id ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$review = get_comment( $product_review_id );
|
||||
|
||||
if ( empty( $product_review_id ) || empty( $review ) || intval( $review->comment_post_ID ) !== $product_id ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$prepared_review = $this->prepare_item_for_database( $request );
|
||||
|
||||
$updated = wp_update_comment( $prepared_review );
|
||||
if ( 0 === $updated ) {
|
||||
return new WP_Error( 'rest_product_review_failed_edit', __( 'Updating product review failed.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
if ( ! empty( $request['rating'] ) ) {
|
||||
update_comment_meta( $product_review_id, 'rating', $request['rating'] );
|
||||
}
|
||||
|
||||
$product_review = get_comment( $product_review_id );
|
||||
$this->update_additional_fields_for_object( $product_review, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Comment $comment Inserted object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_product_review", $product_review, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $product_review, $request );
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a product review.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$product_review_id = absint( is_array( $request['id'] ) ? $request['id']['id'] : $request['id'] );
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
$product_review = get_comment( $product_review_id );
|
||||
if ( empty( $product_review_id ) || empty( $product_review->comment_ID ) || empty( $product_review->comment_post_ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid product review ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter whether a product review is trashable.
|
||||
*
|
||||
* Return false to disable trash support for the product review.
|
||||
*
|
||||
* @param boolean $supports_trash Whether the object supports trashing.
|
||||
* @param WP_Post $product_review The object being considered for trashing support.
|
||||
*/
|
||||
$supports_trash = apply_filters( 'rest_product_review_trashable', ( EMPTY_TRASH_DAYS > 0 ), $product_review );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $product_review, $request );
|
||||
|
||||
if ( $force ) {
|
||||
$result = wp_delete_comment( $product_review_id, true );
|
||||
} else {
|
||||
if ( ! $supports_trash ) {
|
||||
return new WP_Error( 'rest_trash_not_supported', __( 'The product review does not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
if ( 'trash' === $product_review->comment_approved ) {
|
||||
return new WP_Error( 'rest_already_trashed', __( 'The comment has already been trashed.', 'woocommerce' ), array( 'status' => 410 ) );
|
||||
}
|
||||
|
||||
$result = wp_trash_comment( $product_review->comment_ID );
|
||||
}
|
||||
|
||||
if ( ! $result ) {
|
||||
return new WP_Error( 'rest_cannot_delete', __( 'The product review cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a product review is deleted via the REST API.
|
||||
*
|
||||
* @param object $product_review The deleted item.
|
||||
* @param WP_REST_Response $response The response data.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'rest_delete_product_review', $product_review, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single product review output for response.
|
||||
*
|
||||
* @param WP_Comment $review Product review object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $review, $request ) {
|
||||
$data = array(
|
||||
'id' => (int) $review->comment_ID,
|
||||
'date_created' => wc_rest_prepare_date_response( $review->comment_date_gmt ),
|
||||
'review' => $review->comment_content,
|
||||
'rating' => (int) get_comment_meta( $review->comment_ID, 'rating', true ),
|
||||
'name' => $review->comment_author,
|
||||
'email' => $review->comment_author_email,
|
||||
'verified' => wc_review_is_from_verified_owner( $review->comment_ID ),
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $review, $request ) );
|
||||
|
||||
/**
|
||||
* Filter product reviews object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param WP_Comment $review Product review object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_product_review', $response, $review, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single product review to be inserted into the database.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return array|WP_Error $prepared_review
|
||||
*/
|
||||
protected function prepare_item_for_database( $request ) {
|
||||
$prepared_review = array( 'comment_approved' => 1, 'comment_type' => 'review' );
|
||||
|
||||
if ( isset( $request['id'] ) ) {
|
||||
$prepared_review['comment_ID'] = (int) $request['id'];
|
||||
}
|
||||
|
||||
if ( isset( $request['review'] ) ) {
|
||||
$prepared_review['comment_content'] = $request['review'];
|
||||
}
|
||||
|
||||
if ( isset( $request['product_id'] ) ) {
|
||||
$prepared_review['comment_post_ID'] = (int) $request['product_id'];
|
||||
}
|
||||
|
||||
if ( isset( $request['name'] ) ) {
|
||||
$prepared_review['comment_author'] = $request['name'];
|
||||
}
|
||||
|
||||
if ( isset( $request['email'] ) ) {
|
||||
$prepared_review['comment_author_email'] = $request['email'];
|
||||
}
|
||||
|
||||
return apply_filters( 'rest_preprocess_product_review', $prepared_review, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param WP_Comment $review Product review object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return array Links for the given product review.
|
||||
*/
|
||||
protected function prepare_links( $review, $request ) {
|
||||
$product_id = (int) $request['product_id'];
|
||||
$base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $review->comment_ID ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||
),
|
||||
'up' => array(
|
||||
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $product_id ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Product Review's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'product_review',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'review' => array(
|
||||
'description' => __( 'The content of the review.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the review was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'rating' => array(
|
||||
'description' => __( 'Review rating (0 to 5).', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Reviewer name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'email' => array(
|
||||
'description' => __( 'Reviewer email.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'verified' => array(
|
||||
'description' => __( 'Shows if the reviewer bought the product or not.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Product Shipping Classes controller
|
||||
*
|
||||
* Handles requests to the products/shipping_classes endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Product Shipping Classes controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Terms_Controller
|
||||
*/
|
||||
class WC_REST_Product_Shipping_Classes_V1_Controller extends WC_REST_Terms_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/shipping_classes';
|
||||
|
||||
/**
|
||||
* Taxonomy.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $taxonomy = 'product_shipping_class';
|
||||
|
||||
/**
|
||||
* Prepare a single product shipping class output for response.
|
||||
*
|
||||
* @param obj $item Term object.
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_REST_Response $response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
$data = array(
|
||||
'id' => (int) $item->term_id,
|
||||
'name' => $item->name,
|
||||
'slug' => $item->slug,
|
||||
'description' => $item->description,
|
||||
'count' => (int) $item->count,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||
|
||||
/**
|
||||
* Filter a term item returned from the API.
|
||||
*
|
||||
* Allows modification of the term data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $item The original term object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Shipping Class schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => $this->taxonomy,
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Shipping class name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
),
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'HTML description of the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'wp_filter_post_kses',
|
||||
),
|
||||
),
|
||||
'count' => array(
|
||||
'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Product Tags controller
|
||||
*
|
||||
* Handles requests to the products/tags endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Product Tags controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Terms_Controller
|
||||
*/
|
||||
class WC_REST_Product_Tags_V1_Controller extends WC_REST_Terms_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'products/tags';
|
||||
|
||||
/**
|
||||
* Taxonomy.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $taxonomy = 'product_tag';
|
||||
|
||||
/**
|
||||
* Prepare a single product tag output for response.
|
||||
*
|
||||
* @param obj $item Term object.
|
||||
* @param WP_REST_Request $request
|
||||
* @return WP_REST_Response $response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
$data = array(
|
||||
'id' => (int) $item->term_id,
|
||||
'name' => $item->name,
|
||||
'slug' => $item->slug,
|
||||
'description' => $item->description,
|
||||
'count' => (int) $item->count,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||
|
||||
/**
|
||||
* Filter a term item returned from the API.
|
||||
*
|
||||
* Allows modification of the term data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $item The original term object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Tag's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => $this->taxonomy,
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Tag name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
),
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'HTML description of the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'wp_filter_post_kses',
|
||||
),
|
||||
),
|
||||
'count' => array(
|
||||
'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,399 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Reports controller
|
||||
*
|
||||
* Handles requests to the reports/sales endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Report Sales controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Report_Sales_V1_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'reports/sales';
|
||||
|
||||
/**
|
||||
* Report instance.
|
||||
*
|
||||
* @var WC_Admin_Report
|
||||
*/
|
||||
protected $report;
|
||||
|
||||
/**
|
||||
* Register the routes for sales reports.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read report.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'reports', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sales reports.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$data = array();
|
||||
$item = $this->prepare_item_for_response( null, $request );
|
||||
$data[] = $this->prepare_response_for_collection( $item );
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a report sales object for serialization.
|
||||
*
|
||||
* @param null $_
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $_, $request ) {
|
||||
// Set date filtering.
|
||||
$filter = array(
|
||||
'period' => $request['period'],
|
||||
'date_min' => $request['date_min'],
|
||||
'date_max' => $request['date_max'],
|
||||
);
|
||||
$this->setup_report( $filter );
|
||||
|
||||
// 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 );
|
||||
$report_data = $this->report->get_report_data();
|
||||
$period_totals = array();
|
||||
|
||||
// Setup period totals by ensuring each period in the interval has data.
|
||||
for ( $i = 0; $i <= $this->report->chart_interval; $i++ ) {
|
||||
|
||||
switch ( $this->report->chart_groupby ) {
|
||||
case 'day' :
|
||||
$time = date( 'Y-m-d', strtotime( "+{$i} DAY", $this->report->start_date ) );
|
||||
break;
|
||||
default :
|
||||
$time = date( 'Y-m', strtotime( "+{$i} MONTH", $this->report->start_date ) );
|
||||
break;
|
||||
}
|
||||
|
||||
// Set the customer signups for each period.
|
||||
$customer_count = 0;
|
||||
foreach ( $customers as $customer ) {
|
||||
if ( date( ( 'day' == $this->report->chart_groupby ) ? 'Y-m-d' : 'Y-m', strtotime( $customer->user_registered ) ) == $time ) {
|
||||
$customer_count++;
|
||||
}
|
||||
}
|
||||
|
||||
$period_totals[ $time ] = array(
|
||||
'sales' => wc_format_decimal( 0.00, 2 ),
|
||||
'orders' => 0,
|
||||
'items' => 0,
|
||||
'tax' => wc_format_decimal( 0.00, 2 ),
|
||||
'shipping' => wc_format_decimal( 0.00, 2 ),
|
||||
'discount' => wc_format_decimal( 0.00, 2 ),
|
||||
'customers' => $customer_count,
|
||||
);
|
||||
}
|
||||
|
||||
// add total sales, total order count, total tax and total shipping for each period
|
||||
foreach ( $report_data->orders as $order ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order->post_date ) ) : date( 'Y-m', strtotime( $order->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$period_totals[ $time ]['sales'] = wc_format_decimal( $order->total_sales, 2 );
|
||||
$period_totals[ $time ]['tax'] = wc_format_decimal( $order->total_tax + $order->total_shipping_tax, 2 );
|
||||
$period_totals[ $time ]['shipping'] = wc_format_decimal( $order->total_shipping, 2 );
|
||||
}
|
||||
|
||||
foreach ( $report_data->order_counts as $order ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order->post_date ) ) : date( 'Y-m', strtotime( $order->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$period_totals[ $time ]['orders'] = (int) $order->count;
|
||||
}
|
||||
|
||||
// Add total order items for each period.
|
||||
foreach ( $report_data->order_items as $order_item ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order_item->post_date ) ) : date( 'Y-m', strtotime( $order_item->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$period_totals[ $time ]['items'] = (int) $order_item->order_item_count;
|
||||
}
|
||||
|
||||
// Add total discount for each period.
|
||||
foreach ( $report_data->coupons as $discount ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $discount->post_date ) ) : date( 'Y-m', strtotime( $discount->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$period_totals[ $time ]['discount'] = wc_format_decimal( $discount->discount_amount, 2 );
|
||||
}
|
||||
|
||||
$sales_data = array(
|
||||
'total_sales' => $report_data->total_sales,
|
||||
'net_sales' => $report_data->net_sales,
|
||||
'average_sales' => $report_data->average_sales,
|
||||
'total_orders' => $report_data->total_orders,
|
||||
'total_items' => $report_data->total_items,
|
||||
'total_tax' => wc_format_decimal( $report_data->total_tax + $report_data->total_shipping_tax, 2 ),
|
||||
'total_shipping' => $report_data->total_shipping,
|
||||
'total_refunds' => $report_data->total_refunds,
|
||||
'total_discount' => $report_data->total_coupons,
|
||||
'totals_grouped_by' => $this->report->chart_groupby,
|
||||
'totals' => $period_totals,
|
||||
'total_customers' => $total_customers,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $sales_data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( array(
|
||||
'about' => array(
|
||||
'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ),
|
||||
),
|
||||
) );
|
||||
|
||||
/**
|
||||
* Filter a report sales returned from the API.
|
||||
*
|
||||
* Allows modification of the report sales data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdClass $data The original report object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_report_sales', $response, (object) $sales_data, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the report object and parse any date filtering.
|
||||
*
|
||||
* @param array $filter date filtering
|
||||
*/
|
||||
protected function setup_report( $filter ) {
|
||||
include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php' );
|
||||
include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-report-sales-by-date.php' );
|
||||
|
||||
$this->report = new WC_Report_Sales_By_Date();
|
||||
|
||||
if ( empty( $filter['period'] ) ) {
|
||||
|
||||
// Custom date range.
|
||||
$filter['period'] = 'custom';
|
||||
|
||||
if ( ! empty( $filter['date_min'] ) || ! empty( $filter['date_max'] ) ) {
|
||||
|
||||
// Overwrite _GET to make use of WC_Admin_Report::calculate_current_range() for custom date ranges.
|
||||
$_GET['start_date'] = $filter['date_min'];
|
||||
$_GET['end_date'] = isset( $filter['date_max'] ) ? $filter['date_max'] : null;
|
||||
|
||||
} else {
|
||||
|
||||
// Default custom range to today.
|
||||
$_GET['start_date'] = $_GET['end_date'] = date( 'Y-m-d', current_time( 'timestamp' ) );
|
||||
}
|
||||
} else {
|
||||
|
||||
$filter['period'] = empty( $filter['period'] ) ? 'week' : $filter['period'];
|
||||
|
||||
// Change "week" period to "7day".
|
||||
if ( 'week' === $filter['period'] ) {
|
||||
$filter['period'] = '7day';
|
||||
}
|
||||
}
|
||||
|
||||
$this->report->calculate_current_range( $filter['period'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Report's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'sales_report',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'total_sales' => array(
|
||||
'description' => __( 'Gross sales in the period.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'net_sales' => array(
|
||||
'description' => __( 'Net sales in the period.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'average_sales' => array(
|
||||
'description' => __( 'Average net daily sales.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_orders' => array(
|
||||
'description' => __( 'Total of orders placed.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_items' => array(
|
||||
'description' => __( 'Total of items purchased.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_tax' => array(
|
||||
'description' => __( 'Total charged for taxes.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_shipping' => array(
|
||||
'description' => __( 'Total charged for shipping.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_refunds' => array(
|
||||
'description' => __( 'Total of refunded orders.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'total_discount' => array(
|
||||
'description' => __( 'Total of coupons used.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'totals_grouped_by' => array(
|
||||
'description' => __( 'Group type.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'totals' => array(
|
||||
'description' => __( 'Totals.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'array',
|
||||
),
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
'period' => array(
|
||||
'description' => __( 'Report period.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'enum' => array( 'week', 'month', 'last_month', 'year' ),
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
'date_min' => array(
|
||||
/* translators: %s: date format */
|
||||
'description' => sprintf( __( 'Return sales for a specific start date, the date need to be in the %s format.', 'woocommerce' ), 'YYYY-MM-AA' ),
|
||||
'type' => 'string',
|
||||
'format' => 'date',
|
||||
'validate_callback' => 'wc_rest_validate_reports_request_arg',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
'date_max' => array(
|
||||
/* translators: %s: date format */
|
||||
'description' => sprintf( __( 'Return sales for a specific end date, the date need to be in the %s format.', 'woocommerce' ), 'YYYY-MM-AA' ),
|
||||
'type' => 'string',
|
||||
'format' => 'date',
|
||||
'validate_callback' => 'wc_rest_validate_reports_request_arg',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,174 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Reports controller
|
||||
*
|
||||
* Handles requests to the reports/top_sellers endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Report Top Sellers controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Report_Sales_V1_Controller
|
||||
*/
|
||||
class WC_REST_Report_Top_Sellers_V1_Controller extends WC_REST_Report_Sales_V1_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'reports/top_sellers';
|
||||
|
||||
/**
|
||||
* Get sales reports.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
// Set date filtering.
|
||||
$filter = array(
|
||||
'period' => $request['period'],
|
||||
'date_min' => $request['date_min'],
|
||||
'date_max' => $request['date_max'],
|
||||
);
|
||||
$this->setup_report( $filter );
|
||||
|
||||
$report_data = $this->report->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'_product_id' => array(
|
||||
'type' => 'order_item_meta',
|
||||
'order_item_type' => 'line_item',
|
||||
'function' => '',
|
||||
'name' => 'product_id',
|
||||
),
|
||||
'_qty' => array(
|
||||
'type' => 'order_item_meta',
|
||||
'order_item_type' => 'line_item',
|
||||
'function' => 'SUM',
|
||||
'name' => 'order_item_qty',
|
||||
),
|
||||
),
|
||||
'order_by' => 'order_item_qty DESC',
|
||||
'group_by' => 'product_id',
|
||||
'limit' => isset( $filter['limit'] ) ? absint( $filter['limit'] ) : 12,
|
||||
'query_type' => 'get_results',
|
||||
'filter_range' => true,
|
||||
) );
|
||||
|
||||
$top_sellers = array();
|
||||
|
||||
foreach ( $report_data as $item ) {
|
||||
$product = wc_get_product( $item->product_id );
|
||||
|
||||
if ( $product ) {
|
||||
$top_sellers[] = array(
|
||||
'name' => $product->get_name(),
|
||||
'product_id' => (int) $item->product_id,
|
||||
'quantity' => wc_stock_amount( $item->order_item_qty ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$data = array();
|
||||
foreach ( $top_sellers as $top_seller ) {
|
||||
$item = $this->prepare_item_for_response( (object) $top_seller, $request );
|
||||
$data[] = $this->prepare_response_for_collection( $item );
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a report sales object for serialization.
|
||||
*
|
||||
* @param stdClass $top_seller
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $top_seller, $request ) {
|
||||
$data = array(
|
||||
'name' => $top_seller->name,
|
||||
'product_id' => $top_seller->product_id,
|
||||
'quantity' => $top_seller->quantity,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( array(
|
||||
'about' => array(
|
||||
'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ),
|
||||
),
|
||||
'product' => array(
|
||||
'href' => rest_url( sprintf( '/%s/products/%s', $this->namespace, $top_seller->product_id ) ),
|
||||
),
|
||||
) );
|
||||
|
||||
/**
|
||||
* Filter a report top sellers returned from the API.
|
||||
*
|
||||
* Allows modification of the report top sellers data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdClass $top_seller The original report object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_report_top_sellers', $response, $top_seller, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Report's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'top_sellers_report',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'name' => array(
|
||||
'description' => __( 'Product name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'product_id' => array(
|
||||
'description' => __( 'Product ID.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'quantity' => array(
|
||||
'description' => __( 'Total number of purchases.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,174 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Reports controller
|
||||
*
|
||||
* Handles requests to the reports endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Reports controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Reports_V1_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'reports';
|
||||
|
||||
/**
|
||||
* Register the routes for reports.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read reports.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'reports', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all reports.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$data = array();
|
||||
$reports = array(
|
||||
array(
|
||||
'slug' => 'sales',
|
||||
'description' => __( 'List of sales reports.', 'woocommerce' ),
|
||||
),
|
||||
array(
|
||||
'slug' => 'top_sellers',
|
||||
'description' => __( 'List of top sellers products.', 'woocommerce' ),
|
||||
),
|
||||
);
|
||||
|
||||
foreach ( $reports as $report ) {
|
||||
$item = $this->prepare_item_for_response( (object) $report, $request );
|
||||
$data[] = $this->prepare_response_for_collection( $item );
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a report object for serialization.
|
||||
*
|
||||
* @param stdClass $report Report data.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $report, $request ) {
|
||||
$data = array(
|
||||
'slug' => $report->slug,
|
||||
'description' => $report->description,
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $report->slug ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
|
||||
),
|
||||
) );
|
||||
|
||||
/**
|
||||
* Filter a report returned from the API.
|
||||
*
|
||||
* Allows modification of the report data right before it is returned.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $report The original report object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_report', $response, $report, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Report's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'report',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'A human-readable description of the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,362 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Tax Classes controller
|
||||
*
|
||||
* Handles requests to the /taxes/classes endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Tax Classes controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Tax_Classes_V1_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'taxes/classes';
|
||||
|
||||
/**
|
||||
* Register the routes for tax classes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<slug>\w[\w\s\-]*)', array(
|
||||
'args' => array(
|
||||
'slug' => array(
|
||||
'description' => __( 'Unique slug for the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read tax classes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access create tax classes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'create' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access delete a tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tax classes.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$tax_classes = array();
|
||||
|
||||
// Add standard class.
|
||||
$tax_classes[] = array(
|
||||
'slug' => 'standard',
|
||||
'name' => __( 'Standard rate', 'woocommerce' ),
|
||||
);
|
||||
|
||||
$classes = WC_Tax::get_tax_classes();
|
||||
|
||||
foreach ( $classes as $class ) {
|
||||
$tax_classes[] = array(
|
||||
'slug' => sanitize_title( $class ),
|
||||
'name' => $class,
|
||||
);
|
||||
}
|
||||
|
||||
$data = array();
|
||||
foreach ( $tax_classes as $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.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
$exists = false;
|
||||
$classes = WC_Tax::get_tax_classes();
|
||||
$tax_class = array(
|
||||
'slug' => sanitize_title( $request['name'] ),
|
||||
'name' => $request['name'],
|
||||
);
|
||||
|
||||
// Check if class exists.
|
||||
foreach ( $classes as $key => $class ) {
|
||||
if ( sanitize_title( $class ) === $tax_class['slug'] ) {
|
||||
$exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Return error if tax class already exists.
|
||||
if ( $exists ) {
|
||||
return new WP_Error( 'woocommerce_rest_tax_class_exists', __( 'Cannot create existing resource.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
// Add the new class.
|
||||
$classes[] = $tax_class['name'];
|
||||
|
||||
update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) );
|
||||
|
||||
$this->update_additional_fields_for_object( $tax_class, $request );
|
||||
|
||||
/**
|
||||
* Fires after a tax class is created or updated via the REST API.
|
||||
*
|
||||
* @param stdClass $tax_class Data used to create the tax class.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating tax class, false when updating tax class.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_tax_class', (object) $tax_class, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $tax_class, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $tax_class['slug'] ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single tax class.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Taxes do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$tax_class = array(
|
||||
'slug' => sanitize_title( $request['slug'] ),
|
||||
'name' => '',
|
||||
);
|
||||
$classes = WC_Tax::get_tax_classes();
|
||||
$deleted = false;
|
||||
|
||||
foreach ( $classes as $key => $class ) {
|
||||
if ( sanitize_title( $class ) === $tax_class['slug'] ) {
|
||||
$tax_class['name'] = $class;
|
||||
unset( $classes[ $key ] );
|
||||
$deleted = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $deleted ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) );
|
||||
|
||||
// Delete tax rate locations locations from the selected class.
|
||||
$wpdb->query( $wpdb->prepare( "
|
||||
DELETE locations.*
|
||||
FROM {$wpdb->prefix}woocommerce_tax_rate_locations AS locations
|
||||
INNER JOIN
|
||||
{$wpdb->prefix}woocommerce_tax_rates AS rates
|
||||
ON rates.tax_rate_id = locations.tax_rate_id
|
||||
WHERE rates.tax_rate_class = '%s'
|
||||
", $tax_class['slug'] ) );
|
||||
|
||||
// Delete tax rates in the selected class.
|
||||
$wpdb->delete( $wpdb->prefix . 'woocommerce_tax_rates', array( 'tax_rate_class' => $tax_class['slug'] ), array( '%s' ) );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $tax_class, $request );
|
||||
|
||||
/**
|
||||
* Fires after a tax class is deleted via the REST API.
|
||||
*
|
||||
* @param stdClass $tax_class The tax data.
|
||||
* @param WP_REST_Response $response The response returned from the API.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_delete_tax', (object) $tax_class, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single tax class output for response.
|
||||
*
|
||||
* @param array $tax_class Tax class data.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $tax_class, $request ) {
|
||||
$data = $tax_class;
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links() );
|
||||
|
||||
/**
|
||||
* Filter tax object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdClass $tax_class Tax object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_tax', $response, (object) $tax_class, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @return array Links for the given tax class.
|
||||
*/
|
||||
protected function prepare_links() {
|
||||
$links = array(
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Tax Classes schema, conforming to JSON Schema
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'tax_class',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'slug' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Tax class name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'required' => true,
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,708 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Taxes controller
|
||||
*
|
||||
* Handles requests to the /taxes endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Taxes controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'taxes';
|
||||
|
||||
/**
|
||||
* Register the routes for taxes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read taxes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access create taxes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'create' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access update a tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access delete a tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access batch create, update and delete items.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return boolean
|
||||
*/
|
||||
public function batch_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'batch' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all taxes.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$prepared_args = array();
|
||||
$prepared_args['exclude'] = $request['exclude'];
|
||||
$prepared_args['include'] = $request['include'];
|
||||
$prepared_args['order'] = $request['order'];
|
||||
$prepared_args['number'] = $request['per_page'];
|
||||
if ( ! empty( $request['offset'] ) ) {
|
||||
$prepared_args['offset'] = $request['offset'];
|
||||
} else {
|
||||
$prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number'];
|
||||
}
|
||||
$orderby_possibles = array(
|
||||
'id' => 'tax_rate_id',
|
||||
'order' => 'tax_rate_order',
|
||||
);
|
||||
$prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ];
|
||||
$prepared_args['class'] = $request['class'];
|
||||
|
||||
/**
|
||||
* Filter arguments, before passing to $wpdb->get_results(), when querying taxes via the REST API.
|
||||
*
|
||||
* @param array $prepared_args Array of arguments for $wpdb->get_results().
|
||||
* @param WP_REST_Request $request The current request.
|
||||
*/
|
||||
$prepared_args = apply_filters( 'woocommerce_rest_tax_query', $prepared_args, $request );
|
||||
|
||||
$query = "
|
||||
SELECT *
|
||||
FROM {$wpdb->prefix}woocommerce_tax_rates
|
||||
WHERE 1 = 1
|
||||
";
|
||||
|
||||
// Filter by tax class.
|
||||
if ( ! empty( $prepared_args['class'] ) ) {
|
||||
$class = 'standard' !== $prepared_args['class'] ? sanitize_title( $prepared_args['class'] ) : '';
|
||||
$query .= " AND tax_rate_class = '$class'";
|
||||
}
|
||||
|
||||
// Order tax rates.
|
||||
$order_by = sprintf( ' ORDER BY %s', sanitize_key( $prepared_args['orderby'] ) );
|
||||
|
||||
// Pagination.
|
||||
$pagination = sprintf( ' LIMIT %d, %d', $prepared_args['offset'], $prepared_args['number'] );
|
||||
|
||||
// Query taxes.
|
||||
$results = $wpdb->get_results( $query . $order_by . $pagination );
|
||||
|
||||
$taxes = array();
|
||||
foreach ( $results as $tax ) {
|
||||
$data = $this->prepare_item_for_response( $tax, $request );
|
||||
$taxes[] = $this->prepare_response_for_collection( $data );
|
||||
}
|
||||
|
||||
$response = rest_ensure_response( $taxes );
|
||||
|
||||
// Store pagation values for headers then unset for count query.
|
||||
$per_page = (int) $prepared_args['number'];
|
||||
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
|
||||
|
||||
// Query only for ids.
|
||||
$wpdb->get_results( str_replace( 'SELECT *', 'SELECT tax_rate_id', $query ) );
|
||||
|
||||
// Calcule totals.
|
||||
$total_taxes = (int) $wpdb->num_rows;
|
||||
$response->header( 'X-WP-Total', (int) $total_taxes );
|
||||
$max_pages = ceil( $total_taxes / $per_page );
|
||||
$response->header( 'X-WP-TotalPages', (int) $max_pages );
|
||||
|
||||
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
|
||||
if ( $page > 1 ) {
|
||||
$prev_page = $page - 1;
|
||||
if ( $prev_page > $max_pages ) {
|
||||
$prev_page = $max_pages;
|
||||
}
|
||||
$prev_link = add_query_arg( 'page', $prev_page, $base );
|
||||
$response->link_header( 'prev', $prev_link );
|
||||
}
|
||||
if ( $max_pages > $page ) {
|
||||
$next_page = $page + 1;
|
||||
$next_link = add_query_arg( 'page', $next_page, $base );
|
||||
$response->link_header( 'next', $next_link );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Take tax data from the request and return the updated or newly created rate.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @param stdClass|null $current Existing tax object.
|
||||
* @return stdClass
|
||||
*/
|
||||
protected function create_or_update_tax( $request, $current = null ) {
|
||||
$id = absint( isset( $request['id'] ) ? $request['id'] : 0 );
|
||||
$data = array();
|
||||
$fields = array(
|
||||
'tax_rate_country',
|
||||
'tax_rate_state',
|
||||
'tax_rate',
|
||||
'tax_rate_name',
|
||||
'tax_rate_priority',
|
||||
'tax_rate_compound',
|
||||
'tax_rate_shipping',
|
||||
'tax_rate_order',
|
||||
'tax_rate_class',
|
||||
);
|
||||
|
||||
foreach ( $fields as $field ) {
|
||||
// Keys via API differ from the stored names returned by _get_tax_rate.
|
||||
$key = 'tax_rate' === $field ? 'rate' : str_replace( 'tax_rate_', '', $field );
|
||||
|
||||
// Remove data that was not posted.
|
||||
if ( ! isset( $request[ $key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Test new data against current data.
|
||||
if ( $current && $current->$field === $request[ $key ] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add to data array.
|
||||
switch ( $key ) {
|
||||
case 'tax_rate_priority' :
|
||||
case 'tax_rate_compound' :
|
||||
case 'tax_rate_shipping' :
|
||||
case 'tax_rate_order' :
|
||||
$data[ $field ] = absint( $request[ $key ] );
|
||||
break;
|
||||
case 'tax_rate_class' :
|
||||
$data[ $field ] = 'standard' !== $request['tax_rate_class'] ? $request['tax_rate_class'] : '';
|
||||
break;
|
||||
default :
|
||||
$data[ $field ] = wc_clean( $request[ $key ] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $id ) {
|
||||
WC_Tax::_update_tax_rate( $id, $data );
|
||||
} else {
|
||||
$id = WC_Tax::_insert_tax_rate( $data );
|
||||
}
|
||||
|
||||
// Add locales.
|
||||
if ( ! empty( $request['postcode'] ) ) {
|
||||
WC_Tax::_update_tax_rate_postcodes( $id, wc_clean( $request['postcode'] ) );
|
||||
}
|
||||
if ( ! empty( $request['city'] ) ) {
|
||||
WC_Tax::_update_tax_rate_cities( $id, wc_clean( $request['city'] ) );
|
||||
}
|
||||
|
||||
return WC_Tax::_get_tax_rate( $id, OBJECT );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_tax_exists', __( 'Cannot create existing resource.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$tax = $this->create_or_update_tax( $request );
|
||||
|
||||
$this->update_additional_fields_for_object( $tax, $request );
|
||||
|
||||
/**
|
||||
* Fires after a tax is created or updated via the REST API.
|
||||
*
|
||||
* @param stdClass $tax Data used to create the tax.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating tax, false when updating tax.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_tax', $tax, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $tax, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $tax->tax_rate_id ) ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$tax_obj = WC_Tax::_get_tax_rate( $id, OBJECT );
|
||||
|
||||
if ( empty( $id ) || empty( $tax_obj ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$tax = $this->prepare_item_for_response( $tax_obj, $request );
|
||||
$response = rest_ensure_response( $tax );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$tax_obj = WC_Tax::_get_tax_rate( $id, OBJECT );
|
||||
|
||||
if ( empty( $id ) || empty( $tax_obj ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$tax = $this->create_or_update_tax( $request, $tax_obj );
|
||||
|
||||
$this->update_additional_fields_for_object( $tax, $request );
|
||||
|
||||
/**
|
||||
* Fires after a tax is created or updated via the REST API.
|
||||
*
|
||||
* @param stdClass $tax Data used to create the tax.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating tax, false when updating tax.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_insert_tax', $tax, $request, false );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $tax, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single tax.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$id = (int) $request['id'];
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Taxes do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$tax = WC_Tax::_get_tax_rate( $id, OBJECT );
|
||||
|
||||
if ( empty( $id ) || empty( $tax ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $tax, $request );
|
||||
|
||||
WC_Tax::_delete_tax_rate( $id );
|
||||
|
||||
if ( 0 === $wpdb->rows_affected ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a tax is deleted via the REST API.
|
||||
*
|
||||
* @param stdClass $tax The tax data.
|
||||
* @param WP_REST_Response $response The response returned from the API.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( 'woocommerce_rest_delete_tax', $tax, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single tax output for response.
|
||||
*
|
||||
* @param stdClass $tax Tax object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $tax, $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$id = (int) $tax->tax_rate_id;
|
||||
$data = array(
|
||||
'id' => $id,
|
||||
'country' => $tax->tax_rate_country,
|
||||
'state' => $tax->tax_rate_state,
|
||||
'postcode' => '',
|
||||
'city' => '',
|
||||
'rate' => $tax->tax_rate,
|
||||
'name' => $tax->tax_rate_name,
|
||||
'priority' => (int) $tax->tax_rate_priority,
|
||||
'compound' => (bool) $tax->tax_rate_compound,
|
||||
'shipping' => (bool) $tax->tax_rate_shipping,
|
||||
'order' => (int) $tax->tax_rate_order,
|
||||
'class' => $tax->tax_rate_class ? $tax->tax_rate_class : 'standard',
|
||||
);
|
||||
|
||||
// Get locales from a tax rate.
|
||||
$locales = $wpdb->get_results( $wpdb->prepare( "
|
||||
SELECT location_code, location_type
|
||||
FROM {$wpdb->prefix}woocommerce_tax_rate_locations
|
||||
WHERE tax_rate_id = %d
|
||||
", $id ) );
|
||||
|
||||
if ( ! is_wp_error( $tax ) && ! is_null( $tax ) ) {
|
||||
foreach ( $locales as $locale ) {
|
||||
$data[ $locale->location_type ] = $locale->location_code;
|
||||
}
|
||||
}
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $tax ) );
|
||||
|
||||
/**
|
||||
* Filter tax object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdClass $tax Tax object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_tax', $response, $tax, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param stdClass $tax Tax object.
|
||||
* @return array Links for the given tax.
|
||||
*/
|
||||
protected function prepare_links( $tax ) {
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $tax->tax_rate_id ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Taxes schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'tax',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'country' => array(
|
||||
'description' => __( 'Country ISO 3166 code.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'state' => array(
|
||||
'description' => __( 'State code.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'postcode' => array(
|
||||
'description' => __( 'Postcode / ZIP.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'city' => array(
|
||||
'description' => __( 'City name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'rate' => array(
|
||||
'description' => __( 'Tax rate.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'Tax rate name.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'priority' => array(
|
||||
'description' => __( 'Tax priority.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'default' => 1,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'compound' => array(
|
||||
'description' => __( 'Whether or not this is a compound rate.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'shipping' => array(
|
||||
'description' => __( 'Whether or not this tax rate also gets applied to shipping.', 'woocommerce' ),
|
||||
'type' => 'boolean',
|
||||
'default' => true,
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'order' => array(
|
||||
'description' => __( 'Indicates the order that will appear in queries.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'class' => array(
|
||||
'description' => __( 'Tax class.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'standard',
|
||||
'enum' => array_merge( array( 'standard' ), WC_Tax::get_tax_class_slugs() ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['context']['default'] = 'view';
|
||||
|
||||
$params['exclude'] = array(
|
||||
'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
$params['include'] = array(
|
||||
'description' => __( 'Limit result set to specific IDs.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
$params['offset'] = array(
|
||||
'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['order'] = array(
|
||||
'default' => 'asc',
|
||||
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
|
||||
'enum' => array( 'asc', 'desc' ),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['orderby'] = array(
|
||||
'default' => 'order',
|
||||
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
|
||||
'enum' => array(
|
||||
'id',
|
||||
'order',
|
||||
),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
$params['class'] = array(
|
||||
'description' => __( 'Sort by tax class.', 'woocommerce' ),
|
||||
'enum' => array_merge( array( 'standard' ), WC_Tax::get_tax_class_slugs() ),
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,329 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Webhooks controller
|
||||
*
|
||||
* Handles requests to the /webhooks/<webhook_id>/deliveries endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Webhook Deliveries controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Controller
|
||||
*/
|
||||
class WC_REST_Webhook_Deliveries_V1_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'webhooks/(?P<webhook_id>[\d]+)/deliveries';
|
||||
|
||||
/**
|
||||
* Register the routes for webhook deliveries.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
'args' => array(
|
||||
'webhook_id' => array(
|
||||
'description' => __( 'Unique identifier for the webhook.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'webhook_id' => array(
|
||||
'description' => __( 'Unique identifier for the webhook.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read webhook deliveries.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_post_permissions( 'shop_webhook', 'read' ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a webhook develivery.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
$post = get_post( (int) $request['webhook_id'] );
|
||||
|
||||
if ( $post && ! wc_rest_check_post_permissions( 'shop_webhook', 'read', $post->ID ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all webhook deliveries.
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$webhook = new WC_Webhook( (int) $request['webhook_id'] );
|
||||
|
||||
if ( empty( $webhook->post_data->post_type ) || 'shop_webhook' !== $webhook->post_data->post_type ) {
|
||||
return new WP_Error( 'woocommerce_rest_webhook_invalid_id', __( 'Invalid webhook ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$logs = $webhook->get_delivery_logs();
|
||||
|
||||
$data = array();
|
||||
foreach ( $logs as $log ) {
|
||||
$delivery = $this->prepare_item_for_response( (object) $log, $request );
|
||||
$delivery = $this->prepare_response_for_collection( $delivery );
|
||||
$data[] = $delivery;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single webhook delivery.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$webhook = new WC_Webhook( (int) $request['webhook_id'] );
|
||||
|
||||
if ( empty( $webhook->post_data->post_type ) || 'shop_webhook' !== $webhook->post_data->post_type ) {
|
||||
return new WP_Error( 'woocommerce_rest_webhook_invalid_id', __( 'Invalid webhook ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$log = $webhook->get_delivery_log( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $log ) ) {
|
||||
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$delivery = $this->prepare_item_for_response( (object) $log, $request );
|
||||
$response = rest_ensure_response( $delivery );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single webhook delivery output for response.
|
||||
*
|
||||
* @param stdClass $log Delivery log object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $log, $request ) {
|
||||
$data = (array) $log;
|
||||
|
||||
// Add timestamp.
|
||||
$data['date_created'] = wc_rest_prepare_date_response( $log->comment->comment_date_gmt );
|
||||
|
||||
// Remove comment object.
|
||||
unset( $data['comment'] );
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $log ) );
|
||||
|
||||
/**
|
||||
* Filter webhook delivery object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param stdClass $log Delivery log object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_rest_prepare_webhook_delivery', $response, $log, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param stdClass $log Delivery log object.
|
||||
* @return array Links for the given webhook delivery.
|
||||
*/
|
||||
protected function prepare_links( $log ) {
|
||||
$webhook_id = (int) $log->request_headers['X-WC-Webhook-ID'];
|
||||
$base = str_replace( '(?P<webhook_id>[\d]+)', $webhook_id, $this->rest_base );
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $log->id ) ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||
),
|
||||
'up' => array(
|
||||
'href' => rest_url( sprintf( '/%s/webhooks/%d', $this->namespace, $webhook_id ) ),
|
||||
),
|
||||
);
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Webhook's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'webhook_delivery',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'duration' => array(
|
||||
'description' => __( 'The delivery duration, in seconds.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'summary' => array(
|
||||
'description' => __( 'A friendly summary of the response including the HTTP response code, message, and body.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'request_url' => array(
|
||||
'description' => __( 'The URL where the webhook was delivered.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'request_headers' => array(
|
||||
'description' => __( 'The URL where the webhook was delivered.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'request_headers' => array(
|
||||
'description' => __( 'Request headers.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
'request_body' => array(
|
||||
'description' => __( 'Request body.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'response_code' => array(
|
||||
'description' => __( 'The HTTP response code from the receiving server.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'response_message' => array(
|
||||
'description' => __( 'The HTTP response message from the receiving server.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'response_headers' => array(
|
||||
'description' => __( 'Array of the response headers from the receiving server.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
'response_body' => array(
|
||||
'description' => __( 'The response body from the receiving server.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the webhook delivery was logged, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,583 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Webhooks controller
|
||||
*
|
||||
* Handles requests to the /webhooks endpoint.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category API
|
||||
* @package WooCommerce/API
|
||||
* @since 2.7.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST API Webhooks controller class.
|
||||
*
|
||||
* @package WooCommerce/API
|
||||
* @extends WC_REST_Posts_Controller
|
||||
*/
|
||||
class WC_REST_Webhooks_V1_Controller extends WC_REST_Posts_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v1';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'webhooks';
|
||||
|
||||
/**
|
||||
* Post type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $post_type = 'shop_webhook';
|
||||
|
||||
/**
|
||||
* Initialize Webhooks actions.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the routes for webhooks.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_item' ),
|
||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
|
||||
'topic' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Webhook topic.', 'woocommerce' ),
|
||||
),
|
||||
'delivery_url' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Webhook delivery URL.', 'woocommerce' ),
|
||||
),
|
||||
'secret' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => __( 'Webhook secret.', 'woocommerce' ),
|
||||
),
|
||||
) ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_item' ),
|
||||
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_item' ),
|
||||
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'force' => array(
|
||||
'default' => false,
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
) );
|
||||
|
||||
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'batch_items' ),
|
||||
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
|
||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_batch_schema' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single webhook.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( ! empty( $request['id'] ) ) {
|
||||
/* translators: %s: post type */
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
// Validate topic.
|
||||
if ( empty( $request['topic'] ) || ! wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic is required and must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
// Validate delivery URL.
|
||||
if ( empty( $request['delivery_url'] ) || ! wc_is_valid_url( $request['delivery_url'] ) ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_delivery_url", __( 'Webhook delivery URL must be a valid URL starting with http:// or https://.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$post = $this->prepare_item_for_database( $request );
|
||||
if ( is_wp_error( $post ) ) {
|
||||
return $post;
|
||||
}
|
||||
|
||||
$post->post_type = $this->post_type;
|
||||
$post_id = wp_insert_post( $post, true );
|
||||
|
||||
if ( is_wp_error( $post_id ) ) {
|
||||
|
||||
if ( in_array( $post_id->get_error_code(), array( 'db_insert_error' ) ) ) {
|
||||
$post_id->add_data( array( 'status' => 500 ) );
|
||||
} else {
|
||||
$post_id->add_data( array( 'status' => 400 ) );
|
||||
}
|
||||
return $post_id;
|
||||
}
|
||||
$post->ID = $post_id;
|
||||
|
||||
$webhook = new WC_Webhook( $post_id );
|
||||
|
||||
// Set topic.
|
||||
$webhook->set_topic( $request['topic'] );
|
||||
|
||||
// Set delivery URL.
|
||||
$webhook->set_delivery_url( $request['delivery_url'] );
|
||||
|
||||
// Set secret.
|
||||
$webhook->set_secret( ! empty( $request['secret'] ) ? $request['secret'] : '' );
|
||||
|
||||
// Set API version to WP API integration v1.
|
||||
$webhook->set_api_version( 'wp_api_v1' );
|
||||
|
||||
// Set status.
|
||||
if ( ! empty( $request['status'] ) ) {
|
||||
$webhook->update_status( $request['status'] );
|
||||
}
|
||||
|
||||
$post = get_post( $post_id );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Post $post Inserted object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post_id ) ) );
|
||||
|
||||
// Send ping.
|
||||
$webhook->deliver_ping();
|
||||
|
||||
// Clear cache.
|
||||
delete_transient( 'woocommerce_webhook_ids' );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single webhook.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$post = get_post( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$webhook = new WC_Webhook( $id );
|
||||
|
||||
// Update topic.
|
||||
if ( ! empty( $request['topic'] ) ) {
|
||||
if ( wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) {
|
||||
$webhook->set_topic( $request['topic'] );
|
||||
} else {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Update delivery URL.
|
||||
if ( ! empty( $request['delivery_url'] ) ) {
|
||||
if ( wc_is_valid_url( $request['delivery_url'] ) ) {
|
||||
$webhook->set_delivery_url( $request['delivery_url'] );
|
||||
} else {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_delivery_url", __( 'Webhook delivery URL must be a valid URL starting with http:// or https://.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Update secret.
|
||||
if ( ! empty( $request['secret'] ) ) {
|
||||
$webhook->set_secret( $request['secret'] );
|
||||
}
|
||||
|
||||
// Update status.
|
||||
if ( ! empty( $request['status'] ) ) {
|
||||
$webhook->update_status( $request['status'] );
|
||||
}
|
||||
|
||||
$post = $this->prepare_item_for_database( $request );
|
||||
if ( is_wp_error( $post ) ) {
|
||||
return $post;
|
||||
}
|
||||
|
||||
// Convert the post object to an array, otherwise wp_update_post will expect non-escaped input.
|
||||
$post_id = wp_update_post( (array) $post, true );
|
||||
if ( is_wp_error( $post_id ) ) {
|
||||
if ( in_array( $post_id->get_error_code(), array( 'db_update_error' ) ) ) {
|
||||
$post_id->add_data( array( 'status' => 500 ) );
|
||||
} else {
|
||||
$post_id->add_data( array( 'status' => 400 ) );
|
||||
}
|
||||
return $post_id;
|
||||
}
|
||||
|
||||
$post = get_post( $post_id );
|
||||
$this->update_additional_fields_for_object( $post, $request );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Post $post Inserted object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @param boolean $creating True when creating item, false when updating.
|
||||
*/
|
||||
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
|
||||
// Clear cache.
|
||||
delete_transient( 'woocommerce_webhook_ids' );
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single webhook.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response|WP_Error
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$id = (int) $request['id'];
|
||||
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
|
||||
|
||||
// We don't support trashing for this type, error out.
|
||||
if ( ! $force ) {
|
||||
return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Webhooks do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$post = get_post( $id );
|
||||
|
||||
if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
|
||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid post ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_item_for_response( $post, $request );
|
||||
|
||||
$result = wp_delete_post( $id, true );
|
||||
|
||||
if ( ! $result ) {
|
||||
/* translators: %s: post type */
|
||||
return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a single item is deleted or trashed via the REST API.
|
||||
*
|
||||
* @param object $post The deleted or trashed item.
|
||||
* @param WP_REST_Response $response The response data.
|
||||
* @param WP_REST_Request $request The request sent to the API.
|
||||
*/
|
||||
do_action( "woocommerce_rest_delete_{$this->post_type}", $post, $response, $request );
|
||||
|
||||
// Clear cache.
|
||||
delete_transient( 'woocommerce_webhook_ids' );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single webhook for create or update.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_Error|stdClass $data Post object.
|
||||
*/
|
||||
protected function prepare_item_for_database( $request ) {
|
||||
global $wpdb;
|
||||
|
||||
$data = new stdClass;
|
||||
|
||||
// Post ID.
|
||||
if ( isset( $request['id'] ) ) {
|
||||
$data->ID = absint( $request['id'] );
|
||||
}
|
||||
|
||||
// Validate required POST fields.
|
||||
if ( 'POST' === $request->get_method() && empty( $data->ID ) ) {
|
||||
// @codingStandardsIgnoreStart
|
||||
$data->post_title = ! empty( $request['name'] ) ? $request['name'] : sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) );
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
// Post author.
|
||||
$data->post_author = get_current_user_id();
|
||||
|
||||
// Post password.
|
||||
$password = strlen( uniqid( 'webhook_' ) );
|
||||
$data->post_password = $password > 20 ? substr( $password, 0, 20 ) : $password;
|
||||
|
||||
// Post status.
|
||||
$data->post_status = 'publish';
|
||||
} else {
|
||||
|
||||
// Allow edit post title.
|
||||
if ( ! empty( $request['name'] ) ) {
|
||||
$data->post_title = $request['name'];
|
||||
}
|
||||
}
|
||||
|
||||
// Comment status.
|
||||
$data->comment_status = 'closed';
|
||||
|
||||
// Ping status.
|
||||
$data->ping_status = 'closed';
|
||||
|
||||
/**
|
||||
* Filter the query_vars used in `get_items` for the constructed query.
|
||||
*
|
||||
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
|
||||
* prepared for insertion.
|
||||
*
|
||||
* @param stdClass $data An object representing a single item prepared
|
||||
* for inserting or updating the database.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $data, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single webhook output for response.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $response Response data.
|
||||
*/
|
||||
public function prepare_item_for_response( $post, $request ) {
|
||||
$id = (int) $post->ID;
|
||||
$webhook = new WC_Webhook( $id );
|
||||
$data = array(
|
||||
'id' => $webhook->id,
|
||||
'name' => $webhook->get_name(),
|
||||
'status' => $webhook->get_status(),
|
||||
'topic' => $webhook->get_topic(),
|
||||
'resource' => $webhook->get_resource(),
|
||||
'event' => $webhook->get_event(),
|
||||
'hooks' => $webhook->get_hooks(),
|
||||
'delivery_url' => $webhook->get_delivery_url(),
|
||||
'date_created' => wc_rest_prepare_date_response( $webhook->get_post_data()->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $webhook->get_post_data()->post_modified_gmt ),
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $post, $request ) );
|
||||
|
||||
/**
|
||||
* Filter webhook object returned from the REST API.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param WC_Webhook $webhook Webhook object used to create response.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $webhook, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Query args.
|
||||
*
|
||||
* @param array $args
|
||||
* @param WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function query_args( $args, $request ) {
|
||||
// Set post_status.
|
||||
switch ( $request['status'] ) {
|
||||
case 'active' :
|
||||
$args['post_status'] = 'publish';
|
||||
break;
|
||||
case 'paused' :
|
||||
$args['post_status'] = 'draft';
|
||||
break;
|
||||
case 'disabled' :
|
||||
$args['post_status'] = 'pending';
|
||||
break;
|
||||
default :
|
||||
$args['post_status'] = 'any';
|
||||
break;
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Webhook's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'webhook',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'description' => __( 'A friendly name for the webhook.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'status' => array(
|
||||
'description' => __( 'Webhook status.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => 'active',
|
||||
'enum' => array( 'active', 'paused', 'disabled' ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'sanitize_callback' => 'wc_is_webhook_valid_topic',
|
||||
),
|
||||
),
|
||||
'topic' => array(
|
||||
'description' => __( 'Webhook topic.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'resource' => array(
|
||||
'description' => __( 'Webhook resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'event' => array(
|
||||
'description' => __( 'Webhook event.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'hooks' => array(
|
||||
'description' => __( 'WooCommerce action names associated with the webhook.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
'delivery_url' => array(
|
||||
'description' => __( 'The URL where the webhook payload is delivered.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'secret' => array(
|
||||
'description' => __( "Secret key used to generate a hash of the delivered webhook and provided in the request headers. This will default is a MD5 hash from the current user's ID|username if not provided.", 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'edit' ),
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the webhook was created, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_modified' => array(
|
||||
'description' => __( "The date the webhook was last modified, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections of attachments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['status'] = array(
|
||||
'default' => 'all',
|
||||
'description' => __( 'Limit result set to webhooks assigned a specific status.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'enum' => array( 'all', 'active', 'paused', 'disabled' ),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
|
@ -123,6 +123,7 @@ class WC_API extends WC_Legacy_API {
|
|||
|
||||
/**
|
||||
* Include REST API classes.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
private function rest_api_includes() {
|
||||
|
@ -141,17 +142,39 @@ class WC_API extends WC_Legacy_API {
|
|||
// Abstract controllers.
|
||||
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-posts-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-shipping-zones-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-terms-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-shipping-zones-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-settings-api.php' );
|
||||
|
||||
// REST API controllers.
|
||||
// REST API v1 controllers.
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-coupons-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-customer-downloads-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-customers-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-orders-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-order-notes-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-order-refunds-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-attribute-terms-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-attributes-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-categories-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-reviews-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-shipping-classes-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-tags-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-products-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-report-sales-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-report-top-sellers-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-reports-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-tax-classes-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-taxes-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-webhook-deliveries.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-webhooks-controller.php' );
|
||||
|
||||
// REST API v2 controllers.
|
||||
include_once( dirname( __FILE__ ) . '/api/class-wc-rest-coupons-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/class-wc-rest-customer-downloads-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/class-wc-rest-customers-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/class-wc-rest-orders-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/class-wc-rest-order-notes-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/class-wc-rest-order-refunds-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/class-wc-rest-orders-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-attribute-terms-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-attributes-controller.php' );
|
||||
include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-categories-controller.php' );
|
||||
|
@ -187,6 +210,29 @@ class WC_API extends WC_Legacy_API {
|
|||
$this->register_wp_admin_settings();
|
||||
|
||||
$controllers = array(
|
||||
// v1 controllers.
|
||||
'WC_REST_Coupons_V1_Controller',
|
||||
'WC_REST_Customer_Downloads_V1_Controller',
|
||||
'WC_REST_Customers_V1_Controller',
|
||||
'WC_REST_Order_Notes_V1_Controller',
|
||||
'WC_REST_Order_Refunds_V1_Controller',
|
||||
'WC_REST_Orders_V1_Controller',
|
||||
'WC_REST_Product_Attribute_Terms_V1_Controller',
|
||||
'WC_REST_Product_Attributes_V1_Controller',
|
||||
'WC_REST_Product_Categories_V1_Controller',
|
||||
'WC_REST_Product_Reviews_V1_Controller',
|
||||
'WC_REST_Product_Shipping_Classes_V1_Controller',
|
||||
'WC_REST_Product_Tags_V1_Controller',
|
||||
'WC_REST_Products_V1_Controller',
|
||||
'WC_REST_Report_Sales_V1_Controller',
|
||||
'WC_REST_Report_Top_Sellers_V1_Controller',
|
||||
'WC_REST_Reports_V1_Controller',
|
||||
'WC_REST_Tax_Classes_V1_Controller',
|
||||
'WC_REST_Taxes_V1_Controller',
|
||||
'WC_REST_Webhook_Deliveries_V1_Controller',
|
||||
'WC_REST_Webhooks_V1_Controller',
|
||||
|
||||
// v2 controllers.
|
||||
'WC_REST_Coupons_Controller',
|
||||
'WC_REST_Customer_Downloads_Controller',
|
||||
'WC_REST_Customers_Controller',
|
||||
|
|
|
@ -26,9 +26,9 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v1/coupons', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/coupons/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/coupons/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -42,7 +42,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
$post_1 = get_post( $coupon_1->get_id() );
|
||||
$coupon_2 = WC_Helper_Coupon::create_coupon( 'dummycoupon-2' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) );
|
||||
$coupons = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -55,11 +55,11 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
'date_modified' => wc_rest_prepare_date_response( $post_1->post_modified_gmt ),
|
||||
'discount_type' => 'fixed_cart',
|
||||
'description' => 'This is a dummy coupon',
|
||||
'expiry_date' => '',
|
||||
'date_expires' => '',
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'exclude_product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => '',
|
||||
'usage_limit_per_user' => '',
|
||||
'limit_usage_to_x_items' => 0,
|
||||
|
@ -75,12 +75,12 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/coupons/' . $coupon_1->get_id() ),
|
||||
'href' => rest_url( '/wc/v2/coupons/' . $coupon_1->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/coupons' ),
|
||||
'href' => rest_url( '/wc/v2/coupons' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -93,7 +93,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_coupons_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -105,7 +105,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$post = get_post( $coupon->get_id() );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons/' . $coupon->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -117,11 +117,11 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt ),
|
||||
'discount_type' => 'fixed_cart',
|
||||
'description' => 'This is a dummy coupon',
|
||||
'expiry_date' => null,
|
||||
'date_expires' => null,
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'exclude_product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => null,
|
||||
'usage_limit_per_user' => null,
|
||||
'limit_usage_to_x_items' => 0,
|
||||
|
@ -143,7 +143,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons/0' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -154,7 +154,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons/' . $coupon->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -164,7 +164,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_create_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/coupons' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request->set_body_params( array(
|
||||
'code' => 'test',
|
||||
'amount' => '5.00',
|
||||
|
@ -184,11 +184,11 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
'date_modified' => $data['date_modified'],
|
||||
'discount_type' => 'fixed_product',
|
||||
'description' => 'Test',
|
||||
'expiry_date' => null,
|
||||
'date_expires' => null,
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'exclude_product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => 10,
|
||||
'usage_limit_per_user' => null,
|
||||
'limit_usage_to_x_items' => 0,
|
||||
|
@ -212,7 +212,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
// test no code...
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/coupons' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request->set_body_params( array(
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
|
@ -231,7 +231,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( 0 );
|
||||
|
||||
// test no code...
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/coupons' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request->set_body_params( array(
|
||||
'code' => 'fail',
|
||||
'amount' => '5.00',
|
||||
|
@ -252,13 +252,13 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$post = get_post( $coupon->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons/' . $coupon->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'This is a dummy coupon', $data['description'] );
|
||||
$this->assertEquals( 'fixed_cart', $data['discount_type'] );
|
||||
$this->assertEquals( '1.00', $data['amount'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/coupons/' . $coupon->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
|
@ -278,7 +278,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/coupons/0' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/0' );
|
||||
$request->set_body_params( array(
|
||||
'code' => 'tester',
|
||||
'amount' => '10.00',
|
||||
|
@ -299,7 +299,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$post = get_post( $coupon->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/coupons/' . $coupon->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
|
@ -316,7 +316,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
public function test_delete_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/coupons/' . $coupon->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -328,7 +328,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_delete_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/coupons/0' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
|
@ -342,7 +342,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
public function test_delete_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/coupons/' . $coupon->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -360,7 +360,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
$coupon_3 = WC_Helper_Coupon::create_coupon( 'dummycoupon-3' );
|
||||
$coupon_4 = WC_Helper_Coupon::create_coupon( 'dummycoupon-4' );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/coupons/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -388,7 +388,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $coupon_2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $coupon_3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/coupons' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/coupons' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
|
@ -401,7 +401,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_coupon_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/coupons' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/coupons' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
@ -414,11 +414,11 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'discount_type', $properties );
|
||||
$this->assertArrayHasKey( 'amount', $properties );
|
||||
$this->assertArrayHasKey( 'expiry_date', $properties );
|
||||
$this->assertArrayHasKey( 'date_expires', $properties );
|
||||
$this->assertArrayHasKey( 'usage_count', $properties );
|
||||
$this->assertArrayHasKey( 'individual_use', $properties );
|
||||
$this->assertArrayHasKey( 'product_ids', $properties );
|
||||
$this->assertArrayHasKey( 'exclude_product_ids', $properties );
|
||||
$this->assertArrayHasKey( 'excluded_product_ids', $properties );
|
||||
$this->assertArrayHasKey( 'usage_limit', $properties );
|
||||
$this->assertArrayHasKey( 'usage_limit_per_user', $properties );
|
||||
$this->assertArrayHasKey( 'limit_usage_to_x_items', $properties );
|
||||
|
|
|
@ -24,9 +24,9 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( '/wc/v1/customers', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/customers/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/customers/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/customers', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/customers/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/customers/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,7 +40,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
$customer_1 = WC_Helper_Customer::create_customer();
|
||||
WC_Helper_Customer::create_customer( 'test2', 'test2', 'test2@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/customers' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/customers' );
|
||||
$request->set_query_params( array(
|
||||
'orderby' => 'id',
|
||||
) );
|
||||
|
@ -91,12 +91,12 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/customers/' . $customer_1->get_id() . '' ),
|
||||
'href' => rest_url( '/wc/v2/customers/' . $customer_1->get_id() . '' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/customers' ),
|
||||
'href' => rest_url( '/wc/v2/customers' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -110,7 +110,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_customers_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/customers' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -123,7 +123,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( 1 );
|
||||
|
||||
// Test just the basics first..
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/customers' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test',
|
||||
'password' => 'test123',
|
||||
|
@ -174,7 +174,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
), $data );
|
||||
|
||||
// Test extra data
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/customers' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test2',
|
||||
'password' => 'test123',
|
||||
|
@ -235,7 +235,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
), $data );
|
||||
|
||||
// Test without required field
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/customers' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test3',
|
||||
'first_name' => 'Test',
|
||||
|
@ -254,7 +254,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_create_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/customers' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test_without_permission',
|
||||
'password' => 'test123',
|
||||
|
@ -272,7 +272,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'get_customer_test', 'test123', 'get_customer_test@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/customers/' . $customer->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( array(
|
||||
|
@ -324,7 +324,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'get_customer_test_without_permission', 'test123', 'get_customer_test_without_permission@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/customers/' . $customer->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -335,7 +335,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/customers/0' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -348,12 +348,12 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'update_customer_test', 'test123', 'update_customer_test@woo.local' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/customers/' . $customer->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'update_customer_test', $data['username'] );
|
||||
$this->assertEquals( 'update_customer_test@woo.local', $data['email'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/customers/' . $customer->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'email' => 'updated_email@woo.local',
|
||||
'first_name' => 'UpdatedTest',
|
||||
|
@ -373,7 +373,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'update_customer_test_without_permission', 'test123', 'update_customer_test_without_permission@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/customers/' . $customer->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -384,7 +384,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_update_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/customers/0' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -397,7 +397,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
public function test_delete_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'delete_customer_test', 'test123', 'delete_customer_test@woo.local' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/customers/' . $customer->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -410,7 +410,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_delete_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/customers/0' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
@ -424,7 +424,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
public function test_delete_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'delete_customer_test_without_permission', 'test123', 'delete_customer_test_without_permission@woo.local' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/customers/' . $customer->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -443,7 +443,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
$customer_3 = WC_Helper_Customer::create_customer( 'test_batch_customer3', 'test123', 'test_batch_customer3@woo.local' );
|
||||
$customer_4 = WC_Helper_Customer::create_customer( 'test_batch_customer4', 'test123', 'test_batch_customer4@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/customers/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -472,7 +472,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $customer_2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $customer_3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/customers' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
|
@ -486,7 +486,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_customer_schema() {
|
||||
wp_set_current_user( 1 );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/customers' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
|
|
@ -41,7 +41,7 @@ class WC_Tests_API_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.6.0
|
||||
*/
|
||||
public function test_wc_rest_validate_reports_request_arg() {
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/foo', array(
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/foo', array(
|
||||
'args' => array(
|
||||
'date' => array(
|
||||
'type' => 'string',
|
||||
|
|
|
@ -30,9 +30,9 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v1/orders', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/orders/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/orders/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/orders', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/orders/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/orders/(?P<id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -57,7 +57,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$this->orders[] = WC_Helper_Order::create_order( $this->user );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/orders' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) );
|
||||
$orders = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -73,7 +73,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_items_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$this->orders[] = WC_Helper_Order::create_order();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/orders' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
$this->stoppit_and_tidyup();
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$this->orders[] = $order;
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/orders/' . $order->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -102,7 +102,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$this->orders[] = $order;
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/orders/' . $order->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
$this->stoppit_and_tidyup();
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_item_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/orders/99999999' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/99999999' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -124,7 +124,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
public function test_create_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/orders' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
|
||||
$request->set_body_params( array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
|
@ -207,7 +207,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// non-existant customer
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/orders' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
|
||||
$request->set_body_params( array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
|
@ -262,7 +262,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
|
@ -288,7 +288,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_order_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
|
@ -306,7 +306,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_update_order_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/orders/999999' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/999999' );
|
||||
$request->set_body_params( array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
|
@ -325,7 +325,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
public function test_delete_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -339,7 +339,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
public function test_delete_order_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -353,7 +353,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_delete_order_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/orders/9999999' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/9999999' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
@ -369,7 +369,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$order2 = WC_Helper_Order::create_order();
|
||||
$order3 = WC_Helper_Order::create_order();
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/orders/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -389,7 +389,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $order2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $order3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/orders' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/orders' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
|
@ -406,7 +406,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
public function test_order_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/orders/' . $order->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
|
|
@ -26,8 +26,8 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v1/payment_gateways', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/payment_gateways/(?P<id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/payment_gateways', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/payment_gateways/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,7 +38,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_payment_gateways() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/payment_gateways' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) );
|
||||
$gateways = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -54,12 +54,12 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/payment_gateways/cheque' ),
|
||||
'href' => rest_url( '/wc/v2/payment_gateways/cheque' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/payment_gateways' ),
|
||||
'href' => rest_url( '/wc/v2/payment_gateways' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -73,7 +73,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_payment_gateways_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/payment_gateways' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_payment_gateway() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/payment_gateways/paypal' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -108,7 +108,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_payment_gateway_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/payment_gateways/paypal' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -119,7 +119,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_payment_gateway_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/payment_gateways/totally_fake_method' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/totally_fake_method' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -132,7 +132,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
// Test defaults
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/payment_gateways/paypal' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] );
|
||||
|
@ -140,7 +140,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test updating single setting
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'email' => 'woo@woo.local',
|
||||
|
@ -155,7 +155,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test updating multiple settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'testmode' => 'yes',
|
||||
|
@ -171,7 +171,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// Test other parameters, and recheck settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
|
@ -186,7 +186,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test bogus
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'paymentaction' => 'afasfasf',
|
||||
|
@ -195,7 +195,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'paymentaction' => 'authorization',
|
||||
|
@ -213,7 +213,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_update_payment_gateway_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'testmode' => 'yes',
|
||||
|
@ -231,7 +231,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_update_payment_gateway_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/payment_gateways/totally_fake_method' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/totally_fake_method' );
|
||||
$request->set_body_params( array(
|
||||
'enabled' => true,
|
||||
) );
|
||||
|
@ -247,7 +247,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
public function test_payment_gateway_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/payment_gateways' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/payment_gateways' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
|
|
@ -26,8 +26,8 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v1/products/(?P<product_id>[\d]+)/reviews', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/products/(?P<product_id>[\d]+)/reviews/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/reviews', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/reviews/(?P<id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -43,7 +43,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/reviews' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/reviews' ) );
|
||||
$product_reviews = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -59,17 +59,17 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/products/' . $product->get_id() . '/reviews/' . $review_id ),
|
||||
'href' => rest_url( '/wc/v2/products/' . $product->get_id() . '/reviews/' . $review_id ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/products/' . $product->get_id() . '/reviews' ),
|
||||
'href' => rest_url( '/wc/v2/products/' . $product->get_id() . '/reviews' ),
|
||||
),
|
||||
),
|
||||
'up' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/products/' . $product->get_id() ),
|
||||
'href' => rest_url( '/wc/v2/products/' . $product->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -84,7 +84,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_product_reviews_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/reviews' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/reviews' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -95,7 +95,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_product_reviews_invalid_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/0/reviews' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/0/reviews' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -109,7 +109,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/reviews/' . $product_review_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/reviews/' . $product_review_id ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -133,7 +133,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/reviews/' . $product_review_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/reviews/' . $product_review_id ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -145,7 +145,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/reviews/0' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/reviews/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -157,7 +157,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
public function test_create_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products/' . $product->get_id() . '/reviews' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/reviews' );
|
||||
$request->set_body_params( array(
|
||||
'review' => 'Hello world.',
|
||||
'name' => 'Admin',
|
||||
|
@ -189,7 +189,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// missing review
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products/' . $product->get_id() . '/reviews' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/reviews' );
|
||||
$request->set_body_params( array(
|
||||
'name' => 'Admin',
|
||||
'email' => 'woo@woo.local',
|
||||
|
@ -200,7 +200,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// missing name
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products/' . $product->get_id() . '/reviews' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/reviews' );
|
||||
$request->set_body_params( array(
|
||||
'review' => 'Hello world.',
|
||||
'email' => 'woo@woo.local',
|
||||
|
@ -211,7 +211,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// missing email
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products/' . $product->get_id() . '/reviews' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/reviews' );
|
||||
$request->set_body_params( array(
|
||||
'review' => 'Hello world.',
|
||||
'name' => 'Admin',
|
||||
|
@ -232,14 +232,14 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/reviews/' . $product_review_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/reviews/' . $product_review_id ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'Review content here', $data['review'] );
|
||||
$this->assertEquals( 'admin', $data['name'] );
|
||||
$this->assertEquals( 'woo@woo.local', $data['email'] );
|
||||
$this->assertEquals( 0, $data['rating'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->get_id() . '/reviews/' . $product_review_id );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/reviews/' . $product_review_id );
|
||||
$request->set_body_params( array(
|
||||
'review' => 'Hello world - updated.',
|
||||
'name' => 'Justin',
|
||||
|
@ -264,7 +264,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->get_id() . '/reviews/' . $product_review_id );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/reviews/' . $product_review_id );
|
||||
$request->set_body_params( array(
|
||||
'review' => 'Hello world.',
|
||||
'name' => 'Admin',
|
||||
|
@ -285,7 +285,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->get_id() . '/reviews/0' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/reviews/0' );
|
||||
$request->set_body_params( array(
|
||||
'review' => 'Hello world.',
|
||||
'name' => 'Admin',
|
||||
|
@ -307,7 +307,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/products/' . $product->get_id() . '/reviews/' . $product_review_id );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/reviews/' . $product_review_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -323,7 +323,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/products/' . $product->get_id() . '/reviews/' . $product_review_id );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/reviews/' . $product_review_id );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -339,7 +339,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/products/' . $product->get_id() . '/reviews/0' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/reviews/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
|
@ -358,7 +358,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$review_3_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$review_4_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products/' . $product->get_id() . '/reviews/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/reviews/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -386,7 +386,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $review_2_id, $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $review_3_id, $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/reviews' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/reviews' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
|
@ -401,7 +401,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
public function test_product_review_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/products/' . $product->get_id() . '/reviews' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() . '/reviews' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
|
|
@ -26,9 +26,9 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v1/products/(?P<product_id>[\d]+)/variations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/products/(?P<product_id>[\d]+)/variations/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/products/(?P<product_id>[\d]+)/variations/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -39,7 +39,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_variations() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/variations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $variations ) );
|
||||
|
@ -56,7 +56,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_variations_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/variations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
$product->delete( true );
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -91,7 +91,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
$product->delete( true );
|
||||
}
|
||||
|
@ -107,12 +107,12 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/variations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 1, count( $variations ) );
|
||||
$product->delete( true );
|
||||
|
@ -129,7 +129,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -144,7 +144,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_delete_variation_with_invalid_id() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/products/' . $product->get_id() . '/variations/0' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
@ -162,7 +162,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variation['sku'] );
|
||||
|
@ -170,7 +170,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEmpty( $variation['sale_price'] );
|
||||
$this->assertEquals( 'small', $variation['attributes'][0]['option'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params( array(
|
||||
'sku' => 'FIXED-SKU',
|
||||
'sale_price' => '8',
|
||||
|
@ -204,7 +204,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params( array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
) );
|
||||
|
@ -221,7 +221,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_variation_with_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->get_id() . '/variations/0' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/0' );
|
||||
$request->set_body_params( array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
) );
|
||||
|
@ -239,11 +239,11 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/variations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 2, count( $variations ) );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products/' . $product->get_id() . '/variations' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$request->set_body_params( array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
|
@ -260,7 +260,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $variation['sku'] );
|
||||
$this->assertEquals( 'medium', $variation['attributes'][0]['option'] );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/variations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 3, count( $variations ) );
|
||||
$product->delete( true );
|
||||
|
@ -275,7 +275,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products/' . $product->get_id() . '/variations' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$request->set_body_params( array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
|
@ -294,7 +294,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products/' . $product->get_id() . '/variations/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -323,7 +323,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'medium', $data['create'][0]['attributes'][0]['option'] );
|
||||
$this->assertEquals( $children[1], $data['delete'][0]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() . '/variations' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
|
@ -339,12 +339,12 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_variation_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/products/' . $product->get_id() . '/variations' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 33, count( $properties ) );
|
||||
$this->assertEquals( 35, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
|
@ -378,6 +378,8 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertArrayHasKey( 'shipping_class_id', $properties );
|
||||
$this->assertArrayHasKey( 'image', $properties );
|
||||
$this->assertArrayHasKey( 'attributes', $properties );
|
||||
$this->assertArrayHasKey( 'menu_order', $properties );
|
||||
$this->assertArrayHasKey( 'meta_data', $properties );
|
||||
$product->delete( true );
|
||||
}
|
||||
|
||||
|
|
|
@ -26,9 +26,9 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v1/products', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/products/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/products/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -41,7 +41,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
WC_Helper_Product::create_external_product();
|
||||
sleep( 1 ); // So both proudcts have different timestamps.
|
||||
WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -61,7 +61,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_products_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -73,7 +73,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$simple = WC_Helper_Product::create_external_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $simple->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $simple->get_id() ) );
|
||||
$product = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -95,7 +95,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -108,12 +108,12 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/products/' . $product->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 0, count( $variations ) );
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_delete_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/products/' . $product->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -139,7 +139,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_delete_product_with_invalid_id() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/products/0' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
@ -155,14 +155,14 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
|
||||
// test simple products
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU', $data['sku'] );
|
||||
$this->assertEquals( 10, $data['regular_price'] );
|
||||
$this->assertEmpty( $data['sale_price'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'sku' => 'FIXED-SKU',
|
||||
'sale_price' => '8',
|
||||
|
@ -182,12 +182,12 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
|
||||
// test variable product (varations are tested in product-variations.php)
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( array( 'small' ), $data['attributes'][0]['options'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'attributes' => array(
|
||||
array( 'id' => 0, 'name' => 'pa_color', 'options' => array( 'red', 'yellow' ), 'visible' => false, 'variation' => 1 ),
|
||||
|
@ -203,13 +203,13 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
|
||||
// test external product
|
||||
$product = WC_Helper_Product::create_external_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products/' . $product->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'Buy external product', $data['button_text'] );
|
||||
$this->assertEquals( 'http://woocommerce.com', $data['external_url'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'button_text' => 'Test API Update',
|
||||
'external_url' => 'http://automattic.com',
|
||||
|
@ -230,7 +230,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/' . $product->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
) );
|
||||
|
@ -247,7 +247,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_product_with_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/products/0' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/0' );
|
||||
$request->set_body_params( array(
|
||||
'sku' => 'FIXED-SKU-INVALID-ID',
|
||||
) );
|
||||
|
@ -264,7 +264,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_create_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products/shipping_classes' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/shipping_classes' );
|
||||
$request->set_body_params( array(
|
||||
'name' => 'Test',
|
||||
) );
|
||||
|
@ -273,7 +273,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
$shipping_class_id = $data['id'];
|
||||
|
||||
// Create simple
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params( array(
|
||||
'type' => 'simple',
|
||||
'name' => 'Test Simple Product',
|
||||
|
@ -293,7 +293,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $shipping_class_id, $data['shipping_class_id'] );
|
||||
|
||||
// Create external
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params( array(
|
||||
'type' => 'external',
|
||||
'name' => 'Test External Product',
|
||||
|
@ -315,7 +315,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'http://wordpress.org', $data['external_url'] );
|
||||
|
||||
// Create variable
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params( array(
|
||||
'type' => 'variable',
|
||||
'name' => 'Test Variable Product',
|
||||
|
@ -341,7 +341,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'variable', $data['type'] );
|
||||
$this->assertEquals( array( 'small', 'medium' ), $data['attributes'][0]['options'] );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/products' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$products = $response->get_data();
|
||||
$this->assertEquals( 3, count( $products ) );
|
||||
}
|
||||
|
@ -354,7 +354,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_create_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params( array(
|
||||
'name' => 'Test Product',
|
||||
'regular_price' => '12',
|
||||
|
@ -370,7 +370,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_2 = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/products/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -408,7 +408,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'simple', $data['create'][1]['type'] );
|
||||
$this->assertEquals( $product_2->get_id(), $data['delete'][0]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
|
@ -436,7 +436,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
}
|
||||
|
||||
// Test filtering with filter[post_status]=publish
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_param( 'filter', array( 'post_status' => 'publish' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
@ -447,7 +447,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
}
|
||||
|
||||
// Test filtering with filter[post_status]=draft
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_param( 'filter', array( 'post_status' => 'draft' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
@ -458,7 +458,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
}
|
||||
|
||||
// Test filtering with status=publish
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_param( 'status', 'publish' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
@ -469,7 +469,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
}
|
||||
|
||||
// Test filtering with status=draft
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_param( 'status', 'draft' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
@ -481,7 +481,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
|
||||
// Test filtering with status=draft and filter[post_status]=publish
|
||||
// filter[post_status]=publish should win
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_param( 'status', 'draft' );
|
||||
$request->set_param( 'filter', array( 'post_status' => 'publish' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
@ -493,7 +493,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
}
|
||||
|
||||
// Test filtering with no filters - which should return 'any' (all 8)
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/products' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
|
@ -508,11 +508,11 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
|||
public function test_product_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/products/' . $product->get_id() );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 61, count( $properties ) );
|
||||
$this->assertEquals( 62, count( $properties ) );
|
||||
$product->delete( true );
|
||||
}
|
||||
|
||||
|
|
|
@ -27,9 +27,9 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v1/settings', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/settings/(?P<group>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/settings/(?P<group>[\w-]+)/(?P<id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/settings', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group>[\w-]+)/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,7 +40,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_groups() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -54,7 +54,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'item' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/settings/test' ),
|
||||
'href' => rest_url( '/wc/v2/settings/test' ),
|
||||
'embeddable' => true,
|
||||
),
|
||||
),
|
||||
|
@ -70,7 +70,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'item' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/settings/sub-test' ),
|
||||
'href' => rest_url( '/wc/v2/settings/sub-test' ),
|
||||
'embeddable' => true,
|
||||
),
|
||||
),
|
||||
|
@ -86,7 +86,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_groups_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -101,7 +101,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
|
||||
remove_all_filters( 'woocommerce_settings_groups' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$this->assertEquals( 500, $response->get_status() );
|
||||
|
||||
WC_Helper_Settings::register();
|
||||
|
@ -113,7 +113,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
* @since 2.7.0
|
||||
*/
|
||||
public function test_get_group_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/settings' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
@ -131,7 +131,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
* @since 2.7.0
|
||||
*/
|
||||
public function test_get_setting_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/settings/test/woocommerce_shop_page_display' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/settings/test/woocommerce_shop_page_display' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
@ -160,15 +160,15 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertIsWPError( $result );
|
||||
|
||||
// test getting a group that does not exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/not-real' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/not-real' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting the 'invalid' group
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/invalid' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/invalid' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting a valid group
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/general' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -182,19 +182,19 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/settings/general/woocommerce_demo_store' ),
|
||||
'href' => rest_url( '/wc/v2/settings/general/woocommerce_demo_store' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/settings/general' ),
|
||||
'href' => rest_url( '/wc/v2/settings/general' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data );
|
||||
|
||||
// test getting a valid group with settings attached to it
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
$this->assertEquals( 'woocommerce_shop_page_display', $data[0]['id'] );
|
||||
|
@ -209,7 +209,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_group_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/coupon-data' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/coupon-data' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -222,12 +222,12 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
// test defaults first
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test/woocommerce_shop_page_display' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( '', $data['value'] );
|
||||
|
||||
// test updating shop display setting
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'both',
|
||||
) );
|
||||
|
@ -237,7 +237,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'both', $data['value'] );
|
||||
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'subcategories',
|
||||
) );
|
||||
|
@ -247,7 +247,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'subcategories', $data['value'] );
|
||||
$this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => '',
|
||||
) );
|
||||
|
@ -267,12 +267,12 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
// test defaults first
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( '', $data[0]['value'] );
|
||||
|
||||
// test setting both at once
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/settings/test/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -287,7 +287,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
// test updating one, but making sure the other value stays the same
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/settings/test/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -311,17 +311,17 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
// test getting an invalid setting from a group that does not exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/not-real/woocommerce_shop_page_display' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/not-real/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting an invalid setting from a group that does exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/invalid/invalid' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/invalid/invalid' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting a valid setting
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test/woocommerce_shop_page_display' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -341,7 +341,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_setting_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test/woocommerce_shop_page_display' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -399,7 +399,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_setting_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'subcategories',
|
||||
) );
|
||||
|
@ -416,7 +416,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_settings_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/settings/test/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -438,7 +438,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_setting_bad_setting_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/settings/test/invalid' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/test/invalid' );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'test',
|
||||
) );
|
||||
|
@ -455,7 +455,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
// Make sure the group is properly registered
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/products' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertTrue( is_array( $data ) );
|
||||
$this->assertContains( array(
|
||||
|
@ -469,25 +469,25 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/settings/products/woocommerce_downloads_require_login' ),
|
||||
'href' => rest_url( '/wc/v2/settings/products/woocommerce_downloads_require_login' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/settings/products' ),
|
||||
'href' => rest_url( '/wc/v2/settings/products' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data );
|
||||
|
||||
// test get single
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/products/woocommerce_dimension_unit' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products/woocommerce_dimension_unit' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'cm', $data['default'] );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'yd',
|
||||
) );
|
||||
|
@ -506,7 +506,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_email_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/email_new_order' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order' ) );
|
||||
$settings = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -522,19 +522,19 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/settings/email_new_order/recipient' ),
|
||||
'href' => rest_url( '/wc/v2/settings/email_new_order/recipient' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/settings/email_new_order' ),
|
||||
'href' => rest_url( '/wc/v2/settings/email_new_order' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $settings );
|
||||
|
||||
// test get single
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/email_new_order/subject' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order/subject' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( array(
|
||||
|
@ -548,7 +548,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
), $setting );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'email_new_order', 'subject' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_new_order', 'subject' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'This is my subject',
|
||||
) );
|
||||
|
@ -566,14 +566,14 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
), $setting );
|
||||
|
||||
// test updating another subject and making sure it works with a "similar" id
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wc/v1/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEmpty( $setting['value'] );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'This is my new subject',
|
||||
) );
|
||||
|
@ -583,7 +583,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'This is my new subject', $setting['value'] );
|
||||
|
||||
// make sure the other is what we left it
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/email_new_order/subject' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order/subject' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'This is my subject', $setting['value'] );
|
||||
|
@ -598,7 +598,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
// test bogus value
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'not_yes_or_no',
|
||||
) );
|
||||
|
@ -606,7 +606,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// test yes
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'yes',
|
||||
) );
|
||||
|
@ -614,7 +614,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
// test no
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'no',
|
||||
) );
|
||||
|
@ -631,7 +631,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
// not a valid option
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'billing2',
|
||||
) );
|
||||
|
@ -639,7 +639,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// valid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'billing',
|
||||
) );
|
||||
|
@ -655,11 +655,11 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_validation_multiselect() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v1/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEmpty( $setting['value'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => array( 'AX', 'DZ', 'MMM' ),
|
||||
) );
|
||||
|
@ -676,12 +676,12 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_validation_select() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v1/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'kg', $setting['value'] );
|
||||
|
||||
// invalid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'pounds', // invalid, should be lbs
|
||||
) );
|
||||
|
@ -689,7 +689,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// valid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'lbs', // invalid, should be lbs
|
||||
) );
|
||||
|
@ -706,12 +706,12 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
public function test_validation_image_width() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v1/settings/%s/%s', 'products', 'shop_thumbnail_image_size' ) ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'products', 'shop_thumbnail_image_size' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( array( 'width' => 180, 'height' => 180, 'crop' => true ), $setting['value'] );
|
||||
|
||||
// test bogus
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'products', 'shop_thumbnail_image_size' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'shop_thumbnail_image_size' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => array(
|
||||
'width' => 400,
|
||||
|
@ -723,7 +723,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$setting = $response->get_data();
|
||||
$this->assertEquals( array( 'width' => 400, 'height' => 200, 'crop' => true ), $setting['value'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'products', 'shop_thumbnail_image_size' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'shop_thumbnail_image_size' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => array(
|
||||
'width' => 200,
|
||||
|
|
|
@ -26,8 +26,8 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v1/shipping_methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/shipping_methods/(?P<id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping_methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping_methods/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,7 +38,7 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_shipping_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping_methods' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods' ) );
|
||||
$methods = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -49,12 +49,12 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping_methods/free_shipping' ),
|
||||
'href' => rest_url( '/wc/v2/shipping_methods/free_shipping' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping_methods' ),
|
||||
'href' => rest_url( '/wc/v2/shipping_methods' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -68,7 +68,7 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_shipping_methods_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping_methods' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,7 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_shipping_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping_methods/local_pickup' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/local_pickup' ) );
|
||||
$method = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -99,7 +99,7 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_shipping_method_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping_methods/local_pickup' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/local_pickup' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -110,7 +110,7 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_shipping_method_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping_methods/fake_method' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/fake_method' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -122,7 +122,7 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
|||
public function test_shipping_method_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/shipping_methods' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping_methods' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
|
|
@ -62,11 +62,11 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v1/shipping/zones', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/shipping/zones/(?P<id>[\d-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/shipping/zones/(?P<id>[\d-]+)/locations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/shipping/zones/(?P<zone_id>[\d-]+)/methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/shipping/zones/(?P<zone_id>[\d-]+)/methods/(?P<instance_id>[\d-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d-]+)/locations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d-]+)/methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d-]+)/methods/(?P<instance_id>[\d-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -77,7 +77,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
// "Rest of the World" zone exists by default
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -89,17 +89,17 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/0' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/0' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/0/locations' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/0/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -108,7 +108,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
// Create a zone and make sure it's in the response
|
||||
$this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -120,17 +120,17 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/1' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/1' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/1/locations' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/1/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -144,7 +144,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_shipping_zones_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -157,7 +157,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
add_filter( 'wc_shipping_enabled', '__return_false' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
remove_filter( 'wc_shipping_enabled', '__return_false' );
|
||||
|
@ -168,7 +168,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
* @since 2.7.0
|
||||
*/
|
||||
public function test_get_shipping_zone_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/shipping/zones' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping/zones' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
@ -188,7 +188,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
public function test_create_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/shipping/zones' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
|
||||
$request->set_body_params( array(
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
|
@ -204,17 +204,17 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $data['id'] ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $data['id'] . '/locations' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -228,7 +228,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
public function test_create_shipping_zone_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/shipping/zones' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
|
||||
$request->set_body_params( array(
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
|
@ -246,7 +246,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/shipping/zones/' . $zone->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
|
@ -262,17 +262,17 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -286,7 +286,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/shipping/zones/555555' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/555555' );
|
||||
$request->set_body_params( array(
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
|
@ -304,7 +304,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/shipping/zones/' . $zone->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
@ -320,7 +320,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( 0 );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/shipping/zones/' . $zone->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -332,7 +332,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_delete_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/shipping/zones/555555' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/555555' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
@ -345,7 +345,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones/' . $zone->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -356,17 +356,17 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -380,7 +380,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_single_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones/1' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
@ -400,7 +400,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
),
|
||||
) );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones/' . $zone->get_id() . '/locations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -412,12 +412,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -432,7 +432,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_locations_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones/1/locations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/locations' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
@ -446,7 +446,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/shipping/zones/' . $zone->get_id() . '/locations' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' );
|
||||
$request->add_header( 'Content-Type', 'application/json' );
|
||||
$request->set_body( json_encode( array(
|
||||
array(
|
||||
|
@ -475,12 +475,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -491,12 +491,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -511,7 +511,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_locations_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v1/shipping/zones/1/locations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/1/locations' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
@ -548,7 +548,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$settings[ $id ] = $data;
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ) );
|
||||
$data = $response->get_data();
|
||||
$expected = array(
|
||||
'instance_id' => $instance_id,
|
||||
|
@ -562,17 +562,17 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods' ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -582,7 +582,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertContains( $expected, $data );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -596,11 +596,11 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_methods_invalid_zone_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones/1/methods' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones/1/methods/1' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
@ -613,7 +613,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods/1' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
@ -631,7 +631,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$method = $methods[ $instance_id ];
|
||||
|
||||
// Test defaults
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
|
@ -643,7 +643,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( '0', $data['settings']['cost']['value'] );
|
||||
|
||||
// Update a single value
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'cost' => 5,
|
||||
|
@ -660,7 +660,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( '5', $data['settings']['cost']['value'] );
|
||||
|
||||
// Test multiple settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'cost' => 10,
|
||||
|
@ -678,7 +678,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( '10', $data['settings']['cost']['value'] );
|
||||
|
||||
// Test bogus
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'cost' => 10,
|
||||
|
@ -692,7 +692,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$this->assertTrue( $data['enabled'] );
|
||||
$this->assertEquals( 1, $data['order'] );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params( array(
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
|
@ -713,7 +713,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
public function test_create_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' );
|
||||
$request->set_body_params( array(
|
||||
'method_id' => 'flat_rate',
|
||||
'enabled' => false,
|
||||
|
@ -738,7 +738,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
|
|
@ -22,9 +22,9 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v1/system_status', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/system_status/tools', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v1/system_status/tools/(?P<id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status/tools', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status/tools/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,7 +34,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_system_status_info_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_system_status_info_returns_root_properties() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'environment', $data );
|
||||
|
@ -65,7 +65,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_system_status_info_environment() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$environment = (array) $data['environment'];
|
||||
|
||||
|
@ -86,7 +86,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_system_status_info_database() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$database = (array) $data['database'];
|
||||
|
||||
|
@ -106,7 +106,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
|
||||
$actual_plugins = array( 'hello.php' );
|
||||
update_option( 'active_plugins', $actual_plugins );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
update_option( 'active_plugins', array() );
|
||||
|
||||
$data = $response->get_data();
|
||||
|
@ -125,7 +125,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
wp_set_current_user( $this->user );
|
||||
$active_theme = wp_get_theme();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$theme = (array) $data['theme'];
|
||||
|
||||
|
@ -147,7 +147,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
$term_response[ $term->slug ] = strtolower( $term->name );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$settings = (array) $data['settings'];
|
||||
|
||||
|
@ -165,7 +165,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
public function test_get_system_status_info_security() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$settings = (array) $data['security'];
|
||||
|
||||
|
@ -181,7 +181,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_system_status_info_pages() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$pages = $data['pages'];
|
||||
$this->assertEquals( 4, count( $pages ) );
|
||||
|
@ -193,7 +193,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
* @since 2.7.0
|
||||
*/
|
||||
public function test_system_status_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/system_status' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
@ -218,7 +218,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
$tools_controller = new WC_REST_System_Status_Tools_Controller;
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -232,7 +232,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'item' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v1/system_status/tools/reset_tracking' ),
|
||||
'href' => rest_url( '/wc/v2/system_status/tools/reset_tracking' ),
|
||||
'embeddable' => true,
|
||||
),
|
||||
),
|
||||
|
@ -247,7 +247,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_system_status_tools_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -263,7 +263,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
$raw_tools = $tools_controller->get_tools();
|
||||
$raw_tool = $raw_tools['recount_terms'];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools/recount_terms' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -281,7 +281,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_get_system_status_tool_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/system_status/tools/recount_terms' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -297,7 +297,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
$raw_tools = $tools_controller->get_tools();
|
||||
$raw_tool = $raw_tools['recount_terms'];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v1/system_status/tools/recount_terms' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'recount_terms', $data['id'] );
|
||||
|
@ -306,7 +306,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
|
||||
$this->assertTrue( $data['success'] );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v1/system_status/tools/not_a_real_tool' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/not_a_real_tool' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -317,7 +317,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_execute_system_status_tool_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v1/system_status/tools/recount_terms' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -327,7 +327,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
* @since 2.7.0
|
||||
*/
|
||||
public function test_system_status_tool_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/system_status/tools' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status/tools' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
|
Loading…
Reference in New Issue