| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* MailerLite Record Api |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace BitCode\BitForm\Core\Integration\MailerLite; |
| 8 |
|
| 9 |
use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
|
| 13 |
/** |
| 14 |
* Provide functionality for Record insert, upsert |
| 15 |
*/ |
| 16 |
class RecordApiHelper |
| 17 |
{ |
| 18 |
private $_integrationID; |
| 19 |
private $_baseUrl; |
| 20 |
|
| 21 |
private $_defaultHeader; |
| 22 |
|
| 23 |
private $_logID; |
| 24 |
|
| 25 |
private $_logResponse; |
| 26 |
|
| 27 |
private $_entryID; |
| 28 |
|
| 29 |
private $_actions; |
| 30 |
|
| 31 |
public function __construct($auth_token, $integId, $logID, $entryID, $actions, $version) |
| 32 |
{ |
| 33 |
$this->_integrationID = $integId; |
| 34 |
if ('v2' === $version) { |
| 35 |
$this->_baseUrl = 'https://connect.mailerlite.com/api/'; |
| 36 |
$this->_defaultHeader = [ |
| 37 |
'Authorization' => "Bearer $auth_token" |
| 38 |
]; |
| 39 |
} else { |
| 40 |
$this->_baseUrl = 'https://api.mailerlite.com/api/v2/'; |
| 41 |
$this->_defaultHeader = [ |
| 42 |
'X-Mailerlite-Apikey' => $auth_token |
| 43 |
]; |
| 44 |
} |
| 45 |
$this->_logID = $logID; |
| 46 |
$this->_logResponse = new UtilApiResponse(); |
| 47 |
$this->_entryID = $entryID; |
| 48 |
$this->_actions = $actions; |
| 49 |
} |
| 50 |
|
| 51 |
public function existSubscriber($auth_token, $email) |
| 52 |
{ |
| 53 |
if (empty($auth_token)) { |
| 54 |
wp_send_json_error( |
| 55 |
__( |
| 56 |
'Requested parameter is empty', |
| 57 |
'bit-integrations' |
| 58 |
), |
| 59 |
400 |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
$apiEndpoints = $this->_baseUrl . "subscribers/$email"; |
| 64 |
|
| 65 |
$response = HttpHelper::get($apiEndpoints, null, $this->_defaultHeader); |
| 66 |
if (property_exists($response, 'error') || 'Resource not found.' === $response->message) { |
| 67 |
return false; |
| 68 |
} else { |
| 69 |
return true; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function enableDoubleOptIn($auth_token) |
| 74 |
{ |
| 75 |
if (empty($auth_token)) { |
| 76 |
wp_send_json_error( |
| 77 |
__( |
| 78 |
'Requested parameter is empty', |
| 79 |
'bit-integrations' |
| 80 |
), |
| 81 |
400 |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
$apiEndpoints = $this->_baseUrl . 'settings/double_optin'; |
| 86 |
$requestParams = [ |
| 87 |
'enable' => true |
| 88 |
]; |
| 89 |
|
| 90 |
HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader); |
| 91 |
} |
| 92 |
|
| 93 |
public function addSubscriber($auth_token, $groupIds, $type, $finalData) |
| 94 |
{ |
| 95 |
$apiEndpoints = $this->_baseUrl . 'subscribers'; |
| 96 |
$splitGroupIds = null; |
| 97 |
if (!empty($groupIds)) { |
| 98 |
$splitGroupIds = explode(',', $groupIds); |
| 99 |
} |
| 100 |
|
| 101 |
if (empty($finalData['email'])) { |
| 102 |
return ['success'=>false, 'message'=>'Required field Email is empty', 'code'=>400]; |
| 103 |
} |
| 104 |
if ('https://connect.mailerlite.com/api/' === $this->_baseUrl) { |
| 105 |
$requestParams = [ |
| 106 |
'email' => $finalData['email'], |
| 107 |
'status' => $type ? $type : 'active', |
| 108 |
]; |
| 109 |
} else { |
| 110 |
$requestParams = [ |
| 111 |
'email' => $finalData['email'], |
| 112 |
'type' => $type ? $type : 'active', |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
foreach ($finalData as $key => $value) { |
| 117 |
if ('email' !== $key) { |
| 118 |
if ('name' === $key) { |
| 119 |
$requestParams[$key] = $value; |
| 120 |
} else { |
| 121 |
$requestParams['fields'][$key] = $value; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
$requestParams['fields'] = !empty($requestParams['fields']) ? (object) $requestParams['fields'] : []; |
| 126 |
$email = $finalData['email']; |
| 127 |
$isExist = $this->existSubscriber($auth_token, $email); |
| 128 |
$response = null; |
| 129 |
|
| 130 |
if ($isExist && !empty($this->_actions->update)) { |
| 131 |
if (!empty($this->_actions->double_opt_in)) { |
| 132 |
$this->enableDoubleOptIn($auth_token); |
| 133 |
} |
| 134 |
if (!empty($groupIds)) { |
| 135 |
if ('https://connect.mailerlite.com/api/' === $this->_baseUrl) { |
| 136 |
$requestParams['groups'] = $splitGroupIds; |
| 137 |
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader); |
| 138 |
} else { |
| 139 |
for ($i = 0; $i < count($splitGroupIds); $i++) { |
| 140 |
$apiEndpoints = $this->_baseUrl . 'groups/' . $splitGroupIds[$i] . '/subscribers'; |
| 141 |
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader); |
| 142 |
}; |
| 143 |
} |
| 144 |
return $response; |
| 145 |
} |
| 146 |
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader); |
| 147 |
$response->update = true; |
| 148 |
} elseif ($isExist && empty($this->_actions->update)) { |
| 149 |
return ['success'=>false, 'message'=>'Subscriber already exist', 'code'=>400]; |
| 150 |
} else { |
| 151 |
if (!empty($this->_actions->double_opt_in)) { |
| 152 |
$this->enableDoubleOptIn($auth_token); |
| 153 |
} |
| 154 |
if (!empty($groupIds)) { |
| 155 |
if ('https://connect.mailerlite.com/api/' === $this->_baseUrl) { |
| 156 |
$requestParams['groups'] = $splitGroupIds; |
| 157 |
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader); |
| 158 |
} else { |
| 159 |
for ($i = 0; $i < count($splitGroupIds); $i++) { |
| 160 |
$apiEndpoints = $this->_baseUrl . 'groups/' . $splitGroupIds[$i] . '/subscribers'; |
| 161 |
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader); |
| 162 |
}; |
| 163 |
} |
| 164 |
return $response; |
| 165 |
} |
| 166 |
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader); |
| 167 |
} |
| 168 |
return $response; |
| 169 |
} |
| 170 |
|
| 171 |
public function generateReqDataFromFieldMap($data, $fieldMap) |
| 172 |
{ |
| 173 |
$dataFinal = []; |
| 174 |
|
| 175 |
foreach ($fieldMap as $value) { |
| 176 |
$triggerValue = $value->formField; |
| 177 |
$actionValue = $value->mailerLiteFormField; |
| 178 |
if ('custom' === $triggerValue) { |
| 179 |
$dataFinal[$actionValue] = $value->customValue; |
| 180 |
} elseif (!is_null($data[$triggerValue])) { |
| 181 |
$dataFinal[$actionValue] = $data[$triggerValue]; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return $dataFinal; |
| 186 |
} |
| 187 |
|
| 188 |
public function executeRecordApi( |
| 189 |
$groupId, |
| 190 |
$type, |
| 191 |
$fieldValues, |
| 192 |
$fieldMap, |
| 193 |
$auth_token |
| 194 |
) { |
| 195 |
$fieldData = []; |
| 196 |
$attributes = []; |
| 197 |
$model = new FormEntryLogModel(); |
| 198 |
$fieldData['attributes'] = (object) $attributes; |
| 199 |
|
| 200 |
$recordApiResponse = null; |
| 201 |
if ($this->_entryID) { |
| 202 |
$result = $model->entryLogCheck($this->_entryID, $this->_integrationID); |
| 203 |
if (!count($result) || isset($result->errors['result_empty'])) { |
| 204 |
$finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap); |
| 205 |
$recordApiResponse = $this->addSubscriber($auth_token, $groupId, $type, $finalData); |
| 206 |
|
| 207 |
if (empty($recordApiResponse)) { |
| 208 |
$recordApiResponse = ['success' => true, 'id' => $fieldData['email']]; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
if (isset($recordApiResponse->data->id) || isset($recordApiResponse->id)) { |
| 214 |
$res = ['success'=>true, 'message'=>isset($recordApiResponse->update) ? 'Subscriber has been updated successfully' : 'Subscriber has been created successfully', 'code'=>200]; |
| 215 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' =>isset($recordApiResponse->update) ? 'update' : 'insert'], 'success', $res); |
| 216 |
} else { |
| 217 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => isset($recordApiResponse->update) ? 'update' : 'insert'], 'error', $recordApiResponse); |
| 218 |
} |
| 219 |
return $recordApiResponse; |
| 220 |
} |
| 221 |
} |
| 222 |
|