WC_REST_Webhooks_V1_Controller::prepare_item_for_response()
Prepare a single webhook output for response.
Method of the class: WC_REST_Webhooks_V1_Controller{}
Hooks from the method
Return
WP_REST_Response
. $response Response data.
Usage
$WC_REST_Webhooks_V1_Controller = new WC_REST_Webhooks_V1_Controller(); $WC_REST_Webhooks_V1_Controller->prepare_item_for_response( $id, $request );
- $id(int) (required)
- Webhook ID or object.
- $request(WP_REST_Request) (required)
- Request object.
WC_REST_Webhooks_V1_Controller::prepare_item_for_response() WC REST Webhooks V1 Controller::prepare item for response code WC 9.8.1
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' => 404 ) ); } $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() ), 'date_modified' => wc_rest_prepare_date_response( $webhook->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->get_id() ) ); /** * 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 ); }