/** * Get the webhook for the given ID * * @since 2.2 * @param int $id webhook ID * @param array $fields * @return array */ public function get_webhook($id, $fields = null) { // ensure webhook ID is valid & user has permission to read $id = $this->validate_request($id, 'shop_webhook', 'read'); if (is_wp_error($id)) { return $id; } $webhook = new WC_Webhook($id); $webhook_data = array('id' => $webhook->id, 'name' => $webhook->get_name(), 'status' => $webhook->get_status(), 'topic' => $webhook->get_topic(), 'resource' => $webhook->get_resource(), 'event' => $webhook->get_event(), 'hooks' => $webhook->get_hooks(), 'delivery_url' => $webhook->get_delivery_url(), 'created_at' => $this->server->format_datetime($webhook->get_post_data()->post_date_gmt), 'updated_at' => $this->server->format_datetime($webhook->get_post_data()->post_modified_gmt)); return array('webhook' => apply_filters('woocommerce_api_webhook_response', $webhook_data, $webhook, $fields, $this)); }
/** * Setup payload for subscription webhook delivery. * * @since 2.0 */ public static function create_payload($payload, $resource, $resource_id, $id) { if ('subscription' == $resource && empty($payload) && wcs_is_subscription($resource_id)) { $webhook = new WC_Webhook($id); $event = $webhook->get_event(); $current_user = get_current_user_id(); wp_set_current_user($webhook->get_user_id()); WC()->api->WC_API_Subscriptions->register_routes(array()); $payload = WC()->api->WC_API_Subscriptions->get_subscription($resource_id); wp_set_current_user($current_user); } return $payload; }
/** * Ensure valid webhook data response. * * @since 2.2 * @param array $response * @param WC_Webhook $webhook */ protected function check_get_webhook_response($response, $webhook) { $this->assertEquals($webhook->id, $response['id']); $this->assertEquals($webhook->get_name(), $response['name']); $this->assertEquals($webhook->get_status(), $response['status']); $this->assertEquals($webhook->get_topic(), $response['topic']); $this->assertEquals($webhook->get_resource(), $response['resource']); $this->assertEquals($webhook->get_event(), $response['event']); $this->assertEquals($webhook->get_hooks(), $response['hooks']); $this->assertEquals($webhook->get_delivery_url(), $response['delivery_url']); $this->assertArrayHasKey('created_at', $response); $this->assertArrayHasKey('updated_at', $response); }
/** * Prepare a single webhook output for response. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response $response Response data. */ public function prepare_item_for_response($post, $request) { $id = (int) $post->ID; $webhook = new WC_Webhook($id); $data = array('id' => $webhook->id, 'name' => $webhook->get_name(), 'status' => $webhook->get_status(), 'topic' => $webhook->get_topic(), 'resource' => $webhook->get_resource(), 'event' => $webhook->get_event(), 'hooks' => $webhook->get_hooks(), 'delivery_url' => $webhook->get_delivery_url(), 'date_created' => wc_rest_prepare_date_response($webhook->get_post_data()->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($webhook->get_post_data()->post_modified_gmt)); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); // Wrap the data in a response object. $response = rest_ensure_response($data); $response->add_links($this->prepare_links($post)); /** * 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); }