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 / Autonami / AutonamiHandler.php

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

171 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Autonami Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\Autonami;
9
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 use BitCode\BitForm\Core\Integration\IntegrationHandler;
15 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
16 use BWFCRM_Fields;
17 use BWFCRM_Lists;
18 use BWFCRM_Tag;
19 use WP_Error;
20
21 class AutonamiHandler
22 {
23 private $_formID;
24 private $_integrationID;
25
26 public function __construct($integrationID, $fromID)
27 {
28 $this->_formID = $fromID;
29 $this->_integrationID = $integrationID;
30 }
31
32 /**
33 * Helps to register ajax function's with wp
34 *
35 * @return null
36 */
37 public static function registerAjax()
38 {
39 add_action('wp_ajax_bitforms_autonami_authorize', [__CLASS__, 'autonamiAuthorize']);
40 add_action('wp_ajax_bitforms_autonami_lists_and_tags', [__CLASS__, 'autonamiListsAndTags']);
41 add_action('wp_ajax_bitforms_autonami_fields', [__CLASS__, 'autonamiFields']);
42 }
43
44 /**
45 * for chen autonami pro plugins are exists
46 */
47 public static function checkedExistsAutonami()
48 {
49 if (!class_exists('BWFCRM_Contact')) {
50 wp_send_json_error(__('Autonami Pro Plugins not found', 'bit-form'), 400);
51 } else {
52 return true;
53 }
54 }
55
56 /**
57 * @return Autonami lists
58 */
59 public static function autonamiListsAndTags()
60 {
61 if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
62 wp_send_json_error(__('Token expired', 'bit-form'), 401);
63 }
64
65 self::checkedExistsAutonami();
66
67 $lists = BWFCRM_Lists::get_lists();
68 $autonamiList = [];
69 foreach ($lists as $list) {
70 $autonamiList[$list['name']] = (object) [
71 'id' => $list['ID'],
72 'title' => $list['name']
73 ];
74 }
75
76 $tags = BWFCRM_Tag::get_tags();
77 $autonamiTags = [];
78 foreach ($tags as $tag) {
79 $autonamiTags[$tag['name']] = (object) [
80 'id' => $tag['ID'],
81 'title' => $tag['name']
82 ];
83 }
84
85 $response['autonamiList'] = $autonamiList;
86 $response['autonamiTags'] = $autonamiTags;
87 wp_send_json_success($response, 200);
88 }
89
90 public static function autonamiFields()
91 {
92 if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
93 wp_send_json_error(__('Token expired', 'bit-form'), 401);
94 }
95
96 self::checkedExistsAutonami();
97
98 $fieldOptions = [];
99 $fieldOptions['Email'] = (object) [
100 'key' => 'email',
101 'label' => 'Email',
102 'type' => 'primary',
103 'required' => true
104 ];
105 foreach (BWFCRM_Fields::get_default_fields() as $key => $column) {
106 $fieldOptions[$column] = (object) [
107 'key' => $key,
108 'label' => $column,
109 'type' => 'primary'
110 ];
111 }
112 foreach (BWFCRM_Fields::get_custom_fields(1, 1) as $field) {
113 $fieldOptions[$field['slug']] = (object) [
114 'key' => $field['slug'],
115 'label' => $field['name'],
116 'type' => 'custom'
117 ];
118 }
119 $response['autonamiFields'] = $fieldOptions;
120 wp_send_json_success($response, 200);
121 }
122
123 /**
124 * @return True Autonami are exists
125 */
126 public static function autonamiAuthorize()
127 {
128 if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
129 wp_send_json_error(__('Token expired', 'bit-form'), 401);
130 }
131
132 if (self::checkedExistsAutonami()) {
133 wp_send_json_success(true);
134 } else {
135 wp_send_json_error(__('Autonami Pro Plugins not found', 'bit-form'), 400);
136 }
137 }
138
139 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
140 {
141 $entryDetails = [
142 'formId' => $this->_formID,
143 'entryId' => $entryID,
144 'fieldValues' => $fieldValues
145 ];
146 if (!class_exists('BWFCRM_Contact')) {
147 (new UtilApiResponse())->apiResponse($logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Autonami Pro Plugins not found', $entryDetails);
148 return;
149 }
150
151 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
152
153 $fieldMap = $integrationDetails->field_map;
154 $lists = $integrationDetails->lists;
155 $tags = $integrationDetails->tags;
156 $actions = $integrationDetails->actions;
157
158 if (empty($fieldMap)) {
159 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Autonami api', 'bit-form'));
160 }
161
162 $recordApiHelper = new RecordApiHelper($this->_integrationID, $logID, $entryID);
163 $autonamiApiResponse = $recordApiHelper->executeRecordApi($fieldValues, $fieldMap, $actions, $lists, $tags, $this->_formID);
164
165 if (is_wp_error($autonamiApiResponse)) {
166 return $autonamiApiResponse;
167 }
168 return $autonamiApiResponse;
169 }
170 }
171