← All changes
|
includes/Core/Integration/MailChimp/MailChimpHandler.php
+64
-24
2.0
→
3.3.1
View file →
| @@ -6,26 +6,38 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\MailChimp; |
| 9 | 9 | |
| 10 | +if (!defined('ABSPATH')) { | |
| 11 | + exit; | |
| 12 | +} | |
| 13 | + | |
| 10 | 14 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 | 15 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 16 | +use BitCode\BitForm\GlobalHelper; | |
| 12 | 17 | use WP_Error; |
| 13 | 18 | |
| 14 | 19 | /** |
| 15 | 20 | * Provide functionality for MailChimp integration |
| 16 | 21 | */ |
| 17 | -class MailChimpHandler { | |
| 22 | +class MailChimpHandler | |
| 23 | +{ | |
| 18 | 24 | private $_integrationID; |
| 19 | 25 | |
| 20 | - public function __construct($integrationID, $fromID) { | |
| 26 | + private $formId; | |
| 27 | + | |
| 28 | + public function __construct($integrationID, $fromID) | |
| 29 | + { | |
| 21 | 30 | $this->_integrationID = $integrationID; |
| 31 | + | |
| 32 | + $this->formId = $fromID; | |
| 22 | 33 | } |
| 23 | 34 | |
| 24 | 35 | /** |
| 25 | 36 | * MailChimp API Endpoint |
| 26 | 37 | */ |
| 27 | - public static function apiEndPoint($dc) { | |
| 38 | + public static function apiEndPoint($dc) | |
| 39 | + { | |
| 28 | 40 | return "https://$dc.api.mailchimp.com/3.0"; |
| 29 | 41 | } |
| 30 | 42 | |
| 31 | 43 | /** |
| @@ -32,9 +44,10 @@ | ||
| 32 | 44 | * Helps to register ajax function's with wp |
| 33 | 45 | * |
| 34 | 46 | * @return null |
| 35 | 47 | */ |
| 36 | - public static function registerAjax() { | |
| 48 | + public static function registerAjax() | |
| 49 | + { | |
| 37 | 50 | add_action('wp_ajax_bitforms_mChimp_generate_token', [__CLASS__, 'generateTokens']); |
| 38 | 51 | add_action('wp_ajax_bitforms_mChimp_refresh_audience', [__CLASS__, 'refreshAudienceAjaxHelper']); |
| 39 | 52 | add_action('wp_ajax_bitforms_mChimp_refresh_fields', [__CLASS__, 'refreshAudienceFields']); |
| 40 | 53 | add_action('wp_ajax_bitforms_mChimp_refresh_tags', [__CLASS__, 'refreshTagsAjaxHelper']); |
| @@ -44,13 +57,20 @@ | ||
| 44 | 57 | * Process ajax request for generate_token |
| 45 | 58 | * |
| 46 | 59 | * @return JSON zoho crm api response and status |
| 47 | 60 | */ |
| 48 | - public static function generateTokens() { | |
| 49 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 61 | + public static function generateTokens() | |
| 62 | + { | |
| 63 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 50 | 64 | $authorizationHeader = null; |
| 51 | - $inputJSON = file_get_contents('php://input'); | |
| 52 | - $requestsParams = json_decode($inputJSON); | |
| 65 | + | |
| 66 | + GlobalHelper::requirePostMethod(); | |
| 67 | + | |
| 68 | + try { | |
| 69 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 70 | + } catch (\InvalidArgumentException $e) { | |
| 71 | + wp_send_json_error($e->getMessage(), 400); | |
| 72 | + } | |
| 53 | 73 | if ( |
| 54 | 74 | empty($requestsParams->clientId) |
| 55 | 75 | || empty($requestsParams->clientSecret) |
| 56 | 76 | || empty($requestsParams->redirectURI) |
| @@ -106,14 +126,20 @@ | ||
| 106 | 126 | * Process ajax request for refresh MailChimp Audience list |
| 107 | 127 | * |
| 108 | 128 | * @return JSON MailChimp data |
| 109 | 129 | */ |
| 110 | - public static function refreshAudienceAjaxHelper() { | |
| 111 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 130 | + public static function refreshAudienceAjaxHelper() | |
| 131 | + { | |
| 132 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 112 | 133 | $authorizationHeader = null; |
| 113 | - $inputJSON = file_get_contents('php://input'); | |
| 114 | - $queryParams = json_decode($inputJSON); | |
| 115 | 134 | |
| 135 | + GlobalHelper::requirePostMethod(); | |
| 136 | + | |
| 137 | + try { | |
| 138 | + $queryParams = GlobalHelper::formatRequestData(); | |
| 139 | + } catch (\InvalidArgumentException $e) { | |
| 140 | + wp_send_json_error($e->getMessage(), 400); | |
| 141 | + } | |
| 116 | 142 | if ( |
| 117 | 143 | empty($queryParams->tokenDetails) |
| 118 | 144 | || empty($queryParams->clientId) |
| 119 | 145 | || empty($queryParams->clientSecret) |
| @@ -145,9 +171,9 @@ | ||
| 145 | 171 | } |
| 146 | 172 | uksort($allList, 'strnatcasecmp'); |
| 147 | 173 | |
| 148 | 174 | $response['audiencelist'] = $allList; |
| 149 | - // wp_send_json_success($response, 200); | |
| 175 | + // wp_send_json_success($response, 200); | |
| 150 | 176 | } else { |
| 151 | 177 | wp_send_json_error( |
| 152 | 178 | $audienceResponse->response->error->message, |
| 153 | 179 | 400 |
| @@ -168,15 +194,21 @@ | ||
| 168 | 194 | /** |
| 169 | 195 | * Process ajax request for refresh MailChimp Audince Fields |
| 170 | 196 | * @return JSON MailChimp Audience fields |
| 171 | 197 | */ |
| 172 | - public static function refreshAudienceFields() { | |
| 198 | + public static function refreshAudienceFields() | |
| 199 | + { | |
| 173 | 200 | $authorizationHeader = null; |
| 174 | 201 | $response = null; |
| 175 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 176 | - $inputJSON = file_get_contents('php://input'); | |
| 177 | - $queryParams = json_decode($inputJSON); | |
| 202 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 178 | 203 | // wp_send_json_success($queryParams); |
| 204 | + GlobalHelper::requirePostMethod(); | |
| 205 | + | |
| 206 | + try { | |
| 207 | + $queryParams = GlobalHelper::formatRequestData(); | |
| 208 | + } catch (\InvalidArgumentException $e) { | |
| 209 | + wp_send_json_error($e->getMessage(), 400); | |
| 210 | + } | |
| 179 | 211 | if ( |
| 180 | 212 | empty($queryParams->tokenDetails) |
| 181 | 213 | || empty($queryParams->listId) |
| 182 | 214 | || empty($queryParams->tokenDetails->dc) |
| @@ -223,15 +255,21 @@ | ||
| 223 | 255 | /** |
| 224 | 256 | * Process ajax request for refresh MailChimp Tags |
| 225 | 257 | * @return JSON MailChimp Tags |
| 226 | 258 | */ |
| 227 | - public static function refreshTagsAjaxHelper() { | |
| 259 | + public static function refreshTagsAjaxHelper() | |
| 260 | + { | |
| 228 | 261 | $authorizationHeader = null; |
| 229 | 262 | $response = null; |
| 230 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 231 | - $inputJSON = file_get_contents('php://input'); | |
| 232 | - $queryParams = json_decode($inputJSON); | |
| 233 | - // wp_send_json_success($queryParams); | |
| 263 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 264 | + GlobalHelper::requirePostMethod(); | |
| 265 | + | |
| 266 | + try { | |
| 267 | + $queryParams = GlobalHelper::formatRequestData(); | |
| 268 | + } catch (\InvalidArgumentException $e) { | |
| 269 | + wp_send_json_error($e->getMessage(), 400); | |
| 270 | + } | |
| 271 | + | |
| 234 | 272 | if ( |
| 235 | 273 | empty($queryParams->tokenDetails) |
| 236 | 274 | || empty($queryParams->listId) |
| 237 | 275 | || empty($queryParams->tokenDetails->dc) |
| @@ -277,9 +315,10 @@ | ||
| 277 | 315 | * @param Obeject $tokenDetails refreshed token info |
| 278 | 316 | * |
| 279 | 317 | * @return null |
| 280 | 318 | */ |
| 281 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { | |
| 319 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 320 | + { | |
| 282 | 321 | $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 283 | 322 | |
| 284 | 323 | $tokenDetails = $integrationDetails->tokenDetails; |
| 285 | 324 | $listId = $integrationDetails->listId; |
| @@ -304,9 +343,10 @@ | ||
| 304 | 343 | $defaultDataConf, |
| 305 | 344 | $fieldValues, |
| 306 | 345 | $fieldMap, |
| 307 | 346 | $actions, |
| 308 | - $addressFields | |
| 347 | + $addressFields, | |
| 348 | + $this->formId | |
| 309 | 349 | ); |
| 310 | 350 | |
| 311 | 351 | if (is_wp_error($mChimpApiResponse)) { |
| 312 | 352 | return $mChimpApiResponse; |