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