← All changes
|
includes/Core/Integration/ElasticEmail/ElasticEmailHandler.php
+48
-16
2.0
→
3.3.1
View file →
| @@ -5,26 +5,49 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | namespace BitCode\BitForm\Core\Integration\ElasticEmail; |
| 8 | 8 | |
| 9 | +if (!defined('ABSPATH')) { | |
| 10 | + exit; | |
| 11 | +} | |
| 12 | + | |
| 9 | 13 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 10 | 14 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 15 | +use BitCode\BitForm\GlobalHelper; | |
| 11 | 16 | use WP_Error; |
| 12 | 17 | |
| 13 | 18 | /** |
| 14 | 19 | * Provide functionality for ZohoCrm integration |
| 15 | 20 | */ |
| 16 | -class ElasticEmailHandler { | |
| 17 | - public static function registerAjax() { | |
| 21 | +class ElasticEmailHandler | |
| 22 | +{ | |
| 23 | + private $formID; | |
| 24 | + | |
| 25 | + private $integrationID; | |
| 26 | + | |
| 27 | + public function __construct($integrationID, $fromID) | |
| 28 | + { | |
| 29 | + $this->formID = $fromID; | |
| 30 | + $this->integrationID = $integrationID; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public static function registerAjax() | |
| 34 | + { | |
| 18 | 35 | add_action('wp_ajax_bitforms_elasticemail_authorize', [__CLASS__, 'elasticEmailAuthorize']); |
| 19 | 36 | add_action('wp_ajax_bitforms_get_all_lists', [__CLASS__, 'getAllLists']); |
| 20 | 37 | } |
| 21 | 38 | |
| 22 | - public static function elasticEmailAuthorize() { | |
| 23 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 24 | - $inputJSON = file_get_contents('php://input'); | |
| 25 | - $requestsParams = json_decode($inputJSON); | |
| 39 | + public static function elasticEmailAuthorize() | |
| 40 | + { | |
| 41 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 42 | + GlobalHelper::requirePostMethod(); | |
| 26 | 43 | |
| 44 | + try { | |
| 45 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 46 | + } catch (\InvalidArgumentException $e) { | |
| 47 | + wp_send_json_error($e->getMessage(), 400); | |
| 48 | + } | |
| 49 | + | |
| 27 | 50 | if (empty($requestsParams->api_key)) { |
| 28 | 51 | wp_send_json_error( |
| 29 | 52 | __( |
| 30 | 53 | 'Requested parameter is empty', |
| @@ -36,15 +59,16 @@ | ||
| 36 | 59 | |
| 37 | 60 | $apiEndpoint = 'https://api.elasticemail.com/v4/lists'; |
| 38 | 61 | $apiKey = $requestsParams->api_key; |
| 39 | 62 | $header = [ |
| 40 | - 'X-ElasticEmail-ApiKey' => 22423423423423423, | |
| 63 | + 'X-ElasticEmail-ApiKey' => $apiKey, | |
| 41 | 64 | 'Accept' => '*/*', |
| 42 | 65 | ]; |
| 43 | 66 | $apiResponse = HttpHelper::get($apiEndpoint, null, $header); |
| 67 | + | |
| 44 | 68 | if (is_wp_error($apiResponse) || isset($apiResponse->Error)) { |
| 45 | 69 | wp_send_json_error( |
| 46 | - empty($apiResponse->code) ? 'Unknown' : $apiResponse->Error, | |
| 70 | + $apiResponse->Error, | |
| 47 | 71 | 400 |
| 48 | 72 | ); |
| 49 | 73 | } |
| 50 | 74 | wp_send_json_success(true); |
| @@ -50,14 +74,21 @@ | ||
| 50 | 74 | wp_send_json_success(true); |
| 51 | 75 | } |
| 52 | 76 | } |
| 53 | 77 | |
| 54 | - public static function getAllLists() { | |
| 55 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 78 | + public static function getAllLists() | |
| 79 | + { | |
| 80 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 56 | 81 | $response = null; |
| 57 | - $inputJSON = file_get_contents('php://input'); | |
| 58 | - $requestsParams = json_decode($inputJSON); | |
| 59 | 82 | |
| 83 | + GlobalHelper::requirePostMethod(); | |
| 84 | + | |
| 85 | + try { | |
| 86 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 87 | + } catch (\InvalidArgumentException $e) { | |
| 88 | + wp_send_json_error($e->getMessage(), 400); | |
| 89 | + } | |
| 90 | + | |
| 60 | 91 | if (empty($requestsParams->apiKey)) { |
| 61 | 92 | wp_send_json_error( |
| 62 | 93 | __( |
| 63 | 94 | 'Requested parameter is empty', |
| @@ -87,9 +118,10 @@ | ||
| 87 | 118 | |
| 88 | 119 | // wp_send_json_success(true); |
| 89 | 120 | } |
| 90 | 121 | |
| 91 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { | |
| 122 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 123 | + { | |
| 92 | 124 | $integrationDetails = $integrationData->integration_details; |
| 93 | 125 | if (is_string($integrationDetails)) { |
| 94 | 126 | $integrationDetails = json_decode($integrationDetails); |
| 95 | 127 | } |
| @@ -103,15 +135,15 @@ | ||
| 103 | 135 | || empty($fieldMap) |
| 104 | 136 | ) { |
| 105 | 137 | return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Elastic Email api', 'bit-form')); |
| 106 | 138 | } |
| 107 | - $recordApiHelper = new RecordApiHelper($api_key, $integId, $logID, $integrationDetails); | |
| 139 | + $recordApiHelper = new RecordApiHelper($api_key, $integId, $logID, $integrationDetails, $entryID); | |
| 108 | 140 | $elasticEmailApiResponse = $recordApiHelper->execute( |
| 109 | 141 | $integId, |
| 110 | 142 | $fieldValues, |
| 111 | 143 | $fieldMap, |
| 112 | - $integrationDetails | |
| 113 | - // $actions | |
| 144 | + $integrationDetails, | |
| 145 | + $this->formID, | |
| 114 | 146 | ); |
| 115 | 147 | |
| 116 | 148 | if (is_wp_error($elasticEmailApiResponse)) { |
| 117 | 149 | return $elasticEmailApiResponse; |