Skip to content

Commit 12eea5e

Browse files
committed
feat: encharge email insert tags combine with existing tags
1 parent 0a1386c commit 12eea5e

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

includes/Actions/Encharge/RecordApiHelper.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace BitCode\FI\Actions\Encharge;
88

9-
use BitCode\FI\Log\LogHandler;
109
use BitCode\FI\Core\Util\Common;
1110
use BitCode\FI\Core\Util\HttpHelper;
11+
use BitCode\FI\Log\LogHandler;
1212

1313
/**
1414
* Provide functionality for Record insert
@@ -19,8 +19,11 @@ class RecordApiHelper
1919

2020
private $_integrationID;
2121

22+
private $_endpoint;
23+
2224
public function __construct($api_key, $integId)
2325
{
26+
$this->_endpoint = 'https://api.encharge.io/v1/people';
2427
$this->_defaultHeader['Content-Type'] = 'application/json';
2528
$this->_defaultHeader['X-Encharge-Token'] = $api_key;
2629
$this->_integrationID = $integId;
@@ -35,9 +38,7 @@ public function __construct($api_key, $integId)
3538
*/
3639
public function insertRecord($data)
3740
{
38-
$insertRecordEndpoint = 'https://api.encharge.io/v1/people';
39-
40-
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
41+
return HttpHelper::post($this->_endpoint, $data, $this->_defaultHeader);
4142
}
4243

4344
public function execute($fieldValues, $fieldMap, $tags)
@@ -55,7 +56,7 @@ public function execute($fieldValues, $fieldMap, $tags)
5556
}
5657
}
5758
if ($tags !== null) {
58-
$fieldData['tags'] = $tags;
59+
$fieldData['tags'] = $this->combineTagsWithExisting($tags, $fieldData['email']);
5960
}
6061
$recordApiResponse = $this->insertRecord(wp_json_encode($fieldData));
6162
$type = 'insert';
@@ -72,4 +73,22 @@ public function execute($fieldValues, $fieldMap, $tags)
7273

7374
return $recordApiResponse;
7475
}
76+
77+
private function combineTagsWithExisting($tags, $email)
78+
{
79+
$endpoint = $this->_endpoint . '?people[0][email]=' . urlencode($email);
80+
81+
$response = HttpHelper::get($endpoint, null, $this->_defaultHeader);
82+
83+
if (is_wp_error($response) || empty($response->users[0]->tags ?? '')) {
84+
return $tags;
85+
}
86+
87+
$existingTags = array_filter(array_map('trim', explode(',', $response->users[0]->tags)));
88+
$newTags = array_filter(array_map('trim', explode(',', $tags)));
89+
90+
$userTags = array_unique(array_merge($existingTags, $newTags));
91+
92+
return implode(',', $userTags);
93+
}
7594
}

0 commit comments

Comments
 (0)