| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoSheet Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\SendinBlue; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provide functionality for ZohoCrm integration |
| 16 |
*/ |
| 17 |
class SendinBlueHandler |
| 18 |
{ |
| 19 |
private $_integrationID; |
| 20 |
public static $api_endpoint = 'https://api.sendinblue.com/v3'; |
| 21 |
|
| 22 |
public function __construct($integrationID, $fromID) |
| 23 |
{ |
| 24 |
$this->_integrationID = $integrationID; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Helps to register ajax function's with wp |
| 29 |
* |
| 30 |
* @return null |
| 31 |
*/ |
| 32 |
public static function registerAjax() |
| 33 |
{ |
| 34 |
add_action('wp_ajax_bitforms_sblue_authorize', [__CLASS__, 'sendinBlueAuthorize']); |
| 35 |
add_action('wp_ajax_bitforms_sblue_refresh_lists', [__CLASS__, 'refreshlists']); |
| 36 |
add_action('wp_ajax_bitforms_sblue_headers', [__CLASS__, 'sendinblueHeaders']); |
| 37 |
add_action('wp_ajax_bitforms_sblue_refresh_template', [__CLASS__, 'refreshTemplate']); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Process ajax request for generate_token |
| 42 |
* |
| 43 |
* @return JSON zoho crm api response and status |
| 44 |
*/ |
| 45 |
public static function sendinBlueAuthorize() |
| 46 |
{ |
| 47 |
$authorizationHeader = null; |
| 48 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 49 |
$inputJSON = file_get_contents('php://input'); |
| 50 |
$requestsParams = json_decode($inputJSON); |
| 51 |
if ( |
| 52 |
empty($requestsParams->api_key) |
| 53 |
) { |
| 54 |
wp_send_json_error( |
| 55 |
__( |
| 56 |
'Requested parameter is empty', |
| 57 |
'bit-form' |
| 58 |
), |
| 59 |
400 |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
$apiEndpoint = self::$api_endpoint . '/account'; |
| 64 |
$authorizationHeader['Accept'] = 'application/json'; |
| 65 |
$authorizationHeader['api-key'] = $requestsParams->api_key; |
| 66 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 67 |
|
| 68 |
if (is_wp_error($apiResponse) || (!empty($apiResponse->code) && 'unauthorized' === $apiResponse->code)) { |
| 69 |
wp_send_json_error( |
| 70 |
empty($apiResponse->code) ? 'Unknown' : $apiResponse->message, |
| 71 |
400 |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
wp_send_json_success(true); |
| 76 |
} else { |
| 77 |
wp_send_json_error( |
| 78 |
__( |
| 79 |
'Token expired', |
| 80 |
'bit-form' |
| 81 |
), |
| 82 |
401 |
| 83 |
); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Process ajax request for refresh crm modules |
| 89 |
* |
| 90 |
* @return JSON crm module data |
| 91 |
*/ |
| 92 |
public static function refreshlists() |
| 93 |
{ |
| 94 |
$authorizationHeader = null; |
| 95 |
$response = null; |
| 96 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 97 |
$inputJSON = file_get_contents('php://input'); |
| 98 |
$requestsParams = json_decode($inputJSON); |
| 99 |
if ( |
| 100 |
empty($requestsParams->api_key) |
| 101 |
) { |
| 102 |
wp_send_json_error( |
| 103 |
__( |
| 104 |
'Requested parameter is empty', |
| 105 |
'bit-form' |
| 106 |
), |
| 107 |
400 |
| 108 |
); |
| 109 |
} |
| 110 |
$apiEndpoint = self::$api_endpoint . '/contacts/lists'; |
| 111 |
$authorizationHeader['Accept'] = 'application/json'; |
| 112 |
$authorizationHeader['api-key'] = $requestsParams->api_key; |
| 113 |
$sblueResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 114 |
|
| 115 |
$allList = []; |
| 116 |
if (!is_wp_error($sblueResponse) && empty($sblueResponse->code)) { |
| 117 |
$sblueList = $sblueResponse->lists; |
| 118 |
|
| 119 |
foreach ($sblueList as $list) { |
| 120 |
$allList[$list->name] = (object) [ |
| 121 |
'id' => $list->id, |
| 122 |
'name' => $list->name |
| 123 |
]; |
| 124 |
} |
| 125 |
uksort($allList, 'strnatcasecmp'); |
| 126 |
|
| 127 |
$response['sblueList'] = $allList; |
| 128 |
} else { |
| 129 |
wp_send_json_error( |
| 130 |
$sblueResponse->message, |
| 131 |
400 |
| 132 |
); |
| 133 |
} |
| 134 |
wp_send_json_success($response, 200); |
| 135 |
} else { |
| 136 |
wp_send_json_error( |
| 137 |
__( |
| 138 |
'Token expired', |
| 139 |
'bit-form' |
| 140 |
), |
| 141 |
401 |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
public static function refreshTemplate() |
| 147 |
{ |
| 148 |
$authorizationHeader = null; |
| 149 |
$response = null; |
| 150 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 151 |
$inputJSON = file_get_contents('php://input'); |
| 152 |
$requestsParams = json_decode($inputJSON); |
| 153 |
if ( |
| 154 |
empty($requestsParams->api_key) |
| 155 |
) { |
| 156 |
wp_send_json_error( |
| 157 |
__( |
| 158 |
'Requested parameter is empty', |
| 159 |
'bit-form' |
| 160 |
), |
| 161 |
400 |
| 162 |
); |
| 163 |
} |
| 164 |
$apiEndpoint = self::$api_endpoint . '/smtp/templates'; |
| 165 |
$authorizationHeader['Accept'] = 'application/json'; |
| 166 |
$authorizationHeader['api-key'] = $requestsParams->api_key; |
| 167 |
$sblueResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 168 |
|
| 169 |
$allList = []; |
| 170 |
if (!is_wp_error($sblueResponse) && $sblueResponse->templates) { |
| 171 |
$sblueTemplates = $sblueResponse->templates; |
| 172 |
|
| 173 |
foreach ($sblueTemplates as $list) { |
| 174 |
$allList[$list->name] = (object) [ |
| 175 |
'id' => $list->id, |
| 176 |
'name' => ucfirst($list->name) |
| 177 |
]; |
| 178 |
} |
| 179 |
|
| 180 |
uksort($allList, 'strnatcasecmp'); |
| 181 |
|
| 182 |
$response['sblueTemplates'] = $allList; |
| 183 |
} else { |
| 184 |
wp_send_json_error( |
| 185 |
$sblueResponse->message, |
| 186 |
400 |
| 187 |
); |
| 188 |
} |
| 189 |
wp_send_json_success($response, 200); |
| 190 |
} else { |
| 191 |
wp_send_json_error( |
| 192 |
__( |
| 193 |
'Token expired', |
| 194 |
'bit-form' |
| 195 |
), |
| 196 |
401 |
| 197 |
); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
public static function sendinblueHeaders() |
| 202 |
{ |
| 203 |
$authorizationHeader = null; |
| 204 |
$response = null; |
| 205 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 206 |
$inputJSON = file_get_contents('php://input'); |
| 207 |
$queryParams = json_decode($inputJSON); |
| 208 |
if ( |
| 209 |
empty($queryParams->api_key) |
| 210 |
) { |
| 211 |
wp_send_json_error( |
| 212 |
__( |
| 213 |
'Requested parameter is empty', |
| 214 |
'bit-form' |
| 215 |
), |
| 216 |
400 |
| 217 |
); |
| 218 |
} |
| 219 |
$apiEndpoint = self::$api_endpoint . '/contacts/attributes'; |
| 220 |
$authorizationHeader['Accept'] = 'application/json'; |
| 221 |
$authorizationHeader['api-key'] = $queryParams->api_key; |
| 222 |
$sblueResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 223 |
$fields = []; |
| 224 |
if (!is_wp_error($sblueResponse)) { |
| 225 |
$allFields = $sblueResponse->attributes; |
| 226 |
// wp_send_json_success($allFields); |
| 227 |
foreach ($allFields as $field) { |
| 228 |
if (!empty($field->type) && 'float' !== $field->type) { |
| 229 |
$fields[$field->name] = (object) [ |
| 230 |
'fieldId' => $field->name, |
| 231 |
'fieldName' => $field->name |
| 232 |
]; |
| 233 |
} |
| 234 |
} |
| 235 |
$fields['Email'] = (object) ['fieldId' => 'email', 'fieldName' => 'Email', 'required' => true]; |
| 236 |
$response['sendinBlueField'] = $fields; |
| 237 |
wp_send_json_success($response); |
| 238 |
} |
| 239 |
} else { |
| 240 |
wp_send_json_error( |
| 241 |
__( |
| 242 |
'Token expired', |
| 243 |
'bit-form' |
| 244 |
), |
| 245 |
401 |
| 246 |
); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 251 |
{ |
| 252 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 253 |
|
| 254 |
$api_key = $integrationDetails->api_key; |
| 255 |
$lists = $integrationDetails->lists; |
| 256 |
$fieldMap = $integrationDetails->field_map; |
| 257 |
$actions = $integrationDetails->actions; |
| 258 |
$defaultDataConf = $integrationDetails->default; |
| 259 |
|
| 260 |
if ( |
| 261 |
empty($api_key) |
| 262 |
|| empty($lists) |
| 263 |
|| empty($fieldMap) |
| 264 |
|| empty($defaultDataConf) |
| 265 |
) { |
| 266 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Sendinblue api', 'bit-form')); |
| 267 |
} |
| 268 |
$recordApiHelper = new RecordApiHelper($api_key, $this->_integrationID, $logID, $entryID); |
| 269 |
$sendinBlueApiResponse = $recordApiHelper->executeRecordApi( |
| 270 |
$lists, |
| 271 |
$defaultDataConf, |
| 272 |
$fieldValues, |
| 273 |
$fieldMap, |
| 274 |
$actions |
| 275 |
); |
| 276 |
|
| 277 |
if (is_wp_error($sendinBlueApiResponse)) { |
| 278 |
return $sendinBlueApiResponse; |
| 279 |
} |
| 280 |
return $sendinBlueApiResponse; |
| 281 |
} |
| 282 |
} |
| 283 |
|