← All changes
|
includes/Core/Integration/MailerLite/MailerLiteHandler.php
+177
-53
2.0
→
3.3.1
View file →
| @@ -5,33 +5,51 @@ | ||
| 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 |
| 15 | 20 | */ |
| 16 | -class MailerLiteHandler { | |
| 21 | +class MailerLiteHandler | |
| 22 | +{ | |
| 17 | 23 | private $_integrationID; |
| 18 | - private static $_baseUrl = 'https://api.mailerlite.com/api/v2/'; | |
| 24 | + private static $_baseUrlV1 = 'https://api.mailerlite.com/api/v2/'; | |
| 25 | + private static $_baseUrlV2 = 'https://connect.mailerlite.com/api/'; | |
| 19 | 26 | protected $_defaultHeader; |
| 20 | 27 | |
| 21 | - public function __construct($integrationID, $fromID) { | |
| 28 | + private $_formID; | |
| 29 | + | |
| 30 | + public function __construct($integrationID, $fromID) | |
| 31 | + { | |
| 22 | 32 | $this->_integrationID = $integrationID; |
| 33 | + $this->_formID = $fromID; | |
| 23 | 34 | } |
| 24 | 35 | |
| 25 | - public static function registerAjax() { | |
| 36 | + public static function registerAjax() | |
| 37 | + { | |
| 26 | 38 | add_action('wp_ajax_bitforms_mailerlite_fetch_all_groups', [__CLASS__, 'fetchAllGroups']); |
| 27 | 39 | add_action('wp_ajax_bitforms_mailerlite_refresh_fields', [__CLASS__, 'mailerliteRefreshFields']); |
| 28 | 40 | } |
| 29 | 41 | |
| 30 | - public static function fetchAllGroups() { | |
| 31 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 32 | - $inputJSON = file_get_contents('php://input'); | |
| 33 | - $requestParams = json_decode($inputJSON); | |
| 42 | + public static function fetchAllGroups() | |
| 43 | + { | |
| 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 | + } | |
| 34 | 52 | if ( |
| 35 | 53 | empty($requestParams->auth_token) |
| 36 | 54 | ) { |
| 37 | 55 | wp_send_json_error( |
| @@ -36,29 +54,85 @@ | ||
| 36 | 54 | ) { |
| 37 | 55 | wp_send_json_error( |
| 38 | 56 | __( |
| 39 | 57 | 'Requested parameter is empty', |
| 40 | - 'bitform' | |
| 58 | + 'bit-form' | |
| 41 | 59 | ), |
| 42 | 60 | 400 |
| 43 | 61 | ); |
| 44 | 62 | } |
| 45 | 63 | |
| 46 | - $apiEndpoints = self::$_baseUrl . 'groups/'; | |
| 64 | + if ('v2' === $requestParams->version) { | |
| 65 | + $apiEndpoints = self::$_baseUrlV2 . 'groups'; | |
| 66 | + $apiKey = trim((string) $requestParams->auth_token); | |
| 67 | + $header = [ | |
| 68 | + 'Authorization' => "Bearer $apiKey", | |
| 69 | + ]; | |
| 70 | + $formattedResponse = []; | |
| 47 | 71 | |
| 48 | - $header = [ | |
| 49 | - 'X-Mailerlite-Apikey' => $requestParams->auth_token, | |
| 50 | - ]; | |
| 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 | + ]; | |
| 51 | 79 | |
| 52 | - $response = HttpHelper::get($apiEndpoints, null, $header); | |
| 53 | - $formattedResponse = []; | |
| 80 | + $response = wp_remote_get($apiEndpoints, $httpArgs); | |
| 54 | 81 | |
| 55 | - foreach ($response as $value) { | |
| 56 | - $formattedResponse[] = | |
| 57 | - [ | |
| 82 | + if (is_wp_error($response)) { | |
| 83 | + wp_send_json_error($response->get_error_message(), 400); | |
| 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[] = [ | |
| 58 | 93 | 'group_id' => $value->id, |
| 59 | 94 | 'name' => $value->name, |
| 60 | 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 | + } | |
| 118 | + } else { | |
| 119 | + $apiEndpoints = self::$_baseUrlV1 . 'groups/'; | |
| 120 | + | |
| 121 | + $header = [ | |
| 122 | + 'X-Mailerlite-Apikey' => $requestParams->auth_token, | |
| 123 | + ]; | |
| 124 | + | |
| 125 | + $response = HttpHelper::get($apiEndpoints, null, $header); | |
| 126 | + $formattedResponse = []; | |
| 127 | + | |
| 128 | + foreach ($response as $value) { | |
| 129 | + $formattedResponse[] = | |
| 130 | + [ | |
| 131 | + 'group_id' => (string)$value->id, | |
| 132 | + 'name' => $value->name, | |
| 133 | + ]; | |
| 134 | + } | |
| 61 | 135 | } |
| 62 | 136 | |
| 63 | 137 | wp_send_json_success($formattedResponse, 200); |
| 64 | 138 | } else { |
| @@ -64,9 +138,9 @@ | ||
| 64 | 138 | } else { |
| 65 | 139 | wp_send_json_error( |
| 66 | 140 | __( |
| 67 | 141 | 'Token expired', |
| 68 | - 'bitform' | |
| 142 | + 'bit-form' | |
| 69 | 143 | ), |
| 70 | 144 | 401 |
| 71 | 145 | ); |
| 72 | 146 | } |
| @@ -71,12 +145,17 @@ | ||
| 71 | 145 | ); |
| 72 | 146 | } |
| 73 | 147 | } |
| 74 | 148 | |
| 75 | - public static function mailerliteRefreshFields() { | |
| 76 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 77 | - $inputJSON = file_get_contents('php://input'); | |
| 78 | - $requestParams = json_decode($inputJSON); | |
| 149 | + public static function mailerliteRefreshFields() | |
| 150 | + { | |
| 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 | + } | |
| 79 | 158 | |
| 80 | 159 | if ( |
| 81 | 160 | empty($requestParams->auth_token) |
| 82 | 161 | ) { |
| @@ -82,48 +161,90 @@ | ||
| 82 | 161 | ) { |
| 83 | 162 | wp_send_json_error( |
| 84 | 163 | __( |
| 85 | 164 | 'Requested parameter is empty', |
| 86 | - 'bitform' | |
| 165 | + 'bit-form' | |
| 87 | 166 | ), |
| 88 | 167 | 400 |
| 89 | 168 | ); |
| 90 | 169 | } |
| 91 | - $apiEndpoints = self::$_baseUrl . 'fields'; | |
| 92 | 170 | |
| 93 | - $apiKey = $requestParams->auth_token; | |
| 94 | - $header = [ | |
| 95 | - 'X-Mailerlite-Apikey' => $apiKey, | |
| 96 | - ]; | |
| 171 | + if ('v2' === $requestParams->version) { | |
| 172 | + $apiEndpoints = self::$_baseUrlV2 . 'fields'; | |
| 97 | 173 | |
| 98 | - $response = HttpHelper::get($apiEndpoints, null, $header); | |
| 174 | + $apiKey = $requestParams->auth_token; | |
| 175 | + $header = [ | |
| 176 | + 'Authorization' => "Bearer $apiKey", | |
| 177 | + ]; | |
| 99 | 178 | |
| 100 | - $formattedResponse = []; | |
| 101 | - foreach ($response as $value) { | |
| 102 | - $formattedResponse[] = [ | |
| 103 | - 'key' => $value->key, | |
| 104 | - 'label' => $value->title, | |
| 105 | - 'required' => 'email' === $value->key ? true : false, | |
| 179 | + $response = HttpHelper::get($apiEndpoints, null, $header); | |
| 180 | + | |
| 181 | + $newResponse = []; | |
| 182 | + foreach ($response->data as $value) { | |
| 183 | + if ('email' !== $value->key) { | |
| 184 | + $newResponse[] = [ | |
| 185 | + 'key' => $value->key, | |
| 186 | + 'label' => $value->name, | |
| 187 | + 'required' => 'email' === $value->key ? true : false, | |
| 188 | + ]; | |
| 189 | + } | |
| 190 | + } | |
| 191 | + | |
| 192 | + $email[] = [ | |
| 193 | + 'key' => 'email', | |
| 194 | + 'label' => 'Email', | |
| 195 | + 'required' => true, | |
| 106 | 196 | ]; |
| 107 | - } | |
| 108 | 197 | |
| 109 | - if (isset($response->error->message) && 'Unauthorized' === $response->error->message) { | |
| 110 | - wp_send_json_error( | |
| 111 | - __( | |
| 112 | - 'Invalid API Token', | |
| 113 | - 'bitform' | |
| 114 | - ), | |
| 115 | - 401 | |
| 116 | - ); | |
| 198 | + $formattedResponse = array_merge($email, $newResponse); | |
| 199 | + | |
| 200 | + if (isset($response->data)) { | |
| 201 | + wp_send_json_success($formattedResponse, 200); | |
| 202 | + } elseif (isset($response->message) && 'Unauthenticated.' === $response->message) { | |
| 203 | + wp_send_json_error( | |
| 204 | + __( | |
| 205 | + 'Invalid API Token', | |
| 206 | + 'bit-form' | |
| 207 | + ), | |
| 208 | + 401 | |
| 209 | + ); | |
| 210 | + } | |
| 211 | + } else { | |
| 212 | + $apiEndpoints = self::$_baseUrlV1 . 'fields'; | |
| 213 | + | |
| 214 | + $apiKey = $requestParams->auth_token; | |
| 215 | + $header = [ | |
| 216 | + 'X-Mailerlite-Apikey' => $apiKey, | |
| 217 | + ]; | |
| 218 | + | |
| 219 | + $response = HttpHelper::get($apiEndpoints, null, $header); | |
| 220 | + | |
| 221 | + $formattedResponse = []; | |
| 222 | + foreach ($response as $value) { | |
| 223 | + $formattedResponse[] = [ | |
| 224 | + 'key' => $value->key, | |
| 225 | + 'label' => $value->title, | |
| 226 | + 'required' => 'email' === $value->key ? true : false, | |
| 227 | + ]; | |
| 228 | + } | |
| 229 | + | |
| 230 | + if (count($response) > 0) { | |
| 231 | + wp_send_json_success($formattedResponse, 200); | |
| 232 | + } elseif (isset($response->error->message) && 'Unauthorized' === $response->error->message) { | |
| 233 | + wp_send_json_error( | |
| 234 | + __( | |
| 235 | + 'Invalid API Token', | |
| 236 | + 'bit-form' | |
| 237 | + ), | |
| 238 | + 401 | |
| 239 | + ); | |
| 240 | + } | |
| 117 | 241 | } |
| 118 | - if (count($response) > 0) { | |
| 119 | - wp_send_json_success($formattedResponse, 200); | |
| 120 | - } | |
| 121 | 242 | } else { |
| 122 | 243 | wp_send_json_error( |
| 123 | 244 | __( |
| 124 | 245 | 'Token expired', |
| 125 | - 'bitform' | |
| 246 | + 'bit-form' | |
| 126 | 247 | ), |
| 127 | 248 | 401 |
| 128 | 249 | ); |
| 129 | 250 | } |
| @@ -128,11 +249,13 @@ | ||
| 128 | 249 | ); |
| 129 | 250 | } |
| 130 | 251 | } |
| 131 | 252 | |
| 132 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { | |
| 253 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 254 | + { | |
| 133 | 255 | $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 134 | 256 | $auth_token = $integrationDetails->auth_token; |
| 257 | + $version = $integrationDetails->version; | |
| 135 | 258 | $groupIds = $integrationDetails->group_ids; |
| 136 | 259 | $fieldMap = $integrationDetails->field_map; |
| 137 | 260 | $type = $integrationDetails->mailer_lite_type; |
| 138 | 261 | $actions = $integrationDetails->actions; |
| @@ -140,17 +263,18 @@ | ||
| 140 | 263 | if ( |
| 141 | 264 | empty($fieldMap) |
| 142 | 265 | || empty($auth_token) |
| 143 | 266 | ) { |
| 144 | - 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')); | |
| 145 | 268 | } |
| 146 | - $recordApiHelper = new RecordApiHelper($auth_token, $this->_integrationID, $logID, $entryID, $actions); | |
| 269 | + $recordApiHelper = new RecordApiHelper($auth_token, $this->_integrationID, $logID, $entryID, $actions, $version); | |
| 147 | 270 | $mailerliteApiResponse = $recordApiHelper->executeRecordApi( |
| 148 | 271 | $groupIds, |
| 149 | 272 | $type, |
| 150 | 273 | $fieldValues, |
| 151 | 274 | $fieldMap, |
| 152 | - $auth_token | |
| 275 | + $auth_token, | |
| 276 | + $this->_formID | |
| 153 | 277 | ); |
| 154 | 278 | |
| 155 | 279 | if (is_wp_error($mailerliteApiResponse)) { |
| 156 | 280 | return $mailerliteApiResponse; |