PluginProbe
MailerLite – WooCommerce integration / 2.0.9
MailerLite – WooCommerce integration v2.0.9
3.1.27 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 All 148 releases
woo-mailerlite / includes / admin / ajax.php

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

338 lines 10.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 (!check_ajax_referer( 'woo_ml_post_nonce', 'nonce', false ) ) {
25 wp_send_json_error( 'Invalid security token sent.' );
26 wp_die();
27 }
28 if (defined('DOING_AJAX') && DOING_AJAX) {
29 $mailerLiteSettings = MailerLiteSettings::getInstance();
30 wp_send_json([
31 'groups' => $mailerLiteSettings->getGroupOptions(),
32 'current' => $mailerLiteSettings->getMlOption('group')
33 ]);
34 }
35 }
36
37 add_action('wp_ajax_nopriv_post_woo_ml_refresh_groups', 'woo_ml_admin_ajax_refresh_groups');
38 add_action('wp_ajax_post_woo_ml_refresh_groups', 'woo_ml_admin_ajax_refresh_groups');
39
40 /**
41 * Sync Customers
42 */
43 function woo_ml_admin_ajax_sync_untracked_resources()
44 {
45 if (!check_ajax_referer( 'woo_ml_post_nonce', 'nonce', false ) ) {
46 wp_send_json_error( 'Invalid security token sent.' );
47 wp_die();
48 }
49 if (defined('DOING_AJAX') && DOING_AJAX) {
50
51 $response = false;
52
53 try {
54 $shop_synced = SynchronizeSettings::getInstance()->syncUntrackedResources();
55 if (is_bool($shop_synced)) {
56 $response = true;
57 } else {
58 $response = $shop_synced;
59 }
60 $responseObj = json_decode($response);
61 if(isset($responseObj->allDone) && $responseObj->allDone) {
62 if((int)get_option('woo_ml_wizard_setup', 0) !== 2) {
63 update_option('woo_ml_wizard_setup', 2);
64 woo_ml_setup_integration();
65 }
66 }
67 echo $response;
68 } catch (\Exception $e) {
69 return true;
70 }
71
72 }
73 exit;
74 }
75
76 add_action('wp_ajax_post_woo_ml_sync_untracked_resources', 'woo_ml_admin_ajax_sync_untracked_resources');
77
78 /**
79 * Is called when the user presses the Reset resources sync button in the plugin admin settings
80 */
81 function woo_ml_reset_resources_sync()
82 {
83 if (!check_ajax_referer( 'woo_ml_post_nonce', 'nonce', false ) ) {
84 wp_send_json_error( 'Invalid security token sent.' );
85 wp_die();
86 }
87 if (isset($_POST['cancelSync'])) {
88
89 update_option('woo_ml_wizard_setup', 1);
90 $settings = get_option('woocommerce_mailerlite_settings', []);
91 if(isset($settings['consumer_key'])) {
92 unset($settings['consumer_key']);
93 }
94 if(isset($settings['consumer_secret'])) {
95 unset($settings['consumer_secret']);
96 }
97 update_option('woocommerce_mailerlite_settings',
98 apply_filters('woocommerce_settings_api_sanitized_fields_mailerlite', $settings), 'yes');
99 }
100 ResetSettings::getInstance()->resetTrackedResources();
101 }
102
103 add_action('wp_ajax_post_woo_ml_reset_resources_sync', 'woo_ml_reset_resources_sync');
104
105 function woo_ml_email_cookie()
106 {
107
108 if (defined('DOING_AJAX') && DOING_AJAX) {
109 try {
110 $email = $_POST['email'] ?? null;
111 $subscribe = isset($_POST['signup']) ? ('true' == $_POST['signup']) : null;
112 $language = $_POST['language'] ?? '';
113
114 $subscriber_fields = [];
115
116 $first_name = $_POST['first_name'] ?? '';
117 $last_name = $_POST['last_name'] ?? '';
118
119 if ( ! empty($first_name)) {
120 $subscriber_fields['name'] = $first_name;
121 }
122
123 if ( ! empty($last_name)) {
124 $subscriber_fields['last_name'] = $last_name;
125 }
126
127 if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
128 // set accepts marketing cookie only when on checkout
129 if (isset($_POST['signup'])) {
130 @setcookie('mailerlite_accepts_marketing', $subscribe, time() + 172800, '/');
131 }
132
133 //setting email cookie and cart token for two days
134 @setcookie('mailerlite_checkout_email', $email, time() + 172800, '/');
135 // get token cookie from request so it remains consistent
136 if (isset($_POST['cookie_mailerlite_checkout_token'])) {
137 $_COOKIE['mailerlite_checkout_token'] = $_POST['cookie_mailerlite_checkout_token'];
138 } else {
139 $token = md5(uniqid(rand(), true));
140 @setcookie('mailerlite_checkout_token', $token, time() + 172800, '/');
141 $_COOKIE['mailerlite_checkout_token'] = $token;
142 }
143
144 if (get_option('mailerlite_disable_checkout_sync') == false) {
145 CartProcess::getInstance()->sendCart($email, $subscribe, $language, $subscriber_fields);
146 }
147 }
148 } catch (\Exception $e) {
149 return true;
150 }
151 }
152 exit;
153 }
154
155 add_action('wp_ajax_nopriv_post_woo_ml_email_cookie', 'woo_ml_email_cookie');
156 add_action('wp_ajax_post_woo_ml_email_cookie', 'woo_ml_email_cookie');
157
158 function woo_ml_validate_key()
159 {
160 if (!check_ajax_referer( 'woo_ml_post_nonce', 'nonce', false ) ) {
161 wp_send_json_error( 'Invalid security token sent.' );
162 wp_die();
163 }
164 if (defined('DOING_AJAX') && DOING_AJAX) {
165 if ( ! empty($_POST['key'])) {
166 ApiSettings::getInstance()->validateApiKey($_POST['key']);
167 }
168 }
169 exit;
170 }
171
172 add_action('wp_ajax_nopriv_post_woo_ml_validate_key', 'woo_ml_validate_key');
173 add_action('wp_ajax_post_woo_ml_validate_key', 'woo_ml_validate_key');
174
175 /**
176 * Update hide checkbox setting
177 */
178 function woo_ml_admin_ajax_update_hide_checkbox()
179 {
180
181 if ( ! is_admin()) {
182 die();
183 }
184
185 if (defined('DOING_AJAX') && DOING_AJAX) {
186
187 if (isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ml-block-settings-update')) {
188
189 if (isset($_POST['hidden'])) {
190
191 $isHidden = filter_var($_POST['hidden'], FILTER_VALIDATE_BOOLEAN);
192
193 MailerLiteSettings::getInstance()->setOption('checkout_hide', $isHidden ? 'yes' : 'no');
194
195 wp_send_json_success([
196 'hidden' => $isHidden,
197 ], 200);
198 }
199 }
200
201 die();
202 }
203 }
204
205 add_action('wp_ajax_woo_ml_admin_ajax_update_hide_checkbox', 'woo_ml_admin_ajax_update_hide_checkbox');
206
207 /**
208 * Update preselect checkbox setting
209 */
210 function woo_ml_admin_ajax_update_preselect_checkbox()
211 {
212
213 if ( ! is_admin()) {
214 die();
215 }
216
217 if (defined('DOING_AJAX') && DOING_AJAX) {
218
219 if (isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ml-block-settings-update')) {
220
221 if (isset($_POST['preselect'])) {
222
223 $isPreselect = filter_var($_POST['preselect'], FILTER_VALIDATE_BOOLEAN);
224
225 MailerLiteSettings::getInstance()->setOption('checkout_preselect', $isPreselect ? 'yes' : 'no');
226
227 wp_send_json_success([
228 'preselect' => $isPreselect,
229 ], 200);
230 }
231 }
232
233 die();
234 }
235 }
236
237 /**
238 * Refresh groups
239 */
240 function woo_ml_admin_create_group()
241 {
242 if (!check_ajax_referer( 'woo_ml_post_nonce', 'nonce', false)) {
243 wp_send_json_error( 'Invalid security token sent.' );
244 wp_die();
245 }
246 if (defined('DOING_AJAX') && DOING_AJAX) {
247 if(!isset($_POST['createGroup'])) {
248 return false;
249 }
250 $mailerLiteSettings = MailerLiteSettings::getInstance();
251 if(in_array($_POST['createGroup'], $mailerLiteSettings->getGroupOptions())) {
252 wp_send_json([
253 'error' => 'Group name already exists'
254 ]);
255 }
256 wp_send_json(MailerLiteSettings::getInstance()->createGroup($_POST['createGroup']));
257 }
258 }
259
260 function woo_ml_admin_get_debug_log()
261 {
262 if (!check_ajax_referer( 'woo_ml_post_nonce', 'nonce', false)) {
263 wp_send_json_error( 'Invalid security token sent.' );
264 wp_die();
265 }
266 $errorPath = ini_get('error_log');
267 $lines = '';
268 if(!empty($errorPath)) {
269 $lines = `tail -500 {$errorPath}`;
270 }
271 if(!empty($lines)) {
272 wp_send_json(['success' => true, 'log' => nl2br($lines)]);
273 } else {
274 wp_send_json(['success' => false, 'log' => [], 'message' => "Couldn't fetch error log"]);
275
276 }
277 }
278
279 function woo_ml_reset_integration()
280 {
281 if (!check_ajax_referer( 'woo_ml_post_nonce', 'nonce', false)) {
282 wp_send_json_error( 'Invalid security token sent.' );
283 wp_die();
284 }
285 delete_option('woo_ml_shop_id');
286 delete_option('woo_ml_wizard_setup');
287 delete_option('woo_ml_key');
288 delete_option('ml_shop_not_active');
289 }
290
291 function woo_ml_admin_save_group()
292 {
293 if (!check_ajax_referer( 'woo_ml_post_nonce', 'nonce', false)) {
294 wp_send_json_error( 'Invalid security token sent.' );
295 wp_die();
296 }
297 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
298
299 $data = [
300 'group' => $_POST['group']
301 ];
302 if ($mailerliteClient->getApiType() == ApiType::CLASSIC) {
303
304 if (!empty($_POST['consumer_key']) && !empty($_POST['consumer_secret'])) {
305 $data['consumer_key'] = $_POST['consumer_key'];
306 $data['consumer_secret'] = $_POST['consumer_secret'];
307 } else {
308 wp_send_json([
309 'error' => true,
310 'message' => 'Consumer key or secret missing!'
311 ]);
312 }
313 }
314
315 $settings = array_merge(get_option('woocommerce_mailerlite_settings'), $data);
316 update_option('woocommerce_mailerlite_settings',
317 apply_filters('woocommerce_settings_api_sanitized_fields_mailerlite', $settings), 'yes');
318 if(get_transient('invalid_consumer_keys')) {
319 wp_send_json([
320 'error' => true,
321 'message' => get_transient('invalid_consumer_keys')
322 ]);
323 delete_transient('invalid_consumer_keys');
324 }
325 wp_send_json([
326 'error' => false,
327 'message' => 'Group Saved successfully'
328 ]);
329 }
330
331
332 add_action('wp_ajax_woo_ml_admin_ajax_update_preselect_checkbox', 'woo_ml_admin_ajax_update_preselect_checkbox');
333
334 add_action('wp_ajax_woo_ml_create_group', 'woo_ml_admin_create_group');
335 add_action('wp_ajax_woo_ml_get_debug_log', 'woo_ml_admin_get_debug_log');
336 add_action('wp_ajax_woo_ml_reset_integration', 'woo_ml_reset_integration');
337 add_action('wp_ajax_woo_ml_save_group', 'woo_ml_admin_save_group');
338