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

GclidHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.9.1, at includes/Core/Integration/Gclid/GclidHandler.php

199 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoBigin Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\Gclid;
9
10 use BitCode\BitForm\Core\Database\GclidInfoModel;
11 use BitCode\BitForm\Core\Integration\IntegrationHandler;
12 use BitCode\BitForm\Core\Util\HttpHelper;
13 use BitCode\BitForm\Core\Util\IpTool;
14
15 /**
16 * Provide functionality for ZohoCrm integration
17 */
18 class GclidHandler
19 {
20 private $_formID;
21 private $_integrationID;
22
23 public function __construct($integrationID, $fromID)
24 {
25 $this->_formID = $fromID;
26 $this->_integrationID = $integrationID;
27 }
28
29 /**
30 * Helps to register ajax function's with wp
31 *
32 * @return null
33 */
34 public static function registerAjax()
35 {
36 add_action('wp_ajax_bitforms_google_generate_token', [__CLASS__, 'generateTokens']);
37 add_action('wp_ajax_bitforms_save_google_refresh_token', [__CLASS__, '_saveRefreshedToken']);
38 add_action('wp_ajax_bitforms_get_gclid_info', [__CLASS__, 'getGclidInfo']);
39 add_action('wp_ajax_bitform_google_adword_config', [__CLASS__, 'getGoogleAdwordConfig']);
40 }
41
42 /**
43 * Process ajax request for generate_token
44 *
45 * @return JSON zoho bigin api response and status
46 */
47 public static function generateTokens()
48 {
49 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
50 $inputJSON = file_get_contents('php://input');
51 $requestsParams = json_decode($inputJSON);
52 if (
53 empty($requestsParams->clientId)
54 || empty($requestsParams->clientSecret)
55 || empty($requestsParams->redirectURI)
56 || empty($requestsParams->code)
57 ) {
58 wp_send_json_error(
59 __(
60 'Requested parameter is empty',
61 'bit-form'
62 ),
63 400
64 );
65 }
66 $apiEndpoint = \urldecode('https://oauth2.googleapis.com/token');
67 $requestParams = [
68 'grant_type' => 'authorization_code',
69 'client_id' => \urldecode($requestsParams->clientId),
70 'client_secret' => \urldecode($requestsParams->clientSecret),
71 'redirect_uri' => \urldecode($requestsParams->redirectURI),
72 'code' => \urldecode($requestsParams->code)
73 ];
74 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams);
75 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
76 wp_send_json_error(
77 empty($apiResponse->error) ? 'Unknown' : $apiResponse->error,
78 400
79 );
80 }
81 $license = get_option('bitformpro_integrate_key_data');
82 $googleAdsApi = new ApiHelper($apiResponse, $requestsParams->clientCustomerId, $license['key']);
83 $response = $googleAdsApi->authenticateGoogleAdword();
84 $xml_data = simplexml_load_string($response->data);
85 $json = json_encode($xml_data);
86 $authenticateCustomer = json_decode($json, true);
87 if (is_wp_error($authenticateCustomer) || !empty($authenticateCustomer['ApiError'])) {
88 wp_send_json_error(
89 empty($authenticateCustomer['ApiError']) ? 'Unknown' : $authenticateCustomer['ApiError']['type'],
90 400
91 );
92 }
93 $apiResponse->generates_on = \time();
94 wp_send_json_success($apiResponse, 200);
95 } else {
96 wp_send_json_error(
97 __(
98 'Token expired',
99 'bit-form'
100 ),
101 401
102 );
103 }
104 }
105
106 /**
107 * Helps to refresh zoho bigin access_token
108 *
109 * @param Array $apiData Contains required data for refresh access token
110 * @return JSON $tokenDetails API token details
111 */
112 public static function _refreshAccessToken($apiData)
113 {
114 if (
115 empty($apiData->clientId)
116 || empty($apiData->clientSecret)
117 || empty($apiData->tokenDetails)
118 ) {
119 return false;
120 }
121 $tokenDetails = $apiData->tokenDetails;
122
123 $apiEndpoint = 'https://oauth2.googleapis.com/token';
124 $requestParams = [
125 'grant_type' => 'refresh_token',
126 'client_id' => $apiData->clientId,
127 'client_secret' => $apiData->clientSecret,
128 'refresh_token' => $tokenDetails->refresh_token,
129 ];
130
131 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams);
132 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
133 return false;
134 }
135 $tokenDetails->generates_on = \time();
136 $tokenDetails->access_token = $apiResponse->access_token;
137 return $tokenDetails;
138 }
139
140 /**
141 * Save updated access_token to avoid unnecessary token generation
142 *
143 * @param Integer $fromID ID of Integration related form
144 * @param Integer $integrationID ID of Zoho bigin Integration
145 * @param Obeject $tokenDetails refreshed token info
146 *
147 * @return null
148 */
149 public static function _saveRefreshedToken()
150 {
151 \ignore_user_abort();
152 if (wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) {
153 unset($_REQUEST['_ajax_nonce'], $_REQUEST['action']);
154 $inputJSON = file_get_contents('php://input');
155 $requestsParams = json_decode($inputJSON);
156 $integrationHandler = new IntegrationHandler(0, IpTool::getUserDetail());
157 $googleDetails = $integrationHandler->getAllIntegration('app', 'Google');
158 if (isset($googleDetails->errors['result_empty'])) {
159 $result = $integrationHandler->saveIntegration('Gclid', 'Google', json_encode($requestsParams), 'app');
160 } else {
161 $result = $integrationHandler->updateIntegration($googleDetails[0]->id, 'Gclid', 'Google', json_encode($requestsParams), 'app');
162 }
163 if (is_wp_error($result)) {
164 return false;
165 }
166 wp_send_json_success($result, 200);
167 }
168 }
169
170 public static function getGclidInfo()
171 {
172 if (wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) {
173 unset($_REQUEST['_ajax_nonce'], $_REQUEST['action']);
174 $inputJSON = file_get_contents('php://input');
175 $requestsParams = json_decode($inputJSON);
176 $gclid = $requestsParams->gclid;
177 $gclidInfoModel = new GclidInfoModel();
178 $gclidInfo = $gclidInfoModel->gclidDetail($gclid);
179 if (is_wp_error($gclidInfo)) {
180 return false;
181 }
182 wp_send_json_success($gclidInfo, 200);
183 }
184 }
185
186 public static function getGoogleAdwordConfig()
187 {
188 if (wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) {
189 unset($_REQUEST['_ajax_nonce'], $_REQUEST['action']);
190 $integrationHandler = new IntegrationHandler(0, IpTool::getUserDetail());
191 $googleDetails = $integrationHandler->getAllIntegration('app', 'Google');
192 if (is_wp_error($googleDetails)) {
193 return false;
194 }
195 wp_send_json_success($googleDetails, 200);
196 }
197 }
198 }
199