Standardisation of prepare_item and prepare_links
This commit is contained in:
parent
5bd17af0da
commit
d2e4c199f4
|
@ -49,6 +49,15 @@ abstract class AbstractController extends WP_REST_Controller {
|
||||||
*/
|
*/
|
||||||
protected $resource_type = '';
|
protected $resource_type = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register route for items requests.
|
* Register route for items requests.
|
||||||
*
|
*
|
||||||
|
@ -246,4 +255,63 @@ abstract class AbstractController extends WP_REST_Controller {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get context for the request.
|
||||||
|
*
|
||||||
|
* @param \WP_REST_Request $request Full details about the request.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function get_request_context( $request ) {
|
||||||
|
return ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare a single item for response.
|
||||||
|
*
|
||||||
|
* @param mixed $item Object used to create response.
|
||||||
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return \WP_REST_Response $response Response data.
|
||||||
|
*/
|
||||||
|
public function prepare_item_for_response( $item, $request ) {
|
||||||
|
$context = $this->get_request_context( $request );
|
||||||
|
$fields = $this->get_fields_for_response( $request );
|
||||||
|
$data = array_intersect_key( $this->get_data_for_response( $item, $request ), array_flip( $fields ) );
|
||||||
|
$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 object returned from the REST API.
|
||||||
|
*
|
||||||
|
* @param \WP_REST_Response $response The response object.
|
||||||
|
* @param mixed $item Object used to create response.
|
||||||
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
*/
|
||||||
|
return apply_filters( "woocommerce_rest_prepare_{$this->singular}", $response, $item, $request );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get data for this object in the format of this endpoint's schema.
|
||||||
|
*
|
||||||
|
* @param mixed $object Object to prepare.
|
||||||
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return mixed Array of data in the correct format.
|
||||||
|
*/
|
||||||
|
protected function get_data_for_response( $object, $request ) {
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare links for the request.
|
||||||
|
*
|
||||||
|
* @param mixed $item Object to prepare.
|
||||||
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function prepare_links( $item, $request ) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -531,14 +531,14 @@ abstract class AbstractObjectsController extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param \WC_Data $object Object data.
|
* @param mixed $item Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array Links for the given post.
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $object, $request ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $item->get_id() ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||||
|
|
|
@ -517,11 +517,11 @@ abstract class AbstractTermsContoller extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param object $term Term object.
|
* @param mixed $item Object to prepare.
|
||||||
* @param \WP_REST_Request $request Full details about the request.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array Links for the given term.
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $term, $request ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
||||||
|
|
||||||
if ( ! empty( $request['attribute_id'] ) ) {
|
if ( ! empty( $request['attribute_id'] ) ) {
|
||||||
|
@ -530,15 +530,15 @@ abstract class AbstractTermsContoller extends AbstractController {
|
||||||
|
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( trailingslashit( $base ) . $term->term_id ),
|
'href' => rest_url( trailingslashit( $base ) . $item->term_id ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( $base ),
|
'href' => rest_url( $base ),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( $term->parent ) {
|
if ( $item->parent ) {
|
||||||
$parent_term = get_term( (int) $term->parent, $term->taxonomy );
|
$parent_term = get_term( (int) $item->parent, $item->taxonomy );
|
||||||
if ( $parent_term ) {
|
if ( $parent_term ) {
|
||||||
$links['up'] = array(
|
$links['up'] = array(
|
||||||
'href' => rest_url( trailingslashit( $base ) . $parent_term->term_id ),
|
'href' => rest_url( trailingslashit( $base ) . $parent_term->term_id ),
|
||||||
|
|
|
@ -42,13 +42,13 @@ class Coupons extends AbstractObjectsController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get formatted item data.
|
* Get data for this object in the format of this endpoint's schema.
|
||||||
*
|
*
|
||||||
* @since 3.0.0
|
* @param \WP_Comment $object Object to prepare.
|
||||||
* @param \WC_Data $object WC_Data instance.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array
|
* @return array Array of data in the correct format.
|
||||||
*/
|
*/
|
||||||
protected function get_formatted_item_data( $object ) {
|
protected function get_data_for_response( $object, $request ) {
|
||||||
$data = $object->get_data();
|
$data = $object->get_data();
|
||||||
|
|
||||||
$format_decimal = array( 'amount', 'minimum_amount', 'maximum_amount' );
|
$format_decimal = array( 'amount', 'minimum_amount', 'maximum_amount' );
|
||||||
|
@ -112,7 +112,7 @@ class Coupons extends AbstractObjectsController {
|
||||||
* @return \WP_REST_Response
|
* @return \WP_REST_Response
|
||||||
*/
|
*/
|
||||||
public function prepare_object_for_response( $object, $request ) {
|
public function prepare_object_for_response( $object, $request ) {
|
||||||
$data = $this->get_formatted_item_data( $object );
|
$data = $this->get_data_for_response( $object, $request );
|
||||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||||
$data = $this->filter_response_by_context( $data, $context );
|
$data = $this->filter_response_by_context( $data, $context );
|
||||||
|
|
|
@ -32,6 +32,15 @@ class CustomerDownloads extends AbstractController {
|
||||||
*/
|
*/
|
||||||
protected $resource_type = 'customers';
|
protected $resource_type = 'customers';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = 'customer_download';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the routes for customers.
|
* Register the routes for customers.
|
||||||
*/
|
*/
|
||||||
|
@ -79,64 +88,46 @@ class CustomerDownloads extends AbstractController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a single download output for response.
|
* Get data for this object in the format of this endpoint's schema.
|
||||||
*
|
*
|
||||||
* @param \stdClass $download Download object.
|
* @param \WP_Comment $object Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return \WP_REST_Response $response Response data.
|
* @return array Array of data in the correct format.
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $download, $request ) {
|
protected function get_data_for_response( $object, $request ) {
|
||||||
$data = array(
|
return array(
|
||||||
'download_id' => $download->download_id,
|
'download_id' => $object->download_id,
|
||||||
'download_url' => $download->download_url,
|
'download_url' => $object->download_url,
|
||||||
'product_id' => $download->product_id,
|
'product_id' => $object->product_id,
|
||||||
'product_name' => $download->product_name,
|
'product_name' => $object->product_name,
|
||||||
'download_name' => $download->download_name,
|
'download_name' => $object->download_name,
|
||||||
'order_id' => $download->order_id,
|
'order_id' => $object->order_id,
|
||||||
'order_key' => $download->order_key,
|
'order_key' => $object->order_key,
|
||||||
'downloads_remaining' => '' === $download->downloads_remaining ? 'unlimited' : $download->downloads_remaining,
|
'downloads_remaining' => '' === $object->downloads_remaining ? 'unlimited' : $object->downloads_remaining,
|
||||||
'access_expires' => $download->access_expires ? wc_rest_prepare_date_response( $download->access_expires ) : 'never',
|
'access_expires' => $object->access_expires ? wc_rest_prepare_date_response( $object->access_expires ) : 'never',
|
||||||
'access_expires_gmt' => $download->access_expires ? wc_rest_prepare_date_response( get_gmt_from_date( $download->access_expires ) ) : 'never',
|
'access_expires_gmt' => $object->access_expires ? wc_rest_prepare_date_response( get_gmt_from_date( $object->access_expires ) ) : 'never',
|
||||||
'file' => $download->file,
|
'file' => $object->file,
|
||||||
);
|
);
|
||||||
|
|
||||||
$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 \stdClass $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.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param \stdClass $download Download object.
|
* @param mixed $item Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array Links for the given customer download.
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $download, $request ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$base = str_replace( '(?P<customer_id>[\d]+)', $request['customer_id'], $this->rest_base );
|
$base = str_replace( '(?P<customer_id>[\d]+)', $request['customer_id'], $this->rest_base );
|
||||||
$links = array(
|
$links = array(
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||||
),
|
),
|
||||||
'product' => array(
|
'product' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $download->product_id ) ),
|
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $item->product_id ) ),
|
||||||
),
|
),
|
||||||
'order' => array(
|
'order' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $download->order_id ) ),
|
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $item->order_id ) ),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,15 @@ class Customers extends AbstractController {
|
||||||
*/
|
*/
|
||||||
protected $resource_type = 'customers';
|
protected $resource_type = 'customers';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = 'customer';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the routes for customers.
|
* Register the routes for customers.
|
||||||
*/
|
*/
|
||||||
|
@ -387,48 +396,34 @@ class Customers extends AbstractController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a single customer output for response.
|
* Get data for this object in the format of this endpoint's schema.
|
||||||
*
|
*
|
||||||
* @param \WC_Customer $customer User object.
|
* @param \WP_Comment $object Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return \WP_REST_Response $response Response data.
|
* @return array Array of data in the correct format.
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $customer, $request ) {
|
protected function get_data_for_response( $object, $request ) {
|
||||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
$formatter = new CustomerResponse();
|
||||||
$customer_response = new CustomerResponse();
|
|
||||||
|
|
||||||
$data = $customer_response->prepare_response( $customer, $context );
|
return $formatter->prepare_response( $object, $this->get_request_context( $request ) );
|
||||||
$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( $customer ) );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter customer data returned from the REST API.
|
|
||||||
*
|
|
||||||
* @param \WP_REST_Response $response The response object.
|
|
||||||
* @param \WC_Customer $customer User object used to create response.
|
|
||||||
* @param \WP_REST_Request $request Request object.
|
|
||||||
*/
|
|
||||||
return apply_filters( 'woocommerce_rest_prepare_customer_object', $response, $customer, $request );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param \WC_Customer $customer Customer object.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given customer.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $customer ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $customer->get_id() ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $item->get_id() ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
return $links;
|
return $links;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,15 @@ class Data extends AbstractController {
|
||||||
*/
|
*/
|
||||||
protected $resource_type = 'settings';
|
protected $resource_type = 'settings';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = 'data';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register routes.
|
* Register routes.
|
||||||
*
|
*
|
||||||
|
@ -88,35 +97,27 @@ class Data extends AbstractController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a data resource object for serialization.
|
* Get data for this object in the format of this endpoint's schema.
|
||||||
*
|
*
|
||||||
* @param \stdClass $resource Resource data.
|
* @param \stdClass $object Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return \WP_REST_Response $response Response data.
|
* @return array Array of data in the correct format.
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $resource, $request ) {
|
protected function get_data_for_response( $object, $request ) {
|
||||||
$data = array(
|
return array(
|
||||||
'slug' => $resource->slug,
|
'slug' => $object->slug,
|
||||||
'description' => $resource->description,
|
'description' => $object->description,
|
||||||
);
|
);
|
||||||
|
|
||||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
|
||||||
$data = $this->filter_response_by_context( $data, 'view' );
|
|
||||||
|
|
||||||
// Wrap the data in a response object.
|
|
||||||
$response = rest_ensure_response( $data );
|
|
||||||
$response->add_links( $this->prepare_links( $resource ) );
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param object $item Data object.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given country.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $item ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $item->slug ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $item->slug ) ),
|
||||||
|
|
|
@ -198,7 +198,7 @@ class Continents extends DataController {
|
||||||
$data = $this->filter_response_by_context( $data, 'view' );
|
$data = $this->filter_response_by_context( $data, 'view' );
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $item ) );
|
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter the location list returned from the API.
|
* Filter the location list returned from the API.
|
||||||
|
@ -215,10 +215,11 @@ class Continents extends DataController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param object $item Data object.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given continent.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $item ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$continent_code = strtolower( $item['code'] );
|
$continent_code = strtolower( $item['code'] );
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
|
|
|
@ -145,7 +145,7 @@ class Countries extends DataController {
|
||||||
$data = $this->filter_response_by_context( $data, 'view' );
|
$data = $this->filter_response_by_context( $data, 'view' );
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $item ) );
|
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter the states list for a country returned from the API.
|
* Filter the states list for a country returned from the API.
|
||||||
|
@ -162,10 +162,11 @@ class Countries extends DataController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param object $item Data object.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given country.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $item ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$country_code = strtolower( $item['code'] );
|
$country_code = strtolower( $item['code'] );
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
|
|
|
@ -151,7 +151,7 @@ class Currencies extends DataController {
|
||||||
$data = $this->filter_response_by_context( $data, 'view' );
|
$data = $this->filter_response_by_context( $data, 'view' );
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $item ) );
|
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter currency returned from the API.
|
* Filter currency returned from the API.
|
||||||
|
@ -166,10 +166,11 @@ class Currencies extends DataController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param object $item Data object.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given currency.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $item ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$code = strtoupper( $item['code'] );
|
$code = strtoupper( $item['code'] );
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
|
|
|
@ -93,7 +93,7 @@ class DownloadIPs extends DataController {
|
||||||
$data = $this->filter_response_by_context( $data, 'view' );
|
$data = $this->filter_response_by_context( $data, 'view' );
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $item ) );
|
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter the list returned from the API.
|
* Filter the list returned from the API.
|
||||||
|
@ -108,10 +108,11 @@ class DownloadIPs extends DataController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param object $item Data object.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $item ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||||
|
|
|
@ -32,6 +32,15 @@ class OrderNotes extends AbstractController {
|
||||||
*/
|
*/
|
||||||
protected $post_type = 'shop_order';
|
protected $post_type = 'shop_order';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = 'order_note';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the routes for order notes.
|
* Register the routes for order notes.
|
||||||
*/
|
*/
|
||||||
|
@ -356,53 +365,36 @@ class OrderNotes extends AbstractController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a single order note output for response.
|
* Get data for this object in the format of this endpoint's schema.
|
||||||
*
|
*
|
||||||
* @param \WP_Comment $note Order note object.
|
* @param \WP_Comment $object Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return \WP_REST_Response $response Response data.
|
* @return array Array of data in the correct format.
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $note, $request ) {
|
protected function get_data_for_response( $object, $request ) {
|
||||||
$data = array(
|
return array(
|
||||||
'id' => (int) $note->comment_ID,
|
'id' => (int) $object->comment_ID,
|
||||||
'author' => __( 'WooCommerce', 'woocommerce' ) === $note->comment_author ? 'system' : $note->comment_author,
|
'author' => __( 'WooCommerce', 'woocommerce' ) === $object->comment_author ? 'system' : $object->comment_author,
|
||||||
'date_created' => wc_rest_prepare_date_response( $note->comment_date ),
|
'date_created' => wc_rest_prepare_date_response( $object->comment_date ),
|
||||||
'date_created_gmt' => wc_rest_prepare_date_response( $note->comment_date_gmt ),
|
'date_created_gmt' => wc_rest_prepare_date_response( $object->comment_date_gmt ),
|
||||||
'note' => $note->comment_content,
|
'note' => $object->comment_content,
|
||||||
'customer_note' => (bool) get_comment_meta( $note->comment_ID, 'is_customer_note', true ),
|
'customer_note' => (bool) get_comment_meta( $object->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.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param WP_Comment $note Delivery order_note object.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given order note.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $note ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$order_id = (int) $note->comment_post_ID;
|
$order_id = (int) $item->comment_post_ID;
|
||||||
$base = str_replace( '(?P<order_id>[\d]+)', $order_id, $this->rest_base );
|
$base = str_replace( '(?P<order_id>[\d]+)', $order_id, $this->rest_base );
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $note->comment_ID ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $item->comment_ID ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||||
|
|
|
@ -127,13 +127,13 @@ class OrderRefunds extends Orders {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get formatted item data.
|
* Get data for this object in the format of this endpoint's schema.
|
||||||
*
|
*
|
||||||
* @since 3.0.0
|
* @param mixed $object Object to prepare.
|
||||||
* @param \WC_Data $object WC_Data instance.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array
|
* @return array Array of data in the correct format.
|
||||||
*/
|
*/
|
||||||
protected function get_formatted_item_data( $object ) {
|
protected function get_data_for_response( $object, $request ) {
|
||||||
$data = $object->get_data();
|
$data = $object->get_data();
|
||||||
$format_decimal = array( 'amount' );
|
$format_decimal = array( 'amount' );
|
||||||
$format_date = array( 'date_created' );
|
$format_date = array( 'date_created' );
|
||||||
|
@ -192,7 +192,7 @@ class OrderRefunds extends Orders {
|
||||||
return new \WP_Error( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 );
|
return new \WP_Error( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $this->get_formatted_item_data( $object );
|
$data = $this->get_data_for_response( $object, $request );
|
||||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||||
$data = $this->filter_response_by_context( $data, $context );
|
$data = $this->filter_response_by_context( $data, $context );
|
||||||
|
@ -336,21 +336,21 @@ class OrderRefunds extends Orders {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param \WC_Data $object Object data.
|
* @param mixed $item Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array Links for the given post.
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $object, $request ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$base = str_replace( '(?P<order_id>[\d]+)', $object->get_parent_id(), $this->rest_base );
|
$base = str_replace( '(?P<order_id>[\d]+)', $item->get_parent_id(), $this->rest_base );
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $object->get_id() ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $item->get_id() ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||||
),
|
),
|
||||||
'up' => array(
|
'up' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $object->get_parent_id() ) ),
|
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $item->get_parent_id() ) ),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -153,29 +153,29 @@ class Orders extends AbstractObjectsController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param \WC_Data $object Object data.
|
* @param mixed $item Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array Links for the given post.
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $object, $request ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $item->get_id() ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( 0 !== (int) $object->get_customer_id() ) {
|
if ( 0 !== (int) $item->get_customer_id() ) {
|
||||||
$links['customer'] = array(
|
$links['customer'] = array(
|
||||||
'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $object->get_customer_id() ) ),
|
'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $item->get_customer_id() ) ),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 0 !== (int) $object->get_parent_id() ) {
|
if ( 0 !== (int) $item->get_parent_id() ) {
|
||||||
$links['up'] = array(
|
$links['up'] = array(
|
||||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $object->get_parent_id() ) ),
|
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $item->get_parent_id() ) ),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,15 @@ class PaymentGateways extends AbstractController {
|
||||||
*/
|
*/
|
||||||
protected $resource_type = 'payment_gateways';
|
protected $resource_type = 'payment_gateways';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = 'payment_gateway';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the route for /payment_gateways and /payment_gateways/<id>
|
* Register the route for /payment_gateways and /payment_gateways/<id>
|
||||||
*/
|
*/
|
||||||
|
@ -210,41 +219,25 @@ class PaymentGateways extends AbstractController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a payment gateway for response.
|
* Get data for this object in the format of this endpoint's schema.
|
||||||
*
|
*
|
||||||
* @param WC_Payment_Gateway $gateway Payment gateway object.
|
* @param \WC_Payment_Gateway $object Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return \WP_REST_Response $response Response data.
|
* @return array Array of data in the correct format.
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $gateway, $request ) {
|
protected function get_data_for_response( $object, $request ) {
|
||||||
$order = (array) get_option( 'woocommerce_gateway_order' );
|
$order = (array) get_option( 'woocommerce_gateway_order' );
|
||||||
$item = array(
|
return array(
|
||||||
'id' => $gateway->id,
|
'id' => $object->id,
|
||||||
'title' => $gateway->title,
|
'title' => $object->title,
|
||||||
'description' => $gateway->description,
|
'description' => $object->description,
|
||||||
'order' => isset( $order[ $gateway->id ] ) ? $order[ $gateway->id ] : '',
|
'order' => isset( $order[ $object->id ] ) ? $order[ $object->id ] : '',
|
||||||
'enabled' => ( 'yes' === $gateway->enabled ),
|
'enabled' => ( 'yes' === $object->enabled ),
|
||||||
'method_title' => $gateway->get_method_title(),
|
'method_title' => $object->get_method_title(),
|
||||||
'method_description' => $gateway->get_method_description(),
|
'method_description' => $object->get_method_description(),
|
||||||
'method_supports' => $gateway->supports,
|
'method_supports' => $object->supports,
|
||||||
'settings' => $this->get_settings( $gateway ),
|
'settings' => $this->get_settings( $object ),
|
||||||
);
|
);
|
||||||
|
|
||||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
|
||||||
$data = $this->add_additional_fields_to_object( $item, $request );
|
|
||||||
$data = $this->filter_response_by_context( $data, $context );
|
|
||||||
|
|
||||||
$response = rest_ensure_response( $data );
|
|
||||||
$response->add_links( $this->prepare_links( $gateway, $request ) );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter payment gateway objects returned from the REST API.
|
|
||||||
*
|
|
||||||
* @param \WP_REST_Response $response The response object.
|
|
||||||
* @param WC_Payment_Gateway $gateway Payment gateway object.
|
|
||||||
* @param \WP_REST_Request $request Request object.
|
|
||||||
*/
|
|
||||||
return apply_filters( 'woocommerce_rest_prepare_payment_gateway', $response, $gateway, $request );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -289,14 +282,14 @@ class PaymentGateways extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param WC_Payment_Gateway $gateway Payment gateway object.
|
* @param mixed $item Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $gateway, $request ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $gateway->id ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $item->id ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||||
|
|
|
@ -321,7 +321,7 @@ class ProductAttributes extends AbstractController {
|
||||||
|
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $item ) );
|
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter a attribute item returned from the API.
|
* Filter a attribute item returned from the API.
|
||||||
|
@ -338,14 +338,15 @@ class ProductAttributes extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param object $attribute Attribute object.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given attribute.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $attribute ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( trailingslashit( $base ) . $attribute->attribute_id ),
|
'href' => rest_url( trailingslashit( $base ) . $item->attribute_id ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( $base ),
|
'href' => rest_url( $base ),
|
||||||
|
|
|
@ -33,6 +33,15 @@ class ProductReviews extends AbstractController {
|
||||||
*/
|
*/
|
||||||
protected $resource_type = 'product_reviews';
|
protected $resource_type = 'product_reviews';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = 'product_reviews';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the routes for product reviews.
|
* Register the routes for product reviews.
|
||||||
*/
|
*/
|
||||||
|
@ -391,7 +400,6 @@ class ProductReviews extends AbstractController {
|
||||||
$request->set_param( 'context', $context );
|
$request->set_param( 'context', $context );
|
||||||
|
|
||||||
$response = $this->prepare_item_for_response( $review, $request );
|
$response = $this->prepare_item_for_response( $review, $request );
|
||||||
$response = rest_ensure_response( $response );
|
|
||||||
|
|
||||||
$response->set_status( 201 );
|
$response->set_status( 201 );
|
||||||
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $review_id ) ) );
|
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $review_id ) ) );
|
||||||
|
@ -411,10 +419,7 @@ class ProductReviews extends AbstractController {
|
||||||
return $review;
|
return $review;
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $this->prepare_item_for_response( $review, $request );
|
return $this->prepare_item_for_response( $review, $request );
|
||||||
$response = rest_ensure_response( $data );
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -498,9 +503,7 @@ class ProductReviews extends AbstractController {
|
||||||
|
|
||||||
$request->set_param( 'context', 'edit' );
|
$request->set_param( 'context', 'edit' );
|
||||||
|
|
||||||
$response = $this->prepare_item_for_response( $review, $request );
|
return $this->prepare_item_for_response( $review, $request );
|
||||||
|
|
||||||
return rest_ensure_response( $response );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -573,32 +576,16 @@ class ProductReviews extends AbstractController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a single product review output for response.
|
* Get data for this object in the format of this endpoint's schema.
|
||||||
*
|
*
|
||||||
* @param \WP_Comment $review Product review object.
|
* @param \WP_Comment $object Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return \WP_REST_Response $response Response data.
|
* @return array Array of data in the correct format.
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $review, $request ) {
|
protected function get_data_for_response( $object, $request ) {
|
||||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
$formatter = new ProductReviewResponse();
|
||||||
$fields = $this->get_fields_for_response( $request );
|
|
||||||
$review_response = new ProductReviewResponse();
|
|
||||||
$data = $review_response->prepare_response( $review, $context );
|
|
||||||
$data = array_intersect_key( $data, array_flip( $fields ) );
|
|
||||||
$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( $review ) );
|
return $formatter->prepare_response( $object, $this->get_request_context( $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 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -657,27 +644,28 @@ class ProductReviews extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param WP_Comment $review Product review object.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given product review.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $review ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $review->comment_ID ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $item->comment_ID ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
if ( 0 !== (int) $review->comment_post_ID ) {
|
if ( 0 !== (int) $item->comment_post_ID ) {
|
||||||
$links['up'] = array(
|
$links['up'] = array(
|
||||||
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $review->comment_post_ID ) ),
|
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $item->comment_post_ID ) ),
|
||||||
'embeddable' => true,
|
'embeddable' => true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if ( 0 !== (int) $review->user_id ) {
|
if ( 0 !== (int) $item->user_id ) {
|
||||||
$links['reviewer'] = array(
|
$links['reviewer'] = array(
|
||||||
'href' => rest_url( 'wp/v2/users/' . $review->user_id ),
|
'href' => rest_url( 'wp/v2/users/' . $item->user_id ),
|
||||||
'embeddable' => true,
|
'embeddable' => true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -847,16 +847,16 @@ class ProductVariations extends Products {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param \WC_Data $object Object data.
|
* @param mixed $item Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array Links for the given post.
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $object, $request ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$product_id = (int) $request['product_id'];
|
$product_id = (int) $request['product_id'];
|
||||||
$base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
|
$base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $object->get_id() ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $item->get_id() ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||||
|
|
|
@ -1092,24 +1092,23 @@ class Products extends AbstractObjectsController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param \WC_Data $object Object data.
|
* @param mixed $item Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
*
|
* @return array
|
||||||
* @return array Links for the given post.
|
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $object, $request ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ), // @codingStandardsIgnoreLine.
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $item->get_id() ) ), // @codingStandardsIgnoreLine.
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), // @codingStandardsIgnoreLine.
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), // @codingStandardsIgnoreLine.
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( $object->get_parent_id() ) {
|
if ( $item->get_parent_id() ) {
|
||||||
$links['up'] = array(
|
$links['up'] = array(
|
||||||
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $object->get_parent_id() ) ), // @codingStandardsIgnoreLine.
|
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $item->get_parent_id() ) ), // @codingStandardsIgnoreLine.
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -106,14 +106,15 @@ class Settings extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param string $group_id Group ID.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given group.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $group_id ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
||||||
$links = array(
|
$links = array(
|
||||||
'options' => array(
|
'options' => array(
|
||||||
'href' => rest_url( trailingslashit( $base ) . $group_id ),
|
'href' => rest_url( trailingslashit( $base ) . $item['id'] ),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -135,7 +136,7 @@ class Settings extends AbstractController {
|
||||||
|
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $item['id'] ) );
|
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace WooCommerce\RestApi\Controllers\Version4;
|
||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
use \WooCommerce\RestApi\Controllers\Version4\Utilities\SettingsTrait;
|
use WooCommerce\RestApi\Controllers\Version4\Utilities\SettingsTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Setting Options controller class.
|
* REST API Setting Options controller class.
|
||||||
|
@ -366,22 +366,22 @@ class SettingsOptions extends AbstractController {
|
||||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||||
$data = $this->filter_response_by_context( $data, empty( $request['context'] ) ? 'view' : $request['context'] );
|
$data = $this->filter_response_by_context( $data, empty( $request['context'] ) ? 'view' : $request['context'] );
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
$response->add_links( $this->prepare_links( $data['id'], $request['group_id'] ) );
|
$response->add_links( $this->prepare_links( $data, $request ) );
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param string $setting_id Setting ID.
|
* @param mixed $item Object to prepare.
|
||||||
* @param string $group_id Group ID.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array Links for the given setting.
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $setting_id, $group_id ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$base = str_replace( '(?P<group_id>[\w-]+)', $group_id, $this->rest_base );
|
$base = str_replace( '(?P<group_id>[\w-]+)', $request['group_id'], $this->rest_base );
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $base, $setting_id ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $base, $item['id'] ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
|
||||||
|
|
|
@ -144,14 +144,14 @@ class ShippingMethods extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param \WC_Shipping_Method $method Shipping method object.
|
* @param mixed $item Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $method, $request ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $method->id ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $item->id ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||||
|
|
|
@ -130,7 +130,7 @@ class ShippingZoneLocations extends AbstractShippingZonesController {
|
||||||
// Wrap the data in a response object.
|
// Wrap the data in a response object.
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( (int) $request['id'] ) );
|
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
@ -138,11 +138,12 @@ class ShippingZoneLocations extends AbstractShippingZonesController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param int $zone_id Given Shipping Zone ID.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given Shipping Zone Location.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $zone_id ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $zone_id;
|
$base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $request['id'];
|
||||||
$links = array(
|
$links = array(
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( $base . '/locations' ),
|
'href' => rest_url( $base . '/locations' ),
|
||||||
|
|
|
@ -298,7 +298,7 @@ class ShippingZoneMethods extends AbstractShippingZonesController {
|
||||||
* Updates settings, order, and enabled status on create.
|
* Updates settings, order, and enabled status on create.
|
||||||
*
|
*
|
||||||
* @param int $instance_id Instance ID.
|
* @param int $instance_id Instance ID.
|
||||||
* @param WC_Shipping_Method $method Shipping method data.
|
* @param \WC_Shipping_Method $method Shipping method data.
|
||||||
* @param \WP_REST_Request $request Request data.
|
* @param \WP_REST_Request $request Request data.
|
||||||
*
|
*
|
||||||
* @return WC_Shipping_Method
|
* @return WC_Shipping_Method
|
||||||
|
@ -377,7 +377,7 @@ class ShippingZoneMethods extends AbstractShippingZonesController {
|
||||||
// Wrap the data in a response object.
|
// Wrap the data in a response object.
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $request['zone_id'], $item->instance_id ) );
|
$response->add_links( $this->prepare_links( $item, $request ) );
|
||||||
|
|
||||||
$response = $this->prepare_response_for_collection( $response );
|
$response = $this->prepare_response_for_collection( $response );
|
||||||
|
|
||||||
|
@ -416,15 +416,15 @@ class ShippingZoneMethods extends AbstractShippingZonesController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param int $zone_id Given Shipping Zone ID.
|
* @param mixed $item Object to prepare.
|
||||||
* @param int $instance_id Given Shipping Zone Method Instance ID.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array Links for the given Shipping Zone Method.
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $zone_id, $instance_id ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $zone_id;
|
$base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $request['zone_id'];
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( $base . '/methods/' . $instance_id ),
|
'href' => rest_url( $base . '/methods/' . $item->instance_id ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( $base . '/methods' ),
|
'href' => rest_url( $base . '/methods' ),
|
||||||
|
|
|
@ -250,7 +250,7 @@ class ShippingZones extends AbstractShippingZonesController {
|
||||||
// Wrap the data in a response object.
|
// Wrap the data in a response object.
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $data['id'] ) );
|
$response->add_links( $this->prepare_links( $data, $request ) );
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
@ -258,20 +258,21 @@ class ShippingZones extends AbstractShippingZonesController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param int $zone_id Given Shipping Zone ID.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given Shipping Zone.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $zone_id ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( trailingslashit( $base ) . $zone_id ),
|
'href' => rest_url( trailingslashit( $base ) . $item['id'] ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( $base ),
|
'href' => rest_url( $base ),
|
||||||
),
|
),
|
||||||
'describedby' => array(
|
'describedby' => array(
|
||||||
'href' => rest_url( trailingslashit( $base ) . $zone_id . '/locations' ),
|
'href' => rest_url( trailingslashit( $base ) . $item['id'] . '/locations' ),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,15 @@ class SystemStatus extends AbstractController {
|
||||||
*/
|
*/
|
||||||
protected $resource_type = 'system_status';
|
protected $resource_type = 'system_status';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = 'system_status';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the route for /system_status
|
* Register the route for /system_status
|
||||||
*/
|
*/
|
||||||
|
@ -585,26 +594,4 @@ class SystemStatus extends AbstractController {
|
||||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Prepare the system status response
|
|
||||||
*
|
|
||||||
* @param array $system_status System status data.
|
|
||||||
* @param \WP_REST_Request $request Request object.
|
|
||||||
* @return \WP_REST_Response
|
|
||||||
*/
|
|
||||||
public function prepare_item_for_response( $system_status, $request ) {
|
|
||||||
$data = $this->add_additional_fields_to_object( $system_status, $request );
|
|
||||||
$data = $this->filter_response_by_context( $data, 'view' );
|
|
||||||
$response = rest_ensure_response( $data );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter the system status returned from the REST API.
|
|
||||||
*
|
|
||||||
* @param \WP_REST_Response $response The response object.
|
|
||||||
* @param mixed $system_status System status
|
|
||||||
* @param \WP_REST_Request $request Request object.
|
|
||||||
*/
|
|
||||||
return apply_filters( 'woocommerce_rest_prepare_system_status', $response, $system_status, $request );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,15 @@ class SystemStatusTools extends AbstractController {
|
||||||
*/
|
*/
|
||||||
protected $resource_type = 'system_status';
|
protected $resource_type = 'system_status';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = 'system_status_tool';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the routes for /system_status/tools/*.
|
* Register the routes for /system_status/tools/*.
|
||||||
*/
|
*/
|
||||||
|
@ -257,25 +266,6 @@ class SystemStatusTools extends AbstractController {
|
||||||
return rest_ensure_response( $response );
|
return rest_ensure_response( $response );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Prepare a tool item for serialization.
|
|
||||||
*
|
|
||||||
* @param array $item Object.
|
|
||||||
* @param \WP_REST_Request $request Request object.
|
|
||||||
* @return \WP_REST_Response $response Response data.
|
|
||||||
*/
|
|
||||||
public function prepare_item_for_response( $item, $request ) {
|
|
||||||
$context = empty( $request['context'] ) ? 'view' : $request['context'];
|
|
||||||
$data = $this->add_additional_fields_to_object( $item, $request );
|
|
||||||
$data = $this->filter_response_by_context( $data, $context );
|
|
||||||
|
|
||||||
$response = rest_ensure_response( $data );
|
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $item['id'] ) );
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the system status tools schema, conforming to JSON Schema.
|
* Get the system status tools schema, conforming to JSON Schema.
|
||||||
*
|
*
|
||||||
|
@ -341,14 +331,15 @@ class SystemStatusTools extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param string $id ID.
|
* @param mixed $item Object to prepare.
|
||||||
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $id ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
$base = '/' . $this->namespace . '/' . $this->rest_base;
|
||||||
$links = array(
|
$links = array(
|
||||||
'item' => array(
|
'item' => array(
|
||||||
'href' => rest_url( trailingslashit( $base ) . $id ),
|
'href' => rest_url( trailingslashit( $base ) . $item['id'] ),
|
||||||
'embeddable' => true,
|
'embeddable' => true,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
@ -30,6 +30,15 @@ class TaxClasses extends AbstractController {
|
||||||
*/
|
*/
|
||||||
protected $resource_type = 'settings';
|
protected $resource_type = 'settings';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = 'tax_class';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the routes for tax classes.
|
* Register the routes for tax classes.
|
||||||
*/
|
*/
|
||||||
|
@ -243,46 +252,19 @@ class TaxClasses extends AbstractController {
|
||||||
* @param \WP_REST_Response $response The response returned from the API.
|
* @param \WP_REST_Response $response The response returned from the API.
|
||||||
* @param \WP_REST_Request $request The request sent to the API.
|
* @param \WP_REST_Request $request The request sent to the API.
|
||||||
*/
|
*/
|
||||||
do_action( 'woocommerce_rest_delete_tax', (object) $tax_class, $response, $request );
|
do_action( 'woocommerce_rest_delete_tax_class', (object) $tax_class, $response, $request );
|
||||||
|
|
||||||
return $response;
|
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.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @return array Links for the given tax class.
|
* @param mixed $item Object to prepare.
|
||||||
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links() {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||||
|
|
|
@ -30,6 +30,15 @@ class Taxes extends AbstractController {
|
||||||
*/
|
*/
|
||||||
protected $resource_type = 'settings';
|
protected $resource_type = 'settings';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singular name for resource type.
|
||||||
|
*
|
||||||
|
* Used in filter/action names for single resources.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $singular = 'tax';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the routes for taxes.
|
* Register the routes for taxes.
|
||||||
*/
|
*/
|
||||||
|
@ -410,29 +419,29 @@ class Taxes extends AbstractController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a single tax output for response.
|
* Get data for this object in the format of this endpoint's schema.
|
||||||
*
|
*
|
||||||
* @param \stdClass $tax Tax object.
|
* @param \stdClass $object Object to prepare.
|
||||||
* @param \WP_REST_Request $request Request object.
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return \WP_REST_Response $response Response data.
|
* @return array Array of data in the correct format.
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $tax, $request ) {
|
protected function get_data_for_response( $object, $request ) {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
$id = (int) $tax->tax_rate_id;
|
$id = (int) $object->tax_rate_id;
|
||||||
$data = array(
|
$data = array(
|
||||||
'id' => $id,
|
'id' => $id,
|
||||||
'country' => $tax->tax_rate_country,
|
'country' => $object->tax_rate_country,
|
||||||
'state' => $tax->tax_rate_state,
|
'state' => $object->tax_rate_state,
|
||||||
'postcode' => '',
|
'postcode' => '',
|
||||||
'city' => '',
|
'city' => '',
|
||||||
'rate' => $tax->tax_rate,
|
'rate' => $object->tax_rate,
|
||||||
'name' => $tax->tax_rate_name,
|
'name' => $object->tax_rate_name,
|
||||||
'priority' => (int) $tax->tax_rate_priority,
|
'priority' => (int) $object->tax_rate_priority,
|
||||||
'compound' => (bool) $tax->tax_rate_compound,
|
'compound' => (bool) $object->tax_rate_compound,
|
||||||
'shipping' => (bool) $tax->tax_rate_shipping,
|
'shipping' => (bool) $object->tax_rate_shipping,
|
||||||
'order' => (int) $tax->tax_rate_order,
|
'order' => (int) $object->tax_rate_order,
|
||||||
'class' => $tax->tax_rate_class ? $tax->tax_rate_class : 'standard',
|
'class' => $object->tax_rate_class ? $object->tax_rate_class : 'standard',
|
||||||
);
|
);
|
||||||
|
|
||||||
// Get locales from a tax rate.
|
// Get locales from a tax rate.
|
||||||
|
@ -447,41 +456,25 @@ class Taxes extends AbstractController {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( ! is_wp_error( $tax ) && ! is_null( $tax ) ) {
|
if ( ! is_wp_error( $locales ) && ! is_null( $locales ) ) {
|
||||||
foreach ( $locales as $locale ) {
|
foreach ( $locales as $locale ) {
|
||||||
$data[ $locale->location_type ] = $locale->location_code;
|
$data[ $locale->location_type ] = $locale->location_code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return $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 );
|
|
||||||
|
|
||||||
// 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.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param \stdClass $tax Tax object.
|
* @param mixed $item Object to prepare.
|
||||||
* @return array Links for the given tax.
|
* @param \WP_REST_Request $request Request object.
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $tax ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $tax->tax_rate_id ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $item->tax_rate_id ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||||
|
|
|
@ -490,7 +490,7 @@ class Webhooks extends AbstractController {
|
||||||
// Wrap the data in a response object.
|
// Wrap the data in a response object.
|
||||||
$response = rest_ensure_response( $data );
|
$response = rest_ensure_response( $data );
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $webhook->get_id() ) );
|
$response->add_links( $this->prepare_links( $webhook, $request ) );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter webhook object returned from the REST API.
|
* Filter webhook object returned from the REST API.
|
||||||
|
@ -505,13 +505,14 @@ class Webhooks extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare links for the request.
|
* Prepare links for the request.
|
||||||
*
|
*
|
||||||
* @param int $id Webhook ID.
|
* @param mixed $item Object to prepare.
|
||||||
|
* @param \WP_REST_Request $request Request object.
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepare_links( $id ) {
|
protected function prepare_links( $item, $request ) {
|
||||||
$links = array(
|
$links = array(
|
||||||
'self' => array(
|
'self' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ),
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $item->get_id() ) ),
|
||||||
),
|
),
|
||||||
'collection' => array(
|
'collection' => array(
|
||||||
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
|
||||||
|
|
Loading…
Reference in New Issue