| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Active Campaign Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ActiveCampaign; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 15 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 16 |
use BitCode\BitForm\GlobalHelper; |
| 17 |
use WP_Error; |
| 18 |
|
| 19 |
/** |
| 20 |
* Provide functionality for ZohoCrm integration |
| 21 |
*/ |
| 22 |
class ActiveCampaignHandler |
| 23 |
{ |
| 24 |
private $_formID; |
| 25 |
private $_integrationID; |
| 26 |
|
| 27 |
public function __construct($integrationID, $fromID) |
| 28 |
{ |
| 29 |
$this->_formID = $fromID; |
| 30 |
$this->_integrationID = $integrationID; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Helps to register ajax function's with wp |
| 35 |
* |
| 36 |
* @return null |
| 37 |
*/ |
| 38 |
public static function registerAjax() |
| 39 |
{ |
| 40 |
add_action('wp_ajax_bitforms_aCampaign_authorize', [__CLASS__, 'activeCampaignAuthorize']); |
| 41 |
add_action('wp_ajax_bitforms_aCampaign_headers', [__CLASS__, 'activeCampaignHeaders']); |
| 42 |
add_action('wp_ajax_bitforms_aCampaign_lists', [__CLASS__, 'activeCampaignLists']); |
| 43 |
add_action('wp_ajax_bitforms_aCampaign_tags', [__CLASS__, 'activeCampaignTags']); |
| 44 |
} |
| 45 |
|
| 46 |
public static function _apiEndpoint($api_url, $method) |
| 47 |
{ |
| 48 |
return "{$api_url}/api/3/{$method}/"; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Process ajax request |
| 53 |
* |
| 54 |
* @return string Active Campaign api response and status |
| 55 |
*/ |
| 56 |
public static function activeCampaignAuthorize() |
| 57 |
{ |
| 58 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 59 |
GlobalHelper::requirePostMethod(); |
| 60 |
|
| 61 |
try { |
| 62 |
$requestsParams = GlobalHelper::formatRequestData(); |
| 63 |
} catch (\InvalidArgumentException $e) { |
| 64 |
wp_send_json_error($e->getMessage(), 400); |
| 65 |
} |
| 66 |
|
| 67 |
if ( |
| 68 |
empty($requestsParams->api_key) |
| 69 |
|| empty($requestsParams->api_url) |
| 70 |
) { |
| 71 |
wp_send_json_error( |
| 72 |
__( |
| 73 |
'Requested parameter is empty', |
| 74 |
'bit-form' |
| 75 |
), |
| 76 |
400 |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
$apiEndpoint = self::_apiEndpoint($requestsParams->api_url, 'accounts'); |
| 81 |
$authorizationHeader = []; |
| 82 |
$authorizationHeader['Api-Token'] = $requestsParams->api_key; |
| 83 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 84 |
|
| 85 |
if (is_wp_error($apiResponse) || empty($apiResponse)) { |
| 86 |
wp_send_json_error( |
| 87 |
empty($apiResponse) ? 'Unknown' : $apiResponse, |
| 88 |
400 |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
wp_send_json_success(true); |
| 93 |
} else { |
| 94 |
wp_send_json_error( |
| 95 |
__( |
| 96 |
'Token expired', |
| 97 |
'bit-form' |
| 98 |
), |
| 99 |
401 |
| 100 |
); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Process ajax request for refresh lists |
| 106 |
* |
| 107 |
* @return JSON active campaign list data |
| 108 |
*/ |
| 109 |
public static function activeCampaignLists() |
| 110 |
{ |
| 111 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 112 |
GlobalHelper::requirePostMethod(); |
| 113 |
try { |
| 114 |
$queryParams = GlobalHelper::formatRequestData(); |
| 115 |
} catch (\InvalidArgumentException $e) { |
| 116 |
wp_send_json_error($e->getMessage(), 400); |
| 117 |
} |
| 118 |
if ( |
| 119 |
empty($queryParams->api_key) |
| 120 |
|| empty($queryParams->api_url) |
| 121 |
) { |
| 122 |
wp_send_json_error( |
| 123 |
__( |
| 124 |
'Requested parameter is empty', |
| 125 |
'bit-form' |
| 126 |
), |
| 127 |
400 |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
$apiEndpoint = self::_apiEndpoint($queryParams->api_url, 'lists'); |
| 132 |
$authorizationHeader = []; |
| 133 |
$authorizationHeader['Api-Token'] = $queryParams->api_key; |
| 134 |
$aCampaignResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 135 |
|
| 136 |
$lists = []; |
| 137 |
if (!is_wp_error($aCampaignResponse)) { |
| 138 |
$allLists = $aCampaignResponse->lists; |
| 139 |
|
| 140 |
foreach ($allLists as $list) { |
| 141 |
$lists[$list->name] = (object) [ |
| 142 |
'listId' => $list->id, |
| 143 |
'listName' => $list->name, |
| 144 |
]; |
| 145 |
} |
| 146 |
$response = []; |
| 147 |
$response['activeCampaignLists'] = $lists; |
| 148 |
wp_send_json_success($response); |
| 149 |
} |
| 150 |
} else { |
| 151 |
wp_send_json_error( |
| 152 |
__( |
| 153 |
'Token expired', |
| 154 |
'bit-form' |
| 155 |
), |
| 156 |
401 |
| 157 |
); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Process ajax request for refresh Tags |
| 163 |
* |
| 164 |
* @return JSON active campaign tags data |
| 165 |
*/ |
| 166 |
public static function activeCampaignTags() |
| 167 |
{ |
| 168 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 169 |
GlobalHelper::requirePostMethod(); |
| 170 |
|
| 171 |
try { |
| 172 |
$queryParams = GlobalHelper::formatRequestData(); |
| 173 |
} catch (\InvalidArgumentException $e) { |
| 174 |
wp_send_json_error($e->getMessage(), 400); |
| 175 |
} |
| 176 |
|
| 177 |
if ( |
| 178 |
empty($queryParams->api_key) |
| 179 |
|| empty($queryParams->api_url) |
| 180 |
) { |
| 181 |
wp_send_json_error( |
| 182 |
__( |
| 183 |
'Requested parameter is empty', |
| 184 |
'bit-form' |
| 185 |
), |
| 186 |
400 |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
$apiEndpoint = self::_apiEndpoint($queryParams->api_url, 'tags?limit=500'); |
| 191 |
$authorizationHeader = []; |
| 192 |
$authorizationHeader['Api-Token'] = $queryParams->api_key; |
| 193 |
$aCampaignResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 194 |
|
| 195 |
$tags = []; |
| 196 |
if (!is_wp_error($aCampaignResponse)) { |
| 197 |
$allTags = $aCampaignResponse->tags; |
| 198 |
|
| 199 |
foreach ($allTags as $tag) { |
| 200 |
$tags[$tag->tag] = (object) [ |
| 201 |
'tagId' => $tag->id, |
| 202 |
'tagName' => $tag->tag, |
| 203 |
]; |
| 204 |
} |
| 205 |
$response = []; |
| 206 |
$response['activeCampaignTags'] = $tags; |
| 207 |
wp_send_json_success($response); |
| 208 |
} |
| 209 |
} else { |
| 210 |
wp_send_json_error( |
| 211 |
__( |
| 212 |
'Token expired', |
| 213 |
'bit-form' |
| 214 |
), |
| 215 |
401 |
| 216 |
); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Process ajax request for refresh crm modules |
| 222 |
* |
| 223 |
* @return JSON crm module data |
| 224 |
*/ |
| 225 |
public static function activeCampaignHeaders() |
| 226 |
{ |
| 227 |
$authorizationHeader = null; |
| 228 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 229 |
GlobalHelper::requirePostMethod(); |
| 230 |
|
| 231 |
try { |
| 232 |
$queryParams = GlobalHelper::formatRequestData(); |
| 233 |
} catch (\InvalidArgumentException $e) { |
| 234 |
wp_send_json_error($e->getMessage(), 400); |
| 235 |
} |
| 236 |
|
| 237 |
if ( |
| 238 |
empty($queryParams->api_key) |
| 239 |
|| empty($queryParams->api_url) |
| 240 |
) { |
| 241 |
wp_send_json_error( |
| 242 |
__( |
| 243 |
'Requested parameter is empty', |
| 244 |
'bit-form' |
| 245 |
), |
| 246 |
400 |
| 247 |
); |
| 248 |
} |
| 249 |
|
| 250 |
// $apiEndpoint = "{$queryParams->api_url}/api/3/fields"; |
| 251 |
$apiEndpoint = self::_apiEndpoint($queryParams->api_url, 'fields'); |
| 252 |
$$authorizationHeader = []; |
| 253 |
$authorizationHeader['Api-Token'] = $queryParams->api_key; |
| 254 |
$aCampaignResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 255 |
|
| 256 |
$fields = []; |
| 257 |
if (!is_wp_error($aCampaignResponse)) { |
| 258 |
$allFields = $aCampaignResponse->fields; |
| 259 |
foreach ($allFields as $field) { |
| 260 |
$fields[$field->title] = (object) [ |
| 261 |
'fieldId' => $field->id, |
| 262 |
'fieldName' => $field->title, |
| 263 |
'required' => '0' === $field->isrequired ? false : true |
| 264 |
]; |
| 265 |
} |
| 266 |
$fields['FirstName'] = (object) ['fieldId' => 'firstName', 'fieldName' => 'First Name', 'required' => false]; |
| 267 |
$fields['LastName'] = (object) ['fieldId' => 'lastName', 'fieldName' => 'Last Name', 'required' => false]; |
| 268 |
$fields['Email'] = (object) ['fieldId' => 'email', 'fieldName' => 'Email', 'required' => true]; |
| 269 |
$fields['Phone'] = (object) ['fieldId' => 'phone', 'fieldName' => 'Phone', 'required' => false]; |
| 270 |
$response = []; |
| 271 |
$response['activeCampaignField'] = $fields; |
| 272 |
wp_send_json_success($response); |
| 273 |
} |
| 274 |
} else { |
| 275 |
wp_send_json_error( |
| 276 |
__( |
| 277 |
'Token expired', |
| 278 |
'bit-form' |
| 279 |
), |
| 280 |
401 |
| 281 |
); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 286 |
{ |
| 287 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 288 |
$api_key = $integrationDetails->api_key; |
| 289 |
$api_url = $integrationDetails->api_url; |
| 290 |
$fieldMap = $integrationDetails->field_map; |
| 291 |
$actions = $integrationDetails->actions; |
| 292 |
$listId = $integrationDetails->listId; |
| 293 |
$tags = $integrationDetails->tagIds; |
| 294 |
|
| 295 |
if ( |
| 296 |
empty($api_key) |
| 297 |
|| empty($api_url) |
| 298 |
|| empty($fieldMap) |
| 299 |
) { |
| 300 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Sendinblue api', 'bit-form')); |
| 301 |
} |
| 302 |
$recordApiHelper = new RecordApiHelper($api_key, $api_url, $this->_integrationID, $logID, $entryID); |
| 303 |
$activeCampaignApiResponse = $recordApiHelper->executeRecordApi( |
| 304 |
$fieldValues, |
| 305 |
$fieldMap, |
| 306 |
$actions, |
| 307 |
$listId, |
| 308 |
$tags, |
| 309 |
$this->_formID |
| 310 |
); |
| 311 |
|
| 312 |
if (is_wp_error($activeCampaignApiResponse)) { |
| 313 |
return $activeCampaignApiResponse; |
| 314 |
} |
| 315 |
return $activeCampaignApiResponse; |
| 316 |
} |
| 317 |
} |
| 318 |
|