PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.4.5
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.4.5
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 / admin / includes / Actions / Sync / ZohoCRMSync.php
commercebird / admin / includes / Actions / Sync Last commit date
ExactOnlineSync.php 11 months ago ZohoCRMSync.php 11 months ago index.php 1 year ago
ZohoCRMSync.php
112 lines
1 <?php
2
3 namespace CommerceBird\Admin\Actions\Sync;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use CMBIRD_API_Handler_Zoho;
10 use CMBIRD_Auth_Zoho;
11 use WP_Error;
12
13 class ZohoCRMSync {
14
15 /* create API Get call to Zoho CRM to get all custom fields
16 *
17 * @param module $module - module name
18 * @return array | \WP_Error
19 */
20 public static function get_custom_fields( $module ) {
21 $zoho_crm_url = get_option( 'cmbird_zoho_crm_url' );
22 $url = $zoho_crm_url . 'crm/v7/settings/fields?module=' . $module;
23 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
24 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
25 if ( is_wp_error( $json ) ) {
26 return $json;
27 } else {
28 // Parse the response
29 $parsed_fields = array();
30
31 foreach ( $json->fields as $field ) {
32 if (
33 ! empty( $field->custom_field ) &&
34 ( empty( $field->read_only ) || $field->read_only === false )
35 ) {
36 $parsed_fields[] = [
37 'id' => $field->id,
38 'apiName' => $field->api_name,
39 'visible' => (bool) $field->visible,
40 'fieldLabel' => $field->field_label,
41 'displayLabel' => $field->display_label,
42 'customField' => true,
43 'dataType' => $field->data_type,
44 ];
45 }
46 }
47
48 return $parsed_fields;
49 }
50 }
51
52 /* create API Get call to Zoho CRM to get ALL fields (standard + custom)
53 *
54 * @param module $module - module name
55 * @return array | \WP_Error
56 */
57 public static function get_all_fields( $module ) {
58 $zoho_crm_url = get_option( 'cmbird_zoho_crm_url' );
59 $url = $zoho_crm_url . 'crm/v7/settings/fields?module=' . $module;
60 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
61 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
62 if ( is_wp_error( $json ) ) {
63 return $json;
64 } else {
65 // Parse the response to get ALL fields (both standard and custom)
66 $parsed_fields = array();
67
68 // Fields to exclude for Deals module
69 $excluded_fields = array( 'Account_Name', 'Contact_Name', 'Stage' );
70
71 foreach ( $json->fields as $field ) {
72 // Include all fields that are not read-only and are visible
73 // For Deals module, also exclude Account Name, Contact Name and Stage
74 if ( ( empty( $field->read_only ) || $field->read_only === false ) && $field->visible ) {
75 // Skip excluded fields for Deals module
76 if ( $module === 'Deals' && in_array( $field->api_name, $excluded_fields ) ) {
77 continue;
78 }
79 $parsed_fields[ $field->api_name ] = $field->field_label;
80 }
81 }
82
83 return $parsed_fields;
84 }
85 }
86
87 /**
88 * Refresh the access token of Zoho CRM.
89 * @since 1.0.0
90 * @return void | \WP_Error
91 */
92 public static function refresh_token() {
93 // return if there is no access token
94 if ( empty( get_option( 'cmbird_zoho_crm_access_token' ) ) ) {
95 return;
96 }
97 $zoho_refresh_token = get_option( 'cmbird_zoho_crm_refresh_token' );
98 $zoho_timestamp = get_option( 'cmbird_zoho_crm_timestamp' );
99 $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) );
100 if ( $zoho_timestamp < $current_time ) {
101 $handlefunction = new CMBIRD_Auth_Zoho();
102 $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_refresh_token, 'zoho_crm' );
103 if ( empty( $respo_at_js ) || ! array_key_exists( 'access_token', $respo_at_js ) ) {
104 return new WP_Error( 403, 'Access denied!' );
105 } else {
106 update_option( 'cmbird_zoho_crm_access_token', $respo_at_js['access_token'] );
107 update_option( 'cmbird_zoho_crm_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] );
108 }
109 }
110 }
111 }
112