Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../includes
File: class-wc-webhook.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Webhook
[2] Fix | Delete
*
[3] Fix | Delete
* This class handles storing and retrieving webhook data from the associated.
[4] Fix | Delete
*
[5] Fix | Delete
* Webhooks are enqueued to their associated actions, delivered, and logged.
[6] Fix | Delete
*
[7] Fix | Delete
* @version 3.2.0
[8] Fix | Delete
* @package WooCommerce\Webhooks
[9] Fix | Delete
* @since 2.2.0
[10] Fix | Delete
*/
[11] Fix | Delete
[12] Fix | Delete
use Automattic\Jetpack\Constants;
[13] Fix | Delete
use Automattic\WooCommerce\Enums\OrderStatus;
[14] Fix | Delete
use Automattic\WooCommerce\Utilities\NumberUtil;
[15] Fix | Delete
use Automattic\WooCommerce\Utilities\OrderUtil;
[16] Fix | Delete
use Automattic\WooCommerce\Utilities\RestApiUtil;
[17] Fix | Delete
use Automattic\WooCommerce\Utilities\LoggingUtil;
[18] Fix | Delete
[19] Fix | Delete
defined( 'ABSPATH' ) || exit;
[20] Fix | Delete
[21] Fix | Delete
require_once __DIR__ . '/legacy/class-wc-legacy-webhook.php';
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Webhook class.
[25] Fix | Delete
*/
[26] Fix | Delete
class WC_Webhook extends WC_Legacy_Webhook {
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Store which object IDs this webhook has processed (ie scheduled to be delivered)
[30] Fix | Delete
* within the current page request.
[31] Fix | Delete
*
[32] Fix | Delete
* @var array
[33] Fix | Delete
*/
[34] Fix | Delete
protected $processed = array();
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* Stores webhook data.
[38] Fix | Delete
*
[39] Fix | Delete
* @var array
[40] Fix | Delete
*/
[41] Fix | Delete
protected $data = array(
[42] Fix | Delete
'date_created' => null,
[43] Fix | Delete
'date_modified' => null,
[44] Fix | Delete
'status' => 'disabled',
[45] Fix | Delete
'delivery_url' => '',
[46] Fix | Delete
'secret' => '',
[47] Fix | Delete
'name' => '',
[48] Fix | Delete
'topic' => '',
[49] Fix | Delete
'hooks' => '',
[50] Fix | Delete
'resource' => '',
[51] Fix | Delete
'event' => '',
[52] Fix | Delete
'failure_count' => 0,
[53] Fix | Delete
'user_id' => 0,
[54] Fix | Delete
'api_version' => 3,
[55] Fix | Delete
'pending_delivery' => false,
[56] Fix | Delete
);
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Load webhook data based on how WC_Webhook is called.
[60] Fix | Delete
*
[61] Fix | Delete
* @param WC_Webhook|int $data Webhook ID or data.
[62] Fix | Delete
* @throws Exception If webhook cannot be read/found and $data is set.
[63] Fix | Delete
*/
[64] Fix | Delete
public function __construct( $data = 0 ) {
[65] Fix | Delete
parent::__construct( $data );
[66] Fix | Delete
[67] Fix | Delete
if ( $data instanceof WC_Webhook ) {
[68] Fix | Delete
$this->set_id( absint( $data->get_id() ) );
[69] Fix | Delete
} elseif ( is_numeric( $data ) ) {
[70] Fix | Delete
$this->set_id( $data );
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
$this->data_store = WC_Data_Store::load( 'webhook' );
[74] Fix | Delete
[75] Fix | Delete
// If we have an ID, load the webhook from the DB.
[76] Fix | Delete
if ( $this->get_id() ) {
[77] Fix | Delete
try {
[78] Fix | Delete
$this->data_store->read( $this );
[79] Fix | Delete
} catch ( Exception $e ) {
[80] Fix | Delete
$this->set_id( 0 );
[81] Fix | Delete
$this->set_object_read( true );
[82] Fix | Delete
}
[83] Fix | Delete
} else {
[84] Fix | Delete
$this->set_object_read( true );
[85] Fix | Delete
}
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Enqueue the hooks associated with the webhook.
[90] Fix | Delete
*
[91] Fix | Delete
* @since 2.2.0
[92] Fix | Delete
*/
[93] Fix | Delete
public function enqueue() {
[94] Fix | Delete
$hooks = $this->get_hooks();
[95] Fix | Delete
$url = $this->get_delivery_url();
[96] Fix | Delete
[97] Fix | Delete
if ( is_array( $hooks ) && ! empty( $url ) ) {
[98] Fix | Delete
foreach ( $hooks as $hook ) {
[99] Fix | Delete
add_action( $hook, array( $this, 'process' ) );
[100] Fix | Delete
}
[101] Fix | Delete
}
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* Process the webhook for delivery by verifying that it should be delivered.
[106] Fix | Delete
* and scheduling the delivery (in the background by default, or immediately).
[107] Fix | Delete
*
[108] Fix | Delete
* @since 2.2.0
[109] Fix | Delete
* @param mixed $arg The first argument provided from the associated hooks.
[110] Fix | Delete
* @return mixed $arg Returns the argument in case the webhook was hooked into a filter.
[111] Fix | Delete
*/
[112] Fix | Delete
public function process( $arg ) {
[113] Fix | Delete
[114] Fix | Delete
// Verify that webhook should be processed for delivery.
[115] Fix | Delete
if ( ! $this->should_deliver( $arg ) ) {
[116] Fix | Delete
return;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
// Mark this $arg as processed to ensure it doesn't get processed again within the current request.
[120] Fix | Delete
$this->processed[] = $arg;
[121] Fix | Delete
[122] Fix | Delete
/**
[123] Fix | Delete
* Process webhook delivery.
[124] Fix | Delete
*
[125] Fix | Delete
* @since 3.3.0
[126] Fix | Delete
* @hooked wc_webhook_process_delivery - 10
[127] Fix | Delete
*/
[128] Fix | Delete
do_action( 'woocommerce_webhook_process_delivery', $this, $arg );
[129] Fix | Delete
[130] Fix | Delete
return $arg;
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* Helper to check if the webhook should be delivered, as some hooks.
[135] Fix | Delete
* (like `wp_trash_post`) will fire for every post type, not just ours.
[136] Fix | Delete
*
[137] Fix | Delete
* @since 2.2.0
[138] Fix | Delete
* @param mixed $arg First hook argument.
[139] Fix | Delete
* @return bool True if webhook should be delivered, false otherwise.
[140] Fix | Delete
*/
[141] Fix | Delete
private function should_deliver( $arg ) {
[142] Fix | Delete
$should_deliver = $this->is_active() && $this->is_valid_topic() && $this->is_valid_action( $arg ) && $this->is_valid_resource( $arg ) && ! $this->is_already_processed( $arg );
[143] Fix | Delete
[144] Fix | Delete
/**
[145] Fix | Delete
* Let other plugins intercept deliver for some messages queue like rabbit/zeromq.
[146] Fix | Delete
*
[147] Fix | Delete
* @param bool $should_deliver True if the webhook should be sent, or false to not send it.
[148] Fix | Delete
* @param WC_Webhook $this The current webhook class.
[149] Fix | Delete
* @param mixed $arg First hook argument.
[150] Fix | Delete
*/
[151] Fix | Delete
return apply_filters( 'woocommerce_webhook_should_deliver', $should_deliver, $this, $arg );
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Returns if webhook is active.
[156] Fix | Delete
*
[157] Fix | Delete
* @since 3.6.0
[158] Fix | Delete
* @return bool True if validation passes.
[159] Fix | Delete
*/
[160] Fix | Delete
private function is_active() {
[161] Fix | Delete
return 'active' === $this->get_status();
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/**
[165] Fix | Delete
* Returns if topic is valid.
[166] Fix | Delete
*
[167] Fix | Delete
* @since 3.6.0
[168] Fix | Delete
* @return bool True if validation passes.
[169] Fix | Delete
*/
[170] Fix | Delete
private function is_valid_topic() {
[171] Fix | Delete
return wc_is_webhook_valid_topic( $this->get_topic() );
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Validates the criteria for certain actions.
[176] Fix | Delete
*
[177] Fix | Delete
* @since 3.6.0
[178] Fix | Delete
* @param mixed $arg First hook argument.
[179] Fix | Delete
* @return bool True if validation passes.
[180] Fix | Delete
*/
[181] Fix | Delete
private function is_valid_action( $arg ) {
[182] Fix | Delete
$current_action = current_action();
[183] Fix | Delete
$return = true;
[184] Fix | Delete
[185] Fix | Delete
switch ( $current_action ) {
[186] Fix | Delete
case 'delete_post':
[187] Fix | Delete
case 'wp_trash_post':
[188] Fix | Delete
case 'untrashed_post':
[189] Fix | Delete
$return = $this->is_valid_post_action( $arg );
[190] Fix | Delete
break;
[191] Fix | Delete
case 'delete_user':
[192] Fix | Delete
$return = $this->is_valid_user_action( $arg );
[193] Fix | Delete
break;
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
if ( 0 === strpos( $current_action, 'woocommerce_process_shop' ) || 0 === strpos( $current_action, 'woocommerce_process_product' ) ) {
[197] Fix | Delete
$return = $this->is_valid_processing_action( $arg );
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
return $return;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Validates post actions.
[205] Fix | Delete
*
[206] Fix | Delete
* @since 3.6.0
[207] Fix | Delete
* @param mixed $arg First hook argument.
[208] Fix | Delete
* @return bool True if validation passes.
[209] Fix | Delete
*/
[210] Fix | Delete
private function is_valid_post_action( $arg ) {
[211] Fix | Delete
// Only deliver deleted/restored event for coupons, orders, and products.
[212] Fix | Delete
if ( isset( $GLOBALS['post_type'] ) && ! in_array( $GLOBALS['post_type'], array( 'shop_coupon', 'shop_order', 'product' ), true ) ) {
[213] Fix | Delete
return false;
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
// Check if is delivering for the correct resource.
[217] Fix | Delete
if ( isset( $GLOBALS['post_type'] ) && str_replace( 'shop_', '', $GLOBALS['post_type'] ) !== $this->get_resource() ) {
[218] Fix | Delete
return false;
[219] Fix | Delete
}
[220] Fix | Delete
return true;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
/**
[224] Fix | Delete
* Validates user actions.
[225] Fix | Delete
*
[226] Fix | Delete
* @since 3.6.0
[227] Fix | Delete
* @param mixed $arg First hook argument.
[228] Fix | Delete
* @return bool True if validation passes.
[229] Fix | Delete
*/
[230] Fix | Delete
private function is_valid_user_action( $arg ) {
[231] Fix | Delete
$user = get_userdata( absint( $arg ) );
[232] Fix | Delete
[233] Fix | Delete
// Only deliver deleted customer event for users with customer role.
[234] Fix | Delete
if ( ! $user || ! in_array( 'customer', (array) $user->roles, true ) ) {
[235] Fix | Delete
return false;
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
return true;
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
/**
[242] Fix | Delete
* Validates WC processing actions.
[243] Fix | Delete
*
[244] Fix | Delete
* @since 3.6.0
[245] Fix | Delete
* @param mixed $arg First hook argument.
[246] Fix | Delete
* @return bool True if validation passes.
[247] Fix | Delete
*/
[248] Fix | Delete
private function is_valid_processing_action( $arg ) {
[249] Fix | Delete
// The `woocommerce_process_shop_*` and `woocommerce_process_product_*` hooks
[250] Fix | Delete
// fire for create and update of products and orders, so check the post
[251] Fix | Delete
// creation date to determine the actual event.
[252] Fix | Delete
$resource = get_post( absint( $arg ) );
[253] Fix | Delete
[254] Fix | Delete
// Drafts don't have post_date_gmt so calculate it here.
[255] Fix | Delete
$gmt_date = get_gmt_from_date( $resource->post_date );
[256] Fix | Delete
[257] Fix | Delete
// A resource is considered created when the hook is executed within 10 seconds of the post creation date.
[258] Fix | Delete
$resource_created = ( ( time() - 10 ) <= strtotime( $gmt_date ) );
[259] Fix | Delete
[260] Fix | Delete
if ( 'created' === $this->get_event() && ! $resource_created ) {
[261] Fix | Delete
return false;
[262] Fix | Delete
} elseif ( 'updated' === $this->get_event() && $resource_created ) {
[263] Fix | Delete
return false;
[264] Fix | Delete
}
[265] Fix | Delete
return true;
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
/**
[269] Fix | Delete
* Checks the resource for this webhook is valid e.g. valid post status.
[270] Fix | Delete
*
[271] Fix | Delete
* @since 3.6.0
[272] Fix | Delete
* @param mixed $arg First hook argument.
[273] Fix | Delete
* @return bool True if validation passes.
[274] Fix | Delete
*/
[275] Fix | Delete
private function is_valid_resource( $arg ) {
[276] Fix | Delete
$resource = $this->get_resource();
[277] Fix | Delete
[278] Fix | Delete
if ( in_array( $resource, array( 'product', 'coupon' ), true ) ) {
[279] Fix | Delete
$status = get_post_status( absint( $arg ) );
[280] Fix | Delete
[281] Fix | Delete
// Ignore auto drafts for all resources.
[282] Fix | Delete
if ( in_array( $status, array( 'auto-draft', 'new' ), true ) ) {
[283] Fix | Delete
return false;
[284] Fix | Delete
}
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
if ( 'order' === $resource ) {
[288] Fix | Delete
// Check registered order types for order types args.
[289] Fix | Delete
if ( ! OrderUtil::is_order( absint( $arg ), wc_get_order_types( 'order-webhooks' ) ) ) {
[290] Fix | Delete
return false;
[291] Fix | Delete
}
[292] Fix | Delete
[293] Fix | Delete
$order = wc_get_order( absint( $arg ) );
[294] Fix | Delete
[295] Fix | Delete
// Ignore standard drafts for orders.
[296] Fix | Delete
if ( in_array( $order->get_status(), array( OrderStatus::DRAFT, OrderStatus::AUTO_DRAFT, 'new' ), true ) ) {
[297] Fix | Delete
return false;
[298] Fix | Delete
}
[299] Fix | Delete
}
[300] Fix | Delete
return true;
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
/**
[304] Fix | Delete
* Checks if the specified resource has already been queued for delivery within the current request.
[305] Fix | Delete
*
[306] Fix | Delete
* Helps avoid duplication of data being sent for topics that have more than one hook defined.
[307] Fix | Delete
*
[308] Fix | Delete
* @param mixed $arg First hook argument.
[309] Fix | Delete
*
[310] Fix | Delete
* @return bool
[311] Fix | Delete
*/
[312] Fix | Delete
protected function is_already_processed( $arg ) {
[313] Fix | Delete
return false !== array_search( $arg, $this->processed, true );
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
/**
[317] Fix | Delete
* Deliver the webhook payload using wp_safe_remote_request().
[318] Fix | Delete
*
[319] Fix | Delete
* @since 2.2.0
[320] Fix | Delete
* @param mixed $arg First hook argument.
[321] Fix | Delete
*/
[322] Fix | Delete
public function deliver( $arg ) {
[323] Fix | Delete
$start_time = microtime( true );
[324] Fix | Delete
$payload = $this->build_payload( $arg );
[325] Fix | Delete
[326] Fix | Delete
// Setup request args.
[327] Fix | Delete
$http_args = array(
[328] Fix | Delete
'method' => 'POST',
[329] Fix | Delete
'timeout' => MINUTE_IN_SECONDS,
[330] Fix | Delete
'redirection' => 0,
[331] Fix | Delete
'httpversion' => '1.0',
[332] Fix | Delete
'blocking' => true,
[333] Fix | Delete
'user-agent' => sprintf( 'WooCommerce/%s Hookshot (WordPress/%s)', Constants::get_constant( 'WC_VERSION' ), $GLOBALS['wp_version'] ),
[334] Fix | Delete
'body' => trim( wp_json_encode( $payload ) ),
[335] Fix | Delete
'headers' => array(
[336] Fix | Delete
'Content-Type' => 'application/json',
[337] Fix | Delete
),
[338] Fix | Delete
'cookies' => array(),
[339] Fix | Delete
);
[340] Fix | Delete
[341] Fix | Delete
$http_args = apply_filters( 'woocommerce_webhook_http_args', $http_args, $arg, $this->get_id() );
[342] Fix | Delete
[343] Fix | Delete
// Add custom headers.
[344] Fix | Delete
$delivery_id = $this->get_new_delivery_id();
[345] Fix | Delete
$http_args['headers']['X-WC-Webhook-Source'] = home_url( '/' ); // Since 2.6.0.
[346] Fix | Delete
$http_args['headers']['X-WC-Webhook-Topic'] = $this->get_topic();
[347] Fix | Delete
$http_args['headers']['X-WC-Webhook-Resource'] = $this->get_resource();
[348] Fix | Delete
$http_args['headers']['X-WC-Webhook-Event'] = $this->get_event();
[349] Fix | Delete
$http_args['headers']['X-WC-Webhook-Signature'] = $this->generate_signature( $http_args['body'] );
[350] Fix | Delete
$http_args['headers']['X-WC-Webhook-ID'] = $this->get_id();
[351] Fix | Delete
$http_args['headers']['X-WC-Webhook-Delivery-ID'] = $delivery_id;
[352] Fix | Delete
[353] Fix | Delete
// Webhook away!
[354] Fix | Delete
$response = wp_safe_remote_request( $this->get_delivery_url(), $http_args );
[355] Fix | Delete
[356] Fix | Delete
$duration = NumberUtil::round( microtime( true ) - $start_time, 5 );
[357] Fix | Delete
[358] Fix | Delete
$this->log_delivery( $delivery_id, $http_args, $response, $duration );
[359] Fix | Delete
[360] Fix | Delete
do_action( 'woocommerce_webhook_delivery', $http_args, $response, $duration, $arg, $this->get_id() );
[361] Fix | Delete
}
[362] Fix | Delete
[363] Fix | Delete
/**
[364] Fix | Delete
* Get WP API integration payload.
[365] Fix | Delete
*
[366] Fix | Delete
* @since 3.0.0
[367] Fix | Delete
* @param string $resource Resource type.
[368] Fix | Delete
* @param int $resource_id Resource ID.
[369] Fix | Delete
* @param string $event Event type.
[370] Fix | Delete
* @return array
[371] Fix | Delete
*/
[372] Fix | Delete
private function get_wp_api_payload( $resource, $resource_id, $event ) {
[373] Fix | Delete
switch ( $resource ) {
[374] Fix | Delete
case 'coupon':
[375] Fix | Delete
case 'customer':
[376] Fix | Delete
case 'order':
[377] Fix | Delete
case 'product':
[378] Fix | Delete
// Bulk and quick edit action hooks return a product object instead of an ID.
[379] Fix | Delete
if ( 'product' === $resource && 'updated' === $event && is_a( $resource_id, 'WC_Product' ) ) {
[380] Fix | Delete
$resource_id = $resource_id->get_id();
[381] Fix | Delete
}
[382] Fix | Delete
[383] Fix | Delete
$version = str_replace( 'wp_api_', '', $this->get_api_version() );
[384] Fix | Delete
$payload = wc_get_container()->get( RestApiUtil::class )->get_endpoint_data( "/wc/{$version}/{$resource}s/{$resource_id}" );
[385] Fix | Delete
break;
[386] Fix | Delete
[387] Fix | Delete
// Custom topics include the first hook argument.
[388] Fix | Delete
case 'action':
[389] Fix | Delete
$payload = array(
[390] Fix | Delete
'action' => current( $this->get_hooks() ),
[391] Fix | Delete
'arg' => $resource_id,
[392] Fix | Delete
);
[393] Fix | Delete
break;
[394] Fix | Delete
[395] Fix | Delete
default:
[396] Fix | Delete
$payload = array();
[397] Fix | Delete
break;
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
return $payload;
[401] Fix | Delete
}
[402] Fix | Delete
[403] Fix | Delete
/**
[404] Fix | Delete
* Build the payload data for the webhook.
[405] Fix | Delete
*
[406] Fix | Delete
* @param mixed $resource_id First hook argument, typically the resource ID.
[407] Fix | Delete
* @return mixed Payload data.
[408] Fix | Delete
* @throws \Exception The webhook is configured to use the Legacy REST API, but the Legacy REST API plugin is not available.
[409] Fix | Delete
* @since 2.2.0
[410] Fix | Delete
*/
[411] Fix | Delete
public function build_payload( $resource_id ) {
[412] Fix | Delete
// Build the payload with the same user context as the user who created
[413] Fix | Delete
// the webhook -- this avoids permission errors as background processing
[414] Fix | Delete
// runs with no user context.
[415] Fix | Delete
$current_user = get_current_user_id();
[416] Fix | Delete
wp_set_current_user( $this->get_user_id() );
[417] Fix | Delete
[418] Fix | Delete
$resource = $this->get_resource();
[419] Fix | Delete
$event = $this->get_event();
[420] Fix | Delete
[421] Fix | Delete
// If a resource has been deleted, just include the ID.
[422] Fix | Delete
if ( 'deleted' === $event ) {
[423] Fix | Delete
$payload = array(
[424] Fix | Delete
'id' => $resource_id,
[425] Fix | Delete
);
[426] Fix | Delete
} elseif ( in_array( $this->get_api_version(), wc_get_webhook_rest_api_versions(), true ) ) {
[427] Fix | Delete
$payload = $this->get_wp_api_payload( $resource, $resource_id, $event );
[428] Fix | Delete
} else {
[429] Fix | Delete
if ( ! WC()->legacy_rest_api_is_available() ) {
[430] Fix | Delete
throw new \Exception( 'The Legacy REST API plugin is not installed on this site. More information: https://developer.woocommerce.com/2023/10/03/the-legacy-rest-api-will-move-to-a-dedicated-extension-in-woocommerce-9-0/ ' );
[431] Fix | Delete
}
[432] Fix | Delete
$payload = wc()->api->get_webhook_api_payload( $resource, $resource_id, $event );
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
// Restore the current user.
[436] Fix | Delete
wp_set_current_user( $current_user );
[437] Fix | Delete
[438] Fix | Delete
return apply_filters( 'woocommerce_webhook_payload', $payload, $resource, $resource_id, $this->get_id() );
[439] Fix | Delete
}
[440] Fix | Delete
[441] Fix | Delete
/**
[442] Fix | Delete
* Generate a base64-encoded HMAC-SHA256 signature of the payload body so the
[443] Fix | Delete
* recipient can verify the authenticity of the webhook. Note that the signature
[444] Fix | Delete
* is calculated after the body has already been encoded (JSON by default).
[445] Fix | Delete
*
[446] Fix | Delete
* @since 2.2.0
[447] Fix | Delete
* @param string $payload Payload data to hash.
[448] Fix | Delete
* @return string
[449] Fix | Delete
*/
[450] Fix | Delete
public function generate_signature( $payload ) {
[451] Fix | Delete
$hash_algo = apply_filters( 'woocommerce_webhook_hash_algorithm', 'sha256', $payload, $this->get_id() );
[452] Fix | Delete
[453] Fix | Delete
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
[454] Fix | Delete
return base64_encode( hash_hmac( $hash_algo, $payload, wp_specialchars_decode( $this->get_secret(), ENT_QUOTES ), true ) );
[455] Fix | Delete
}
[456] Fix | Delete
[457] Fix | Delete
/**
[458] Fix | Delete
* Generate a new unique hash as a delivery id based on current time and wehbook id.
[459] Fix | Delete
* Return the hash for inclusion in the webhook request.
[460] Fix | Delete
*
[461] Fix | Delete
* @since 2.2.0
[462] Fix | Delete
* @return string
[463] Fix | Delete
*/
[464] Fix | Delete
public function get_new_delivery_id() {
[465] Fix | Delete
// Since we no longer use comments to store delivery logs, we generate a unique hash instead based on current time and webhook ID.
[466] Fix | Delete
return wp_hash( $this->get_id() . strtotime( 'now' ) );
[467] Fix | Delete
}
[468] Fix | Delete
[469] Fix | Delete
/**
[470] Fix | Delete
* Log the delivery request/response.
[471] Fix | Delete
*
[472] Fix | Delete
* @since 2.2.0
[473] Fix | Delete
* @param string $delivery_id Previously created hash.
[474] Fix | Delete
* @param array $request Request data.
[475] Fix | Delete
* @param array|WP_Error $response Response data.
[476] Fix | Delete
* @param float $duration Request duration.
[477] Fix | Delete
*/
[478] Fix | Delete
public function log_delivery( $delivery_id, $request, $response, $duration ) {
[479] Fix | Delete
$logger = wc_get_logger();
[480] Fix | Delete
$message = array(
[481] Fix | Delete
'Webhook Delivery' => array(
[482] Fix | Delete
'Delivery ID' => $delivery_id,
[483] Fix | Delete
'Date' => date_i18n( __( 'M j, Y @ G:i', 'woocommerce' ), strtotime( 'now' ), true ),
[484] Fix | Delete
'URL' => $this->get_delivery_url(),
[485] Fix | Delete
'Duration' => $duration,
[486] Fix | Delete
'Request' => array(
[487] Fix | Delete
'Method' => $request['method'],
[488] Fix | Delete
'Headers' => array_merge(
[489] Fix | Delete
array(
[490] Fix | Delete
'User-Agent' => $request['user-agent'],
[491] Fix | Delete
),
[492] Fix | Delete
$request['headers']
[493] Fix | Delete
),
[494] Fix | Delete
),
[495] Fix | Delete
'Body' => wp_slash( $request['body'] ),
[496] Fix | Delete
),
[497] Fix | Delete
);
[498] Fix | Delete
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function