PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.36
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.36
51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / widgets / Form_Builder / helpers / Subscribe_Mailchimp.php

Subscribe_Mailchimp.php in King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder 51.1.36, at includes/widgets/Form_Builder/helpers/Subscribe_Mailchimp.php

136 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace King_Addons;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 class Subscribe_Mailchimp
10 {
11
12 public function __construct()
13 {
14
15 add_action('wp_ajax_king_addons_form_builder_mailchimp', [$this, 'king_addons_form_builder_mailchimp']);
16 add_action('wp_ajax_nopriv_king_addons_form_builder_mailchimp', [$this, 'king_addons_form_builder_mailchimp']);
17 }
18
19
20 public static function king_addons_form_builder_mailchimp()
21 {
22
23 $nonce = $_POST['nonce'];
24
25 if (!wp_verify_nonce($nonce, 'king-addons-js')) {
26 return;
27 }
28
29
30 $api_key = get_option('king_addons_mailchimp_api_key') ? get_option('king_addons_mailchimp_api_key') : '';
31
32 $api_key_sufix = explode('-', $api_key)[1];
33
34
35 $list_id = isset($_POST['listId']) ? sanitize_text_field(wp_unslash($_POST['listId'])) : '';
36
37
38 $fields = $_POST['form_data'] ?? [];
39
40 $group_ids = isset($fields['group_id']) ? array_map('sanitize_text_field', array_map('trim', explode(',', wp_unslash($fields['group_id'])))) : [];
41
42
43 $merge_fields = [
44 'FNAME' => !empty($fields['first_name_field']) ? sanitize_text_field($fields['first_name_field']) : '',
45 'LNAME' => !empty($fields['last_name_field']) ? sanitize_text_field($fields['last_name_field']) : '',
46 'PHONE' => !empty ($fields['phone_field']) ? sanitize_text_field($fields['phone_field']) : '',
47 'BIRTHDAY' => !empty ($fields['birthday_field']) ? sanitize_text_field($fields['birthday_field']) : '',
48 ];
49
50 $requiredKeys = ['address_field', 'country_field', 'city_field', 'state_field', 'zip_field'];
51
52 if (!empty(array_intersect_key($fields, array_flip($requiredKeys)))) {
53 $merge_fields = array_merge($merge_fields, [
54 'ADDRESS' => [
55 'addr1' => !empty ($fields['address_field']) ? sanitize_text_field($fields['address_field']) : 'none',
56 'country' => !empty ($fields['country_field']) ? sanitize_text_field($fields['country_field']) : 'none',
57 'city' => !empty ($fields['city_field']) ? sanitize_text_field($fields['city_field']) : 'none',
58 'state' => !empty ($fields['state_field']) ? sanitize_text_field($fields['state_field']) : 'none',
59 'zip' => !empty ($fields['zip_field']) ? sanitize_text_field($fields['zip_field']) : 'none',
60 ]
61 ]);
62 }
63
64
65 $api_url = 'https://' . $api_key_sufix . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower(sanitize_text_field($fields['email_field'])));
66
67 $api_body = [
68 'email_address' => sanitize_text_field($fields['email_field']),
69 'status' => 'subscribed',
70 'merge_fields' => $merge_fields
71 ];
72
73 if (!empty($group_ids)) {
74 $api_body['interests'] = self::group_ids_to_interests_array($group_ids);
75 }
76
77
78 $api_args = [
79 'method' => 'PUT',
80 'headers' => [
81 'Content-Type' => 'application/json',
82 'Authorization' => 'apikey ' . $api_key,
83 ],
84 'body' => json_encode($api_body),
85 ];
86
87
88 $request = wp_remote_post($api_url, $api_args);
89
90 if (!is_wp_error($request)) {
91 $request = json_decode(wp_remote_retrieve_body($request));
92
93 if (!empty($request)) {
94 if ($request->status == 'subscribed') {
95
96 wp_send_json_success(array(
97 'action' => 'king_addons_form_builder_mailchimp',
98 'status' => 'success',
99 'message' => 'Mailchimp subscription was successful',
100 'request' => $request
101 ));
102
103 } else {
104 wp_send_json_error([
105 'action' => 'king_addons_form_builder_mailchimp',
106 'status' => 'error',
107 'message' => 'Mailchimp subscription failed',
108 'request' => $request
109 ]);
110 }
111 }
112 } else {
113
114 wp_send_json_error([
115 'action' => 'king_addons_form_builder_mailchimp',
116 'status' => 'error',
117 'message' => 'Mailchimp subscription failed',
118 'request' => $request
119 ]);
120 }
121 }
122
123 public static function group_ids_to_interests_array($group_ids)
124 {
125 $interests_array = [];
126
127 foreach ($group_ids as $group_id) {
128 $interests_array[$group_id] = true;
129 }
130
131 return $interests_array;
132 }
133
134 }
135
136 new Subscribe_Mailchimp();