PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.1.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.1.4
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / includes / Core / Integration / Getgist / GetgistHandler.php

GetgistHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.1.4, at includes/Core/Integration/Getgist/GetgistHandler.php

152 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Integration\Getgist;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 use BitCode\BitForm\Core\Integration\IntegrationHandler;
10 use BitCode\BitForm\Core\Util\HttpHelper;
11 use BitCode\BitForm\GlobalHelper;
12 use WP_Error;
13
14 class GetgistHandler
15 {
16 public static $api_endpoint = 'https://api.getgist.com';
17
18 private $integrationID;
19
20 private $formId;
21
22 public function __construct($integrationID, $fromID)
23 {
24 $this->integrationID = $integrationID;
25 $this->formId = $fromID;
26 }
27
28 public static function registerAjax()
29 {
30 add_action('wp_ajax_bitforms_getgist_authorize', [__CLASS__, 'getgistAuthorize']);
31 add_action('wp_ajax_bitforms_getgist_tags', [__CLASS__, 'getAllTags']);
32 }
33
34 public static function getgistAuthorize()
35 {
36 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
37 $authorizationHeader = null;
38
39 GlobalHelper::requirePostMethod();
40
41 try {
42 $requestsParams = GlobalHelper::formatRequestData();
43 } catch (\InvalidArgumentException $e) {
44 wp_send_json_error($e->getMessage(), 400);
45 }
46
47 if (empty($requestsParams->api_key)) {
48 wp_send_json_error(
49 __(
50 'Requested parameter is empty',
51 'bit-form'
52 ),
53 400
54 );
55 }
56
57 $apiEndpoint = self::$api_endpoint . '/contacts';
58 $authorizationHeader['Authorization'] = "Bearer {$requestsParams->api_key}";
59 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
60
61 if (is_wp_error($apiResponse) || 'authentication_failed' === $apiResponse->code) {
62 wp_send_json_error(
63 empty($apiResponse->code) ? 'Unknown' : $apiResponse->message,
64 400
65 );
66 }
67
68 wp_send_json_success(true);
69 }
70 }
71
72 public static function getAllTags()
73 {
74 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
75 $authorizationHeader = null;
76
77 GlobalHelper::requirePostMethod();
78
79 try {
80 $requestsParams = GlobalHelper::formatRequestData();
81 } catch (\InvalidArgumentException $e) {
82 wp_send_json_error($e->getMessage(), 400);
83 }
84
85 if (empty($requestsParams->apiKey)) {
86 wp_send_json_error(
87 __(
88 'Requested parameter is empty',
89 'bit-form'
90 ),
91 400
92 );
93 }
94
95 $apiEndpoint = self::$api_endpoint . '/tags';
96 $authorizationHeader['Authorization'] = "Bearer {$requestsParams->apiKey}";
97 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
98
99 if (is_wp_error($apiResponse) || 'error' === $apiResponse->status) {
100 wp_send_json_error(
101 empty($apiResponse->code) ? 'Unknown' : $apiResponse->message,
102 400
103 );
104 }
105 $response = [];
106
107 foreach ($apiResponse->tags as $tag) {
108 $response[] = [
109 'tagId' => $tag->id,
110 'tagName' => $tag->name,
111 ];
112 }
113 wp_send_json_success($response, 200);
114 }
115 }
116
117 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
118 {
119 //$integrationDetails = $integrationData->flow_details;
120
121 $integrationDetails = $integrationData->integration_details;
122 if (is_string($integrationDetails)) {
123 $integrationDetails = json_decode($integrationDetails);
124 }
125
126 $integId = $integrationData->id;
127
128 $api_key = $integrationDetails->api_key;
129 $fieldMap = $integrationDetails->field_map;
130 $actions = $integrationDetails->actions;
131 if (
132 empty($api_key)
133 || empty($fieldMap)
134 ) {
135 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Gist api', 'bit-form'));
136 }
137 $recordApiHelper = new RecordApiHelper($api_key, $integId, $logID, $entryID);
138 $getgistApiResponse = $recordApiHelper->execute(
139 $integId,
140 $fieldValues,
141 $fieldMap,
142 $integrationDetails,
143 $this->formId,
144 );
145
146 if (is_wp_error($getgistApiResponse)) {
147 return $getgistApiResponse;
148 }
149 return $getgistApiResponse;
150 }
151 }
152