PluginProbe
MailerLite – WooCommerce integration / 2.0.8
MailerLite – WooCommerce integration v2.0.8
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.8, at includes/admin/ajax.php

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