WC_Webhook::deliver_pingpublicWC 2.2.0

Send a test ping to the delivery URL, sent when the webhook is first created.

Method of the class: WC_Webhook{}

Hooks from the method

Returns

true|false|WP_Error.

Usage

$WC_Webhook = new WC_Webhook();
$WC_Webhook->deliver_ping();

Changelog

Since 2.2.0 Introduced.

WC_Webhook::deliver_ping() code WC 10.5.0

public function deliver_ping() {
	$args = array(
		'user-agent' => sprintf( 'WooCommerce/%s Hookshot (WordPress/%s)', Constants::get_constant( 'WC_VERSION' ), $GLOBALS['wp_version'] ),
		'body'       => 'webhook_id=' . $this->get_id(),
	);

	/**
	 * Filters the HTTP arguments for the webhook delivery ping.
	 *
	 * @since 3.3.0
	 * @param array $args The HTTP arguments.
	 * @param null  $arg The first hook argument. Null since this is a ping.
	 * @param int   $webhook_id The webhook ID.
	 * @return array The filtered HTTP arguments.
	 */
	$http_args = apply_filters( 'woocommerce_webhook_http_args', $args, null, $this->get_id() );

	$test          = wp_safe_remote_post( $this->get_delivery_url(), $http_args );
	$response_code = wp_remote_retrieve_response_code( $test );

	if ( is_wp_error( $test ) ) {
		/* translators: error message */
		return new WP_Error( 'error', sprintf( __( 'Error: Delivery URL cannot be reached: %s', 'woocommerce' ), $test->get_error_message() ) );
	}

	if ( 200 !== $response_code ) {
		/* translators: error message */
		return new WP_Error( 'error', sprintf( __( 'Error: Delivery URL returned response code: %s', 'woocommerce' ), absint( $response_code ) ) );
	}

	$this->set_pending_delivery( false );
	$this->save();

	return true;
}