PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.2
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.2
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 5.6.2 All 33 releases
automatorwp / integrations / mailchimp / includes / functions.php

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

423 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions
4 *
5 * @package AutomatorWP\Mailchimp\Functions
6 * @since 1.0.0
7 */
8 // Exit if accessed directly
9 if( !defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Get authorization from Mailchimp
13 *
14 * @since 1.0.0
15 *
16 * @return MailchimpMarketing\ApiClient|false
17 */
18 function automatorwp_mailchimp_get_authorization( ) {
19
20 $api_key = automatorwp_mailchimp_get_option( 'api_key', '' );
21 $server_prefix = automatorwp_mailchimp_get_option( 'server_prefix', '' );
22
23 if( empty( $api_key ) || empty( $server_prefix ) ) {
24 return false;
25 }
26
27 // Mailchimp API
28 require_once AUTOMATORWP_MAILCHIMP_DIR . 'vendor/autoload.php';
29
30 $mailchimp = new MailchimpMarketing\ApiClient();
31
32 $mailchimp->setConfig([
33 'apiKey' => $api_key,
34 'server' => $server_prefix
35 ]);
36
37 return $mailchimp;
38
39 }
40
41
42 /**
43 * Get lists/audiences from Mailchimp
44 *
45 * @since 1.0.0
46 *
47 * @param stdClass $field
48 *
49 * @return array
50 */
51 function automatorwp_mailchimp_options_cb_lists( $field ) {
52
53 // Setup vars
54 $value = $field->escaped_value;
55 $none_value = 'any';
56 $none_label = __( 'any list', 'automatorwp' );
57 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
58
59 if( ! empty( $value ) ) {
60 if( ! is_array( $value ) ) {
61 $value = array( $value );
62 }
63
64 foreach( $value as $list_id ) {
65
66 // Skip option none
67 if( $list_id === $none_value ) {
68 continue;
69 }
70
71 $options[$list_id] = automatorwp_mailchimp_get_list_name( $list_id );
72 }
73 }
74
75 return $options;
76
77 }
78
79 /**
80 * Get the list/audience name
81 *
82 * @since 1.0.0
83 *
84 * @param string $list_id
85 *
86 * @return array
87 */
88 function automatorwp_mailchimp_get_list_name( $list_id ) {
89
90 $audiences = array();
91
92 $mailchimp = automatorwp_mailchimp_get_authorization( );
93
94 // Bail if no authorization
95 if ( ! $mailchimp ){
96 return;
97 }
98
99 $list = $mailchimp->lists->getList( $list_id );
100
101 return $list->name;
102 }
103
104
105
106 /**
107 * Get the lists/audiences
108 *
109 * @since 1.0.0
110 *
111 * @return array
112 */
113 function automatorwp_mailchimp_get_lists() {
114
115 $audiences = array();
116
117 $mailchimp = automatorwp_mailchimp_get_authorization();
118
119 // Bail if no authorization
120 if ( ! $mailchimp ){
121 return;
122 }
123
124 $all_lists = $mailchimp->lists->getAllLists();
125
126 foreach ( $all_lists->lists as $list ){
127
128 $audiences[] = array(
129 'id' => $list->id,
130 'name' => $list->name,
131 );
132
133 }
134
135 return $audiences;
136 }
137
138 /**
139 * Options callback for select2 fields assigned to tags
140 *
141 * @since 1.0.0
142 *
143 * @param stdClass $field
144 *
145 * @return array
146 */
147 function automatorwp_mailchimp_options_cb_tags( $field ) {
148
149 // Setup vars
150 $value = $field->escaped_value;
151 $none_value = 'any';
152 $none_label = __( 'any tag', 'automatorwp' );
153 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
154
155 $list_id = ct_get_object_meta( $field->object_id, 'audience', true );
156
157
158 if( ! empty( $value ) ) {
159 if( ! is_array( $value ) ) {
160 $value = array( $value );
161 }
162
163 foreach( $value as $tag_id ) {
164
165 // Skip option none
166 if( $tag_id === $none_value ) {
167 continue;
168 }
169
170 $options[$tag_id] = automatorwp_mailchimp_get_tag_name( $list_id, $tag_id );
171 }
172 }
173
174 return $options;
175
176 }
177
178 /**
179 * Get tags from Mailchimp
180 *
181 * @since 1.0.0
182 *
183 * @return array
184 */
185 function automatorwp_mailchimp_get_tags( $list_id ) {
186
187 $tags = array();
188 $mailchimp = automatorwp_mailchimp_get_authorization();
189
190 // Bail if no authorization
191 if ( ! $mailchimp ) {
192 return array();
193 }
194
195 $all_tags = $mailchimp->lists->listSegments( $list_id, $fields = null, $exclude_fields = null, $count = '1000', $offset = '0', $type = 'static' );
196
197 foreach ( $all_tags->segments as $tag ){
198 $tags[] = array(
199 'id' => $tag->id,
200 'name' => $tag->name,
201 );
202 }
203
204 return $tags;
205 }
206
207 /**
208 * Get the tag names
209 *
210 * @since 1.0.0
211 *
212 * @param string $list_id
213 *
214 * @return string
215 */
216 function automatorwp_mailchimp_get_tag_name( $list_id, $tag_id ) {
217
218 $mailchimp = automatorwp_mailchimp_get_authorization();
219
220 // Bail if no authorization
221 if ( ! $mailchimp ){
222 return '';
223 }
224
225 $all_tags = $mailchimp->lists->listSegments( $list_id, $fields = null, $exclude_fields = null, $count = '1000', $offset = '0', $type = 'static' );
226
227 foreach ( $all_tags->segments as $tag ){
228
229 if ( $tag_id == $tag->id ){
230 $tag_name = $tag->name;
231 break;
232 }
233 }
234
235 return $tag_name;
236
237 }
238
239 /**
240 * Options callback for select2 fields assigned to campaigns
241 *
242 * @since 1.0.0
243 *
244 * @param stdClass $field
245 *
246 * @return array
247 */
248 function automatorwp_mailchimp_options_cb_campaigns( $field ) {
249
250 // Setup vars
251 $value = $field->escaped_value;
252 $none_value = 'any';
253 $none_label = __( 'any campaign', 'automatorwp' );
254 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
255
256 if( ! empty( $value ) ) {
257 if( ! is_array( $value ) ) {
258 $value = array( $value );
259 }
260
261 foreach( $value as $campaign_id ) {
262
263 // Skip option none
264 if( $campaign_id === $none_value ) {
265 continue;
266 }
267
268 $options[$campaign_id] = automatorwp_mailchimp_get_campaign_name( $campaign_id );
269 }
270 }
271
272 return $options;
273
274 }
275
276 /**
277 * Get the list of campaigns
278 *
279 * @since 1.0.0
280 *
281 * @return array
282 */
283 function automatorwp_mailchimp_get_campaigns() {
284
285 $options = array();
286
287 $mailchimp = automatorwp_mailchimp_get_authorization();
288
289 // Bail if no authorization
290 if ( ! $mailchimp ){
291 return;
292 }
293
294 $all_campaigns = $mailchimp->campaigns->list();
295
296 foreach ( $all_campaigns->campaigns as $campaign ){
297
298 $options[] = array(
299 'id' => $campaign->id,
300 'name' => $campaign->settings->title,
301 );
302
303 }
304
305 return $options;
306 }
307
308 /**
309 * Get the campaign names
310 *
311 * @since 1.0.0
312 *
313 * @param string $list_id
314 *
315 * @return array
316 */
317 function automatorwp_mailchimp_get_campaign_name( $campaign_id ) {
318
319 $mailchimp = automatorwp_mailchimp_get_authorization();
320
321 // Bail if no authorization
322 if ( ! $mailchimp ){
323 return;
324 }
325
326 $campaign_name = $mailchimp->campaigns->get( $campaign_id );
327
328 return $campaign_name->settings->title;
329
330 }
331
332 /**
333 * Options callback for select2 fields assigned to campaigns
334 *
335 * @since 1.0.0
336 *
337 * @param stdClass $field
338 *
339 * @return array
340 */
341 function automatorwp_mailchimp_options_cb_templates( $field ) {
342
343 // Setup vars
344 $value = $field->escaped_value;
345 $none_value = 'any';
346 $none_label = __( 'any template', 'automatorwp' );
347 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
348
349 if( ! empty( $value ) ) {
350 if( ! is_array( $value ) ) {
351 $value = array( $value );
352 }
353
354 foreach( $value as $template_id ) {
355
356 // Skip option none
357 if( $template_id === $none_value ) {
358 continue;
359 }
360
361 $options[$template_id] = automatorwp_mailchimp_get_template_name( $template_id );
362 }
363 }
364
365 return $options;
366
367 }
368
369 /**
370 * Get the list of campaigns
371 *
372 * @since 1.0.0
373 *
374 * @return array
375 */
376 function automatorwp_mailchimp_get_templates() {
377
378 $options = array();
379
380 $mailchimp = automatorwp_mailchimp_get_authorization();
381
382 // Bail if no authorization
383 if ( ! $mailchimp ){
384 return;
385 }
386
387 $all_templates = $mailchimp->templates->list();
388
389 foreach ( $all_templates->templates as $template ){
390
391 $options[] = array(
392 'id' => $template->id,
393 'name' => $template->name,
394 );
395
396 }
397
398 return $options;
399 }
400
401 /**
402 * Get the campaign names
403 *
404 * @since 1.0.0
405 *
406 * @param string $list_id
407 *
408 * @return array
409 */
410 function automatorwp_mailchimp_get_template_name( $template_id ) {
411
412 $mailchimp = automatorwp_mailchimp_get_authorization();
413
414 // Bail if no authorization
415 if ( ! $mailchimp ){
416 return;
417 }
418
419 $template = $mailchimp->templates->getTemplate( $template_id );
420
421 return $template->name;
422
423 }