PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.1
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.1
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / integrations / mailchimp / includes / ajax-functions.php

ajax-functions.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.1, at integrations/mailchimp/includes/ajax-functions.php

296 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 * Ajax Functions
4 *
5 * @package AutomatorWP\Integrations\Mailchimp\Ajax_Functions
6 * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Handler to save OAuth credentials
14 *
15 * @since 1.0.0
16 */
17 function automatorwp_mailchimp_ajax_save_oauth_credentials() {
18
19 // Security check, forces to die if not security passed
20 check_ajax_referer( 'automatorwp_admin', 'nonce' );
21
22 // Permissions check
23 if( ! current_user_can( automatorwp_get_manager_capability() ) ) {
24 wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) );
25 }
26
27 $prefix = "automatorwp_mailchimp_";
28
29 /* sanitize incoming data */
30 $api_key = sanitize_text_field( $_POST["api_key"] );
31 $server_prefix = sanitize_text_field( $_POST["server_prefix"] );
32
33 if( $api_key == '' && $server_prefix == '' ) {
34 // return error one of the field is missing
35
36 wp_send_json_error();
37 } else {
38 try {
39
40 // Mailchimp API
41 require_once AUTOMATORWP_MAILCHIMP_DIR . 'vendor/autoload.php';
42
43 $mailchimp = new MailchimpMarketing\ApiClient();
44
45 $mailchimp->setConfig([
46 'apiKey' => $api_key,
47 'server' => $server_prefix
48 ]);
49
50 $response = $mailchimp->ping->get();
51
52 $credentials = get_option( 'automatorwp_settings' );
53
54 $credentials[$prefix . 'api_key'] = $api_key;
55 $credentials[$prefix . 'server_prefix'] = $server_prefix;
56 $credentials[$prefix . 'access_valid'] = true;
57 $credentials = array_filter( $credentials );
58
59 update_option( 'automatorwp_settings', $credentials );
60 wp_send_json_success();
61
62 } catch(Exception $e) {
63
64 wp_send_json_error();
65 }
66 }
67
68 }
69 add_action( 'wp_ajax_automatorwp_mailchimp_save_oauth_credentials', 'automatorwp_mailchimp_ajax_save_oauth_credentials' );
70
71 /**
72 * Handler to delete OAuth credentials
73 *
74 * @since 1.0.0
75 */
76 function automatorwp_mailchimp_ajax_delete_oauth_credentials() {
77
78 // Security check, forces to die if not security passed
79 check_ajax_referer( 'automatorwp_admin', 'nonce' );
80
81 // Permissions check
82 if( ! current_user_can( automatorwp_get_manager_capability() ) ) {
83 wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) );
84 }
85
86 $prefix = "automatorwp_mailchimp_";
87 $credentials = get_option( 'automatorwp_settings' );
88
89 $credentials[$prefix . 'api_key'] = null;
90 $credentials[$prefix . 'server_prefix'] = null;
91 $credentials[$prefix . 'access_valid'] = false;
92 $credentials = array_filter( $credentials );
93
94 update_option( 'automatorwp_settings', $credentials );
95
96 wp_send_json_success();
97
98 }
99 add_action( 'wp_ajax_automatorwp_mailchimp_delete_oauth_credentials', 'automatorwp_mailchimp_ajax_delete_oauth_credentials' );
100
101
102 /**
103 * Ajax function for selecting lists
104 *
105 * @since 1.0.0
106 */
107 function automatorwp_mailchimp_ajax_get_lists() {
108 // Security check
109 check_ajax_referer( 'automatorwp_admin', 'nonce' );
110
111 // Permissions check
112 if( ! current_user_can( automatorwp_get_manager_capability() ) ) {
113 wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) );
114 }
115
116 global $wpdb;
117
118 // Pull back the search string
119 $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : '';
120
121 $lists = automatorwp_mailchimp_get_lists();
122
123 $results = array();
124
125 // Parse lists results to match select2 results
126 foreach ( $lists as $list ) {
127
128 if( ! empty( $search ) ) {
129 if( strpos( strtolower( $list['name'] ), strtolower( $search ) ) === false ) {
130 continue;
131 }
132 }
133
134 $results[] = array(
135 'id' => $list['id'],
136 'text' => $list['name']
137 );
138 }
139
140 // Prepend option none
141 $results = automatorwp_ajax_parse_extra_options( $results );
142
143 // Return our results
144 wp_send_json_success( $results );
145 die;
146
147 }
148 add_action( 'wp_ajax_automatorwp_mailchimp_get_lists', 'automatorwp_mailchimp_ajax_get_lists' );
149
150 /**
151 * Ajax function for selecting tags
152 *
153 * @since 1.0.0
154 */
155 function automatorwp_mailchimp_ajax_get_tags() {
156 // Security check
157 check_ajax_referer( 'automatorwp_admin', 'nonce' );
158
159 // Permissions check
160 if( ! current_user_can( automatorwp_get_manager_capability() ) ) {
161 wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) );
162 }
163
164 global $wpdb;
165
166 // Pull back the search string
167 $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : '';
168
169 // Get Audience ID
170 $list_id = isset( $_REQUEST['table'] ) ? sanitize_text_field( $_REQUEST['table'] ) : '';
171
172 $tags = automatorwp_mailchimp_get_tags( $list_id );
173
174 $results = array();
175
176 // Parse tag results to match select2 results
177 foreach ( $tags as $tag ) {
178
179 if( ! empty( $search ) ) {
180 if( strpos( strtolower( $tag['name'] ), strtolower( $search ) ) === false ) {
181 continue;
182 }
183 }
184
185 $results[] = array(
186 'id' => $tag['id'],
187 'text' => $tag['name']
188 );
189 }
190
191 // Prepend option none
192 $results = automatorwp_ajax_parse_extra_options( $results );
193
194 // Return our results
195 wp_send_json_success( $results );
196 die;
197
198 }
199 add_action( 'wp_ajax_automatorwp_mailchimp_get_tags', 'automatorwp_mailchimp_ajax_get_tags' );
200
201 /**
202 * Ajax function for selecting campaigns
203 *
204 * @since 1.0.0
205 */
206 function automatorwp_mailchimp_ajax_get_campaigns() {
207 // Security check
208 check_ajax_referer( 'automatorwp_admin', 'nonce' );
209
210 // Permissions check
211 if( ! current_user_can( automatorwp_get_manager_capability() ) ) {
212 wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) );
213 }
214
215 global $wpdb;
216
217 // Pull back the search string
218 $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : '';
219
220 $campaigns = automatorwp_mailchimp_get_campaigns();
221
222 $results = array();
223
224 // Parse campaigns results to match select2 results
225 foreach ( $campaigns as $campaign ) {
226
227 if( ! empty( $search ) ) {
228 if( strpos( strtolower( $campaign['name'] ), strtolower( $search ) ) === false ) {
229 continue;
230 }
231 }
232
233 $results[] = array(
234 'id' => $campaign['id'],
235 'text' => $campaign['name']
236 );
237 }
238
239 // Prepend option none
240 $results = automatorwp_ajax_parse_extra_options( $results );
241
242 // Return our results
243 wp_send_json_success( $results );
244 die;
245
246 }
247 add_action( 'wp_ajax_automatorwp_mailchimp_get_campaigns', 'automatorwp_mailchimp_ajax_get_campaigns' );
248
249
250 /**
251 * Ajax function for selecting templates
252 *
253 * @since 1.0.0
254 */
255 function automatorwp_mailchimp_ajax_get_templates() {
256 // Security check
257 check_ajax_referer( 'automatorwp_admin', 'nonce' );
258
259 // Permissions check
260 if( ! current_user_can( automatorwp_get_manager_capability() ) ) {
261 wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) );
262 }
263
264 global $wpdb;
265
266 // Pull back the search string
267 $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : '';
268
269 $templates = automatorwp_mailchimp_get_templates();
270
271 $results = array();
272
273 // Parse templates results to match select2 results
274 foreach ( $templates as $template ) {
275
276 if( ! empty( $search ) ) {
277 if( strpos( strtolower( $template['name'] ), strtolower( $search ) ) === false ) {
278 continue;
279 }
280 }
281
282 $results[] = array(
283 'id' => $template['id'],
284 'text' => $template['name']
285 );
286 }
287
288 // Prepend option none
289 $results = automatorwp_ajax_parse_extra_options( $results );
290
291 // Return our results
292 wp_send_json_success( $results );
293 die;
294
295 }
296 add_action( 'wp_ajax_automatorwp_mailchimp_get_templates', 'automatorwp_mailchimp_ajax_get_templates' );