PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.8
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.8
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / classes / class-api-handler-zoho.php
commercebird / includes / classes Last commit date
apis 1 year ago purchase-orders 1 year ago zoho-crm 1 year ago zoho-inventory 1 year ago class-api-handler-zoho.php 1 year ago class-auth-zoho.php 1 year ago class-common.php 1 year ago class-plugin.php 1 year ago class-wc-api.php 1 year ago index.php 1 year ago
class-api-handler-zoho.php
393 lines
1 <?php
2
3 /**
4 * All Execute Call Class related functions.
5 *
6 * @package Inventory
7 */
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 class CMBIRD_API_Handler_Zoho {
13 /**
14 * @var array|array[]
15 */
16 private array $config;
17
18 private $filesystem;
19
20 public function __construct() {
21 $this->config = array(
22 'ExecutecallZI' => array(
23 'OID' => get_option( 'cmbird_zoho_inventory_oid' ),
24 'ATOKEN' => get_option( 'cmbird_zoho_inventory_access_token' ),
25 'RTOKEN' => get_option( 'cmbird_zoho_inventory_refresh_token' ),
26 'EXPIRESTIME' => get_option( 'cmbird_zoho_inventory_timestamp' ),
27 ),
28 'ExecutecallZCRM' => array(
29 'ATOKEN' => get_option( 'cmbird_zoho_crm_access_token' ),
30 'RTOKEN' => get_option( 'cmbird_zoho_crm_refresh_token' ),
31 'EXPIRESTIME' => get_option( 'cmbird_zoho_crm_timestamp' ),
32 ),
33 );
34 if ( null === $this->filesystem ) {
35 global $wp_filesystem;
36
37 if ( empty( $wp_filesystem ) ) {
38 require_once ABSPATH . '/wp-admin/includes/file.php';
39 WP_Filesystem();
40 }
41
42 $this->filesystem = $wp_filesystem;
43 }
44 }
45
46 // Get Call Zoho
47 public function execute_curl_call_get( $url ) {
48 // Sleep for .5 sec for each api calls
49 usleep( 500000 );
50 $handlefunction = new CMBIRD_Auth_Zoho();
51
52 // if $url contains 'inventory' use zoho_access_token, refresh token and timestamp
53 if ( strpos( $url, 'inventory' ) !== false ) {
54 $app_name = 'zoho_inventory';
55 $zoho_access_token = $this->config['ExecutecallZI']['ATOKEN'];
56 $zoho_refresh_token = $this->config['ExecutecallZI']['RTOKEN'];
57 $zoho_timestamp = $this->config['ExecutecallZI']['EXPIRESTIME'];
58 $authorization_header = 'Bearer';
59 } else {
60 $app_name = 'zoho_crm';
61 $zoho_access_token = $this->config['ExecutecallZCRM']['ATOKEN'];
62 $zoho_refresh_token = $this->config['ExecutecallZCRM']['RTOKEN'];
63 $zoho_timestamp = $this->config['ExecutecallZCRM']['EXPIRESTIME'];
64 $authorization_header = 'Zoho-oauthtoken';
65 }
66 $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) );
67 if ( $zoho_timestamp < $current_time ) {
68
69 $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_refresh_token, $app_name );
70 if ( empty( $respo_at_js ) || ! array_key_exists( 'access_token', $respo_at_js ) ) {
71 return new WP_Error( 403, 'Access denied!' );
72 }
73 $zoho_access_token = $respo_at_js['access_token'];
74 if ( 'zoho_inventory' === $app_name ) {
75 update_option( 'cmbird_zoho_inventory_access_token', $respo_at_js['access_token'] );
76 update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] );
77 } else {
78 update_option( 'cmbird_zoho_crm_access_token', $respo_at_js['access_token'] );
79 update_option( 'cmbird_zoho_crm_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] );
80 }
81 }
82
83 $args = array(
84 'headers' => array(
85 'Authorization' => "$authorization_header $zoho_access_token",
86 ),
87 );
88
89 $response = wp_remote_get( $url, $args );
90
91 // get the status code of the response
92 $status_code = wp_remote_retrieve_response_code( $response );
93 // if code is 429, update the option "zoho_rate_limit_exceeded" to true
94 if ( 429 === $status_code ) {
95 update_option( 'cmbird_zoho_rate_limit_exceeded', true );
96 } else {
97 update_option( 'cmbird_zoho_rate_limit_exceeded', false );
98 }
99
100 // Check if the request was successful
101 if ( ! is_wp_error( $response ) ) {
102 // If successful, get the body of the response
103 $body = wp_remote_retrieve_body( $response );
104
105 // Decode JSON response
106 return json_decode( $body );
107 } else {
108 // If there was an error, handle it
109 $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.';
110 return "Error: $error_message";
111 }
112 }
113
114 // Post Call Zoho
115
116 public function execute_curl_call_post( $url, $data ) {
117 $handlefunction = new CMBIRD_Auth_Zoho();
118
119 // if $url contains 'inventory' use zoho__access_token, refresh token and timestamp
120 if ( strpos( $url, 'inventory' ) !== false ) {
121 $app_name = 'zoho_inventory';
122 $zoho_access_token = $this->config['ExecutecallZI']['ATOKEN'];
123 $zoho_refresh_token = $this->config['ExecutecallZI']['RTOKEN'];
124 $zoho_timestamp = $this->config['ExecutecallZI']['EXPIRESTIME'];
125 $authorization_header = 'Bearer';
126 } else {
127 $app_name = 'zoho_crm';
128 $zoho_access_token = $this->config['ExecutecallZCRM']['ATOKEN'];
129 $zoho_refresh_token = $this->config['ExecutecallZCRM']['RTOKEN'];
130 $zoho_timestamp = $this->config['ExecutecallZCRM']['EXPIRESTIME'];
131 $authorization_header = 'Zoho-oauthtoken';
132 }
133
134 $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) );
135
136 if ( $zoho_timestamp < $current_time ) {
137
138 $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_refresh_token, $app_name );
139
140 $zoho_access_token = $respo_at_js['access_token'];
141 if ( 'zoho_inventory' === $app_name ) {
142 update_option( 'cmbird_zoho_inventory_access_token', $respo_at_js['access_token'] );
143 update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] );
144 } else {
145 update_option( 'cmbird_zoho_crm_access_token', $respo_at_js['access_token'] );
146 update_option( 'cmbird_zoho_crm_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] );
147 }
148 }
149
150 $args = array(
151 'body' => $data,
152 'headers' => array(
153 'Authorization' => "$authorization_header $zoho_access_token",
154 ),
155 );
156 $response = wp_remote_post( $url, $args );
157
158 // get the status code of the response
159 $status_code = wp_remote_retrieve_response_code( $response );
160 // if code is 429, update the option "zoho_rate_limit_exceeded" to true
161 if ( 429 === $status_code ) {
162 update_option( 'cmbird_zoho_rate_limit_exceeded', true );
163 } else {
164 update_option( 'cmbird_zoho_rate_limit_exceeded', false );
165 }
166
167 // Check if the request was successful
168 if ( ! is_wp_error( $response ) ) {
169 // If successful, get the body of the response
170 $body = wp_remote_retrieve_body( $response );
171 // Decode JSON response
172 return json_decode( $body );
173 } else {
174 // If there was an error, return the entire WP Error object
175 return $response;
176 }
177 }
178
179 // Put Call Zoho
180
181 public function execute_curl_call_put( $url, $data ) {
182
183 $handlefunction = new CMBIRD_Auth_Zoho();
184
185 // if $url contains 'inventory' use zoho_inventory_access_token, refresh token and timestamp
186 if ( strpos( $url, 'inventory' ) !== false ) {
187 $app_name = 'zoho_inventory';
188 $zoho_inventory_access_token = $this->config['ExecutecallZI']['ATOKEN'];
189 $zoho_inventory_refresh_token = $this->config['ExecutecallZI']['RTOKEN'];
190 $zoho_inventory_timestamp = $this->config['ExecutecallZI']['EXPIRESTIME'];
191 $authorization_header = 'Bearer';
192 } else {
193 $app_name = 'zoho_crm';
194 $zoho_inventory_access_token = $this->config['ExecutecallZCRM']['ATOKEN'];
195 $zoho_inventory_refresh_token = $this->config['ExecutecallZCRM']['RTOKEN'];
196 $zoho_inventory_timestamp = $this->config['ExecutecallZCRM']['EXPIRESTIME'];
197 $authorization_header = 'Zoho-oauthtoken';
198 }
199
200 $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) );
201
202 if ( $zoho_inventory_timestamp < $current_time ) {
203
204 $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_inventory_refresh_token, $app_name );
205 $zoho_inventory_access_token = $respo_at_js['access_token'];
206 if ( 'zoho_inventory' === $app_name ) {
207 update_option( 'cmbird_zoho_inventory_access_token', $respo_at_js['access_token'] );
208 update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] );
209 } else {
210 update_option( 'cmbird_zoho_crm_access_token', $respo_at_js['access_token'] );
211 update_option( 'cmbird_zoho_crm_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] );
212 }
213 }
214
215 $args = array(
216 'body' => $data,
217 'headers' => array(
218 'Authorization' => "$authorization_header $zoho_inventory_access_token",
219 ),
220 'method' => 'PUT',
221 );
222 $response = wp_remote_request( $url, $args );
223
224 // get the status code of the response
225 $status_code = wp_remote_retrieve_response_code( $response );
226 // if code is 429, update the option "zoho_rate_limit_exceeded" to true
227 if ( 429 === $status_code ) {
228 update_option( 'cmbird_zoho_rate_limit_exceeded', true );
229 } else {
230 update_option( 'cmbird_zoho_rate_limit_exceeded', false );
231 }
232
233 // Check if the request was successful
234 if ( ! is_wp_error( $response ) ) {
235 // If successful, get the body of the response
236 $body = wp_remote_retrieve_body( $response );
237 // Decode JSON response
238 return json_decode( $body );
239 } else {
240 // If there was an error, handle it
241 $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.';
242 return "Error: $error_message";
243 }
244 }
245
246 /**
247 * Delete Call to Zoho
248 * @param string $url
249 * @return mixed
250 */
251 public function execute_curl_call_delete( $url ) {
252 $handlefunction = new CMBIRD_Auth_Zoho();
253
254 // if $url contains 'inventory' use zoho_inventory_access_token, refresh token and timestamp
255 if ( strpos( $url, 'inventory' ) !== false ) {
256 $app_name = 'zoho_inventory';
257 $zoho_inventory_access_token = $this->config['ExecutecallZI']['ATOKEN'];
258 $zoho_inventory_refresh_token = $this->config['ExecutecallZI']['RTOKEN'];
259 $zoho_inventory_timestamp = $this->config['ExecutecallZI']['EXPIRESTIME'];
260 $authorization_header = 'Bearer';
261 } else {
262 $app_name = 'zoho_crm';
263 $zoho_inventory_access_token = $this->config['ExecutecallZCRM']['ATOKEN'];
264 $zoho_inventory_refresh_token = $this->config['ExecutecallZCRM']['RTOKEN'];
265 $zoho_inventory_timestamp = $this->config['ExecutecallZCRM']['EXPIRESTIME'];
266 $authorization_header = 'Zoho-oauthtoken';
267 }
268
269 $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) );
270
271 if ( $zoho_inventory_timestamp < $current_time ) {
272
273 $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_inventory_refresh_token, $app_name );
274 $zoho_inventory_access_token = $respo_at_js['access_token'];
275 if ( 'zoho_inventory' === $app_name ) {
276 update_option( 'cmbird_zoho_inventory_access_token', $respo_at_js['access_token'] );
277 update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] );
278 } else {
279 update_option( 'cmbird_zoho_crm_access_token', $respo_at_js['access_token'] );
280 update_option( 'cmbird_zoho_crm_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] );
281 }
282 }
283
284 $args = array(
285 'headers' => array(
286 'Authorization' => "$authorization_header $zoho_inventory_access_token",
287 ),
288 'method' => 'DELETE',
289 );
290 $response = wp_remote_request( $url, $args );
291
292 // get the status code of the response
293 $status_code = wp_remote_retrieve_response_code( $response );
294 // if code is 429, update the option "zoho_rate_limit_exceeded" to true
295 if ( 429 === $status_code ) {
296 update_option( 'cmbird_zoho_rate_limit_exceeded', true );
297 } else {
298 update_option( 'cmbird_zoho_rate_limit_exceeded', false );
299 }
300
301 // Check if the request was successful
302 if ( ! is_wp_error( $response ) ) {
303 // If successful, get the body of the response
304 $body = wp_remote_retrieve_body( $response );
305 // Decode JSON response
306 return json_decode( $body );
307 } else {
308 // If there was an error, handle it
309 $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.';
310 return "Error: $error_message";
311 }
312 }
313
314 /**
315 *
316 * Get Call Zoho Image
317 * @param mixed $url - URL of the image.
318 * @return string
319 */
320 public function execute_curl_call_image_get( $url, $image_name ) {
321 // $fd = fopen( __DIR__ . '/execute_curl_call_image_get.txt', 'w' );
322
323 global $wp_filesystem;
324
325 $handlefunction = new CMBIRD_Auth_Zoho();
326 $zoho_inventory_access_token = $this->config['ExecutecallZI']['ATOKEN'];
327 $zoho_inventory_refresh_token = $this->config['ExecutecallZI']['RTOKEN'];
328 $zoho_inventory_timestamp = $this->config['ExecutecallZI']['EXPIRESTIME'];
329
330 $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) );
331
332 if ( $zoho_inventory_timestamp < $current_time ) {
333
334 $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_inventory_refresh_token, 'zoho_inventory' );
335
336 $zoho_inventory_access_token = $respo_at_js['access_token'];
337 update_option( 'cmbird_zoho_inventory_access_token', $respo_at_js['access_token'] );
338 update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] );
339
340 }
341 $args = array(
342 'headers' => array(
343 'Authorization' => 'Bearer ' . $zoho_inventory_access_token,
344 ),
345 );
346 $response = wp_remote_get( $url, $args );
347 // fwrite( $fd, PHP_EOL . 'Response : ' . print_r( $response, true ) );
348 // fclose( $fd );
349
350 // Check if the request was successful
351 if ( ! is_wp_error( $response ) ) {
352 // If successful, get the body of the response
353 $body = wp_remote_retrieve_body( $response );
354
355 // Set up the upload directory
356 $upload = wp_upload_dir();
357 $absolute_upload_path = $upload['basedir'] . '/zoho_image/';
358 $url_upload_path = $upload['baseurl'] . '/zoho_image/';
359
360 // Generate a unique image name
361 $img = $image_name;
362 $upload_dir = $absolute_upload_path . '/' . $img;
363
364 // remove the file if it exists
365 if ( file_exists( $upload_dir ) ) {
366 wp_delete_file( $upload_dir );
367 }
368
369 // Create the directory if it doesn't exist
370 if ( ! is_dir( $absolute_upload_path ) ) {
371 wp_mkdir_p( $absolute_upload_path );
372 }
373 // Save the image file
374 try {
375 $wp_filesystem->put_contents( $upload_dir, $body );
376 } catch (Exception $e) {
377 wp_delete_file( $upload_dir );
378 // If there was an error, handle it
379 $error_message = $e->getMessage();
380 echo esc_html( "Error image import: $error_message" );
381 return '';
382 }
383 // Use trailingslashit to make sure the URL ends with a single slash
384 return trailingslashit( $url_upload_path ) . $img;
385 } else {
386 // If there was an error, handle it
387 $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.';
388 echo esc_html( "Error image import: $error_message" );
389 return '';
390 }
391 }
392 }
393