PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.0
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 3.2.0, at includes/Core/Integration/ZohoMail/ZohoMailHandler.php

217 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 GlobalHelper::requirePostMethod();
58
59 try {
60 $requestsParams = GlobalHelper::formatRequestData();
61 } catch (\InvalidArgumentException $e) {
62 wp_send_json_error($e->getMessage(), 400);
63 }
64
65 if (
66 empty($requestsParams->{'accounts-server'})
67 || empty($requestsParams->dataCenter)
68 || empty($requestsParams->clientId)
69 || empty($requestsParams->clientSecret)
70 || empty($requestsParams->redirectURI)
71 || empty($requestsParams->code)
72 ) {
73 wp_send_json_error(
74 __(
75 'Requested parameter is empty',
76 'bit-form'
77 ),
78 400
79 );
80 }
81
82 $apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token';
83 $requestParams = [
84 'grant_type' => 'authorization_code',
85 'client_id' => $requestsParams->clientId,
86 'client_secret' => $requestsParams->clientSecret,
87 'redirect_uri' => \urldecode($requestsParams->redirectURI),
88 'code' => $requestsParams->code
89 ];
90 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams);
91
92 $accountIdEndpoint = "http://mail.zoho.{$requestsParams->dataCenter}/api/accounts";
93 $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$apiResponse->access_token}";
94 $accountResponse = HttpHelper::get($accountIdEndpoint, null, $authorizationHeader);
95
96 $apiResponse->accountId = $accountResponse->data[0]->accountId;
97 $apiResponse->accountEmail = $accountResponse->data[0]->primaryEmailAddress;
98
99 if (is_wp_error($apiResponse) || !empty($apiResponse->error) || is_wp_error($accountResponse) || !empty($accountResponse->errors)) {
100 wp_send_json_error(
101 empty($apiResponse->error) ? 'Unknown' : $apiResponse->error,
102 400
103 );
104 }
105 $apiResponse->generates_on = \time();
106 wp_send_json_success($apiResponse, 200);
107 } else {
108 wp_send_json_error(
109 __(
110 'Token expired',
111 'bit-form'
112 ),
113 401
114 );
115 }
116 }
117
118 /**
119 * Helps to refresh zoho crm access_token
120 *
121 * @param object $apiData Contains required data for refresh access token
122 * @return string|boolean $tokenDetails API token details
123 */
124 protected static function _refreshAccessToken($apiData)
125 {
126 if (
127 empty($apiData->dataCenter)
128 || empty($apiData->clientId)
129 || empty($apiData->clientSecret)
130 || empty($apiData->tokenDetails)
131 ) {
132 return false;
133 }
134 $tokenDetails = $apiData->tokenDetails;
135
136 $dataCenter = $apiData->dataCenter;
137 $apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token";
138 $requestParams = [
139 'grant_type' => 'refresh_token',
140 'client_id' => $apiData->clientId,
141 'client_secret' => $apiData->clientSecret,
142 'refresh_token' => $tokenDetails->refresh_token,
143 ];
144
145 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams);
146 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
147 return false;
148 }
149 $tokenDetails->generates_on = \time();
150 $tokenDetails->access_token = $apiResponse->access_token;
151 return $tokenDetails;
152 }
153
154 /**
155 * Save updated access_token to avoid unnecessary token generation
156 *
157 * @param integer $fromID ID of Integration related form
158 * @param integer $integrationID ID of Zoho crm Integration
159 * @param object $tokenDetails refreshed token info
160 *
161 * @return null
162 */
163 protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null)
164 {
165 if (empty($formID) || empty($integrationID)) {
166 return;
167 }
168
169 $integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail());
170 $zmailDetails = $integrationHandler->getAIntegration($integrationID);
171
172 if (is_wp_error($zmailDetails)) {
173 return;
174 }
175 $newDetails = json_decode($zmailDetails[0]->integration_details);
176
177 $newDetails->tokenDetails = $tokenDetails;
178
179 $integrationHandler->updateIntegration($integrationID, $zmailDetails[0]->integration_name, 'Zoho Mail', wp_json_encode($newDetails), 'form');
180 }
181
182 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
183 {
184 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
185
186 $tokenDetails = $integrationDetails->tokenDetails;
187
188 if (empty($tokenDetails)) {
189 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for zoho mail api', 'bit-form'));
190 }
191
192 $requiredParams = null;
193 if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) {
194 $requiredParams['clientId'] = $integrationDetails->clientId;
195 $requiredParams['clientSecret'] = $integrationDetails->clientSecret;
196 $requiredParams['dataCenter'] = $integrationDetails->dataCenter;
197 $requiredParams['tokenDetails'] = $tokenDetails;
198 $newTokenDetails = ZohoMailHandler::_refreshAccessToken((object)$requiredParams);
199 if ($newTokenDetails) {
200 ZohoMailHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails);
201 $tokenDetails = $newTokenDetails;
202 }
203 }
204 // $actions = $integrationDetails->actions;
205 $recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID);
206
207 $zmailApiResponse = $recordApiHelper->executeRecordApi(
208 $integrationDetails,
209 $fieldValues,
210 $this->_formID,
211 $entryID
212 );
213
214 return $zmailApiResponse;
215 }
216 }
217