PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.1
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.1
6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / app / FormActions / Actions / WebhookActionHandler.php
kirki / app / FormActions / Actions Last commit date
EmailActionHandler.php 3 days ago WebhookActionHandler.php 3 days ago
WebhookActionHandler.php
80 lines
1 <?php
2
3 namespace Kirki\App\FormActions\Actions;
4
5 use Kirki\HelperFunctions;
6
7 defined('ABSPATH') || exit;
8
9 use Kirki\App\Constants\Form\FormWebhookMethods;
10 use Kirki\App\Contracts\FormActionHandler;
11 use Kirki\App\DTO\Form\FormConfigDTO;
12 use Kirki\Framework\Supports\Facades\Http;
13
14 /**
15 * Delivers a submission to a single configured webhook endpoint.
16 *
17 * Unlike the other channels, a webhook transport failure marks the whole
18 * submission as failed, so this is the only handler whose return value can be
19 * false.
20 */
21 class WebhookActionHandler implements FormActionHandler
22 {
23 public function handle(array $webhook, array $form_data, FormConfigDTO $form_config)
24 {
25 if (!isset($webhook['action'], $webhook['method'])) {
26 return false;
27 }
28
29 if ($webhook['method'] === FormWebhookMethods::GET) {
30 return $this->send_get($webhook['action'], $form_data);
31 }
32
33 if ($webhook['method'] === FormWebhookMethods::POST) {
34 return $this->send_post($webhook['action'], $form_data);
35 }
36
37 return false;
38 }
39
40 /**
41 * Send a GET webhook request.
42 *
43 * @param string $url Webhook URL.
44 * @param array $form_data Form data.
45 * @return bool Success status.
46 */
47 protected function send_get($url, $form_data)
48 {
49 if (!HelperFunctions::is_safe_url($url)) {
50 return false;
51 }
52
53 $query_string = http_build_query($form_data);
54 $url = rtrim($url, '/');
55 $url .= '/';
56
57 $response = Http::get($url . '?' . $query_string);
58
59 return $response->status() !== 0;
60 }
61
62 /**
63 * Send a POST webhook request.
64 *
65 * @param string $url Webhook URL.
66 * @param array $form_data Form data.
67 * @return bool Success status.
68 */
69 protected function send_post($url, $form_data)
70 {
71 if (!HelperFunctions::is_safe_url($url)) {
72 return false;
73 }
74
75 $response = Http::as_form()->post($url, $form_data);
76
77 return $response->status() !== 0;
78 }
79 }
80