| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Elastic Email Integration |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace BitCode\BitForm\Core\Integration\ElasticEmail; |
| 8 |
|
| 9 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 10 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 11 |
use WP_Error; |
| 12 |
|
| 13 |
/** |
| 14 |
* Provide functionality for ZohoCrm integration |
| 15 |
*/ |
| 16 |
class ElasticEmailHandler |
| 17 |
{ |
| 18 |
public static function registerAjax() |
| 19 |
{ |
| 20 |
add_action('wp_ajax_bitforms_elasticemail_authorize', [__CLASS__, 'elasticEmailAuthorize']); |
| 21 |
add_action('wp_ajax_bitforms_get_all_lists', [__CLASS__, 'getAllLists']); |
| 22 |
} |
| 23 |
|
| 24 |
public static function elasticEmailAuthorize() |
| 25 |
{ |
| 26 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 27 |
$inputJSON = file_get_contents('php://input'); |
| 28 |
$requestsParams = json_decode($inputJSON); |
| 29 |
|
| 30 |
if (empty($requestsParams->api_key)) { |
| 31 |
wp_send_json_error( |
| 32 |
__( |
| 33 |
'Requested parameter is empty', |
| 34 |
'bit-form' |
| 35 |
), |
| 36 |
400 |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
$apiEndpoint = 'https://api.elasticemail.com/v4/lists'; |
| 41 |
$apiKey = $requestsParams->api_key; |
| 42 |
$header = [ |
| 43 |
'X-ElasticEmail-ApiKey' => 22423423423423423, |
| 44 |
'Accept' => '*/*', |
| 45 |
]; |
| 46 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $header); |
| 47 |
if (is_wp_error($apiResponse) || isset($apiResponse->Error)) { |
| 48 |
wp_send_json_error( |
| 49 |
empty($apiResponse->code) ? 'Unknown' : $apiResponse->Error, |
| 50 |
400 |
| 51 |
); |
| 52 |
} |
| 53 |
wp_send_json_success(true); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public static function getAllLists() |
| 58 |
{ |
| 59 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 60 |
$response = null; |
| 61 |
$inputJSON = file_get_contents('php://input'); |
| 62 |
$requestsParams = json_decode($inputJSON); |
| 63 |
|
| 64 |
if (empty($requestsParams->apiKey)) { |
| 65 |
wp_send_json_error( |
| 66 |
__( |
| 67 |
'Requested parameter is empty', |
| 68 |
'bit-form' |
| 69 |
), |
| 70 |
400 |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
$apiEndpoint = 'https://api.elasticemail.com/v4/lists'; |
| 75 |
$apiKey = $requestsParams->apiKey; |
| 76 |
$header = [ |
| 77 |
'X-ElasticEmail-ApiKey' => $apiKey, |
| 78 |
'Accept' => '*/*', |
| 79 |
]; |
| 80 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $header); |
| 81 |
$data = []; |
| 82 |
foreach ($apiResponse as $list) { |
| 83 |
$data[] = (object) [ |
| 84 |
'listId' => $list->PublicListID, |
| 85 |
'listName' => $list->ListName |
| 86 |
]; |
| 87 |
} |
| 88 |
$response['lists'] = $data; |
| 89 |
wp_send_json_success($response, 200); |
| 90 |
} |
| 91 |
|
| 92 |
// wp_send_json_success(true); |
| 93 |
} |
| 94 |
|
| 95 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 96 |
{ |
| 97 |
$integrationDetails = $integrationData->integration_details; |
| 98 |
if (is_string($integrationDetails)) { |
| 99 |
$integrationDetails = json_decode($integrationDetails); |
| 100 |
} |
| 101 |
$integId = $integrationData->id; |
| 102 |
|
| 103 |
$api_key = $integrationDetails->api_key; |
| 104 |
$fieldMap = $integrationDetails->field_map; |
| 105 |
$actions = $integrationDetails->actions; |
| 106 |
if ( |
| 107 |
empty($api_key) |
| 108 |
|| empty($fieldMap) |
| 109 |
) { |
| 110 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Elastic Email api', 'bit-form')); |
| 111 |
} |
| 112 |
$recordApiHelper = new RecordApiHelper($api_key, $integId, $logID, $integrationDetails); |
| 113 |
$elasticEmailApiResponse = $recordApiHelper->execute( |
| 114 |
$integId, |
| 115 |
$fieldValues, |
| 116 |
$fieldMap, |
| 117 |
$integrationDetails |
| 118 |
// $actions |
| 119 |
); |
| 120 |
|
| 121 |
if (is_wp_error($elasticEmailApiResponse)) { |
| 122 |
return $elasticEmailApiResponse; |
| 123 |
} |
| 124 |
return $elasticEmailApiResponse; |
| 125 |
} |
| 126 |
} |
| 127 |
|