PluginProbe
MailerLite – WooCommerce integration / 2.0
MailerLite – WooCommerce integration v2.0
3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 All 147 releases
woo-mailerlite / includes / admin / ajax.php

ajax.php in MailerLite – WooCommerce integration 2.0, at includes/admin/ajax.php

288 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MailerLite\Includes\Classes\Process\CartProcess;
4 use MailerLite\Includes\Classes\Settings\ApiSettings;
5 use MailerLite\Includes\Classes\Settings\MailerLiteSettings;
6 use MailerLite\Includes\Classes\Settings\ResetSettings;
7 use MailerLite\Includes\Classes\Settings\SynchronizeSettings;
8 use MailerLite\Includes\Shared\Api\ApiType;
9 use MailerLite\Includes\Shared\Api\PlatformAPI;
10 /**
11 * Ajax
12 */
13
14 // Exit if accessed directly
15 if ( ! defined('ABSPATH')) {
16 exit;
17 }
18
19 /**
20 * Refresh groups
21 */
22 function woo_ml_admin_ajax_refresh_groups()
23 {
24 if (defined('DOING_AJAX') && DOING_AJAX) {
25 $mailerLiteSettings = MailerLiteSettings::getInstance();
26 wp_send_json([
27 'groups' => $mailerLiteSettings->getGroupOptions(),
28 'current' => $mailerLiteSettings->getMlOption('group')
29 ]);
30 }
31 }
32
33 add_action('wp_ajax_nopriv_post_woo_ml_refresh_groups', 'woo_ml_admin_ajax_refresh_groups');
34 add_action('wp_ajax_post_woo_ml_refresh_groups', 'woo_ml_admin_ajax_refresh_groups');
35
36 /**
37 * Sync Customers
38 */
39 function woo_ml_admin_ajax_sync_untracked_resources()
40 {
41 if (defined('DOING_AJAX') && DOING_AJAX) {
42
43 $response = false;
44
45 try {
46 $shop_synced = SynchronizeSettings::getInstance()->syncUntrackedResources();
47 if (is_bool($shop_synced)) {
48 $response = true;
49 } else {
50 $response = $shop_synced;
51 }
52 $responseObj = json_decode($response);
53 if(isset($responseObj->allDone) && $responseObj->allDone) {
54 if((int)get_option('woo_ml_wizard_setup', 0) !== 2) {
55 update_option('woo_ml_wizard_setup', 2);
56 }
57 }
58 echo $response;
59 } catch (\Exception $e) {
60 return true;
61 }
62
63 }
64 exit;
65 }
66
67 add_action('wp_ajax_post_woo_ml_sync_untracked_resources', 'woo_ml_admin_ajax_sync_untracked_resources');
68
69 /**
70 * Is called when the user presses the Reset resources sync button in the plugin admin settings
71 */
72 function woo_ml_reset_resources_sync()
73 {
74 if (isset($_POST['cancelSync'])) {
75
76 update_option('woo_ml_wizard_setup', 1);
77 $settings = get_option('woocommerce_mailerlite_settings', []);
78 if(isset($settings['consumer_key'])) {
79 unset($settings['consumer_key']);
80 }
81 if(isset($settings['consumer_secret'])) {
82 unset($settings['consumer_secret']);
83 }
84 update_option('woocommerce_mailerlite_settings',
85 apply_filters('woocommerce_settings_api_sanitized_fields_mailerlite', $settings), 'yes');
86 }
87 ResetSettings::getInstance()->resetTrackedResources();
88 }
89
90 add_action('wp_ajax_post_woo_ml_reset_resources_sync', 'woo_ml_reset_resources_sync');
91
92 function woo_ml_email_cookie()
93 {
94
95 if (defined('DOING_AJAX') && DOING_AJAX) {
96 try {
97 $email = $_POST['email'] ?? null;
98 $subscribe = isset($_POST['signup']) ? ('true' == $_POST['signup']) : null;
99 $language = $_POST['language'] ?? '';
100
101 $subscriber_fields = [];
102
103 $first_name = $_POST['first_name'] ?? '';
104 $last_name = $_POST['last_name'] ?? '';
105
106 if ( ! empty($first_name)) {
107 $subscriber_fields['name'] = $first_name;
108 }
109
110 if ( ! empty($last_name)) {
111 $subscriber_fields['last_name'] = $last_name;
112 }
113
114 if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
115
116 // set accepts marketing cookie only when on checkout
117 if (isset($_POST['signup'])) {
118 @setcookie('mailerlite_accepts_marketing', $subscribe, time() + 172800, '/');
119 }
120
121 //setting email cookie and cart token for two days
122 @setcookie('mailerlite_checkout_email', $email, time() + 172800, '/');
123 if ( ! isset($_COOKIE['mailerlite_checkout_token'])) {
124 @setcookie('mailerlite_checkout_token', md5(uniqid(rand(), true)), time() + 172800, '/');
125 }
126
127 if (get_option('mailerlite_disable_checkout_sync') == false) {
128 CartProcess::getInstance()->sendCart($email, $subscribe, $language, $subscriber_fields);
129 }
130 }
131 } catch (\Exception $e) {
132 return true;
133 }
134 }
135 exit;
136 }
137
138 add_action('wp_ajax_nopriv_post_woo_ml_email_cookie', 'woo_ml_email_cookie');
139 add_action('wp_ajax_post_woo_ml_email_cookie', 'woo_ml_email_cookie');
140
141 function woo_ml_validate_key()
142 {
143
144 if (defined('DOING_AJAX') && DOING_AJAX) {
145 if ( ! empty($_POST['key'])) {
146 ApiSettings::getInstance()->validateApiKey($_POST['key']);
147 }
148 }
149 exit;
150 }
151
152 add_action('wp_ajax_nopriv_post_woo_ml_validate_key', 'woo_ml_validate_key');
153 add_action('wp_ajax_post_woo_ml_validate_key', 'woo_ml_validate_key');
154
155 /**
156 * Update hide checkbox setting
157 */
158 function woo_ml_admin_ajax_update_hide_checkbox()
159 {
160
161 if ( ! is_admin()) {
162 die();
163 }
164
165 if (defined('DOING_AJAX') && DOING_AJAX) {
166
167 if (isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ml-block-settings-update')) {
168
169 if (isset($_POST['hidden'])) {
170
171 $isHidden = filter_var($_POST['hidden'], FILTER_VALIDATE_BOOLEAN);
172
173 MailerLiteSettings::getInstance()->setOption('checkout_hide', $isHidden ? 'yes' : 'no');
174
175 wp_send_json_success([
176 'hidden' => $isHidden,
177 ], 200);
178 }
179 }
180
181 die();
182 }
183 }
184
185 add_action('wp_ajax_woo_ml_admin_ajax_update_hide_checkbox', 'woo_ml_admin_ajax_update_hide_checkbox');
186
187 /**
188 * Update preselect checkbox setting
189 */
190 function woo_ml_admin_ajax_update_preselect_checkbox()
191 {
192
193 if ( ! is_admin()) {
194 die();
195 }
196
197 if (defined('DOING_AJAX') && DOING_AJAX) {
198
199 if (isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ml-block-settings-update')) {
200
201 if (isset($_POST['preselect'])) {
202
203 $isPreselect = filter_var($_POST['preselect'], FILTER_VALIDATE_BOOLEAN);
204
205 MailerLiteSettings::getInstance()->setOption('checkout_preselect', $isPreselect ? 'yes' : 'no');
206
207 wp_send_json_success([
208 'preselect' => $isPreselect,
209 ], 200);
210 }
211 }
212
213 die();
214 }
215 }
216
217 /**
218 * Refresh groups
219 */
220 function woo_ml_admin_create_group()
221 {
222 if (defined('DOING_AJAX') && DOING_AJAX) {
223 if(!isset($_POST['createGroup'])) {
224 return false;
225 }
226 $mailerLiteSettings = MailerLiteSettings::getInstance();
227 if(in_array($_POST['createGroup'], $mailerLiteSettings->getGroupOptions())) {
228 wp_send_json([
229 'error' => 'Group name already exists'
230 ]);
231 }
232 wp_send_json(MailerLiteSettings::getInstance()->createGroup($_POST['createGroup']));
233 }
234 }
235
236 function woo_ml_reset_integration()
237 {
238 delete_option('woo_ml_shop_id');
239 delete_option('woo_ml_wizard_setup');
240 delete_option('woo_ml_key');
241 delete_option('ml_shop_not_active');
242 }
243
244 function woo_ml_admin_save_group()
245 {
246 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
247
248 $data = [
249 'group' => $_POST['group']
250 ];
251 if ($mailerliteClient->getApiType() == ApiType::CLASSIC) {
252
253 if (!empty($_POST['consumer_key']) && !empty($_POST['consumer_secret'])) {
254 $data['consumer_key'] = $_POST['consumer_key'];
255 $data['consumer_secret'] = $_POST['consumer_secret'];
256 } else {
257 wp_send_json([
258 'error' => true,
259 'message' => 'Consumer key or secret missing!'
260 ]);
261 }
262 }
263
264 $settings = array_merge(get_option('woocommerce_mailerlite_settings'), $data);
265 update_option('woocommerce_mailerlite_settings',
266 apply_filters('woocommerce_settings_api_sanitized_fields_mailerlite', $settings), 'yes');
267 if(get_transient('invalid_consumer_keys')) {
268 wp_send_json([
269 'error' => true,
270 'message' => get_transient('invalid_consumer_keys')
271 ]);
272 delete_transient('invalid_consumer_keys');
273 }
274 wp_send_json([
275 'error' => false,
276 'message' => 'Group Saved successfully'
277 ]);
278 }
279
280
281 add_action('wp_ajax_woo_ml_admin_ajax_update_preselect_checkbox', 'woo_ml_admin_ajax_update_preselect_checkbox');
282
283 add_action('wp_ajax_woo_ml_create_group', 'woo_ml_admin_create_group');
284 add_action('wp_ajax_woo_ml_reset_integration', 'woo_ml_reset_integration');
285 add_action('wp_ajax_woo_ml_save_group', 'woo_ml_admin_save_group');
286
287
288