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