Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Frontend/Services/
?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Frontend
<?php
namespace KS_PAC_DCFH\Frontend\Services;
use KS_PAC_DCFH\Frontend\Controllers\ContactFormDataRepository;
if (!defined('ABSPATH')) {
exit;
}
class WebhookHandler
{
private static $_instance;
private ContactFormDataRepository $contact_form_rep;
public function __construct()
{
$this->contact_form_rep = ContactFormDataRepository::instance();
}
public static function instance(): self
{
if (self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
public function payload(): void
{
$contact_form_id = $this->contact_form_rep->contact_form_id;
$endpoint_url = $this->contact_form_rep->webhook_url;
if (empty($contact_form_id) || empty($endpoint_url)) {
return;
}
$payload = $this->prepare_payload($contact_form_id);
$headers = $this->prepare_headers($contact_form_id);
$response = wp_remote_request($endpoint_url, [
'method' => 'POST',
'body' => $payload,
'headers' => $headers,
]);
$this->handle_response($response);
}
private function prepare_payload(string $contact_form_id): array
{
return [
'contact_form_id' => $contact_form_id,
'entry_number' => $this->contact_form_rep->entry_number,
'processed_data' => $this->contact_form_rep->contact_form_processed_data,
'attachments' => $this->contact_form_rep->files_arr['baseurl'] ?? [],
'referer_url' => $this->contact_form_rep->contact_form_referrer_url,
'ip_address' => et_core_get_ip_address(),
'user_agent' => ks_pac_dcfh_app()->misc_utils()::get_user_agent(),
'raw_data' => wp_unslash($_POST), // phpcs:ignore WordPress.Security.NonceVerification
];
}
private function prepare_headers(string $contact_form_id): array
{
return [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic '.$contact_form_id,
'Token' => 'Bearer '.$contact_form_id,
];
}
private function handle_response($response): void
{
if (is_wp_error($response)) {
//error_log('Webhook request failed: '.$response->get_error_message());
} else {
wp_remote_retrieve_body($response);
}
}
}