WC_Webhook::build_payloadpublicWC 2.2.0

Build the payload data for the webhook.

Method of the class: WC_Webhook{}

Hooks from the method

Returns

Mixed. Payload data.

Usage

$WC_Webhook = new WC_Webhook();
$WC_Webhook->build_payload( $resource_id );
$resource_id(mixed) (required)
First hook argument, typically the resource ID.

Changelog

Since 2.2.0 Introduced.

WC_Webhook::build_payload() code WC 10.9.3

public function build_payload( $resource_id ) {
	// Build the payload with the same user context as the user who created
	// the webhook -- this avoids permission errors as background processing
	// runs with no user context.
	$current_user = get_current_user_id();
	wp_set_current_user( $this->get_user_id() );

	try {
		$resource = $this->get_resource();
		$event    = $this->get_event();

		// If a resource has been deleted, just include the ID in the payload.
		$payload = array(
			'id' => $resource_id,
		);

		if ( 'deleted' !== $event ) {
			$api_version = $this->get_api_version();

			if ( in_array( $api_version, wc_get_webhook_rest_api_versions(), true ) ) {
				$payload = $this->get_wp_api_payload( $resource, $resource_id, $event );
			} elseif ( 'legacy_v3' === $api_version && WC()->legacy_rest_api_is_available() ) {
				wc_deprecated_function( 'Webhook delivery via the Legacy REST API', '9.0.0', 'editing the webhook to use a current API version' );
				$payload = wc()->api->get_webhook_api_payload( $resource, $resource_id, $event );
			} else {
				throw new \Exception( esc_html__( 'Unsupported webhook API version. Please edit this webhook to use a current REST API version.', 'woocommerce' ) );
			}
		}

		/**
		 * Filters the webhook payload before delivery.
		 *
		 * @since 2.2.0
		 * @param mixed  $payload     Payload data.
		 * @param string $resource    Resource type (e.g. 'order').
		 * @param mixed  $resource_id Resource ID.
		 * @param int    $webhook_id  Webhook ID.
		 */
		return apply_filters( 'woocommerce_webhook_payload', $payload, $resource, $resource_id, $this->get_id() );
	} finally {
		// Restore the current user.
		wp_set_current_user( $current_user );
	}
}