WC_Webhook::log_delivery()publicWC 2.2.0

Log the delivery request/response.

Method of the class: WC_Webhook{}

No Hooks.

Return

null. Nothing (null).

Usage

$WC_Webhook = new WC_Webhook();
$WC_Webhook->log_delivery( $delivery_id, $request, $response, $duration );
$delivery_id(string) (required)
Previously created hash.
$request(array) (required)
Request data.
$response(array|WP_Error) (required)
Response data.
$duration(float) (required)
Request duration.

Changelog

Since 2.2.0 Introduced.

WC_Webhook::log_delivery() code WC 8.6.1

public function log_delivery( $delivery_id, $request, $response, $duration ) {
	$logger  = wc_get_logger();
	$message = array(
		'Webhook Delivery' => array(
			'Delivery ID' => $delivery_id,
			'Date'        => date_i18n( __( 'M j, Y @ G:i', 'woocommerce' ), strtotime( 'now' ), true ),
			'URL'         => $this->get_delivery_url(),
			'Duration'    => $duration,
			'Request'     => array(
				'Method'  => $request['method'],
				'Headers' => array_merge(
					array(
						'User-Agent' => $request['user-agent'],
					),
					$request['headers']
				),
			),
			'Body'        => wp_slash( $request['body'] ),
		),
	);

	// Parse response.
	if ( is_wp_error( $response ) ) {
		$response_code    = $response->get_error_code();
		$response_message = $response->get_error_message();
		$response_headers = array();
		$response_body    = '';
	} else {
		$response_code    = wp_remote_retrieve_response_code( $response );
		$response_message = wp_remote_retrieve_response_message( $response );
		$response_headers = wp_remote_retrieve_headers( $response );
		$response_body    = wp_remote_retrieve_body( $response );
	}

	$message['Webhook Delivery']['Response'] = array(
		'Code'    => $response_code,
		'Message' => $response_message,
		'Headers' => $response_headers,
		'Body'    => $response_body,
	);

	if ( ! Constants::is_true( 'WP_DEBUG' ) ) {
		$message['Webhook Delivery']['Body']             = 'Webhook body is not logged unless WP_DEBUG mode is turned on. This is to avoid the storing of personal data in the logs.';
		$message['Webhook Delivery']['Response']['Body'] = 'Webhook body is not logged unless WP_DEBUG mode is turned on. This is to avoid the storing of personal data in the logs.';
	}

	$logger->info(
		wc_print_r( $message, true ),
		array(
			'source' => 'webhooks-delivery',
		)
	);

	// Track failures.
	// Check for a success, which is a 2xx, 301 or 302 Response Code.
	if ( intval( $response_code ) >= 200 && intval( $response_code ) < 303 ) {
		$this->set_failure_count( 0 );
		$this->save();
	} else {
		$this->failed_delivery();
	}
}