ZohoCRMSync.php
77 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 | /** |
| 53 | * Refresh the access token of Zoho CRM. |
| 54 | * @since 1.0.0 |
| 55 | * @return void | \WP_Error |
| 56 | */ |
| 57 | public static function refresh_token() { |
| 58 | // return if there is no access token |
| 59 | if ( empty( get_option( 'cmbird_zoho_crm_access_token' ) ) ) { |
| 60 | return; |
| 61 | } |
| 62 | $zoho_refresh_token = get_option( 'cmbird_zoho_crm_refresh_token' ); |
| 63 | $zoho_timestamp = get_option( 'cmbird_zoho_crm_timestamp' ); |
| 64 | $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) ); |
| 65 | if ( $zoho_timestamp < $current_time ) { |
| 66 | $handlefunction = new CMBIRD_Auth_Zoho(); |
| 67 | $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_refresh_token, 'zoho_crm' ); |
| 68 | if ( empty( $respo_at_js ) || ! array_key_exists( 'access_token', $respo_at_js ) ) { |
| 69 | return new WP_Error( 403, 'Access denied!' ); |
| 70 | } else { |
| 71 | update_option( 'cmbird_zoho_crm_access_token', $respo_at_js['access_token'] ); |
| 72 | update_option( 'cmbird_zoho_crm_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] ); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 |