| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Rapidmail Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\Rapidmail; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
class RapidmailHandler |
| 15 |
{ |
| 16 |
private $_integrationID; |
| 17 |
public static $apiBaseUri = 'https://apiv3.emailsys.net/v1'; |
| 18 |
protected $_defaultHeader; |
| 19 |
|
| 20 |
public function __construct($integrationID) |
| 21 |
{ |
| 22 |
$this->_integrationID = $integrationID; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Helps to register ajax function's with wp |
| 27 |
* |
| 28 |
* @return null |
| 29 |
*/ |
| 30 |
public static function registerAjax() |
| 31 |
{ |
| 32 |
add_action('wp_ajax_bitforms_rapidmail_authorization', [__CLASS__, 'checkAuthorization']); |
| 33 |
add_action('wp_ajax_bitforms_rapidmail_get_all_recipients', [__CLASS__, 'getAllRecipients']); |
| 34 |
add_action('wp_ajax_bitforms_rapidmail_get_all_fields', [__CLASS__, 'getAllFields']); |
| 35 |
} |
| 36 |
|
| 37 |
public static function checkAuthorization() |
| 38 |
{ |
| 39 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 40 |
$inputJSON = file_get_contents('php://input'); |
| 41 |
$tokenRequestParams = json_decode($inputJSON); |
| 42 |
|
| 43 |
if ( |
| 44 |
empty($tokenRequestParams->username) |
| 45 |
|| empty($tokenRequestParams->password) |
| 46 |
) { |
| 47 |
wp_send_json_error( |
| 48 |
__( |
| 49 |
'Requested parameter is empty', |
| 50 |
'bit-form' |
| 51 |
), |
| 52 |
400 |
| 53 |
); |
| 54 |
} |
| 55 |
$header = [ |
| 56 |
'Authorization' => 'Basic ' . base64_encode("$tokenRequestParams->username:$tokenRequestParams->password"), |
| 57 |
'Accept' => '*/*', |
| 58 |
'verify' => false |
| 59 |
]; |
| 60 |
$apiEndpoint = self::$apiBaseUri . '/apiusers'; |
| 61 |
|
| 62 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $header); |
| 63 |
if (!(property_exists($apiResponse, '_embedded') && property_exists($apiResponse->_embedded, 'apiusers'))) { |
| 64 |
wp_send_json_error( |
| 65 |
// empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, |
| 66 |
'Unauthorize', |
| 67 |
400 |
| 68 |
); |
| 69 |
} else { |
| 70 |
$apiResponse->generates_on = \time(); |
| 71 |
wp_send_json_success($apiResponse, 200); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Process request for getting recipientlists from rapidmail |
| 78 |
* |
| 79 |
* @param $queryParams Mandatory params to get recipients |
| 80 |
* |
| 81 |
* @return JSON rapidmailmail recipientlists data |
| 82 |
*/ |
| 83 |
public static function getAllRecipients() |
| 84 |
{ |
| 85 |
$response = null; |
| 86 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 87 |
$inputJSON = file_get_contents('php://input'); |
| 88 |
$queryParams = json_decode($inputJSON); |
| 89 |
|
| 90 |
if ( |
| 91 |
empty($queryParams->username) |
| 92 |
|| empty($queryParams->password) |
| 93 |
) { |
| 94 |
wp_send_json_error( |
| 95 |
__( |
| 96 |
'Requested parameter is empty', |
| 97 |
'bit-form' |
| 98 |
), |
| 99 |
400 |
| 100 |
); |
| 101 |
} |
| 102 |
$header = [ |
| 103 |
'Authorization' => 'Basic ' . base64_encode("$queryParams->username:$queryParams->password"), |
| 104 |
'Accept' => '*/*', |
| 105 |
'verify' => false |
| 106 |
]; |
| 107 |
$recipientApiEndpoint = self::$apiBaseUri . '/recipientlists'; |
| 108 |
$apiResponse = HttpHelper::get($recipientApiEndpoint, null, $header); |
| 109 |
$tempRecipient = $apiResponse->_embedded->recipientlists; |
| 110 |
$data = []; |
| 111 |
|
| 112 |
foreach ($tempRecipient as $list) { |
| 113 |
$data[] = (object) [ |
| 114 |
'id' => $list->id, |
| 115 |
'name' => $list->name |
| 116 |
]; |
| 117 |
} |
| 118 |
$response['recipientlists'] = $data; |
| 119 |
wp_send_json_success($response, 200); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public static function getAllFields($queryParams) |
| 124 |
{ |
| 125 |
if ( |
| 126 |
empty($queryParams->username) |
| 127 |
|| empty($queryParams->password) |
| 128 |
) { |
| 129 |
wp_send_json_error( |
| 130 |
__( |
| 131 |
'Requested parameter is empty', |
| 132 |
'bit-form' |
| 133 |
), |
| 134 |
400 |
| 135 |
); |
| 136 |
} |
| 137 |
$header = [ |
| 138 |
'Authorization' => 'Basic ' . base64_encode("$queryParams->username:$queryParams->password"), |
| 139 |
'Accept' => '*/*', |
| 140 |
'verify' => false |
| 141 |
]; |
| 142 |
$recipientApiEndpoint = self::$apiBaseUri . '/recipientlists'; |
| 143 |
$apiResponse = HttpHelper::get($recipientApiEndpoint, null, $header); |
| 144 |
$tempRecipient = $apiResponse->_embedded->recipientlists; |
| 145 |
$data = []; |
| 146 |
|
| 147 |
foreach ($tempRecipient as $list) { |
| 148 |
$data[] = (object) [ |
| 149 |
'id' => $list->id, |
| 150 |
'name' => $list->name |
| 151 |
]; |
| 152 |
} |
| 153 |
$response['recipientlists'] = $data; |
| 154 |
wp_send_json_success($response, 200); |
| 155 |
} |
| 156 |
|
| 157 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 158 |
{ |
| 159 |
$integrationDetails = $integrationData->integration_details; |
| 160 |
if (is_string($integrationDetails)) { |
| 161 |
$integrationDetails = json_decode($integrationDetails); |
| 162 |
} |
| 163 |
$fieldMap = $integrationDetails->field_map; |
| 164 |
$defaultDataConf = $integrationDetails->default; |
| 165 |
$username = $integrationDetails->username; |
| 166 |
$password = $integrationDetails->password; |
| 167 |
$recipientLists = $defaultDataConf->recipientlists; |
| 168 |
|
| 169 |
if ( |
| 170 |
empty($username) |
| 171 |
|| empty($password) |
| 172 |
|| empty($fieldMap) |
| 173 |
) { |
| 174 |
$error = new WP_Error('REQ_FIELD_EMPTY', __('username, password, fields are required for rapidmail api', 'bit-form')); |
| 175 |
return $error; |
| 176 |
} |
| 177 |
if (empty($recipientLists)) { |
| 178 |
$error = new WP_Error('REQ_FIELD_EMPTY', __('Recipient List are required for rapidmail api', 'bit-form')); |
| 179 |
return $error; |
| 180 |
} |
| 181 |
$actions = $integrationDetails->actions; |
| 182 |
|
| 183 |
$recordApiHelper = new RecordApiHelper($integrationDetails, $username, $password, $logID); |
| 184 |
$rapidmailResponse = $recordApiHelper->executeRecordApi( |
| 185 |
$this->_integrationID, |
| 186 |
$defaultDataConf, |
| 187 |
$recipientLists, |
| 188 |
$fieldValues, |
| 189 |
$fieldMap, |
| 190 |
$actions |
| 191 |
); |
| 192 |
if (is_wp_error($rapidmailResponse)) { |
| 193 |
return $rapidmailResponse; |
| 194 |
} |
| 195 |
return $rapidmailResponse; |
| 196 |
} |
| 197 |
} |
| 198 |
|