← All changes
|
includes/elementor/modules/optin/actions/mailchimp.php
+86
-0
1.7.5
→
2.7.0
View file →
| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | /** |
| 8 | 8 | * Initializing the MailChimp action by extending Action base and using CRM trait. |
| 9 | 9 | * |
| 10 | 10 | * @since 1.5.0 |
| 11 | + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) | |
| 11 | 12 | */ |
| 12 | 13 | class Sellkit_Elementor_Optin_Action_Mailchimp extends Sellkit_Elementor_Optin_Action_Base { |
| 13 | 14 | use Sellkit_Elementor_Optin_CRM; |
| 14 | 15 | |
| @@ -249,8 +250,15 @@ | ||
| 249 | 250 | ], |
| 250 | 251 | ]; |
| 251 | 252 | } |
| 252 | 253 | |
| 254 | + /** | |
| 255 | + * Create the subscriber object for MailChimp. | |
| 256 | + * | |
| 257 | + * @since 1.5.0 | |
| 258 | + * @return array | |
| 259 | + * @SuppressWarnings(PHPMD.NPathComplexity) | |
| 260 | + */ | |
| 253 | 261 | protected function create_subscriber_object() { |
| 254 | 262 | $mapped_fields = $this->get_field_mappings(); |
| 255 | 263 | $subscriber = [ |
| 256 | 264 | 'ip_opt' => static::get_client_ip(), |
| @@ -301,9 +309,87 @@ | ||
| 301 | 309 | $groups = is_array( $groups ) ? $groups : [ $groups ]; |
| 302 | 310 | $subscriber['interests'] = array_fill_keys( $groups, true ); |
| 303 | 311 | } |
| 304 | 312 | |
| 313 | + if ( isset( $subscriber['merge_fields'] ) && ! empty( $subscriber['merge_fields']['BIRTHDAY'] ) ) { | |
| 314 | + $formatted_birthday = gmdate( 'Y/m/d', strtotime( $subscriber['merge_fields']['BIRTHDAY'] ) ); | |
| 315 | + | |
| 316 | + $subscriber['merge_fields']['BIRTHDAY'] = $formatted_birthday; | |
| 317 | + } | |
| 318 | + | |
| 319 | + if ( isset( $subscriber['merge_fields'] ) && ! empty( $subscriber['merge_fields']['ADDRESS'] ) ) { | |
| 320 | + $fields = $this->ajax_handler->form_data['fields']; | |
| 321 | + $formatted_address = $subscriber['merge_fields']['ADDRESS']; | |
| 322 | + | |
| 323 | + $mapping_fields = $this->ajax_handler->form['settings'][ $this->get_name() . '_fields_mapping' ]; | |
| 324 | + | |
| 325 | + foreach ( $mapping_fields as $map_field ) { | |
| 326 | + if ( ! isset( $map_field['remote_field'] ) || empty( $map_field['local_field'] ) ) { | |
| 327 | + continue; | |
| 328 | + } | |
| 329 | + | |
| 330 | + if ( 'ADDRESS' !== $map_field['remote_field'] ) { | |
| 331 | + continue; | |
| 332 | + } | |
| 333 | + | |
| 334 | + if ( empty( $fields[ "hidden_{$map_field['local_field']}" ] ) ) { | |
| 335 | + continue; | |
| 336 | + } | |
| 337 | + | |
| 338 | + $formatted_address = $this->handle_mailchimp_address( $fields, $map_field ); | |
| 339 | + } | |
| 340 | + | |
| 341 | + $subscriber['merge_fields']['ADDRESS'] = $formatted_address; | |
| 342 | + } | |
| 343 | + | |
| 305 | 344 | return $subscriber; |
| 345 | + } | |
| 346 | + | |
| 347 | + /** | |
| 348 | + * Handle the address field for MailChimp. | |
| 349 | + * | |
| 350 | + * @param array $fields | |
| 351 | + * @param array $map_field | |
| 352 | + * @since 2.3.0 | |
| 353 | + * @return array | |
| 354 | + */ | |
| 355 | + protected function handle_mailchimp_address( $fields, $map_field ) { | |
| 356 | + if ( empty( $fields[ "hidden_{$map_field['local_field']}" ] ) ) { | |
| 357 | + return []; | |
| 358 | + } | |
| 359 | + | |
| 360 | + $address_json = stripslashes( $fields[ "hidden_{$map_field['local_field']}" ] ); | |
| 361 | + $addresses = json_decode( $address_json, true ); | |
| 362 | + | |
| 363 | + $mapping = [ | |
| 364 | + 'addr1' => [ 'street_number', 'route' ], | |
| 365 | + 'city' => 'locality', | |
| 366 | + 'state' => 'administrative_area_level_1', | |
| 367 | + 'zip' => 'postal_code', | |
| 368 | + 'country' => 'country' | |
| 369 | + ]; | |
| 370 | + | |
| 371 | + $address_data = []; | |
| 372 | + | |
| 373 | + foreach ( $mapping as $mailchimp_key => $source_keys ) { | |
| 374 | + if ( is_array( $source_keys ) ) { | |
| 375 | + $value = ''; | |
| 376 | + | |
| 377 | + foreach ( $source_keys as $key ) { | |
| 378 | + if ( ! empty( $addresses[ $key ] ) ) { | |
| 379 | + $value .= $addresses[ $key ] . ' '; | |
| 380 | + } | |
| 381 | + } | |
| 382 | + | |
| 383 | + $address_data[ $mailchimp_key ] = trim( $value ); | |
| 384 | + | |
| 385 | + continue; | |
| 386 | + } | |
| 387 | + | |
| 388 | + $address_data[ $mailchimp_key ] = ! empty( $addresses[ $source_keys ] ) ? $addresses[ $source_keys ] : ''; | |
| 389 | + } | |
| 390 | + | |
| 391 | + return $address_data; | |
| 306 | 392 | } |
| 307 | 393 | |
| 308 | 394 | protected function subscribe( $subscriber_data, $list_id ) { |
| 309 | 395 | if ( empty( $subscriber_data['email_address'] ) ) { |