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

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