← All changes
|
includes/Core/Integration/MailerLite/MailerLiteHandler.php
+80
-40
2.10.0
→
V3.3.2
View file →
| @@ -5,10 +5,15 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | namespace BitCode\BitForm\Core\Integration\MailerLite; |
| 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 MailerLite integration |
| @@ -19,11 +24,14 @@ | ||
| 19 | 24 | private static $_baseUrlV1 = 'https://api.mailerlite.com/api/v2/'; |
| 20 | 25 | private static $_baseUrlV2 = 'https://connect.mailerlite.com/api/'; |
| 21 | 26 | protected $_defaultHeader; |
| 22 | 27 | |
| 28 | + private $_formID; | |
| 29 | + | |
| 23 | 30 | public function __construct($integrationID, $fromID) |
| 24 | 31 | { |
| 25 | 32 | $this->_integrationID = $integrationID; |
| 33 | + $this->_formID = $fromID; | |
| 26 | 34 | } |
| 27 | 35 | |
| 28 | 36 | public static function registerAjax() |
| 29 | 37 | { |
| @@ -32,11 +40,16 @@ | ||
| 32 | 40 | } |
| 33 | 41 | |
| 34 | 42 | public static function fetchAllGroups() |
| 35 | 43 | { |
| 36 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 37 | - $inputJSON = file_get_contents('php://input'); | |
| 38 | - $requestParams = json_decode($inputJSON); | |
| 44 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 45 | + GlobalHelper::requirePostMethod(); | |
| 46 | + | |
| 47 | + try { | |
| 48 | + $requestParams = GlobalHelper::formatRequestData(); | |
| 49 | + } catch (\InvalidArgumentException $e) { | |
| 50 | + wp_send_json_error($e->getMessage(), 400); | |
| 51 | + } | |
| 39 | 52 | if ( |
| 40 | 53 | empty($requestParams->auth_token) |
| 41 | 54 | ) { |
| 42 | 55 | wp_send_json_error( |
| @@ -41,9 +54,9 @@ | ||
| 41 | 54 | ) { |
| 42 | 55 | wp_send_json_error( |
| 43 | 56 | __( |
| 44 | 57 | 'Requested parameter is empty', |
| 45 | - 'bitform' | |
| 58 | + 'bit-form' | |
| 46 | 59 | ), |
| 47 | 60 | 400 |
| 48 | 61 | ); |
| 49 | 62 | } |
| @@ -48,39 +61,61 @@ | ||
| 48 | 61 | ); |
| 49 | 62 | } |
| 50 | 63 | |
| 51 | 64 | if ('v2' === $requestParams->version) { |
| 52 | - $apiEndpoints = self::$_baseUrlV2 . 'groups/'; | |
| 53 | - $apiKey = $requestParams->auth_token; | |
| 65 | + $apiEndpoints = self::$_baseUrlV2 . 'groups'; | |
| 66 | + $apiKey = trim((string) $requestParams->auth_token); | |
| 54 | 67 | $header = [ |
| 55 | - 'Authorization: Bearer ' . $apiKey | |
| 68 | + 'Authorization' => "Bearer $apiKey", | |
| 56 | 69 | ]; |
| 70 | + $formattedResponse = []; | |
| 57 | 71 | |
| 58 | - $curl = curl_init(); | |
| 59 | - curl_setopt_array($curl, [ | |
| 60 | - CURLOPT_URL => $apiEndpoints, | |
| 61 | - CURLOPT_RETURNTRANSFER => true, | |
| 62 | - CURLOPT_ENCODING => '', | |
| 63 | - CURLOPT_MAXREDIRS => 10, | |
| 64 | - CURLOPT_TIMEOUT => 0, | |
| 65 | - CURLOPT_FOLLOWLOCATION => true, | |
| 66 | - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
| 67 | - CURLOPT_CUSTOMREQUEST => 'GET', | |
| 68 | - CURLOPT_HTTPHEADER => $header, | |
| 69 | - ]); | |
| 70 | - $data = curl_exec($curl); | |
| 71 | - curl_close($curl); | |
| 72 | - $response = json_decode($data); | |
| 72 | + $httpArgs = [ | |
| 73 | + 'headers' => $header, | |
| 74 | + 'timeout' => 30, | |
| 75 | + 'redirection' => 0, | |
| 76 | + 'httpversion' => '1.1', | |
| 77 | + 'user-agent' => 'Bitforms/' . BITFORMS_VERSION . '; ' . get_bloginfo('url') | |
| 78 | + ]; | |
| 73 | 79 | |
| 74 | - $formattedResponse = []; | |
| 80 | + $response = wp_remote_get($apiEndpoints, $httpArgs); | |
| 75 | 81 | |
| 76 | - foreach ($response->data as $value) { | |
| 77 | - $formattedResponse[] = | |
| 78 | - [ | |
| 79 | - 'group_id' => $value->id, | |
| 80 | - 'name' => $value->name, | |
| 81 | - ]; | |
| 82 | + if (is_wp_error($response)) { | |
| 83 | + wp_send_json_error($response->get_error_message(), 400); | |
| 82 | 84 | } |
| 85 | + | |
| 86 | + $responseCode = wp_remote_retrieve_response_code($response); | |
| 87 | + $responseBody = wp_remote_retrieve_body($response); | |
| 88 | + $responseObj = json_decode($responseBody); | |
| 89 | + | |
| 90 | + if (200 === $responseCode && isset($responseObj->data)) { | |
| 91 | + foreach ($responseObj->data as $value) { | |
| 92 | + $formattedResponse[] = [ | |
| 93 | + 'group_id' => $value->id, | |
| 94 | + 'name' => $value->name, | |
| 95 | + ]; | |
| 96 | + } | |
| 97 | + } elseif (isset($responseObj->message) && 'Unauthenticated.' === $responseObj->message) { | |
| 98 | + wp_send_json_error( | |
| 99 | + __( | |
| 100 | + 'Invalid API Token', | |
| 101 | + 'bit-form' | |
| 102 | + ), | |
| 103 | + 401 | |
| 104 | + ); | |
| 105 | + } elseif (isset($responseObj->message)) { | |
| 106 | + wp_send_json_error($responseObj->message, $responseCode ?: 400); | |
| 107 | + } elseif (!empty($responseBody)) { | |
| 108 | + wp_send_json_error($responseBody, $responseCode ?: 400); | |
| 109 | + } else { | |
| 110 | + wp_send_json_error( | |
| 111 | + __( | |
| 112 | + 'Unable to fetch MailerLite groups', | |
| 113 | + 'bit-form' | |
| 114 | + ), | |
| 115 | + $responseCode ?: 400 | |
| 116 | + ); | |
| 117 | + } | |
| 83 | 118 | } else { |
| 84 | 119 | $apiEndpoints = self::$_baseUrlV1 . 'groups/'; |
| 85 | 120 | |
| 86 | 121 | $header = [ |
| @@ -92,9 +127,9 @@ | ||
| 92 | 127 | |
| 93 | 128 | foreach ($response as $value) { |
| 94 | 129 | $formattedResponse[] = |
| 95 | 130 | [ |
| 96 | - 'group_id' => $value->id, | |
| 131 | + 'group_id' => (string)$value->id, | |
| 97 | 132 | 'name' => $value->name, |
| 98 | 133 | ]; |
| 99 | 134 | } |
| 100 | 135 | } |
| @@ -103,9 +138,9 @@ | ||
| 103 | 138 | } else { |
| 104 | 139 | wp_send_json_error( |
| 105 | 140 | __( |
| 106 | 141 | 'Token expired', |
| 107 | - 'bitform' | |
| 142 | + 'bit-form' | |
| 108 | 143 | ), |
| 109 | 144 | 401 |
| 110 | 145 | ); |
| 111 | 146 | } |
| @@ -112,11 +147,15 @@ | ||
| 112 | 147 | } |
| 113 | 148 | |
| 114 | 149 | public static function mailerliteRefreshFields() |
| 115 | 150 | { |
| 116 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 117 | - $inputJSON = file_get_contents('php://input'); | |
| 118 | - $requestParams = json_decode($inputJSON); | |
| 151 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 152 | + GlobalHelper::requirePostMethod(); | |
| 153 | + try { | |
| 154 | + $requestParams = GlobalHelper::formatRequestData(); | |
| 155 | + } catch (\InvalidArgumentException $e) { | |
| 156 | + wp_send_json_error($e->getMessage(), 400); | |
| 157 | + } | |
| 119 | 158 | |
| 120 | 159 | if ( |
| 121 | 160 | empty($requestParams->auth_token) |
| 122 | 161 | ) { |
| @@ -122,9 +161,9 @@ | ||
| 122 | 161 | ) { |
| 123 | 162 | wp_send_json_error( |
| 124 | 163 | __( |
| 125 | 164 | 'Requested parameter is empty', |
| 126 | - 'bitform' | |
| 165 | + 'bit-form' | |
| 127 | 166 | ), |
| 128 | 167 | 400 |
| 129 | 168 | ); |
| 130 | 169 | } |
| @@ -163,9 +202,9 @@ | ||
| 163 | 202 | } elseif (isset($response->message) && 'Unauthenticated.' === $response->message) { |
| 164 | 203 | wp_send_json_error( |
| 165 | 204 | __( |
| 166 | 205 | 'Invalid API Token', |
| 167 | - 'bitform' | |
| 206 | + 'bit-form' | |
| 168 | 207 | ), |
| 169 | 208 | 401 |
| 170 | 209 | ); |
| 171 | 210 | } |
| @@ -193,9 +232,9 @@ | ||
| 193 | 232 | } elseif (isset($response->error->message) && 'Unauthorized' === $response->error->message) { |
| 194 | 233 | wp_send_json_error( |
| 195 | 234 | __( |
| 196 | 235 | 'Invalid API Token', |
| 197 | - 'bitform' | |
| 236 | + 'bit-form' | |
| 198 | 237 | ), |
| 199 | 238 | 401 |
| 200 | 239 | ); |
| 201 | 240 | } |
| @@ -203,9 +242,9 @@ | ||
| 203 | 242 | } else { |
| 204 | 243 | wp_send_json_error( |
| 205 | 244 | __( |
| 206 | 245 | 'Token expired', |
| 207 | - 'bitform' | |
| 246 | + 'bit-form' | |
| 208 | 247 | ), |
| 209 | 248 | 401 |
| 210 | 249 | ); |
| 211 | 250 | } |
| @@ -224,9 +263,9 @@ | ||
| 224 | 263 | if ( |
| 225 | 264 | empty($fieldMap) |
| 226 | 265 | || empty($auth_token) |
| 227 | 266 | ) { |
| 228 | - return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for MailerLite api', 'bit-integrations')); | |
| 267 | + return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for MailerLite api', 'bit-form')); | |
| 229 | 268 | } |
| 230 | 269 | $recordApiHelper = new RecordApiHelper($auth_token, $this->_integrationID, $logID, $entryID, $actions, $version); |
| 231 | 270 | $mailerliteApiResponse = $recordApiHelper->executeRecordApi( |
| 232 | 271 | $groupIds, |
| @@ -232,9 +271,10 @@ | ||
| 232 | 271 | $groupIds, |
| 233 | 272 | $type, |
| 234 | 273 | $fieldValues, |
| 235 | 274 | $fieldMap, |
| 236 | - $auth_token | |
| 275 | + $auth_token, | |
| 276 | + $this->_formID | |
| 237 | 277 | ); |
| 238 | 278 | |
| 239 | 279 | if (is_wp_error($mailerliteApiResponse)) { |
| 240 | 280 | return $mailerliteApiResponse; |