Standardisation of prepare_item and prepare_links

This commit is contained in:
Mike Jolley 2019-06-18 10:44:41 +01:00
parent 5bd17af0da
commit d2e4c199f4
30 changed files with 404 additions and 415 deletions

View File

@ -49,6 +49,15 @@ abstract class AbstractController extends WP_REST_Controller {
*/
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.
*
@ -246,4 +255,63 @@ abstract class AbstractController extends WP_REST_Controller {
}
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();
}
}

View File

@ -531,14 +531,14 @@ abstract class AbstractObjectsController extends AbstractController {
/**
* Prepare links for the request.
*
* @param \WC_Data $object Object data.
* @param mixed $item Object to prepare.
* @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(
'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(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),

View File

@ -517,11 +517,11 @@ abstract class AbstractTermsContoller extends AbstractController {
/**
* Prepare links for the request.
*
* @param object $term Term object.
* @param \WP_REST_Request $request Full details about the request.
* @return array Links for the given term.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $term, $request ) {
protected function prepare_links( $item, $request ) {
$base = '/' . $this->namespace . '/' . $this->rest_base;
if ( ! empty( $request['attribute_id'] ) ) {
@ -530,15 +530,15 @@ abstract class AbstractTermsContoller extends AbstractController {
$links = array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . $term->term_id ),
'href' => rest_url( trailingslashit( $base ) . $item->term_id ),
),
'collection' => array(
'href' => rest_url( $base ),
),
);
if ( $term->parent ) {
$parent_term = get_term( (int) $term->parent, $term->taxonomy );
if ( $item->parent ) {
$parent_term = get_term( (int) $item->parent, $item->taxonomy );
if ( $parent_term ) {
$links['up'] = array(
'href' => rest_url( trailingslashit( $base ) . $parent_term->term_id ),

View File

@ -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 \WC_Data $object WC_Data instance.
* @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_formatted_item_data( $object ) {
protected function get_data_for_response( $object, $request ) {
$data = $object->get_data();
$format_decimal = array( 'amount', 'minimum_amount', 'maximum_amount' );
@ -112,7 +112,7 @@ class Coupons extends AbstractObjectsController {
* @return \WP_REST_Response
*/
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';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );

View File

@ -32,6 +32,15 @@ class CustomerDownloads extends AbstractController {
*/
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.
*/
@ -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.
* @return \WP_REST_Response $response Response data.
* @return array Array of data in the correct format.
*/
public function prepare_item_for_response( $download, $request ) {
$data = array(
'download_id' => $download->download_id,
'download_url' => $download->download_url,
'product_id' => $download->product_id,
'product_name' => $download->product_name,
'download_name' => $download->download_name,
'order_id' => $download->order_id,
'order_key' => $download->order_key,
'downloads_remaining' => '' === $download->downloads_remaining ? 'unlimited' : $download->downloads_remaining,
'access_expires' => $download->access_expires ? wc_rest_prepare_date_response( $download->access_expires ) : 'never',
'access_expires_gmt' => $download->access_expires ? wc_rest_prepare_date_response( get_gmt_from_date( $download->access_expires ) ) : 'never',
'file' => $download->file,
protected function get_data_for_response( $object, $request ) {
return array(
'download_id' => $object->download_id,
'download_url' => $object->download_url,
'product_id' => $object->product_id,
'product_name' => $object->product_name,
'download_name' => $object->download_name,
'order_id' => $object->order_id,
'order_key' => $object->order_key,
'downloads_remaining' => '' === $object->downloads_remaining ? 'unlimited' : $object->downloads_remaining,
'access_expires' => $object->access_expires ? wc_rest_prepare_date_response( $object->access_expires ) : 'never',
'access_expires_gmt' => $object->access_expires ? wc_rest_prepare_date_response( get_gmt_from_date( $object->access_expires ) ) : 'never',
'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.
*
* @param \stdClass $download Download object.
* @param mixed $item Object to prepare.
* @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 );
$links = array(
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
),
'product' => array(
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $download->product_id ) ),
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $item->product_id ) ),
),
'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 ) ),
),
);

View File

@ -34,6 +34,15 @@ class Customers extends AbstractController {
*/
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.
*/
@ -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_REST_Request $request Request object.
* @return \WP_REST_Response $response Response data.
* @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( $customer, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$customer_response = new CustomerResponse();
protected function get_data_for_response( $object, $request ) {
$formatter = new CustomerResponse();
$data = $customer_response->prepare_response( $customer, $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( $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 );
return $formatter->prepare_response( $object, $this->get_request_context( $request ) );
}
/**
* Prepare links for the request.
*
* @param \WC_Customer $customer Customer object.
* @return array Links for the given customer.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $customer ) {
protected function prepare_links( $item, $request ) {
$links = 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(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
),
);
return $links;
}

View File

@ -30,6 +30,15 @@ class Data extends AbstractController {
*/
protected $resource_type = 'settings';
/**
* Singular name for resource type.
*
* Used in filter/action names for single resources.
*
* @var string
*/
protected $singular = 'data';
/**
* 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 \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response Response data.
* @param \stdClass $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( $resource, $request ) {
$data = array(
'slug' => $resource->slug,
'description' => $resource->description,
protected function get_data_for_response( $object, $request ) {
return array(
'slug' => $object->slug,
'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.
*
* @param object $item Data object.
* @return array Links for the given country.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $item ) {
protected function prepare_links( $item, $request ) {
$links = array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $item->slug ) ),

View File

@ -198,7 +198,7 @@ class Continents extends DataController {
$data = $this->filter_response_by_context( $data, 'view' );
$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.
@ -215,10 +215,11 @@ class Continents extends DataController {
/**
* Prepare links for the request.
*
* @param object $item Data object.
* @return array Links for the given continent.
* @param mixed $item Object to prepare.
* @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'] );
$links = array(
'self' => array(

View File

@ -145,7 +145,7 @@ class Countries extends DataController {
$data = $this->filter_response_by_context( $data, 'view' );
$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.
@ -162,10 +162,11 @@ class Countries extends DataController {
/**
* Prepare links for the request.
*
* @param object $item Data object.
* @return array Links for the given country.
* @param mixed $item Object to prepare.
* @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'] );
$links = array(
'self' => array(

View File

@ -151,7 +151,7 @@ class Currencies extends DataController {
$data = $this->filter_response_by_context( $data, 'view' );
$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.
@ -166,10 +166,11 @@ class Currencies extends DataController {
/**
* Prepare links for the request.
*
* @param object $item Data object.
* @return array Links for the given currency.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $item ) {
protected function prepare_links( $item, $request ) {
$code = strtoupper( $item['code'] );
$links = array(
'self' => array(

View File

@ -93,7 +93,7 @@ class DownloadIPs extends DataController {
$data = $this->filter_response_by_context( $data, 'view' );
$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.
@ -108,10 +108,11 @@ class DownloadIPs extends DataController {
/**
* Prepare links for the request.
*
* @param object $item Data object.
* @return array Links for the given object.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $item ) {
protected function prepare_links( $item, $request ) {
$links = array(
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),

View File

@ -32,6 +32,15 @@ class OrderNotes extends AbstractController {
*/
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.
*/
@ -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.
* @return \WP_REST_Response $response Response data.
* @return array Array of data in the correct format.
*/
public function prepare_item_for_response( $note, $request ) {
$data = array(
'id' => (int) $note->comment_ID,
'author' => __( 'WooCommerce', 'woocommerce' ) === $note->comment_author ? 'system' : $note->comment_author,
'date_created' => wc_rest_prepare_date_response( $note->comment_date ),
'date_created_gmt' => wc_rest_prepare_date_response( $note->comment_date_gmt ),
'note' => $note->comment_content,
'customer_note' => (bool) get_comment_meta( $note->comment_ID, 'is_customer_note', true ),
protected function get_data_for_response( $object, $request ) {
return array(
'id' => (int) $object->comment_ID,
'author' => __( 'WooCommerce', 'woocommerce' ) === $object->comment_author ? 'system' : $object->comment_author,
'date_created' => wc_rest_prepare_date_response( $object->comment_date ),
'date_created_gmt' => wc_rest_prepare_date_response( $object->comment_date_gmt ),
'note' => $object->comment_content,
'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.
*
* @param WP_Comment $note Delivery order_note object.
* @return array Links for the given order note.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $note ) {
$order_id = (int) $note->comment_post_ID;
protected function prepare_links( $item, $request ) {
$order_id = (int) $item->comment_post_ID;
$base = str_replace( '(?P<order_id>[\d]+)', $order_id, $this->rest_base );
$links = array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $note->comment_ID ) ),
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $item->comment_ID ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),

View File

@ -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 \WC_Data $object WC_Data instance.
* @return array
* @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_formatted_item_data( $object ) {
protected function get_data_for_response( $object, $request ) {
$data = $object->get_data();
$format_decimal = array( 'amount' );
$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 );
}
$data = $this->get_formatted_item_data( $object );
$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 );
@ -336,21 +336,21 @@ class OrderRefunds extends Orders {
/**
* Prepare links for the request.
*
* @param \WC_Data $object Object data.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array Links for the given post.
* @return array
*/
protected function prepare_links( $object, $request ) {
$base = str_replace( '(?P<order_id>[\d]+)', $object->get_parent_id(), $this->rest_base );
protected function prepare_links( $item, $request ) {
$base = str_replace( '(?P<order_id>[\d]+)', $item->get_parent_id(), $this->rest_base );
$links = 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(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
),
'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() ) ),
),
);

View File

@ -153,29 +153,29 @@ class Orders extends AbstractObjectsController {
/**
* Prepare links for the request.
*
* @param \WC_Data $object Object data.
* @param mixed $item Object to prepare.
* @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(
'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(
'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(
'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(
'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() ) ),
);
}

View File

@ -33,6 +33,15 @@ class PaymentGateways extends AbstractController {
*/
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>
*/
@ -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 \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response Response data.
* @param \WC_Payment_Gateway $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( $gateway, $request ) {
protected function get_data_for_response( $object, $request ) {
$order = (array) get_option( 'woocommerce_gateway_order' );
$item = array(
'id' => $gateway->id,
'title' => $gateway->title,
'description' => $gateway->description,
'order' => isset( $order[ $gateway->id ] ) ? $order[ $gateway->id ] : '',
'enabled' => ( 'yes' === $gateway->enabled ),
'method_title' => $gateway->get_method_title(),
'method_description' => $gateway->get_method_description(),
'method_supports' => $gateway->supports,
'settings' => $this->get_settings( $gateway ),
return array(
'id' => $object->id,
'title' => $object->title,
'description' => $object->description,
'order' => isset( $order[ $object->id ] ) ? $order[ $object->id ] : '',
'enabled' => ( 'yes' === $object->enabled ),
'method_title' => $object->get_method_title(),
'method_description' => $object->get_method_description(),
'method_supports' => $object->supports,
'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.
*
* @param WC_Payment_Gateway $gateway Payment gateway object.
* @param \WP_REST_Request $request Request object.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $gateway, $request ) {
protected function prepare_links( $item, $request ) {
$links = 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(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),

View File

@ -321,7 +321,7 @@ class ProductAttributes extends AbstractController {
$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.
@ -338,14 +338,15 @@ class ProductAttributes extends AbstractController {
/**
* Prepare links for the request.
*
* @param object $attribute Attribute object.
* @return array Links for the given attribute.
* @param mixed $item Object to prepare.
* @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;
$links = array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . $attribute->attribute_id ),
'href' => rest_url( trailingslashit( $base ) . $item->attribute_id ),
),
'collection' => array(
'href' => rest_url( $base ),

View File

@ -33,6 +33,15 @@ class ProductReviews extends AbstractController {
*/
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.
*/
@ -391,7 +400,6 @@ class ProductReviews extends AbstractController {
$request->set_param( 'context', $context );
$response = $this->prepare_item_for_response( $review, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$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;
}
$data = $this->prepare_item_for_response( $review, $request );
$response = rest_ensure_response( $data );
return $response;
return $this->prepare_item_for_response( $review, $request );
}
/**
@ -498,9 +503,7 @@ class ProductReviews extends AbstractController {
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $review, $request );
return rest_ensure_response( $response );
return $this->prepare_item_for_response( $review, $request );
}
/**
@ -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.
* @return \WP_REST_Response $response Response data.
* @return array Array of data in the correct format.
*/
public function prepare_item_for_response( $review, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$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 );
protected function get_data_for_response( $object, $request ) {
$formatter = new ProductReviewResponse();
$response->add_links( $this->prepare_links( $review ) );
/**
* 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 );
return $formatter->prepare_response( $object, $this->get_request_context( $request ) );
}
/**
@ -657,27 +644,28 @@ class ProductReviews extends AbstractController {
/**
* Prepare links for the request.
*
* @param WP_Comment $review Product review object.
* @return array Links for the given product review.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $review ) {
protected function prepare_links( $item, $request ) {
$links = 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(
'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(
'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,
);
}
if ( 0 !== (int) $review->user_id ) {
if ( 0 !== (int) $item->user_id ) {
$links['reviewer'] = array(
'href' => rest_url( 'wp/v2/users/' . $review->user_id ),
'href' => rest_url( 'wp/v2/users/' . $item->user_id ),
'embeddable' => true,
);
}

View File

@ -847,16 +847,16 @@ class ProductVariations extends Products {
/**
* Prepare links for the request.
*
* @param \WC_Data $object Object data.
* @param mixed $item Object to prepare.
* @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'];
$base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
$links = array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $object->get_id() ) ),
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $item->get_id() ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),

View File

@ -1092,24 +1092,23 @@ class Products extends AbstractObjectsController {
/**
* Prepare links for the request.
*
* @param \WC_Data $object Object data.
* @param mixed $item Object to prepare.
* @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(
'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(
'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(
'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.
);
}

View File

@ -106,14 +106,15 @@ class Settings extends AbstractController {
/**
* Prepare links for the request.
*
* @param string $group_id Group ID.
* @return array Links for the given group.
* @param mixed $item Object to prepare.
* @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;
$links = 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->add_links( $this->prepare_links( $item['id'] ) );
$response->add_links( $this->prepare_links( $item, $request ) );
return $response;
}

View File

@ -11,7 +11,7 @@ namespace WooCommerce\RestApi\Controllers\Version4;
defined( 'ABSPATH' ) || exit;
use \WooCommerce\RestApi\Controllers\Version4\Utilities\SettingsTrait;
use WooCommerce\RestApi\Controllers\Version4\Utilities\SettingsTrait;
/**
* 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->filter_response_by_context( $data, empty( $request['context'] ) ? 'view' : $request['context'] );
$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;
}
/**
* Prepare links for the request.
*
* @param string $setting_id Setting ID.
* @param string $group_id Group ID.
* @return array Links for the given setting.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $setting_id, $group_id ) {
$base = str_replace( '(?P<group_id>[\w-]+)', $group_id, $this->rest_base );
protected function prepare_links( $item, $request ) {
$base = str_replace( '(?P<group_id>[\w-]+)', $request['group_id'], $this->rest_base );
$links = 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(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),

View File

@ -144,14 +144,14 @@ class ShippingMethods extends AbstractController {
/**
* Prepare links for the request.
*
* @param \WC_Shipping_Method $method Shipping method object.
* @param \WP_REST_Request $request Request object.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $method, $request ) {
protected function prepare_links( $item, $request ) {
$links = 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(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),

View File

@ -130,7 +130,7 @@ class ShippingZoneLocations extends AbstractShippingZonesController {
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( (int) $request['id'] ) );
$response->add_links( $this->prepare_links( $item, $request ) );
return $response;
}
@ -138,11 +138,12 @@ class ShippingZoneLocations extends AbstractShippingZonesController {
/**
* Prepare links for the request.
*
* @param int $zone_id Given Shipping Zone ID.
* @return array Links for the given Shipping Zone Location.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $zone_id ) {
$base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $zone_id;
protected function prepare_links( $item, $request ) {
$base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $request['id'];
$links = array(
'collection' => array(
'href' => rest_url( $base . '/locations' ),

View File

@ -297,8 +297,8 @@ class ShippingZoneMethods extends AbstractShippingZonesController {
/**
* Updates settings, order, and enabled status on create.
*
* @param int $instance_id Instance ID.
* @param WC_Shipping_Method $method Shipping method data.
* @param int $instance_id Instance ID.
* @param \WC_Shipping_Method $method Shipping method data.
* @param \WP_REST_Request $request Request data.
*
* @return WC_Shipping_Method
@ -377,7 +377,7 @@ class ShippingZoneMethods extends AbstractShippingZonesController {
// Wrap the data in a response object.
$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 );
@ -416,15 +416,15 @@ class ShippingZoneMethods extends AbstractShippingZonesController {
/**
* Prepare links for the request.
*
* @param int $zone_id Given Shipping Zone ID.
* @param int $instance_id Given Shipping Zone Method Instance ID.
* @return array Links for the given Shipping Zone Method.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $zone_id, $instance_id ) {
$base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $zone_id;
protected function prepare_links( $item, $request ) {
$base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $request['zone_id'];
$links = array(
'self' => array(
'href' => rest_url( $base . '/methods/' . $instance_id ),
'href' => rest_url( $base . '/methods/' . $item->instance_id ),
),
'collection' => array(
'href' => rest_url( $base . '/methods' ),

View File

@ -250,7 +250,7 @@ class ShippingZones extends AbstractShippingZonesController {
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $data['id'] ) );
$response->add_links( $this->prepare_links( $data, $request ) );
return $response;
}
@ -258,20 +258,21 @@ class ShippingZones extends AbstractShippingZonesController {
/**
* Prepare links for the request.
*
* @param int $zone_id Given Shipping Zone ID.
* @return array Links for the given Shipping Zone.
* @param mixed $item Object to prepare.
* @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;
$links = array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . $zone_id ),
'href' => rest_url( trailingslashit( $base ) . $item['id'] ),
),
'collection' => array(
'href' => rest_url( $base ),
),
'describedby' => array(
'href' => rest_url( trailingslashit( $base ) . $zone_id . '/locations' ),
'href' => rest_url( trailingslashit( $base ) . $item['id'] . '/locations' ),
),
);

View File

@ -30,6 +30,15 @@ class SystemStatus extends AbstractController {
*/
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
*/
@ -585,26 +594,4 @@ class SystemStatus extends AbstractController {
'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 );
}
}

View File

@ -30,6 +30,15 @@ class SystemStatusTools extends AbstractController {
*/
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/*.
*/
@ -257,25 +266,6 @@ class SystemStatusTools extends AbstractController {
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.
*
@ -341,14 +331,15 @@ class SystemStatusTools extends AbstractController {
/**
* Prepare links for the request.
*
* @param string $id ID.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $id ) {
protected function prepare_links( $item, $request ) {
$base = '/' . $this->namespace . '/' . $this->rest_base;
$links = array(
'item' => array(
'href' => rest_url( trailingslashit( $base ) . $id ),
'href' => rest_url( trailingslashit( $base ) . $item['id'] ),
'embeddable' => true,
),
);

View File

@ -30,6 +30,15 @@ class TaxClasses extends AbstractController {
*/
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.
*/
@ -243,46 +252,19 @@ class TaxClasses extends AbstractController {
* @param \WP_REST_Response $response The response returned from the API.
* @param \WP_REST_Request $request The request sent to the API.
*/
do_action( 'woocommerce_rest_delete_tax', (object) $tax_class, $response, $request );
do_action( 'woocommerce_rest_delete_tax_class', (object) $tax_class, $response, $request );
return $response;
}
/**
* Prepare a single tax class output for response.
*
* @param array $tax_class Tax class data.
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $tax_class, $request ) {
$data = $tax_class;
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links() );
/**
* Filter tax object returned from the REST API.
*
* @param \WP_REST_Response $response The response object.
* @param \stdClass $tax_class Tax object used to create response.
* @param \WP_REST_Request $request Request object.
*/
return apply_filters( 'woocommerce_rest_prepare_tax', $response, (object) $tax_class, $request );
}
/**
* Prepare links for the request.
*
* @return array Links for the given tax class.
* @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(
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),

View File

@ -30,6 +30,15 @@ class Taxes extends AbstractController {
*/
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.
*/
@ -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.
* @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;
$id = (int) $tax->tax_rate_id;
$id = (int) $object->tax_rate_id;
$data = array(
'id' => $id,
'country' => $tax->tax_rate_country,
'state' => $tax->tax_rate_state,
'country' => $object->tax_rate_country,
'state' => $object->tax_rate_state,
'postcode' => '',
'city' => '',
'rate' => $tax->tax_rate,
'name' => $tax->tax_rate_name,
'priority' => (int) $tax->tax_rate_priority,
'compound' => (bool) $tax->tax_rate_compound,
'shipping' => (bool) $tax->tax_rate_shipping,
'order' => (int) $tax->tax_rate_order,
'class' => $tax->tax_rate_class ? $tax->tax_rate_class : 'standard',
'rate' => $object->tax_rate,
'name' => $object->tax_rate_name,
'priority' => (int) $object->tax_rate_priority,
'compound' => (bool) $object->tax_rate_compound,
'shipping' => (bool) $object->tax_rate_shipping,
'order' => (int) $object->tax_rate_order,
'class' => $object->tax_rate_class ? $object->tax_rate_class : 'standard',
);
// 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 ) {
$data[ $locale->location_type ] = $locale->location_code;
}
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $tax ) );
/**
* Filter tax object returned from the REST API.
*
* @param \WP_REST_Response $response The response object.
* @param \stdClass $tax Tax object used to create response.
* @param \WP_REST_Request $request Request object.
*/
return apply_filters( 'woocommerce_rest_prepare_tax', $response, $tax, $request );
return $data;
}
/**
* Prepare links for the request.
*
* @param \stdClass $tax Tax object.
* @return array Links for the given tax.
* @param mixed $item Object to prepare.
* @param \WP_REST_Request $request Request object.
* @return array
*/
protected function prepare_links( $tax ) {
protected function prepare_links( $item, $request ) {
$links = 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(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),

View File

@ -490,7 +490,7 @@ class Webhooks extends AbstractController {
// Wrap the data in a response object.
$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.
@ -505,13 +505,14 @@ class Webhooks extends AbstractController {
/**
* 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
*/
protected function prepare_links( $id ) {
protected function prepare_links( $item, $request ) {
$links = 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(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),