Consistent responses across all classes

This commit is contained in:
Mike Jolley 2019-06-18 12:45:34 +01:00
parent d2e4c199f4
commit a4e89f89ce
24 changed files with 458 additions and 762 deletions

View File

@ -274,14 +274,18 @@ abstract class AbstractController extends WP_REST_Controller {
* @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 ) );
try {
$context = $this->get_request_context( $request );
$fields = $this->get_fields_for_response( $request );
$data = $this->get_data_for_response( $item, $request );
$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( $item, $request ) );
} catch ( \WC_REST_Exception $e ) {
$response = rest_ensure_response( new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ) );
}
/**
* Filter object returned from the REST API.
@ -290,7 +294,17 @@ abstract class AbstractController extends WP_REST_Controller {
* @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 );
return apply_filters( 'woocommerce_rest_prepare_' . $this->get_hook_suffix( $item ), $response, $item, $request );
}
/**
* Return suffix for item action hooks.
*
* @param mixed $item Object used to create response.
* @return string
*/
protected function get_hook_suffix( $item ) {
return $this->singular;
}
/**

View File

@ -38,16 +38,6 @@ abstract class AbstractObjectsController extends AbstractController {
*/
abstract protected function get_object( $id );
/**
* Prepares the object for the REST response.
*
* @since 3.0.0
* @param \WC_Data $object Object data.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response Response object on success, or \WP_Error object on failure.
*/
abstract protected function prepare_object_for_response( $object, $request );
/**
* Prepares one object for create or update operation.
*
@ -171,7 +161,7 @@ abstract class AbstractObjectsController extends AbstractController {
return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
$data = $this->prepare_object_for_response( $object, $request );
$data = $this->prepare_item_for_response( $object, $request );
$response = rest_ensure_response( $data );
return $response;
@ -241,7 +231,7 @@ abstract class AbstractObjectsController extends AbstractController {
do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, true );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_object_for_response( $object, $request );
$response = $this->prepare_item_for_response( $object, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) );
@ -286,7 +276,7 @@ abstract class AbstractObjectsController extends AbstractController {
do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, false );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_object_for_response( $object, $request );
$response = $this->prepare_item_for_response( $object, $request );
return rest_ensure_response( $response );
}
@ -393,7 +383,7 @@ abstract class AbstractObjectsController extends AbstractController {
continue;
}
$data = $this->prepare_object_for_response( $object, $request );
$data = $this->prepare_item_for_response( $object, $request );
$objects[] = $this->prepare_response_for_collection( $data );
}
@ -460,7 +450,7 @@ abstract class AbstractObjectsController extends AbstractController {
}
$request->set_param( 'context', 'edit' );
$previous = $this->prepare_object_for_response( $object, $request );
$previous = $this->prepare_item_for_response( $object, $request );
// If we're forcing, then delete permanently.
if ( $force ) {
@ -833,4 +823,14 @@ abstract class AbstractObjectsController extends AbstractController {
return $args['meta_query'];
}
/**
* Return suffix for item action hooks.
*
* @param mixed $item Object used to create response.
* @return string
*/
protected function get_hook_suffix( $item ) {
return $this->post_type . '_object';
}
}

View File

@ -749,4 +749,14 @@ abstract class AbstractTermsContoller extends AbstractController {
return $this->taxonomy;
}
/**
* Return suffix for item action hooks.
*
* @param mixed $item Object used to create response.
* @return string
*/
protected function get_hook_suffix( $item ) {
return $this->taxonomy;
}
}

View File

@ -103,35 +103,6 @@ class Coupons extends AbstractObjectsController {
);
}
/**
* Prepare a single coupon output for response.
*
* @since 3.0.0
* @param \WC_Data $object Object data.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response
*/
public function prepare_object_for_response( $object, $request ) {
$data = $this->get_data_for_response( $object, $request );
$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( $object, $request ) );
/**
* Filter the data for a response.
*
* The dynamic portion of the hook name, $this->post_type,
* refers to object type being prepared for the response.
*
* @param \WP_REST_Response $response The response object.
* @param \WC_Data $object Object data.
* @param \WP_REST_Request $request Request object.
*/
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request );
}
/**
* Prepare objects query.
*

View File

@ -25,6 +25,15 @@ class Continents extends DataController {
*/
protected $rest_base = 'data/continents';
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'continent';
/**
* Register routes.
*
@ -186,30 +195,14 @@ class Continents extends DataController {
}
/**
* Prepare the data object for response.
* Get data for this object in the format of this endpoint's schema.
*
* @since 3.5.0
* @param object $item Data object.
* @param mixed $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response Response data.
* @return mixed Array of data in the correct format.
*/
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item, $request ) );
/**
* Filter the location list returned from the API.
*
* Allows modification of the loction data right before it is returned.
*
* @param \WP_REST_Response $response The response object.
* @param array $item The original list of continent(s), countries, and states.
* @param \WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_data_continent', $response, $item, $request );
protected function get_data_for_response( $object, $request ) {
return $object;
}
/**

View File

@ -25,6 +25,15 @@ class Countries extends DataController {
*/
protected $rest_base = 'data/countries';
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'country';
/**
* Register routes.
*
@ -133,30 +142,14 @@ class Countries extends DataController {
}
/**
* Prepare the data object for response.
* Get data for this object in the format of this endpoint's schema.
*
* @since 3.5.0
* @param object $item Data object.
* @param mixed $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response Response data.
* @return mixed Array of data in the correct format.
*/
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item, $request ) );
/**
* Filter the states list for a country returned from the API.
*
* Allows modification of the loction data right before it is returned.
*
* @param \WP_REST_Response $response The response object.
* @param array $data The original country's states list.
* @param \WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_data_country', $response, $item, $request );
protected function get_data_for_response( $object, $request ) {
return $object;
}
/**

View File

@ -25,6 +25,15 @@ class Currencies extends DataController {
*/
protected $rest_base = 'data/currencies';
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'currency';
/**
* Register routes.
*/
@ -140,27 +149,14 @@ class Currencies extends DataController {
}
/**
* Prepare the data object for response.
* Get data for this object in the format of this endpoint's schema.
*
* @param object $item Data object.
* @param mixed $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response Response data.
* @return mixed Array of data in the correct format.
*/
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item, $request ) );
/**
* Filter currency returned from the API.
*
* @param \WP_REST_Response $response The response object.
* @param array $item Currency data.
* @param \WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_data_currency', $response, $item, $request );
protected function get_data_for_response( $object, $request ) {
return $object;
}
/**

View File

@ -25,6 +25,15 @@ class DownloadIPs extends DataController {
*/
protected $rest_base = 'data/download-ips';
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'download_ip';
/**
* Register routes.
*
@ -72,7 +81,7 @@ class DownloadIPs extends DataController {
if ( ! empty( $downloads ) ) {
foreach ( $downloads as $download ) {
$response = $this->prepare_item_for_response( $download, $request );
$response = $this->prepare_item_for_response( (array) $download, $request );
$data[] = $this->prepare_response_for_collection( $response );
}
}
@ -81,28 +90,14 @@ class DownloadIPs extends DataController {
}
/**
* Prepare the data object for response.
* Get data for this object in the format of this endpoint's schema.
*
* @since 3.5.0
* @param object $item Data object.
* @param mixed $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response Response data.
* @return mixed Array of data in the correct format.
*/
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item, $request ) );
/**
* Filter the list returned from the API.
*
* @param \WP_REST_Response $response The response object.
* @param array $item The original item.
* @param \WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_data_download_ip', $response, $item, $request );
protected function get_data_for_response( $object, $request ) {
return $object;
}
/**

View File

@ -30,13 +30,6 @@ class OrderRefunds extends Orders {
*/
protected $post_type = 'shop_order_refund';
/**
* Stores the request.
*
* @var array
*/
protected $request = array();
/**
* Order refunds actions.
*/
@ -129,19 +122,32 @@ class OrderRefunds extends Orders {
/**
* Get data for this object in the format of this endpoint's schema.
*
* @throws \WC_REST_Exception Exception on invalid data.
*
* @param mixed $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
protected function get_data_for_response( $object, $request ) {
$order = wc_get_order( (int) $request['order_id'] );
if ( ! $order ) {
throw new \WC_REST_Exception( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 );
}
if ( ! $object || $object->get_parent_id() !== $order->get_id() ) {
throw new \WC_REST_Exception( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 );
}
$data = $object->get_data();
$format_decimal = array( 'amount' );
$format_date = array( 'date_created' );
$format_line_items = array( 'line_items' );
$dp = is_null( $request['dp'] ) ? wc_get_price_decimals() : absint( $request['dp'] );
// Format decimal values.
foreach ( $format_decimal as $key ) {
$data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] );
$data[ $key ] = wc_format_decimal( $data[ $key ], $dp );
}
// Format date values.
@ -153,7 +159,7 @@ class OrderRefunds extends Orders {
// Format line items.
foreach ( $format_line_items as $key ) {
$data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) );
$data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ], $request ) );
}
return array(
@ -170,167 +176,56 @@ class OrderRefunds extends Orders {
}
/**
* Prepare a single order output for response.
* Expands an order item to get its data.
*
* @since 3.0.0
*
* @param \WC_Data $object Object data.
* @param \WP_REST_Request $request Request object.
*
* @return \WP_Error|\WP_REST_Response
* @param \WC_Order_item $item Order item data.
* @param \WP_REST_Response $response The response object.
* @return array
*/
public function prepare_object_for_response( $object, $request ) {
$this->request = $request;
$this->request['dp'] = is_null( $this->request['dp'] ) ? wc_get_price_decimals() : absint( $this->request['dp'] );
$order = wc_get_order( (int) $request['order_id'] );
protected function get_order_item_data( $item, $request ) {
$data = $item->get_data();
$format_decimal = array( 'subtotal', 'subtotal_tax', 'total', 'total_tax', 'tax_total', 'shipping_tax_total' );
$dp = is_null( $request['dp'] ) ? wc_get_price_decimals() : absint( $request['dp'] );
if ( ! $order ) {
return new \WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 );
}
if ( ! $object || $object->get_parent_id() !== $order->get_id() ) {
return new \WP_Error( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 );
}
$data = $this->get_data_for_response( $object, $request );
$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( $object, $request ) );
/**
* Filter the data for a response.
*
* The dynamic portion of the hook name, $this->post_type,
* refers to object type being prepared for the response.
*
* @param \WP_REST_Response $response The response object.
* @param \WC_Data $object Object data.
* @param \WP_REST_Request $request Request object.
*/
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request );
}
/**
* Prepare a single order refund output for response.
*
* @param \WP_Post $post Post object.
* @param \WP_REST_Request $request Request object.
*
* @return \WP_Error|\WP_REST_Response
*/
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 = is_null( $request['dp'] ) ? wc_get_price_decimals() : absint( $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 = $item->get_product();
$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();
// Format decimal values.
foreach ( $format_decimal as $key ) {
if ( isset( $data[ $key ] ) ) {
$data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] );
}
}
$item_meta = array();
// 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_quantity() ? $item->get_total() / $item->get_quantity() : 0;
}
$hideprefix = 'true' === $request['all_item_meta'] ? null : '_';
// Format taxes.
if ( ! empty( $data['taxes']['total'] ) ) {
$taxes = array();
foreach ( $item->get_formatted_meta_data( $hideprefix, true ) as $meta_key => $formatted_meta ) {
$item_meta[] = array(
'key' => $formatted_meta->key,
'label' => $formatted_meta->display_key,
'value' => wc_clean( $formatted_meta->display_value ),
foreach ( $data['taxes']['total'] as $tax_rate_id => $tax ) {
$taxes[] = array(
'id' => $tax_rate_id,
'total' => $tax,
'subtotal' => isset( $data['taxes']['subtotal'][ $tax_rate_id ] ) ? $data['taxes']['subtotal'][ $tax_rate_id ] : '',
);
}
$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;
$data['taxes'] = $taxes;
} elseif ( isset( $data['taxes'] ) ) {
$data['taxes'] = array();
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Remove names for coupons, taxes and shipping.
if ( isset( $data['code'] ) || isset( $data['rate_code'] ) || isset( $data['method_title'] ) ) {
unset( $data['name'] );
}
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
// Remove props we don't want to expose.
unset( $data['order_id'] );
unset( $data['type'] );
$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 );
return $data;
}
/**

View File

@ -40,13 +40,6 @@ class Orders extends AbstractObjectsController {
*/
protected $hierarchical = true;
/**
* Stores the request.
*
* @var array
*/
protected $request = array();
/**
* Get object. Return false if object is not of required type.
*
@ -65,89 +58,20 @@ class Orders extends AbstractObjectsController {
}
/**
* Expands an order item to get its data.
* Get data for this object in the format of this endpoint's schema.
*
* @param \WC_Order_item $item Order item data.
* @return array
* @param \WP_Comment $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
protected function get_order_item_data( $item ) {
$data = $item->get_data();
$format_decimal = array( 'subtotal', 'subtotal_tax', 'total', 'total_tax', 'tax_total', 'shipping_tax_total' );
// Format decimal values.
foreach ( $format_decimal as $key ) {
if ( isset( $data[ $key ] ) ) {
$data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] );
}
}
// 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_quantity() ? $item->get_total() / $item->get_quantity() : 0;
}
// Format taxes.
if ( ! empty( $data['taxes']['total'] ) ) {
$taxes = array();
foreach ( $data['taxes']['total'] as $tax_rate_id => $tax ) {
$taxes[] = array(
'id' => $tax_rate_id,
'total' => $tax,
'subtotal' => isset( $data['taxes']['subtotal'][ $tax_rate_id ] ) ? $data['taxes']['subtotal'][ $tax_rate_id ] : '',
);
}
$data['taxes'] = $taxes;
} elseif ( isset( $data['taxes'] ) ) {
$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'] );
return $data;
}
/**
* Prepare a single order output for response.
*
* @since 3.0.0
* @param \WC_Data $object Object data.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response
*/
public function prepare_object_for_response( $object, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$order_response = new OrderResponse();
protected function get_data_for_response( $object, $request ) {
$formatter = new OrderResponse();
if ( ! is_null( $request['dp'] ) ) {
$order_response->set_dp( $request['dp'] );
$formatter->set_dp( $request['dp'] );
}
$data = $order_response->prepare_response( $object, $context );
$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( $object, $request ) );
/**
* Filter the data for a response.
*
* The dynamic portion of the hook name, $this->post_type,
* refers to object type being prepared for the response.
*
* @param \WP_REST_Response $response The response object.
* @param \WC_Data $object Object data.
* @param \WP_REST_Request $request Request object.
*/
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request );
return $formatter->prepare_response( $object, $this->get_request_context( $request ) );
}
/**

View File

@ -131,43 +131,23 @@ class ProductAttributeTerms extends AbstractTermsContoller {
}
/**
* Prepare a single product attribute term output for response.
* Get data for this object in the format of this endpoint's schema.
*
* @param \WP_Term $item Term object.
* @param \WP_REST_Request $request Request params.
* @return \WP_REST_Response $response
* @param \WP_Term $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
public function prepare_item_for_response( $item, $request ) {
// Get term order.
$menu_order = get_term_meta( $item->term_id, 'order_' . $this->taxonomy, true );
protected function get_data_for_response( $object, $request ) {
$menu_order = get_term_meta( $object->term_id, 'order_' . $this->taxonomy, true );
$data = array(
'id' => (int) $item->term_id,
'name' => $item->name,
'slug' => $item->slug,
'description' => $item->description,
return array(
'id' => (int) $object->term_id,
'name' => $object->name,
'slug' => $object->slug,
'description' => $object->description,
'menu_order' => (int) $menu_order,
'count' => (int) $item->count,
'count' => (int) $object->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 );
}
/**

View File

@ -37,6 +37,15 @@ class ProductAttributes extends AbstractController {
*/
protected $attribute = '';
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'product_attribute';
/**
* Register the routes for product attributes.
*/
@ -299,40 +308,21 @@ class ProductAttributes extends AbstractController {
}
/**
* Prepare a single product attribute output for response.
* Get data for this object in the format of this endpoint's schema.
*
* @param obj $item Term object.
* @param \WP_REST_Request $request Request params.
* @return \WP_REST_Response $response
* @param object $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
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,
protected function get_data_for_response( $object, $request ) {
return array(
'id' => (int) $object->attribute_id,
'name' => $object->attribute_label,
'slug' => wc_attribute_taxonomy_name( $object->attribute_name ),
'type' => $object->attribute_type,
'order_by' => $object->attribute_orderby,
'has_archives' => (bool) $object->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, $request ) );
/**
* 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 );
}
/**

View File

@ -31,33 +31,37 @@ class ProductCategories extends AbstractTermsContoller {
protected $taxonomy = 'product_cat';
/**
* Prepare a single product category output for response.
* Singular name for resource type.
*
* @param WP_Term $item Term object.
* @param \WP_REST_Request $request Request instance.
* @return \WP_REST_Response
* Used in filter/action names for single resources.
*
* @var string
*/
public function prepare_item_for_response( $item, $request ) {
// Get category display type.
$display_type = get_term_meta( $item->term_id, 'display_type', true );
protected $singular = 'product_cat';
// Get category order.
$menu_order = get_term_meta( $item->term_id, 'order', true );
$data = array(
'id' => (int) $item->term_id,
'name' => $item->name,
'slug' => $item->slug,
'parent' => (int) $item->parent,
'description' => $item->description,
/**
* Get data for this object in the format of this endpoint's schema.
*
* @param \WP_Term $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
protected function get_data_for_response( $object, $request ) {
$display_type = get_term_meta( $object->term_id, 'display_type', true );
$menu_order = get_term_meta( $object->term_id, 'order', true );
$image_id = get_term_meta( $object->term_id, 'thumbnail_id', true );
$data = array(
'id' => (int) $object->term_id,
'name' => $object->name,
'slug' => $object->slug,
'parent' => (int) $object->parent,
'description' => $object->description,
'display' => $display_type ? $display_type : 'default',
'image' => null,
'menu_order' => (int) $menu_order,
'count' => (int) $item->count,
'count' => (int) $object->count,
);
// Get category image.
$image_id = get_term_meta( $item->term_id, 'thumbnail_id', true );
if ( $image_id ) {
$attachment = get_post( $image_id );
@ -72,31 +76,13 @@ class ProductCategories extends AbstractTermsContoller {
'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 );
return $data;
}
/**
* Update term meta fields.
*
* @param WP_Term $term Term object.
* @param \WP_Term $term Term object.
* @param \WP_REST_Request $request Request instance.
* @return bool|\WP_Error
*

View File

@ -31,39 +31,29 @@ class ProductShippingClasses extends AbstractTermsContoller {
protected $taxonomy = 'product_shipping_class';
/**
* Prepare a single product shipping class output for response.
* Singular name for resource type.
*
* @param object $item Term object.
* @param \WP_REST_Request $request Request params.
* @return \WP_REST_Response $response
* Used in filter/action names for single resources.
*
* @var string
*/
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,
protected $singular = 'product_shipping_class';
/**
* Get data for this object in the format of this endpoint's schema.
*
* @param \WP_Term $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
protected function get_data_for_response( $object, $request ) {
return array(
'id' => (int) $object->term_id,
'name' => $object->name,
'slug' => $object->slug,
'description' => $object->description,
'count' => (int) $object->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 );
}
/**

View File

@ -31,39 +31,29 @@ class ProductTags extends AbstractTermsContoller {
protected $taxonomy = 'product_tag';
/**
* Prepare a single product tag output for response.
* Singular name for resource type.
*
* @param object $item Term object.
* @param \WP_REST_Request $request Request params.
* @return \WP_REST_Response $response
* Used in filter/action names for single resources.
*
* @var string
*/
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,
protected $singular = 'product_tag';
/**
* Get data for this object in the format of this endpoint's schema.
*
* @param \WP_Term $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
protected function get_data_for_response( $object, $request ) {
return array(
'id' => (int) $object->term_id,
'name' => $object->name,
'slug' => $object->slug,
'description' => $object->description,
'count' => (int) $object->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 );
}
/**

View File

@ -34,6 +34,15 @@ class ProductVariations extends Products {
*/
protected $post_type = 'product_variation';
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'product_variation';
/**
* Register the routes for products.
*/
@ -582,32 +591,16 @@ class ProductVariations extends Products {
}
/**
* Prepare a single variation output for response.
* Get data for this object in the format of this endpoint's schema.
*
* @param \WC_Data $object Object data.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response
* @param \WC_Variation $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
public function prepare_object_for_response( $object, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$variation_response = new ProductVariationResponse();
$data = $variation_response->prepare_response( $object, $context );
$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( $object, $request ) );
protected function get_data_for_response( $object, $request ) {
$formatter = new ProductVariationResponse();
/**
* Filter the data for a response.
*
* The dynamic portion of the hook name, $this->post_type,
* refers to object type being prepared for the response.
*
* @param \WP_REST_Response $response The response object.
* @param \WC_Data $object Object data.
* @param \WP_REST_Request $request Request object.
*/
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request );
return $formatter->prepare_response( $object, $this->get_request_context( $request ) );
}
/**
@ -750,7 +743,7 @@ class ProductVariations extends Products {
// If we're forcing, then delete permanently.
if ( $force ) {
$previous = $this->prepare_object_for_response( $object, $request );
$previous = $this->prepare_item_for_response( $object, $request );
$object->delete( true );
@ -788,7 +781,7 @@ class ProductVariations extends Products {
$result = 'trash' === $object->get_status();
}
$response = $this->prepare_object_for_response( $object, $request );
$response = $this->prepare_item_for_response( $object, $request );
}
if ( ! $result ) {

View File

@ -41,6 +41,15 @@ class Products extends AbstractObjectsController {
*/
protected $hierarchical = true;
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'product';
/**
* Get the Product's schema, conforming to JSON Schema.
*
@ -822,34 +831,16 @@ class Products extends AbstractObjectsController {
}
/**
* Prepare a single product output for response.
* Get data for this object in the format of this endpoint's schema.
*
* @param \WC_Data $object Object data.
* @param \WC_Product $object Object to prepare.
* @param \WP_REST_Request $request Request object.
*
* @since 3.0.0
* @return \WP_REST_Response
* @return array Array of data in the correct format.
*/
public function prepare_object_for_response( $object, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$product_response = new ProductResponse();
$data = $product_response->prepare_response( $object, $context );
$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( $object, $request ) );
protected function get_data_for_response( $object, $request ) {
$formatter = new ProductResponse();
/**
* Filter the data for a response.
*
* The dynamic portion of the hook name, $this->post_type,
* refers to object type being prepared for the response.
*
* @param \WP_REST_Response $response The response object.
* @param \WC_Data $object Object data.
* @param \WP_REST_Request $request Request object.
*/
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request );
return $formatter->prepare_response( $object, $this->get_request_context( $request ) );
}
/**
@ -1175,7 +1166,7 @@ class Products extends AbstractObjectsController {
// If we're forcing, then delete permanently.
if ( $force ) {
$previous = $this->prepare_object_for_response( $object, $request );
$previous = $this->prepare_item_for_response( $object, $request );
$object->delete( true );
$result = 0 === $object->get_id();
@ -1217,7 +1208,7 @@ class Products extends AbstractObjectsController {
$result = 'trash' === $object->get_status();
}
$response = $this->prepare_object_for_response( $object, $request );
$response = $this->prepare_item_for_response( $object, $request );
}
if ( ! $result ) {

View File

@ -30,6 +30,15 @@ class Settings extends AbstractController {
*/
protected $resource_type = 'settings';
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'setting';
/**
* Register routes.
*
@ -121,26 +130,6 @@ class Settings extends AbstractController {
return $links;
}
/**
* Prepare a report sales object for serialization.
*
* @since 3.0.0
* @param array $item Group 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, $request ) );
return $response;
}
/**
* Filters out bad values from the groups array/filter so we
* only return known values via the API.

View File

@ -33,6 +33,15 @@ class SettingsOptions extends AbstractController {
*/
protected $rest_base = 'settings/(?P<group_id>[\w-]+)';
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'setting_option';
/**
* Register routes.
*
@ -354,20 +363,15 @@ class SettingsOptions extends AbstractController {
}
/**
* Prepare a single setting object for response.
* Get data for this object in the format of this endpoint's schema.
*
* @param object $item Setting object.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response Response data.
* @param \WC_Shipping_Method $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
public function prepare_item_for_response( $item, $request ) {
unset( $item['option_key'] );
$data = $this->filter_setting( $item );
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, empty( $request['context'] ) ? 'view' : $request['context'] );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $data, $request ) );
return $response;
protected function get_data_for_response( $object, $request ) {
unset( $object['option_key'] );
return $this->filter_setting( $object );
}
/**

View File

@ -30,6 +30,15 @@ class ShippingMethods extends AbstractController {
*/
protected $resource_type = 'shipping_methods';
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'shipping_method';
/**
* Register the route for /shipping_methods and /shipping_methods/<method>
*/
@ -109,36 +118,18 @@ class ShippingMethods extends AbstractController {
}
/**
* Prepare a shipping method for response.
* Get data for this object in the format of this endpoint's schema.
*
* @param \WC_Shipping_Method $method Shipping method object.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response Response data.
* @param \WC_Shipping_Method $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
public function prepare_item_for_response( $method, $request ) {
$data = array(
'id' => $method->id,
'title' => $method->method_title,
'description' => $method->method_description,
protected function get_data_for_response( $object, $request ) {
return array(
'id' => $object->id,
'title' => $object->method_title,
'description' => $object->method_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( $this->prepare_links( $method, $request ) );
/**
* Filter shipping methods object returned from the REST API.
*
* @param \WP_REST_Response $response The response object.
* @param \WC_Shipping_Method $method Shipping method object used to create response.
* @param \WP_REST_Request $request Request object.
*/
return apply_filters( 'woocommerce_rest_prepare_shipping_method', $response, $method, $request );
}
/**

View File

@ -16,6 +16,15 @@ defined( 'ABSPATH' ) || exit;
*/
class ShippingZoneLocations extends AbstractShippingZonesController {
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'shipping_zone_location';
/**
* Register the routes for Shipping Zone Locations.
*/
@ -64,7 +73,7 @@ class ShippingZoneLocations extends AbstractShippingZonesController {
$data = array();
foreach ( $locations as $location_obj ) {
$location = $this->prepare_item_for_response( $location_obj, $request );
$location = $this->prepare_item_for_response( (array) $location_obj, $request );
$location = $this->prepare_response_for_collection( $location );
$data[] = $location;
}
@ -115,26 +124,6 @@ class ShippingZoneLocations extends AbstractShippingZonesController {
return $this->get_items( $request );
}
/**
* Prepare the Shipping Zone Location for the REST response.
*
* @param array $item Shipping Zone Location.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response
*/
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 );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item, $request ) );
return $response;
}
/**
* Prepare links for the request.
*

View File

@ -19,6 +19,15 @@ use \WooCommerce\RestApi\Controllers\Version4\Utilities\SettingsTrait;
class ShippingZoneMethods extends AbstractShippingZonesController {
use SettingsTrait;
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'shipping_zone_method';
/**
* Register the routes for Shipping Zone Methods.
*/
@ -351,36 +360,36 @@ class ShippingZoneMethods extends AbstractShippingZonesController {
}
/**
* Prepare the Shipping Zone Method for the REST response.
* Get data for this object in the format of this endpoint's schema.
*
* @param array $item Shipping Zone Method.
* @param array $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response
* @return array Array of data in the correct format.
*/
protected function get_data_for_response( $object, $request ) {
return array(
'id' => $object->instance_id,
'instance_id' => $object->instance_id,
'title' => $object->instance_settings['title'],
'order' => $object->method_order,
'enabled' => ( 'yes' === $object->enabled ),
'method_id' => $object->id,
'method_title' => $object->method_title,
'method_description' => $object->method_description,
'settings' => $this->get_settings( $object ),
);
}
/**
* 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 ) {
$method = array(
'id' => $item->instance_id,
'instance_id' => $item->instance_id,
'title' => $item->instance_settings['title'],
'order' => $item->method_order,
'enabled' => ( 'yes' === $item->enabled ),
'method_id' => $item->id,
'method_title' => $item->method_title,
'method_description' => $item->method_description,
'settings' => $this->get_settings( $item ),
);
$context = empty( $request['context'] ) ? 'view' : $request['context'];
$data = $this->add_additional_fields_to_object( $method, $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( $item, $request ) );
$response = parent::prepare_item_for_response( $item, $request );
$response = $this->prepare_response_for_collection( $response );
return $response;
}

View File

@ -16,6 +16,15 @@ defined( 'ABSPATH' ) || exit;
*/
class ShippingZones extends AbstractShippingZonesController {
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'shipping_zone';
/**
* Register the routes for Shipping Zones.
*/
@ -230,29 +239,18 @@ class ShippingZones extends AbstractShippingZonesController {
}
/**
* Prepare the Shipping Zone for the REST response.
* Get data for this object in the format of this endpoint's schema.
*
* @param array $item Shipping Zone.
* @param \WP_Comment $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response
* @return array Array of data in the correct format.
*/
public function prepare_item_for_response( $item, $request ) {
$data = array(
'id' => (int) $item['id'],
'name' => $item['zone_name'],
'order' => (int) $item['zone_order'],
protected function get_data_for_response( $object, $request ) {
return array(
'id' => (int) $object['id'],
'name' => $object['zone_name'],
'order' => (int) $object['zone_order'],
);
$context = empty( $request['context'] ) ? 'view' : $request['context'];
$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( $data, $request ) );
return $response;
}
/**

View File

@ -169,7 +169,13 @@ class Webhooks extends AbstractController {
$webhook_ids = $results->webhooks;
foreach ( $webhook_ids as $webhook_id ) {
$data = $this->prepare_item_for_response( $webhook_id, $request );
$object = $this->get_object( $webhook_id );
if ( ! $object || 0 === $object->get_id() ) {
continue;
}
$data = $this->prepare_item_for_response( $object, $request );
$webhooks[] = $this->prepare_response_for_collection( $data );
}
@ -200,6 +206,22 @@ class Webhooks extends AbstractController {
return $response;
}
/**
* Get object.
*
* @param int $id Object ID.
* @return \WC_Webhook|bool
*/
protected function get_object( $id ) {
$webhook = wc_get_webhook( $id );
if ( empty( $webhook ) || is_null( $webhook ) ) {
return false;
}
return $webhook;
}
/**
* Get a single item.
*
@ -207,16 +229,13 @@ class Webhooks extends AbstractController {
* @return \WP_Error|\WP_REST_Response
*/
public function get_item( $request ) {
$id = (int) $request['id'];
$object = $this->get_object( (int) $request['id'] );
if ( empty( $id ) ) {
if ( ! $object || 0 === $object->get_id() ) {
return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
$data = $this->prepare_item_for_response( $id, $request );
$response = rest_ensure_response( $data );
return $response;
return $this->prepare_item_for_response( $object, $request );
}
/**
@ -268,7 +287,7 @@ class Webhooks extends AbstractController {
do_action( 'woocommerce_rest_insert_webhook_object', $webhook, $request, true );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $webhook->get_id(), $request );
$response = $this->prepare_item_for_response( $webhook, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $webhook->get_id() ) ) );
@ -348,7 +367,7 @@ class Webhooks extends AbstractController {
do_action( 'woocommerce_rest_insert_webhook_object', $webhook, $request, false );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $webhook->get_id(), $request );
$response = $this->prepare_item_for_response( $webhook, $request );
return rest_ensure_response( $response );
}
@ -455,51 +474,27 @@ class Webhooks extends AbstractController {
}
/**
* Prepare a single webhook output for response.
* Get data for this object in the format of this endpoint's schema.
*
* @param int $id Webhook ID.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response
* @param \WP_Comment $object Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Array of data in the correct format.
*/
public function prepare_item_for_response( $id, $request ) {
$webhook = wc_get_webhook( $id );
if ( empty( $webhook ) || is_null( $webhook ) ) {
return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
}
$data = array(
'id' => $webhook->get_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_date_created(), false ),
'date_created_gmt' => wc_rest_prepare_date_response( $webhook->get_date_created() ),
'date_modified' => wc_rest_prepare_date_response( $webhook->get_date_modified(), false ),
'date_modified_gmt' => wc_rest_prepare_date_response( $webhook->get_date_modified() ),
protected function get_data_for_response( $object, $request ) {
return array(
'id' => $object->get_id(),
'name' => $object->get_name(),
'status' => $object->get_status(),
'topic' => $object->get_topic(),
'resource' => $object->get_resource(),
'event' => $object->get_event(),
'hooks' => $object->get_hooks(),
'delivery_url' => $object->get_delivery_url(),
'date_created' => wc_rest_prepare_date_response( $object->get_date_created(), false ),
'date_created_gmt' => wc_rest_prepare_date_response( $object->get_date_created() ),
'date_modified' => wc_rest_prepare_date_response( $object->get_date_modified(), false ),
'date_modified_gmt' => wc_rest_prepare_date_response( $object->get_date_modified() ),
);
$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( $webhook, $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 );
}
/**
@ -694,4 +689,14 @@ class Webhooks extends AbstractController {
return $params;
}
/**
* Return suffix for item action hooks.
*
* @param mixed $item Object used to create response.
* @return string
*/
protected function get_hook_suffix( $item ) {
return $this->post_type;
}
}