PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.13
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.13
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 2.10.2 All 137 releases
bit-form / includes / Core / Integration / ZohoMail / ZohoMailHandler.php

ZohoMailHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.13, at includes/Core/Integration/ZohoMail/ZohoMailHandler.php

218 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoMail Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ZohoMail;
9
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 use BitCode\BitForm\Core\Integration\IntegrationHandler;
15 use BitCode\BitForm\Core\Util\HttpHelper;
16 use BitCode\BitForm\Core\Util\IpTool;
17 use BitCode\BitForm\GlobalHelper;
18 use WP_Error;
19
20 /**
21 * Provide functionality for ZohoCrm integration
22 */
23 class ZohoMailHandler
24 {
25 private $_formID;
26 private $_integrationID;
27
28 public function __construct($integrationID, $fromID)
29 {
30 $this->_formID = $fromID;
31 $this->_integrationID = $integrationID;
32 }
33
34 /**
35 * Helps to register ajax function's with wp
36 *
37 * @return null
38 */
39 public static function registerAjax()
40 {
41 add_action('wp_ajax_bitforms_zmail_generate_token', [__CLASS__, 'generateTokens']);
42 add_action('wp_ajax_bitforms_zmail_refresh_workspaces', [__CLASS__, 'refreshWorkspacesAjaxHelper']);
43 add_action('wp_ajax_bitforms_zmail_refresh_tables', [__CLASS__, 'refreshTablesAjaxHelper']);
44 add_action('wp_ajax_bitforms_zmail_refresh_table_headers', [__CLASS__, 'refreshTableHeadersAjaxHelper']);
45 }
46
47 /**
48 * Process ajax request for generate_token
49 *
50 * @return JSON zoho crm api response and status
51 */
52 public static function generateTokens()
53 {
54 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
55 $authorizationHeader = null;
56
57
58 GlobalHelper::requirePostMethod();
59
60 try {
61 $requestsParams = GlobalHelper::formatRequestData();
62 } catch (\InvalidArgumentException $e) {
63 wp_send_json_error($e->getMessage(), 400);
64 }
65
66 if (
67 empty($requestsParams->{'accounts-server'})
68 || empty($requestsParams->dataCenter)
69 || empty($requestsParams->clientId)
70 || empty($requestsParams->clientSecret)
71 || empty($requestsParams->redirectURI)
72 || empty($requestsParams->code)
73 ) {
74 wp_send_json_error(
75 __(
76 'Requested parameter is empty',
77 'bit-form'
78 ),
79 400
80 );
81 }
82
83 $apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token';
84 $requestParams = [
85 'grant_type' => 'authorization_code',
86 'client_id' => $requestsParams->clientId,
87 'client_secret' => $requestsParams->clientSecret,
88 'redirect_uri' => \urldecode($requestsParams->redirectURI),
89 'code' => $requestsParams->code
90 ];
91 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams);
92
93 $accountIdEndpoint = "http://mail.zoho.{$requestsParams->dataCenter}/api/accounts";
94 $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$apiResponse->access_token}";
95 $accountResponse = HttpHelper::get($accountIdEndpoint, null, $authorizationHeader);
96
97 $apiResponse->accountId = $accountResponse->data[0]->accountId;
98 $apiResponse->accountEmail = $accountResponse->data[0]->primaryEmailAddress;
99
100 if (is_wp_error($apiResponse) || !empty($apiResponse->error) || is_wp_error($accountResponse) || !empty($accountResponse->errors)) {
101 wp_send_json_error(
102 empty($apiResponse->error) ? 'Unknown' : $apiResponse->error,
103 400
104 );
105 }
106 $apiResponse->generates_on = \time();
107 wp_send_json_success($apiResponse, 200);
108 } else {
109 wp_send_json_error(
110 __(
111 'Token expired',
112 'bit-form'
113 ),
114 401
115 );
116 }
117 }
118
119 /**
120 * Helps to refresh zoho crm access_token
121 *
122 * @param object $apiData Contains required data for refresh access token
123 * @return string|boolean $tokenDetails API token details
124 */
125 protected static function _refreshAccessToken($apiData)
126 {
127 if (
128 empty($apiData->dataCenter)
129 || empty($apiData->clientId)
130 || empty($apiData->clientSecret)
131 || empty($apiData->tokenDetails)
132 ) {
133 return false;
134 }
135 $tokenDetails = $apiData->tokenDetails;
136
137 $dataCenter = $apiData->dataCenter;
138 $apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token";
139 $requestParams = [
140 'grant_type' => 'refresh_token',
141 'client_id' => $apiData->clientId,
142 'client_secret' => $apiData->clientSecret,
143 'refresh_token' => $tokenDetails->refresh_token,
144 ];
145
146 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams);
147 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
148 return false;
149 }
150 $tokenDetails->generates_on = \time();
151 $tokenDetails->access_token = $apiResponse->access_token;
152 return $tokenDetails;
153 }
154
155 /**
156 * Save updated access_token to avoid unnecessary token generation
157 *
158 * @param integer $fromID ID of Integration related form
159 * @param integer $integrationID ID of Zoho crm Integration
160 * @param object $tokenDetails refreshed token info
161 *
162 * @return null
163 */
164 protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null)
165 {
166 if (empty($formID) || empty($integrationID)) {
167 return;
168 }
169
170 $integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail());
171 $zmailDetails = $integrationHandler->getAIntegration($integrationID);
172
173 if (is_wp_error($zmailDetails)) {
174 return;
175 }
176 $newDetails = json_decode($zmailDetails[0]->integration_details);
177
178 $newDetails->tokenDetails = $tokenDetails;
179
180 $integrationHandler->updateIntegration($integrationID, $zmailDetails[0]->integration_name, 'Zoho Mail', wp_json_encode($newDetails), 'form');
181 }
182
183 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
184 {
185 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
186
187 $tokenDetails = $integrationDetails->tokenDetails;
188
189 if (empty($tokenDetails)) {
190 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for zoho mail api', 'bit-form'));
191 }
192
193 $requiredParams = null;
194 if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) {
195 $requiredParams['clientId'] = $integrationDetails->clientId;
196 $requiredParams['clientSecret'] = $integrationDetails->clientSecret;
197 $requiredParams['dataCenter'] = $integrationDetails->dataCenter;
198 $requiredParams['tokenDetails'] = $tokenDetails;
199 $newTokenDetails = ZohoMailHandler::_refreshAccessToken((object)$requiredParams);
200 if ($newTokenDetails) {
201 ZohoMailHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails);
202 $tokenDetails = $newTokenDetails;
203 }
204 }
205 // $actions = $integrationDetails->actions;
206 $recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID);
207
208 $zmailApiResponse = $recordApiHelper->executeRecordApi(
209 $integrationDetails,
210 $fieldValues,
211 $this->_formID,
212 $entryID
213 );
214
215 return $zmailApiResponse;
216 }
217 }
218