PluginProbe
MailerLite – WooCommerce integration / 1.8.7
MailerLite – WooCommerce integration v1.8.7
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 1.8.7, at includes/admin/ajax.php

194 lines 5.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
9 /**
10 * Ajax
11 */
12
13 // Exit if accessed directly
14 if ( ! defined('ABSPATH')) {
15 exit;
16 }
17
18 /**
19 * Refresh groups
20 */
21 function woo_ml_admin_ajax_refresh_groups()
22 {
23 if (defined('DOING_AJAX') && DOING_AJAX) {
24 $mailerLiteSettings = MailerLiteSettings::getInstance();
25 wp_send_json([
26 'groups' => $mailerLiteSettings->getGroupOptions(),
27 'current' => $mailerLiteSettings->getMlOption('group')
28 ]);
29 }
30 }
31
32 add_action('wp_ajax_nopriv_post_woo_ml_refresh_groups', 'woo_ml_admin_ajax_refresh_groups');
33 add_action('wp_ajax_post_woo_ml_refresh_groups', 'woo_ml_admin_ajax_refresh_groups');
34
35 /**
36 * Sync Customers
37 */
38 function woo_ml_admin_ajax_sync_untracked_resources()
39 {
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
53 echo $response;
54 } catch (\Exception $e) {
55 return true;
56 }
57
58 }
59 exit;
60 }
61
62 add_action('wp_ajax_post_woo_ml_sync_untracked_resources', 'woo_ml_admin_ajax_sync_untracked_resources');
63
64 /**
65 * Is called when the user presses the Reset resources sync button in the plugin admin settings
66 */
67 function woo_ml_reset_resources_sync()
68 {
69
70 ResetSettings::getInstance()->resetTrackedResources();
71 }
72
73 add_action('wp_ajax_post_woo_ml_reset_resources_sync', 'woo_ml_reset_resources_sync');
74
75 function woo_ml_email_cookie()
76 {
77
78 if (defined('DOING_AJAX') && DOING_AJAX) {
79 try {
80 $email = $_POST['email'] ?? null;
81 $subscribe = isset($_POST['signup']) ? ('true' == $_POST['signup']) : null;
82 $language = $_POST['language'] ?? '';
83
84 $subscriber_fields = [];
85
86 $first_name = $_POST['first_name'] ?? '';
87 $last_name = $_POST['last_name'] ?? '';
88
89 if ( ! empty($first_name)) {
90 $subscriber_fields['name'] = $first_name;
91 }
92
93 if ( ! empty($last_name)) {
94 $subscriber_fields['last_name'] = $last_name;
95 }
96
97 if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
98
99 //setting email cookie and cart token for two days
100 @setcookie('mailerlite_checkout_email', $email, time() + 172800, '/');
101 if ( ! isset($_COOKIE['mailerlite_checkout_token'])) {
102 @setcookie('mailerlite_checkout_token', md5(uniqid(rand(), true)), time() + 172800, '/');
103 }
104
105 if (get_option('mailerlite_disable_checkout_sync') == false) {
106 CartProcess::getInstance()->sendCart($email, $subscribe, $language, $subscriber_fields);
107 }
108 }
109 } catch (\Exception $e) {
110 return true;
111 }
112 }
113 exit;
114 }
115
116 add_action('wp_ajax_nopriv_post_woo_ml_email_cookie', 'woo_ml_email_cookie');
117 add_action('wp_ajax_post_woo_ml_email_cookie', 'woo_ml_email_cookie');
118
119 function woo_ml_validate_key()
120 {
121 if (defined('DOING_AJAX') && DOING_AJAX) {
122 if ( ! empty($_POST['key'])) {
123 ApiSettings::getInstance()->validateApiKey($_POST['key']);
124 }
125 }
126 exit;
127 }
128
129 add_action('wp_ajax_nopriv_post_woo_ml_validate_key', 'woo_ml_validate_key');
130 add_action('wp_ajax_post_woo_ml_validate_key', 'woo_ml_validate_key');
131
132 /**
133 * Update hide checkbox setting
134 */
135 function woo_ml_admin_ajax_update_hide_checkbox()
136 {
137
138 if ( ! is_admin()) {
139 die();
140 }
141
142 if (defined('DOING_AJAX') && DOING_AJAX) {
143
144 if (isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ml-block-settings-update')) {
145
146 if (isset($_POST['hidden'])) {
147
148 $isHidden = filter_var($_POST['hidden'], FILTER_VALIDATE_BOOLEAN);
149
150 MailerLiteSettings::getInstance()->setOption('checkout_hide', $isHidden ? 'yes' : 'no');
151
152 wp_send_json_success([
153 'hidden' => $isHidden,
154 ], 200);
155 }
156 }
157
158 die();
159 }
160 }
161
162 add_action('wp_ajax_woo_ml_admin_ajax_update_hide_checkbox', 'woo_ml_admin_ajax_update_hide_checkbox');
163
164 /**
165 * Update preselect checkbox setting
166 */
167 function woo_ml_admin_ajax_update_preselect_checkbox()
168 {
169
170 if ( ! is_admin()) {
171 die();
172 }
173
174 if (defined('DOING_AJAX') && DOING_AJAX) {
175
176 if (isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ml-block-settings-update')) {
177
178 if (isset($_POST['preselect'])) {
179
180 $isPreselect = filter_var($_POST['preselect'], FILTER_VALIDATE_BOOLEAN);
181
182 MailerLiteSettings::getInstance()->setOption('checkout_preselect', $isPreselect ? 'yes' : 'no');
183
184 wp_send_json_success([
185 'preselect' => $isPreselect,
186 ], 200);
187 }
188 }
189
190 die();
191 }
192 }
193
194 add_action('wp_ajax_woo_ml_admin_ajax_update_preselect_checkbox', 'woo_ml_admin_ajax_update_preselect_checkbox');