| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\Acumbamail; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 10 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 11 |
use BitCode\BitForm\GlobalHelper; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provide functionality for Acumbamail integration |
| 16 |
*/ |
| 17 |
class AcumbamailHandler |
| 18 |
{ |
| 19 |
private $integrationID; |
| 20 |
public static $baseUrl = 'https://acumbamail.com/api/1/'; |
| 21 |
|
| 22 |
private $formId; |
| 23 |
|
| 24 |
public function __construct($integrationID, $fromID) |
| 25 |
{ |
| 26 |
$this->integrationID = $integrationID; |
| 27 |
|
| 28 |
$this->formId = $fromID; |
| 29 |
} |
| 30 |
|
| 31 |
public static function registerAjax() |
| 32 |
{ |
| 33 |
add_action('wp_ajax_bitforms_acumbamail_authorization_and_fetch_subscriber_list', [__CLASS__, 'acumbamailAuthAndFetchSubscriberList']); |
| 34 |
add_action('wp_ajax_bitforms_acumbamail_fetch_all_list', [__CLASS__, 'fetchAllLists']); |
| 35 |
add_action('wp_ajax_bitforms_acumbamail_refresh_fields', [__CLASS__, 'acumbamailRefreshFields']); |
| 36 |
} |
| 37 |
|
| 38 |
public static function fetchAllLists() |
| 39 |
{ |
| 40 |
if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 41 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 42 |
} |
| 43 |
|
| 44 |
GlobalHelper::requirePostMethod(); |
| 45 |
|
| 46 |
try { |
| 47 |
$requestParams = GlobalHelper::formatRequestData(); |
| 48 |
} catch (\InvalidArgumentException $e) { |
| 49 |
wp_send_json_error($e->getMessage(), 400); |
| 50 |
} |
| 51 |
|
| 52 |
if (empty($requestParams->auth_token)) { |
| 53 |
wp_send_json_error( |
| 54 |
__( |
| 55 |
'Requested parameter is empty', |
| 56 |
'bit-form' |
| 57 |
), |
| 58 |
400 |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
$apiEndpoint = self::$baseUrl . 'getLists/'; |
| 63 |
$requestParams = [ |
| 64 |
'auth_token' => $requestParams->auth_token, |
| 65 |
]; |
| 66 |
|
| 67 |
$response = HttpHelper::post($apiEndpoint, $requestParams); |
| 68 |
|
| 69 |
if ('Unauthorized' !== $response) { |
| 70 |
wp_send_json_success($response, 200); |
| 71 |
} else { |
| 72 |
wp_send_json_error( |
| 73 |
'The token is invalid', |
| 74 |
400 |
| 75 |
); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public static function acumbamailAuthAndFetchSubscriberList() |
| 80 |
{ |
| 81 |
if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 82 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 83 |
} |
| 84 |
|
| 85 |
GlobalHelper::requirePostMethod(); |
| 86 |
try { |
| 87 |
$requestParams = GlobalHelper::formatRequestData(); |
| 88 |
} catch (\InvalidArgumentException $e) { |
| 89 |
wp_send_json_error($e->getMessage(), 400); |
| 90 |
} |
| 91 |
if (empty($requestParams->auth_token)) { |
| 92 |
wp_send_json_error( |
| 93 |
__( |
| 94 |
'Requested parameter is empty', |
| 95 |
'bit-form' |
| 96 |
), |
| 97 |
400 |
| 98 |
); |
| 99 |
} |
| 100 |
$apiEndpoint = self::$baseUrl . 'getLists/'; |
| 101 |
|
| 102 |
$queryParams = [ |
| 103 |
'auth_token' => $requestParams->auth_token, |
| 104 |
]; |
| 105 |
|
| 106 |
$response = HttpHelper::post($apiEndpoint, $queryParams, null); |
| 107 |
|
| 108 |
if ('Unauthorized' !== $response) { |
| 109 |
wp_send_json_success($response, 200); |
| 110 |
} else { |
| 111 |
wp_send_json_error( |
| 112 |
'The token is invalid', |
| 113 |
400 |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public static function acumbamailRefreshFields() |
| 119 |
{ |
| 120 |
if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 121 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 122 |
} |
| 123 |
|
| 124 |
GlobalHelper::requirePostMethod(); |
| 125 |
|
| 126 |
try { |
| 127 |
$refreshFieldsRequestParams = GlobalHelper::formatRequestData(); |
| 128 |
} catch (\InvalidArgumentException $e) { |
| 129 |
wp_send_json_error($e->getMessage(), 400); |
| 130 |
} |
| 131 |
|
| 132 |
if (empty($refreshFieldsRequestParams->auth_token) || empty($refreshFieldsRequestParams->list_id)) { |
| 133 |
wp_send_json_error( |
| 134 |
__( |
| 135 |
'Requested parameter is empty', |
| 136 |
'bit-form' |
| 137 |
), |
| 138 |
400 |
| 139 |
); |
| 140 |
} |
| 141 |
$apiEndpoint = self::$baseUrl . 'getFields/'; |
| 142 |
|
| 143 |
$requestParams = [ |
| 144 |
'auth_token' => $refreshFieldsRequestParams->auth_token, |
| 145 |
'list_id' => $refreshFieldsRequestParams->list_id, |
| 146 |
]; |
| 147 |
|
| 148 |
$response = HttpHelper::post($apiEndpoint, $requestParams); |
| 149 |
|
| 150 |
$formattedResponse = []; |
| 151 |
foreach ($response as $key => $value) { |
| 152 |
if ('email' === $key) { |
| 153 |
$formattedResponse[$key] = [ |
| 154 |
$key => $value, |
| 155 |
'required' => true, |
| 156 |
]; |
| 157 |
} else { |
| 158 |
$formattedResponse[$key] = [ |
| 159 |
$key => $value, |
| 160 |
'required' => false, |
| 161 |
]; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if ('Unauthorized' !== $response) { |
| 166 |
wp_send_json_success($formattedResponse, 200); |
| 167 |
} else { |
| 168 |
wp_send_json_error( |
| 169 |
'The token is invalid', |
| 170 |
400 |
| 171 |
); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 176 |
{ |
| 177 |
$integrationDetails = json_decode($integrationData->integration_details); |
| 178 |
$auth_token = $integrationDetails->auth_token; |
| 179 |
$listId = $integrationDetails->listId; |
| 180 |
$mainAction = $integrationDetails->mainAction; |
| 181 |
$fieldMap = $integrationDetails->field_map; |
| 182 |
$defaultDataConf = $integrationDetails->default; |
| 183 |
|
| 184 |
if ( |
| 185 |
empty($listId) |
| 186 |
|| empty($fieldMap) |
| 187 |
|| empty($auth_token) |
| 188 |
) { |
| 189 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Acumbamail api', 'bit-form')); |
| 190 |
} |
| 191 |
$recordApiHelper = new RecordApiHelper($auth_token, $this->integrationID, $logID, $entryID); |
| 192 |
$acumbamailApiResponse = $recordApiHelper->execute( |
| 193 |
$listId, |
| 194 |
$mainAction, |
| 195 |
$defaultDataConf, |
| 196 |
$fieldValues, |
| 197 |
$fieldMap, |
| 198 |
$auth_token, |
| 199 |
$this->formId |
| 200 |
); |
| 201 |
|
| 202 |
if (is_wp_error($acumbamailApiResponse)) { |
| 203 |
return $acumbamailApiResponse; |
| 204 |
} |
| 205 |
return $acumbamailApiResponse; |
| 206 |
} |
| 207 |
} |
| 208 |
|