/** GHL API v3 bridge. Credentials are read from wp-config.php constants location_id and private_key. */ if (!function_exists('redbox_ghl_v3_request')) { function redbox_ghl_v3_request($method, $path, $payload = null) { if (!defined('location_id') || !defined('private_key') || !constant('location_id') || !constant('private_key')) { error_log('GHL v3: location_id/private_key constants are missing from wp-config.php.'); return new WP_Error('ghl_config_missing', 'GHL configuration is missing.'); } $args = [ 'method' => $method, 'timeout' => 20, 'headers' => [ 'Authorization' => 'Bearer ' . constant('private_key'), 'Version' => 'v3', 'Accept' => 'application/json', 'Content-Type' => 'application/json', ], ]; if ($payload !== null) $args['body'] = wp_json_encode($payload); $response = wp_remote_request('https://services.leadconnectorhq.com/' . ltrim($path, '/'), $args); if (is_wp_error($response)) { error_log('GHL v3 request failed (' . $method . ' ' . $path . '): ' . $response->get_error_message()); return $response; } $status = wp_remote_retrieve_response_code($response); $body = json_decode(wp_remote_retrieve_body($response), true); if ($status < 200 || $status >= 300) { error_log('GHL v3 request failed (' . $method . ' ' . $path . '), HTTP ' . $status . '.'); return new WP_Error('ghl_http_error', 'GHL API returned HTTP ' . $status, ['status' => $status, 'body' => $body]); } return is_array($body) ? $body : []; } } if (!function_exists('redbox_ghl_v3_sync')) { function redbox_ghl_v3_sync($email, $first_name, $last_name, $full_name, $phone, $legacy_fields, $pipeline_id, $stage_id, $tags) { $location = (string) constant('location_id'); $custom_fields = []; if (isset($legacy_fields['customField']) && is_array($legacy_fields['customField'])) { foreach ($legacy_fields['customField'] as $field_id => $value) { if ((string) $field_id !== '' && !is_array($value) && !is_object($value)) { $custom_fields[] = ['id' => (string) $field_id, 'fieldValue' => (string) $value]; } } } $contact_data = [ 'locationId' => $location, 'firstName' => (string) $first_name, 'lastName' => (string) $last_name, 'name' => (string) $full_name, 'email' => (string) $email, 'phone' => (string) $phone, 'tags' => array_values((array) $tags), ]; if ($custom_fields) $contact_data['customFields'] = $custom_fields; $contact_id = ''; $lookup = null; if ($email !== '') { $lookup = redbox_ghl_v3_request('GET', 'contacts/lookup?locationId=' . rawurlencode($location) . '&email=' . rawurlencode((string) $email)); } elseif ($phone !== '') { $lookup = redbox_ghl_v3_request('GET', 'contacts/lookup?locationId=' . rawurlencode($location) . '&phone=' . rawurlencode((string) $phone)); } if (!is_wp_error($lookup) && !empty($lookup['contacts'][0]['id'])) { $contact_id = (string) $lookup['contacts'][0]['id']; $updated = redbox_ghl_v3_request('PUT', 'contacts/' . rawurlencode($contact_id), $contact_data); if (is_wp_error($updated)) return false; } else { $created = redbox_ghl_v3_request('POST', 'contacts', $contact_data); if (is_wp_error($created)) return false; $contact_id = (string) ($created['contact']['id'] ?? $created['id'] ?? ''); } if ($contact_id === '') { error_log('GHL v3: contact request succeeded but returned no contact ID.'); return false; } $opportunity = redbox_ghl_v3_request('POST', 'opportunities', [ 'pipelineId' => (string) $pipeline_id, 'locationId' => $location, 'name' => mb_substr((string) $full_name, 0, 255), 'pipelineStageId' => (string) $stage_id, 'status' => 'open', 'contactId' => $contact_id, ]); return !is_wp_error($opportunity); } } Comments on: Accueil https://monsieurmath.com Tutorat en mathématique pour primaire et secondaire Tue, 08 Oct 2024 15:49:25 +0000 hourly 1 https://wordpress.org/?v=7.1.5