PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.2
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Integration / ElasticEmail / RecordApiHelper.php

RecordApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.2.2, at includes/Core/Integration/ElasticEmail/RecordApiHelper.php

92 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Integration\ElasticEmail;
4
5 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
6 use BitCode\BitForm\Core\Util\HttpHelper;
7
8 class RecordApiHelper
9 {
10 private $_defaultHeader;
11
12 private $_logID;
13
14 private $_logResponse;
15
16 private $integrationDetails;
17
18 private $entryId;
19
20 public function __construct($api_key, $integId, $logID, $integrationDetails, $entryId)
21 {
22 $this->integrationDetails = $integrationDetails;
23 $this->_defaultHeader['Content-Type'] = 'application/json';
24 $this->_defaultHeader['Authorization'] = "Bearer $api_key";
25 $this->_logResponse = new UtilApiResponse();
26 $this->_logID = $logID;
27 $this->entryId = $entryId;
28 }
29
30 public function createContact($data, $listName, $apiKey)
31 {
32 $tmpData = \is_string($data) ? $data : wp_json_encode([(object) $data]);
33 $header = [
34 'X-ElasticEmail-ApiKey' => $apiKey,
35 'Content-Type' => 'application/json',
36 ];
37
38 $insertRecordEndpoint = "https://api.elasticemail.com/v4/contacts?$listName";
39 return HttpHelper::post($insertRecordEndpoint, $tmpData, $header);
40 }
41
42 public function generateReqDataFromFieldMap($data, $fieldMap)
43 {
44 $dataFinal = [];
45
46 foreach ($fieldMap as $value) {
47 $triggerValue = $value->formField;
48 $actionValue = $value->elasticEmailField;
49 if ('custom' === $triggerValue) {
50 $dataFinal[$actionValue] = $value->customValue;
51 } elseif (!is_null($data[$triggerValue])) {
52 $dataFinal[$actionValue] = $data[$triggerValue];
53 }
54 }
55 if ($this->integrationDetails->actions) {
56 if (property_exists($this->integrationDetails->actions, 'status') && $this->integrationDetails->actions->status) {
57 $dataFinal['Status'] = $this->integrationDetails->status;
58 }
59 }
60 return $dataFinal;
61 }
62
63 public function execute($integId, $fieldValues, $fieldMap, $integrationDetails, $formID)
64 {
65 $finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap);
66 $listName = $integrationDetails->list_id;
67 $query = '';
68
69 foreach ($listName as $key => $val) {
70 $query .= 'listnames=' . $val . '&';
71 };
72 if (strlen($query)) {
73 $query = substr($query, 0, -1);
74 }
75 $api_key = $integrationDetails->api_key;
76 $apiResponse = $this->createContact($finalData, $query, $api_key);
77
78 $entryDetails = [
79 'formId' => $formID,
80 'entryId' => $this->entryId,
81 'fieldValues' => $fieldValues
82 ];
83
84 if (isset($apiResponse->Error)) {
85 $this->_logResponse->apiResponse($this->_logID, $integId, ['type' => 'contact', 'type_name' => 'contact_add'], 'errors', $apiResponse, $entryDetails);
86 } else {
87 $this->_logResponse->apiResponse($this->_logID, $integId, ['type' => 'contact', 'type_name' => 'contact_add'], 'success', $apiResponse, $entryDetails);
88 }
89 return $apiResponse;
90 }
91 }
92