PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.83
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.83
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.83, at includes/widgets/Form_Builder/helpers/Subscribe_Mailchimp.php

140 lines 4.8 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 // Security fix: Generate nonce server-side instead of relying on client-provided nonce
26 $server_nonce = wp_create_nonce('king-addons-js');
27 if (!wp_verify_nonce($nonce, 'king-addons-js')) {
28 return;
29 }
30
31 Form_Builder_Security::guard_spam();
32
33
34 $api_key = get_option('king_addons_mailchimp_api_key') ? get_option('king_addons_mailchimp_api_key') : '';
35
36 $api_key_sufix = explode('-', $api_key)[1];
37
38
39 $list_id = isset($_POST['listId']) ? sanitize_text_field(wp_unslash($_POST['listId'])) : '';
40
41
42 $fields = $_POST['form_data'] ?? [];
43
44 $group_ids = isset($fields['group_id']) ? array_map('sanitize_text_field', array_map('trim', explode(',', wp_unslash($fields['group_id'])))) : [];
45
46
47 $merge_fields = [
48 'FNAME' => !empty($fields['first_name_field']) ? sanitize_text_field($fields['first_name_field']) : '',
49 'LNAME' => !empty($fields['last_name_field']) ? sanitize_text_field($fields['last_name_field']) : '',
50 'PHONE' => !empty ($fields['phone_field']) ? sanitize_text_field($fields['phone_field']) : '',
51 'BIRTHDAY' => !empty ($fields['birthday_field']) ? sanitize_text_field($fields['birthday_field']) : '',
52 ];
53
54 $requiredKeys = ['address_field', 'country_field', 'city_field', 'state_field', 'zip_field'];
55
56 if (!empty(array_intersect_key($fields, array_flip($requiredKeys)))) {
57 $merge_fields = array_merge($merge_fields, [
58 'ADDRESS' => [
59 'addr1' => !empty ($fields['address_field']) ? sanitize_text_field($fields['address_field']) : 'none',
60 'country' => !empty ($fields['country_field']) ? sanitize_text_field($fields['country_field']) : 'none',
61 'city' => !empty ($fields['city_field']) ? sanitize_text_field($fields['city_field']) : 'none',
62 'state' => !empty ($fields['state_field']) ? sanitize_text_field($fields['state_field']) : 'none',
63 'zip' => !empty ($fields['zip_field']) ? sanitize_text_field($fields['zip_field']) : 'none',
64 ]
65 ]);
66 }
67
68
69 $api_url = 'https://' . $api_key_sufix . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower(sanitize_text_field($fields['email_field'])));
70
71 $api_body = [
72 'email_address' => sanitize_text_field($fields['email_field']),
73 'status' => 'subscribed',
74 'merge_fields' => $merge_fields
75 ];
76
77 if (!empty($group_ids)) {
78 $api_body['interests'] = self::group_ids_to_interests_array($group_ids);
79 }
80
81
82 $api_args = [
83 'method' => 'PUT',
84 'headers' => [
85 'Content-Type' => 'application/json',
86 'Authorization' => 'apikey ' . $api_key,
87 ],
88 'body' => json_encode($api_body),
89 ];
90
91
92 $request = wp_remote_post($api_url, $api_args);
93
94 if (!is_wp_error($request)) {
95 $request = json_decode(wp_remote_retrieve_body($request));
96
97 if (!empty($request)) {
98 if ($request->status == 'subscribed') {
99
100 wp_send_json_success(array(
101 'action' => 'king_addons_form_builder_mailchimp',
102 'status' => 'success',
103 'message' => 'Mailchimp subscription was successful',
104 'request' => $request
105 ));
106
107 } else {
108 wp_send_json_error([
109 'action' => 'king_addons_form_builder_mailchimp',
110 'status' => 'error',
111 'message' => 'Mailchimp subscription failed',
112 'request' => $request
113 ]);
114 }
115 }
116 } else {
117
118 wp_send_json_error([
119 'action' => 'king_addons_form_builder_mailchimp',
120 'status' => 'error',
121 'message' => 'Mailchimp subscription failed',
122 'request' => $request
123 ]);
124 }
125 }
126
127 public static function group_ids_to_interests_array($group_ids)
128 {
129 $interests_array = [];
130
131 foreach ($group_ids as $group_id) {
132 $interests_array[$group_id] = true;
133 }
134
135 return $interests_array;
136 }
137
138 }
139
140 new Subscribe_Mailchimp();