Edit File by line
/home/zeestwma/ceyloniy.../wp-conte.../plugins/wpforms-.../src/Integrat.../Square/Api
File: WebhooksManager.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WPForms\Integrations\Square\Api;
[2] Fix | Delete
[3] Fix | Delete
use Exception;
[4] Fix | Delete
use WPForms\Vendor\Square\SquareClient;
[5] Fix | Delete
use WPForms\Integrations\Square\Helpers;
[6] Fix | Delete
use WPForms\Integrations\Square\WebhooksHealthCheck;
[7] Fix | Delete
use WPForms\Vendor\Square\Models\WebhookSubscription;
[8] Fix | Delete
use WPForms\Vendor\Square\Models\CreateWebhookSubscriptionRequest;
[9] Fix | Delete
use WPForms\Vendor\Square\Models\UpdateWebhookSubscriptionRequest;
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Webhooks Manager.
[13] Fix | Delete
*
[14] Fix | Delete
* @since 1.9.5
[15] Fix | Delete
*/
[16] Fix | Delete
class WebhooksManager {
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Square client.
[20] Fix | Delete
*
[21] Fix | Delete
* @since 1.9.5
[22] Fix | Delete
*
[23] Fix | Delete
* @var SquareClient
[24] Fix | Delete
*/
[25] Fix | Delete
private $client;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Create webhook endpoint.
[29] Fix | Delete
* Retrieve the existing one when the endpoint already exists.
[30] Fix | Delete
*
[31] Fix | Delete
* @since 1.9.5
[32] Fix | Delete
*/
[33] Fix | Delete
public function connect() {
[34] Fix | Delete
[35] Fix | Delete
// Security and permissions check.
[36] Fix | Delete
if (
[37] Fix | Delete
! check_ajax_referer( 'wpforms-admin', 'nonce', false ) ||
[38] Fix | Delete
! wpforms_current_user_can()
[39] Fix | Delete
) {
[40] Fix | Delete
wp_send_json_error( [ 'message ' => esc_html__( 'You are not allowed to perform this action', 'wpforms-lite' ) ] );
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
$personal_access_token = ! empty( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '';
[44] Fix | Delete
[45] Fix | Delete
if ( empty( $personal_access_token ) ) {
[46] Fix | Delete
wp_send_json_error( [ 'message' => esc_html__( 'Personal access token is required.', 'wpforms-lite' ) ] );
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
$webhook = $this->create( $personal_access_token );
[50] Fix | Delete
[51] Fix | Delete
// Register AS task.
[52] Fix | Delete
( new WebhooksHealthCheck() )->maybe_schedule_task();
[53] Fix | Delete
[54] Fix | Delete
// Store endpoint ID and secret.
[55] Fix | Delete
if ( ! empty( $webhook ) ) {
[56] Fix | Delete
$this->save_settings( $webhook );
[57] Fix | Delete
[58] Fix | Delete
wp_send_json_success( [ 'message' => esc_html__( 'Webhook created successfully!', 'wpforms-lite' ) ] );
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
wp_send_json_error( [ 'message' => esc_html__( 'Failed to create webhook.', 'wpforms-lite' ) ] );
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* Create or update a webhook endpoint.
[66] Fix | Delete
*
[67] Fix | Delete
* @since 1.9.5
[68] Fix | Delete
*
[69] Fix | Delete
* @param string $personal_access_token Personal access token.
[70] Fix | Delete
*
[71] Fix | Delete
* @return array Endpoint ID and secret.
[72] Fix | Delete
*/
[73] Fix | Delete
private function create( string $personal_access_token ): array {
[74] Fix | Delete
[75] Fix | Delete
$this->client = new SquareClient(
[76] Fix | Delete
[
[77] Fix | Delete
'accessToken' => $personal_access_token,
[78] Fix | Delete
'environment' => Helpers::get_mode(),
[79] Fix | Delete
]
[80] Fix | Delete
);
[81] Fix | Delete
[82] Fix | Delete
// Check if the webhook already exists.
[83] Fix | Delete
$existing_webhook = $this->webhook_exists();
[84] Fix | Delete
[85] Fix | Delete
// Prepare a webhook subscription object.
[86] Fix | Delete
$webhook_subscription = new WebhookSubscription();
[87] Fix | Delete
$webhook_subscription->setName( sprintf( 'WPForms endpoint (%1$s mode)', Helpers::get_mode() ) );
[88] Fix | Delete
$webhook_subscription->setNotificationUrl( Helpers::get_webhook_url() );
[89] Fix | Delete
$webhook_subscription->setEventTypes( WebhookRoute::get_webhooks_events_list() );
[90] Fix | Delete
[91] Fix | Delete
$webhooks_api = $this->client->getWebhookSubscriptionsApi();
[92] Fix | Delete
[93] Fix | Delete
if ( $existing_webhook ) {
[94] Fix | Delete
try {
[95] Fix | Delete
// Create an update request and set the subscription payload.
[96] Fix | Delete
$request = new UpdateWebhookSubscriptionRequest();
[97] Fix | Delete
[98] Fix | Delete
$request->setSubscription( $webhook_subscription );
[99] Fix | Delete
[100] Fix | Delete
// Update the existing webhook subscription.
[101] Fix | Delete
$response = $webhooks_api->updateWebhookSubscription( $existing_webhook['id'], $request );
[102] Fix | Delete
[103] Fix | Delete
if ( $response->isSuccess() ) {
[104] Fix | Delete
$subscription = $response->getResult()->getSubscription();
[105] Fix | Delete
[106] Fix | Delete
return [
[107] Fix | Delete
'id' => $subscription->getId(),
[108] Fix | Delete
'signature_key' => $existing_webhook['signature_key'] ?? '', // getSignatureKey() isn't available in the update response, fall back to the existing webhook's signature key.
[109] Fix | Delete
];
[110] Fix | Delete
}
[111] Fix | Delete
// If the update fails, return the existing webhook details.
[112] Fix | Delete
return $existing_webhook;
[113] Fix | Delete
} catch ( Exception $e ) {
[114] Fix | Delete
return $existing_webhook;
[115] Fix | Delete
}
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
// Create a new webhook subscription if none exists.
[119] Fix | Delete
$request = new CreateWebhookSubscriptionRequest( $webhook_subscription );
[120] Fix | Delete
[121] Fix | Delete
$request->setIdempotencyKey( uniqid() );
[122] Fix | Delete
[123] Fix | Delete
try {
[124] Fix | Delete
// Create the webhook subscription.
[125] Fix | Delete
$response = $webhooks_api->createWebhookSubscription( $request );
[126] Fix | Delete
[127] Fix | Delete
if ( $response->isSuccess() ) {
[128] Fix | Delete
$subscription = $response->getResult()->getSubscription();
[129] Fix | Delete
[130] Fix | Delete
return [
[131] Fix | Delete
'id' => $subscription->getId(),
[132] Fix | Delete
'signature_key' => $subscription->getSignatureKey(),
[133] Fix | Delete
];
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
return [];
[137] Fix | Delete
} catch ( Exception $e ) {
[138] Fix | Delete
return [];
[139] Fix | Delete
}
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* Check if webhook already exists.
[144] Fix | Delete
*
[145] Fix | Delete
* @since 1.9.5
[146] Fix | Delete
*
[147] Fix | Delete
* @return array
[148] Fix | Delete
*/
[149] Fix | Delete
private function webhook_exists(): array {
[150] Fix | Delete
[151] Fix | Delete
try {
[152] Fix | Delete
$response = $this->client->getWebhookSubscriptionsApi()->listWebhookSubscriptions();
[153] Fix | Delete
[154] Fix | Delete
if ( ! $response->isSuccess() || empty( $response->getResult()->getSubscriptions() ) ) {
[155] Fix | Delete
return [];
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
foreach ( $response->getResult()->getSubscriptions() as $subscription ) {
[159] Fix | Delete
[160] Fix | Delete
if ( $subscription->getNotificationUrl() !== Helpers::get_webhook_url() ) {
[161] Fix | Delete
continue;
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
$signature = $this->client->getWebhookSubscriptionsApi()->retrieveWebhookSubscription( $subscription->getId() );
[165] Fix | Delete
[166] Fix | Delete
return $signature->isSuccess() ? [
[167] Fix | Delete
'id' => $signature->getResult()->getSubscription()->getId(),
[168] Fix | Delete
'signature_key' => $signature->getResult()->getSubscription()->getSignatureKey(),
[169] Fix | Delete
] : [];
[170] Fix | Delete
}
[171] Fix | Delete
} catch ( Exception $e ) {
[172] Fix | Delete
return [];
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
return [];
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* Save webhook settings.
[181] Fix | Delete
*
[182] Fix | Delete
* @since 1.9.5
[183] Fix | Delete
*
[184] Fix | Delete
* @param array $webhook Webhook endpoint.
[185] Fix | Delete
*/
[186] Fix | Delete
private function save_settings( array $webhook ) {
[187] Fix | Delete
[188] Fix | Delete
$mode = Helpers::get_mode();
[189] Fix | Delete
$settings = (array) get_option( 'wpforms_settings', [] );
[190] Fix | Delete
[191] Fix | Delete
// Save webhooks endpoint ID and secret.
[192] Fix | Delete
$settings[ 'square-webhooks-id-' . $mode ] = sanitize_text_field( $webhook['id'] );
[193] Fix | Delete
$settings[ 'square-webhooks-secret-' . $mode ] = sanitize_text_field( $webhook['signature_key'] );
[194] Fix | Delete
[195] Fix | Delete
WebhooksHealthCheck::save_status( WebhooksHealthCheck::ENDPOINT_OPTION, WebhooksHealthCheck::STATUS_OK );
[196] Fix | Delete
[197] Fix | Delete
// Enable webhooks setting shouldn't be rewritten.
[198] Fix | Delete
if ( empty( $settings['square-webhooks-enabled'] ) ) {
[199] Fix | Delete
$settings['square-webhooks-enabled'] = true;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
update_option( 'wpforms_settings', $settings );
[203] Fix | Delete
}
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function