WC_REST_Webhooks_V2_Controller::prepare_item_for_response()publicWC 1.0

Prepare a single webhook output for response.

Метод класса: WC_REST_Webhooks_V2_Controller{}

Хуки из метода

Возвращает

WP_REST_Response. $response

Использование

$WC_REST_Webhooks_V2_Controller = new WC_REST_Webhooks_V2_Controller();
$WC_REST_Webhooks_V2_Controller->prepare_item_for_response( $id, $request );
$id(int) (обязательный)
Webhook ID.
$request(WP_REST_Request) (обязательный)
Request object.

Код WC_REST_Webhooks_V2_Controller::prepare_item_for_response() WC 9.3.3

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(), 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() ),
	);

	$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(), $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 );
}