PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 5.8.6
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v5.8.6
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 / includes / utilities.php

utilities.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 5.8.6, at includes/utilities.php

1,924 lines 59.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Utilities
4 *
5 * @package AutomatorWP\Utilities
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 * Utility function to get the times option parameter
14 *
15 * @since 1.0.0
16 *
17 * @return array
18 */
19 function automatorwp_utilities_times_option() {
20 return array(
21 'from' => 'times',
22 'fields' => array(
23 'times' => array(
24 'name' => __( 'Number of times:', 'automatorwp' ),
25 'type' => 'text',
26 'attributes' => array(
27 'type' => 'number',
28 ),
29 'sanitization_cb' => 'automatorwp_times_option_sanitization_cb',
30 'default' => 1
31 )
32 )
33 );
34 }
35
36 /**
37 * Handles sanitization for the times option field. Ensures a field's value is greater than 0.
38 *
39 * @since 1.0.0
40 *
41 * @param mixed $value The unsanitized value from the form.
42 * @param array $field_args Array of field arguments.
43 * @param CMB2_Field $field The field object
44 *
45 * @return int Sanitized value to be stored.
46 */
47 function automatorwp_times_option_sanitization_cb( $value, $field_args, $field ) {
48
49 // Prevent incorrect values or numbers less than 1
50 if ( ! is_numeric( $value ) || $value < 1 ) {
51 $sanitized_value = 1;
52 } else {
53 $sanitized_value = absint( $value );
54 }
55
56 return $sanitized_value;
57
58 }
59
60 /**
61 * Utility function to get the post option parameter
62 *
63 * @since 1.0.0
64 *
65 * @param array $args
66 *
67 * @return array
68 */
69 function automatorwp_utilities_post_option( $args = array() ) {
70
71 $args = automatorwp_utilities_parse_selector_args( $args, array(
72 'name' => __( 'Post:', 'automatorwp' ),
73 'post_type' => 'post',
74 'post_type_cb' => '',
75 'placeholder' => __( 'Select a post', 'automatorwp' ),
76 'option_none_label' => __( 'any post', 'automatorwp' ),
77 'option_custom_desc' => __( 'Post ID', 'automatorwp' ),
78 ) );
79
80 $option = array(
81 'from' => 'post',
82 'default' => ( isset( $args['option_default'] ) ? $args['option_default'] : '' ),
83 'fields' => array(
84 'post' => automatorwp_utilities_post_field( $args )
85 )
86 );
87
88 // Add the custom field
89 if( $args['option_custom'] ) {
90 $option['fields']['post_custom'] = automatorwp_utilities_custom_field( $args );
91 }
92
93 return $option;
94
95 }
96
97 /**
98 * Utility function to get a post field parameters
99 *
100 * @since 1.0.0
101 *
102 * @param array $args
103 *
104 * @return array
105 */
106 function automatorwp_utilities_post_field( $args = array() ) {
107
108 $args = automatorwp_utilities_parse_selector_args( $args, array(
109 'name' => __( 'Post:', 'automatorwp' ),
110 'post_type' => 'post',
111 'post_type_cb' => '',
112 'placeholder' => __( 'Select a post', 'automatorwp' ),
113 'option_none_label' => __( 'any post', 'automatorwp' ),
114 'option_custom_desc' => __( 'Post ID', 'automatorwp' ),
115 ) );
116
117 if( ! is_array( $args['post_type'] ) ) {
118 $args['post_type'] = array( $args['post_type'] );
119 }
120
121 $attributes = automatorwp_utilities_get_selector_attributes( $args );
122 $attributes['data-post-type'] = implode(',', $args['post_type'] );
123 $attributes['data-post-type-cb'] = $args['post_type_cb'];
124
125 return array(
126 'name' => $args['name'],
127 'desc' => $args['desc'],
128 'type' => ( $args['multiple'] ? 'automatorwp_select' : 'select' ),
129 'classes' => 'automatorwp-post-selector',
130 'option_none' => $args['option_none'],
131 'option_none_value' => $args['option_none_value'],
132 'option_none_label' => $args['option_none_label'],
133 'option_custom' => $args['option_custom'],
134 'option_custom_value' => $args['option_custom_value'],
135 'option_custom_label' => $args['option_custom_label'],
136 'post_type_cb' => '',
137 'attributes' => $attributes,
138 'options_cb' => 'automatorwp_options_cb_posts',
139 'default' => $args['default']
140 );
141
142 }
143
144 /**
145 * Utility function to get the term option parameter
146 *
147 * @since 1.0.0
148 *
149 * @param array $args
150 *
151 * @return array
152 */
153 function automatorwp_utilities_term_option( $args = array() ) {
154
155 $args = automatorwp_utilities_parse_selector_args( $args, array(
156 'name' => __( 'Category:', 'automatorwp' ),
157 'taxonomy' => 'category',
158 'placeholder' => __( 'Select a category', 'automatorwp' ),
159 'option_none_label' => __( 'any category', 'automatorwp' ),
160 'option_custom_desc' => __( 'Category ID', 'automatorwp' ),
161 ) );
162
163 $option = array(
164 'from' => 'term',
165 'default' => ( isset( $args['option_default'] ) ? $args['option_default'] : '' ),
166 'fields' => array(
167 'term' => automatorwp_utilities_term_field( $args )
168 )
169 );
170
171 // Add the custom field
172 if( $args['option_custom'] ) {
173 $option['fields']['term_custom'] = automatorwp_utilities_custom_field( $args );
174 }
175
176 return $option;
177
178 }
179
180 /**
181 * Utility function to get a term field parameters
182 *
183 * @since 1.0.0
184 *
185 * @param array $args
186 *
187 * @return array
188 */
189 function automatorwp_utilities_term_field( $args = array() ) {
190
191 $args = automatorwp_utilities_parse_selector_args( $args, array(
192 'name' => __( 'Category:', 'automatorwp' ),
193 'taxonomy' => 'category',
194 'placeholder' => __( 'Select a category', 'automatorwp' ),
195 'option_none_label' => __( 'any category', 'automatorwp' ),
196 'option_custom_desc' => __( 'Category ID', 'automatorwp' ),
197 ) );
198
199 $attributes = automatorwp_utilities_get_selector_attributes( $args );
200 $attributes['data-taxonomy'] = $args['taxonomy'];
201
202 return array(
203 'name' => $args['name'],
204 'desc' => $args['desc'],
205 'type' => ( $args['multiple'] ? 'automatorwp_select' : 'select' ),
206 'classes' => 'automatorwp-term-selector',
207 'option_none' => $args['option_none'],
208 'option_none_value' => $args['option_none_value'],
209 'option_none_label' => $args['option_none_label'],
210 'option_custom' => $args['option_custom'],
211 'option_custom_value' => $args['option_custom_value'],
212 'option_custom_label' => $args['option_custom_label'],
213 'taxonomy' => $args['taxonomy'],
214 'attributes' => $attributes,
215 'options_cb' => 'automatorwp_options_cb_terms',
216 'default' => $args['default']
217 );
218
219 }
220
221 /**
222 * Utility function to get the taxonomy option parameter
223 *
224 * @since 1.0.0
225 *
226 * @param array $args
227 *
228 * @return array
229 */
230 function automatorwp_utilities_taxonomy_option( $args = array() ) {
231
232 $args = automatorwp_utilities_parse_selector_args( $args, array(
233 'name' => __( 'Taxonomy:', 'automatorwp' ),
234 'option_default' => __( 'any taxonomy', 'automatorwp' ),
235 'multiple' => false,
236 'placeholder' => __( 'Select a taxonomy', 'automatorwp' ),
237 'option_none_label' => __( 'any taxonomy', 'automatorwp' ),
238 'option_custom_desc' => __( 'Taxonomy', 'automatorwp' ),
239 ) );
240
241 $attributes = automatorwp_utilities_get_selector_attributes( $args );
242
243 $term_args = $args;
244
245 $term_args['name'] = __( 'Term:', 'automatorwp' );
246 $term_args['option_none_label'] = __( 'any term', 'automatorwp' );
247 $term_args['option_custom_desc'] = __( 'Term ID', 'automatorwp' );
248
249 return array(
250 'from' => 'term',
251 'default' => $args['option_default'],
252 'fields' => array(
253 'taxonomy' => array(
254 'name' => $args['name'],
255 'desc' => $args['desc'],
256 'type' => ( $args['multiple'] ? 'automatorwp_select' : 'select' ),
257 'classes' => 'automatorwp-taxonomy-selector',
258 'option_none' => $args['option_none'],
259 'option_none_value' => $args['option_none_value'],
260 'option_none_label' => $args['option_none_label'],
261 'option_custom' => $args['option_custom'],
262 'option_custom_value' => $args['option_custom_value'],
263 'option_custom_label' => $args['option_custom_label'],
264 'attributes' => $attributes,
265 'options_cb' => 'automatorwp_options_cb_taxonomies',
266 'default' => $args['default']
267 ),
268 'term' => automatorwp_utilities_term_field( $term_args )
269 )
270 );
271
272 }
273
274 /**
275 * Utility function to get ajax selector option parameter
276 *
277 * @since 1.0.0
278 *
279 * @param array $args
280 *
281 * @return array
282 */
283 function automatorwp_utilities_ajax_selector_option( $args = array() ) {
284
285 $args = automatorwp_utilities_parse_selector_args( $args, array(
286 'field' => 'ajax_options',
287 'action_cb' => '',
288 ) );
289
290 $option = array(
291 'from' => $args['field'],
292 'default' => $args['option_default'],
293 'fields' => array(
294 $args['field'] => automatorwp_utilities_ajax_selector_field( $args )
295 )
296 );
297
298 // Add the custom field
299 if( $args['option_custom'] ) {
300 $option['fields'][$args['field'] . '_custom'] = automatorwp_utilities_custom_field( $args );
301 }
302
303 return $option;
304
305 }
306
307 /**
308 * Utility function to get ajax selector field
309 *
310 * @since 1.0.0
311 *
312 * @param array $args
313 *
314 * @return array
315 */
316 function automatorwp_utilities_ajax_selector_field( $args = array() ) {
317
318 $args = automatorwp_utilities_parse_selector_args( $args, array(
319 'field' => 'ajax_options',
320 'action_cb' => '',
321 ) );
322
323 $attributes = automatorwp_utilities_get_selector_attributes( $args );
324 $attributes['data-action'] = $args['action_cb'];
325
326 return array(
327 'name' => $args['name'],
328 'desc' => $args['desc'],
329 'type' => ( $args['multiple'] ? 'automatorwp_select' : 'select' ),
330 'classes' => 'automatorwp-ajax-selector',
331 'option_none' => $args['option_none'],
332 'option_none_value' => $args['option_none_value'],
333 'option_none_label' => $args['option_none_label'],
334 'option_custom' => $args['option_custom'],
335 'option_custom_value' => $args['option_custom_value'],
336 'option_custom_label' => $args['option_custom_label'],
337 'attributes' => $attributes,
338 'options_cb' => $args['options_cb'],
339 'default' => $args['default']
340 );
341
342 }
343
344 /**
345 * Utility function to get the automation option parameter
346 *
347 * @since 1.0.0
348 *
349 * @param array $args
350 *
351 * @return array
352 */
353 function automatorwp_utilities_automation_option( $args = array() ) {
354
355 $args = automatorwp_utilities_parse_selector_args( $args, array(
356 'name' => __( 'Automation:', 'automatorwp' ),
357 'option_none_label' => __( 'any automation', 'automatorwp' ),
358 'option_custom_desc' => __( 'Automation ID', 'automatorwp' ),
359 ) );
360
361 $attributes = automatorwp_utilities_get_selector_attributes( $args );
362 $attributes['data-table'] = 'automatorwp_automations';
363
364 $option = array(
365 'from' => 'automation',
366 'default' => $args['option_default'],
367 'fields' => array(
368 'automation' => array(
369 'name' => $args['name'],
370 'desc' => $args['desc'],
371 'type' => ( $args['multiple'] ? 'automatorwp_select' : 'select' ),
372 'classes' => 'automatorwp-object-selector',
373 'option_none' => $args['option_none'],
374 'option_none_value' => $args['option_none_value'],
375 'option_none_label' => $args['option_none_label'],
376 'option_custom' => $args['option_custom'],
377 'option_custom_value' => $args['option_custom_value'],
378 'option_custom_label' => $args['option_custom_label'],
379 'attributes' => $attributes,
380 'options_cb' => 'automatorwp_options_cb_objects',
381 'default' => $args['default']
382 )
383 )
384 );
385
386 // Add the custom field
387 if( $args['option_custom'] ) {
388 $option['fields']['automation_custom'] = automatorwp_utilities_custom_field( $args );
389 }
390
391 return $option;
392
393 }
394
395 /**
396 * Utility function to get the role option parameter
397 *
398 * @since 1.0.0
399 *
400 * @param array $args
401 *
402 * @return array
403 */
404 function automatorwp_utilities_role_option( $args = array() ) {
405
406 $args = automatorwp_utilities_parse_selector_args( $args, array(
407 'name' => __( 'Role:', 'automatorwp' ),
408 'placeholder' => __( 'Select a role', 'automatorwp' ),
409 'option_none_label' => __( 'any role', 'automatorwp' ),
410 'option_custom_desc' => __( 'Role name.', 'automatorwp' ),
411 ) );
412
413 $option = array(
414 'from' => 'role',
415 'default' => $args['option_default'],
416 'fields' => array(
417 'role' => automatorwp_utilities_role_field( $args )
418 )
419 );
420
421 // Add the custom field
422 if( $args['option_custom'] ) {
423 $option['fields']['role_custom'] = automatorwp_utilities_custom_field( $args );
424 }
425
426 return $option;
427
428 }
429
430 /**
431 * Utility function to get a role field parameters
432 *
433 * @since 1.0.0
434 *
435 * @param array $args
436 *
437 * @return array
438 */
439 function automatorwp_utilities_role_field( $args = array() ) {
440
441 $args = automatorwp_utilities_parse_selector_args( $args, array(
442 'name' => __( 'Role:', 'automatorwp' ),
443 'placeholder' => __( 'Select a role', 'automatorwp' ),
444 'option_none_label' => __( 'any role', 'automatorwp' ),
445 'option_custom_desc' => __( 'Role name.', 'automatorwp' ),
446 ) );
447
448 $attributes = automatorwp_utilities_get_selector_attributes( $args );
449
450 return array(
451 'name' => $args['name'],
452 'desc' => $args['desc'],
453 'type' => ( $args['multiple'] ? 'automatorwp_select' : 'select' ),
454 'classes' => 'automatorwp-selector',
455 'option_none' => $args['option_none'],
456 'option_none_value' => $args['option_none_value'],
457 'option_none_label' => $args['option_none_label'],
458 'option_custom' => $args['option_custom'],
459 'option_custom_value' => $args['option_custom_value'],
460 'option_custom_label' => $args['option_custom_label'],
461 'attributes' => $attributes,
462 'options_cb' => 'automatorwp_options_cb_roles',
463 'default' => $args['default']
464 );
465
466 }
467
468 /**
469 * Utility function to get a selector custom field parameters
470 *
471 * @since 1.3.7
472 *
473 * @param array $args
474 *
475 * @return array
476 */
477 function automatorwp_utilities_custom_field( $args = array() ) {
478
479 return array(
480 'name' => ( isset( $args['option_custom_name'] ) ? $args['option_custom_name'] : '' ),
481 'desc' => ( isset( $args['option_custom_desc'] ) ? $args['option_custom_desc'] : __( 'Post ID', 'automatorwp' ) ),
482 'type' => 'text',
483 'classes' => 'automatorwp-selector-custom-input',
484 );
485
486 }
487
488 /**
489 * Utility function to parse selector args
490 *
491 * @since 1.3.7
492 *
493 * @param array $args
494 * @param array $defaults
495 *
496 * @return array
497 */
498 function automatorwp_utilities_parse_selector_args( $args = array(), $defaults = array() ) {
499
500 $selector_defaults = array(
501 'name' => '',
502 'desc' => '',
503 'option_default' => '',
504 'multiple' => false,
505 'placeholder' => '',
506 'default' => 'any',
507 // Option none
508 'option_none' => true,
509 'option_none_value' => 'any',
510 'option_none_label' => '',
511 // Option custom
512 'option_custom' => false,
513 'option_custom_value' => 'custom',
514 'option_custom_label' => __( 'Use a custom value', 'automatorwp' ),
515 'option_custom_name' => '',
516 'option_custom_desc' => '',
517 );
518
519 $final_defaults = array_merge( $selector_defaults, $defaults );
520
521 $args = wp_parse_args( $args, $final_defaults );
522
523 return $args;
524
525 }
526
527 /**
528 * Utility function to get selector attributes
529 *
530 * @since 1.3.7
531 *
532 * @param array $args
533 * @param array $defaults
534 *
535 * @return array
536 */
537 function automatorwp_utilities_get_selector_attributes( $args = array() ) {
538
539 $args = automatorwp_utilities_parse_selector_args( $args, array() );
540
541 $attributes = array(
542 'data-placeholder' => $args['placeholder'],
543 // Option none
544 'data-option-none' => $args['option_none'],
545 'data-option-none-value' => $args['option_none_value'],
546 'data-option-none-label' => $args['option_none_label'],
547 // Option custom
548 'data-option-custom' => $args['option_custom'],
549 'data-option-custom-value' => $args['option_custom_value'],
550 'data-option-custom-label' => $args['option_custom_label'],
551 );
552
553 if( $args['multiple'] ) {
554 $attributes['multiple'] = true;
555 }
556
557 return $attributes;
558
559 }
560
561 /**
562 * Utility function to get the times tag
563 *
564 * @since 1.0.0
565 *
566 * @param bool $only_args Set to true to return only tag args
567 *
568 * @return array
569 */
570 function automatorwp_utilities_times_tag( $only_args = false ) {
571
572 $args = array(
573 'label' => __( 'Number of times', 'automatorwp' ),
574 'type' => 'integer',
575 'preview' => '1',
576 );
577
578 if( $only_args ) {
579 return $args;
580 }
581
582 return array(
583 'times' => $args
584 );
585 }
586
587 /**
588 * Utility function to get the post tags
589 *
590 * @since 1.0.0
591 *
592 * @return array
593 */
594 function automatorwp_utilities_post_tags( $post_label = '' ) {
595
596 if( empty( $post_label ) ) {
597 $post_label = __( 'Post', 'automatorwp' );
598 }
599
600 $site_url = get_option( 'home' );
601
602 /**
603 * Filter to setup custom post tags
604 *
605 * @since 1.0.0
606 *
607 * @param array $post_tags
608 *
609 * @return array
610 */
611 return apply_filters( 'automatorwp_utilities_post_tags', array(
612 'post_id' => array(
613 /* translators: %s: Post label (by default: Post). */
614 'label' => sprintf( __( '%s ID', 'automatorwp' ), $post_label ),
615 'type' => 'integer',
616 'preview' => '123',
617 ),
618 'post_title' => array(
619 /* translators: %s: Post label (by default: Post). */
620 'label' => sprintf( __( '%s Title', 'automatorwp' ), $post_label ),
621 'type' => 'text',
622 'preview' => __( 'The title', 'automatorwp' ),
623 ),
624 'post_url' => array(
625 /* translators: %s: Post label (by default: Post). */
626 'label' => sprintf( __( '%s URL', 'automatorwp' ), $post_label ),
627 'type' => 'text',
628 'preview' => $site_url . '/sample-' . strtolower( $post_label ),
629 ),
630 'post_link' => array(
631 /* translators: %s: Post label (by default: Post). */
632 'label' => sprintf( __( '%s Link', 'automatorwp' ), $post_label ),
633 'type' => 'text',
634 /* translators: %s: Post label (by default: Post). */
635 'preview' => '<a href="' . $site_url . '/sample-' . strtolower( $post_label ) . '">' . sprintf( __( '%s Title', 'automatorwp' ), $post_label ) . '</a>',
636 ),
637 'post_type' => array(
638 /* translators: %s: Post type (by default: post). */
639 'label' => sprintf( __( '%s Type', 'automatorwp' ), $post_label ),
640 'type' => 'text',
641 'preview' => 'post',
642 ),
643 'post_type_label' => array(
644 /* translators: %s: Post type label (by default: Post). */
645 'label' => sprintf( __( '%s Type Label', 'automatorwp' ), $post_label ),
646 'type' => 'text',
647 'preview' => 'post',
648 ),
649 'post_author' => array(
650 /* translators: %s: Post label (by default: Post). */
651 'label' => sprintf( __( '%s Author ID', 'automatorwp' ), $post_label ),
652 'type' => 'integer',
653 'preview' => '123',
654 ),
655 'post_author_email' => array(
656 /* translators: %s: Post label (by default: Post). */
657 'label' => sprintf( __( '%s Author Email', 'automatorwp' ), $post_label ),
658 'type' => 'email',
659 'preview' => '[email protected]',
660 ),
661 'post_content' => array(
662 /* translators: %s: Post label (by default: Post). */
663 'label' => sprintf( __( '%s Content', 'automatorwp' ), $post_label ),
664 'type' => 'text',
665 'preview' => __( 'The content', 'automatorwp' ),
666 ),
667 'post_excerpt' => array(
668 /* translators: %s: Post label (by default: Post). */
669 'label' => sprintf( __( '%s Excerpt', 'automatorwp' ), $post_label ),
670 'type' => 'text',
671 'preview' => __( 'The excerpt', 'automatorwp' ),
672 ),
673 'post_thumbnail' => array(
674 /* translators: %s: Post label (by default: Post). */
675 'label' => sprintf( __( '%s Featured Image', 'automatorwp' ), $post_label ),
676 'type' => 'text',
677 'preview' => '<img src="' . $site_url . '/sample-' . strtolower( $post_label ) . '-image"/>',
678 ),
679 'post_thumbnail_id' => array(
680 /* translators: %s: Post label (by default: Post). */
681 'label' => sprintf( __( '%s Featured Image ID', 'automatorwp' ), $post_label ),
682 'type' => 'integer',
683 'preview' => '123',
684 ),
685 'post_thumbnail_url' => array(
686 /* translators: %s: Post label (by default: Post). */
687 'label' => sprintf( __( '%s Featured Image URL', 'automatorwp' ), $post_label ),
688 'type' => 'text',
689 'preview' => $site_url . '/sample-' . strtolower( $post_label ) . '-image',
690 ),
691 'post_status' => array(
692 /* translators: %s: Post label (by default: Post). */
693 'label' => sprintf( __( '%s Status', 'automatorwp' ), $post_label ),
694 'type' => 'text',
695 'preview' => 'publish',
696 ),
697 'post_date' => array(
698 /* translators: %s: Post label (by default: Post). */
699 'label' => sprintf( __( '%s Date', 'automatorwp' ), $post_label ),
700 'type' => 'text',
701 'preview' => date( 'Y-m-d H:i:s' ),
702 ),
703 'post_date_gmt' => array(
704 /* translators: %s: Post label (by default: Post). */
705 'label' => sprintf( __( '%s Date GMT', 'automatorwp' ), $post_label ),
706 'type' => 'text',
707 'preview' => date( 'Y-m-d H:i:s' ),
708 ),
709 'post_modified' => array(
710 /* translators: %s: Post label (by default: Post). */
711 'label' => sprintf( __( '%s Modified Date', 'automatorwp' ), $post_label ),
712 'type' => 'text',
713 'preview' => date( 'Y-m-d H:i:s' ),
714 ),
715 'post_modified_gmt' => array(
716 /* translators: %s: Post label (by default: Post). */
717 'label' => sprintf( __( '%s Modified Date GMT', 'automatorwp' ), $post_label ),
718 'type' => 'text',
719 'preview' => date( 'Y-m-d H:i:s' ),
720 ),
721 'post_parent' => array(
722 /* translators: %s: Post label (by default: Post). */
723 'label' => sprintf( __( '%s Parent ID', 'automatorwp' ), $post_label ),
724 'type' => 'integer',
725 'preview' => '123',
726 ),
727 'menu_order' => array(
728 /* translators: %s: Post label (by default: Post). */
729 'label' => sprintf( __( '%s Menu Order', 'automatorwp' ), $post_label ),
730 'type' => 'integer',
731 'preview' => '1',
732 ),
733 'post_terms:TAXONOMY' => array(
734 /* translators: %s: Post label (by default: Post). */
735 'label' => sprintf( __( '%s Terms', 'automatorwp' ), $post_label ),
736 'type' => 'text',
737 'preview' => sprintf( __( 'Terms assigned to the %s, replace "TAXONOMY" by the terms taxonomy (eg: category, post_tag, etc)', 'automatorwp' ), $post_label, strtolower( $post_label ) ),
738 ),
739 'post_meta:META_KEY' => array(
740 /* translators: %s: Post label (by default: Post). */
741 'label' => sprintf( __( '%s Meta', 'automatorwp' ), $post_label ),
742 'type' => 'text',
743 'preview' => sprintf( __( '%s meta value, replace "META_KEY" by the %s meta key', 'automatorwp' ), $post_label, strtolower( $post_label ) ),
744 ),
745 ), $post_label );
746
747 }
748
749 /**
750 * Post meta tags from a GROUP BY from the database
751 *
752 * @since 1.0.0
753 *
754 * @param array $tags The post tags
755 * @param string $post_label The post label
756 *
757 * @return array
758 */
759 function automatorwp_utilities_post_meta_tags( $tags, $post_label ) {
760
761 global $wpdb;
762
763 $cache = automatorwp_get_cache( 'post_meta_tags', array(), false );
764
765 // If result already cached, return it
766 if( is_array( $cache ) && count( $cache ) ) {
767 return array_merge( $tags, $cache );
768 }
769
770 $metas = get_transient( 'automatorwp_post_metas_query' );
771
772 if ( $metas === false ) {
773 $postmeta = AutomatorWP()->db->postmeta;
774
775 // Query all post meta keys
776 $metas = $wpdb->get_results("SELECT um.meta_key, um.meta_value FROM {$postmeta} AS um GROUP BY um.meta_key ORDER BY um.meta_key ASC");
777 // Store the result for an hour
778 set_transient( 'automatorwp_post_metas_query', $metas, DAY_IN_SECONDS );
779 }
780
781 // post_meta:META_KEY
782 $meta_tags['post_meta:META_KEY'] = array(
783 /* translators: %s: Post label (by default: Post). */
784 'label' => sprintf( __( '%s Meta', 'automatorwp' ), $post_label ),
785 'type' => 'text',
786 'preview' => sprintf( __( '%s meta value, replace "META_KEY" by the %s meta key', 'automatorwp' ), $post_label, strtolower( $post_label ) ),
787 );
788
789 foreach( $metas as $meta ) {
790
791 $meta_value = $meta->meta_value;
792
793 if( empty( $meta_value ) ) {
794 $meta_value = __( '(no preview available)', 'automatorwp' );
795 }
796
797 // Shortcuts for the post_meta:META_KEY
798 $meta_tags['post_meta:' . $meta->meta_key] = array(
799 'label' => $meta->meta_key,
800 'type' => 'text',
801 'preview' => $meta_value,
802 );
803
804 }
805
806 automatorwp_set_cache( 'post_meta_tags', $meta_tags );
807
808 return array_merge( $tags, $meta_tags );
809
810 }
811 //add_filter( 'automatorwp_utilities_post_tags', 'automatorwp_utilities_post_meta_tags', 10, 2 );
812
813 /**
814 * Utility function to get the comment tags
815 *
816 * @since 1.3.0
817 *
818 * @return array
819 */
820 function automatorwp_utilities_comment_tags( $comment_label = '' ) {
821
822 if( empty( $comment_label ) ) {
823 $comment_label = __( 'Comment', 'automatorwp' );
824 }
825
826 /**
827 * Filter to setup custom comment tags
828 *
829 * @since 1.3.0
830 *
831 * @param array $comment_tags
832 *
833 * @return array
834 */
835 return apply_filters( 'automatorwp_utilities_comment_tags', array(
836 'comment_id' => array(
837 /* translators: %s: Comment label (by default: Comment). */
838 'label' => sprintf( __( '%s ID', 'automatorwp' ), $comment_label ),
839 'type' => 'integer',
840 'preview' => '123',
841 ),
842 'comment_post_id' => array(
843 /* translators: %s: Comment label (by default: Comment). */
844 'label' => sprintf( __( '%s Post ID', 'automatorwp' ), $comment_label ),
845 'type' => 'integer',
846 'preview' => '123',
847 ),
848 'comment_post_title' => array(
849 /* translators: %s: Comment label (by default: Comment). */
850 'label' => sprintf( __( '%s Post Title', 'automatorwp' ), $comment_label ),
851 'type' => 'text',
852 'preview' => __( 'The post title', 'automatorwp' ),
853 ),
854 'comment_user_id' => array(
855 /* translators: %s: Comment label (by default: Comment). */
856 'label' => sprintf( __( '%s User ID', 'automatorwp' ), $comment_label ),
857 'type' => 'integer',
858 'preview' => '123',
859 ),
860 'comment_author' => array(
861 /* translators: %s: Comment label (by default: Comment). */
862 'label' => sprintf( __( '%s Author Name', 'automatorwp' ), $comment_label ),
863 'type' => 'text',
864 'preview' => 'AutomatorWP',
865 ),
866 'comment_author_email' => array(
867 /* translators: %s: Comment label (by default: Comment). */
868 'label' => sprintf( __( '%s Author Email', 'automatorwp' ), $comment_label ),
869 'type' => 'email',
870 'preview' => '[email protected]',
871 ),
872 'comment_author_url' => array(
873 /* translators: %s: Comment label (by default: Comment). */
874 'label' => sprintf( __( '%s Author URL', 'automatorwp' ), $comment_label ),
875 'type' => 'text',
876 'preview' => 'https://automatorwp.com',
877 ),
878 'comment_author_ip' => array(
879 /* translators: %s: Comment label (by default: Comment). */
880 'label' => sprintf( __( '%s Author IP', 'automatorwp' ), $comment_label ),
881 'type' => 'text',
882 'preview' => '255.255.255.255',
883 ),
884 'comment_content' => array(
885 /* translators: %s: Comment label (by default: Comment). */
886 'label' => sprintf( __( '%s Content', 'automatorwp' ), $comment_label ),
887 'type' => 'text',
888 'preview' => __( 'The content', 'automatorwp' ),
889 ),
890 'comment_type' => array(
891 /* translators: %s: Comment label (by default: Comment). */
892 'label' => sprintf( __( '%s Type', 'automatorwp' ), $comment_label ),
893 'type' => 'text',
894 'preview' => 'comment',
895 ),
896 ) );
897
898 }
899
900 /**
901 * Utility function to get the user tags
902 *
903 * @since 1.0.0
904 *
905 * @return array
906 */
907 function automatorwp_utilities_user_tags( $user_label = '' ) {
908
909 if( empty( $user_label ) ) {
910 $user_label = __( 'User', 'automatorwp' );
911 }
912
913 /**
914 * Filter to setup custom post tags
915 *
916 * @since 1.0.0
917 *
918 * @param array $post_tags
919 *
920 * @return array
921 */
922 return apply_filters( 'automatorwp_utilities_user_tags', array(
923 'user_id' => array(
924 /* translators: %s: User label (by default: User). */
925 'label' => sprintf( __( '%s ID', 'automatorwp' ), $user_label ),
926 'type' => 'integer',
927 'preview' => '123',
928 ),
929 'user_login' => array(
930 /* translators: %s: User label (by default: User). */
931 'label' => sprintf( __( '%s Username', 'automatorwp' ), $user_label ),
932 'type' => 'text',
933 'preview' => __( 'automatorwp', 'automatorwp' ),
934 ),
935 'user_email' => array(
936 /* translators: %s: User label (by default: User). */
937 'label' => sprintf( __( '%s Email', 'automatorwp' ), $user_label ),
938 'type' => 'text',
939 'preview' => '[email protected]',
940 ),
941 'display_name' => array(
942 /* translators: %s: User label (by default: User). */
943 'label' => sprintf( __( '%s Display name', 'automatorwp' ), $user_label ),
944 'type' => 'text',
945 'preview' => 'AutomatorWP Plugin',
946 ),
947 'first_name' => array(
948 /* translators: %s: User label (by default: User). */
949 'label' => sprintf( __( '%s First name', 'automatorwp' ), $user_label ),
950 'type' => 'text',
951 'preview' => 'AutomatorWP',
952 ),
953 'last_name' => array(
954 /* translators: %s: User label (by default: User). */
955 'label' => sprintf( __( '%s Last name', 'automatorwp' ), $user_label ),
956 'type' => 'text',
957 'preview' => 'Plugin',
958 ),
959 'user_url' => array(
960 /* translators: %s: User label (by default: User). */
961 'label' => sprintf( __( '%s Website URL', 'automatorwp' ), $user_label ),
962 'type' => 'text',
963 'preview' => 'https://automatorwp.com',
964 ),
965 'avatar' => array(
966 /* translators: %s: User label (by default: User). */
967 'label' => sprintf( __( '%s Avatar', 'automatorwp' ), $user_label ),
968 'type' => 'text',
969 'preview' => '<img src="' . get_option( 'home' ) . '/wp-content/uploads/avatar.jpg' . '"/>',
970 ),
971 'avatar_url' => array(
972 /* translators: %s: User label (by default: User). */
973 'label' => sprintf( __( '%s Avatar URL', 'automatorwp' ), $user_label ),
974 'type' => 'text',
975 'preview' => get_option( 'home' ) . '/wp-content/uploads/avatar.jpg',
976 ),
977 'reset_password_url' => array(
978 /* translators: %s: User label (by default: User). */
979 'label' => sprintf( __( '%s Reset password URL', 'automatorwp' ), $user_label ),
980 'type' => 'text',
981 'preview' => get_option( 'home' ) . '/wp-login.php?action=rp',
982 ),
983 'reset_password_link' => array(
984 /* translators: %s: User label (by default: User). */
985 'label' => sprintf( __( '%s Reset password link', 'automatorwp' ), $user_label ),
986 'type' => 'text',
987 'preview' => '<a href="' . get_option( 'home' ) . '/wp-login.php?action=rp' . '">' . __( 'Click here to reset your password', 'automatorwp' ) . '</a>',
988 ),
989 'user_meta:META_KEY' => array(
990 /* translators: %s: User label (by default: User). */
991 'label' => sprintf( __( '%s Meta', 'automatorwp' ), $user_label ),
992 'type' => 'text',
993 'preview' => sprintf( __( '%s meta value, replace "META_KEY" by the %s meta key', 'automatorwp' ), $user_label, strtolower( $user_label ) ),
994 ),
995 ) );
996
997 }
998
999 /**
1000 * Utility function to get the condition option parameter
1001 *
1002 * @since 1.0.0
1003 *
1004 * @return array
1005 */
1006 function automatorwp_utilities_condition_option() {
1007 return array(
1008 'from' => 'condition',
1009 'fields' => array(
1010 'condition' => automatorwp_utilities_condition_field()
1011 )
1012 );
1013 }
1014
1015 /**
1016 * Utility function to get the condition field
1017 *
1018 * @since 1.0.0
1019 *
1020 * @return array
1021 */
1022 function automatorwp_utilities_condition_field() {
1023 return array(
1024 'name' => __( 'Condition:', 'automatorwp' ),
1025 'type' => 'select',
1026 'options' => array(
1027 'equal' => __( 'is equal to', 'automatorwp' ),
1028 'not_equal' => __( 'is not equal to', 'automatorwp' ),
1029 'contains' => __( 'contains', 'automatorwp' ),
1030 'not_contains' => __( 'does not contain', 'automatorwp' ),
1031 'start_with' => __( 'starts with', 'automatorwp' ),
1032 'not_start_with' => __( 'does not start with', 'automatorwp' ),
1033 'ends_with' => __( 'ends with', 'automatorwp' ),
1034 'not_ends_with' => __( 'does not end with', 'automatorwp' ),
1035 'less_than' => __( 'is less than', 'automatorwp' ),
1036 'greater_than' => __( 'is greater than', 'automatorwp' ),
1037 'less_or_equal' => __( 'is less than or equal to', 'automatorwp' ),
1038 'greater_or_equal' => __( 'is greater than or equal to', 'automatorwp' ),
1039 ),
1040 'default' => 'equal'
1041 );
1042 }
1043
1044 /**
1045 * Utility function to get the condition option parameter
1046 *
1047 * @since 1.0.0
1048 *
1049 * @return array
1050 */
1051 function automatorwp_utilities_number_condition_option() {
1052 return array(
1053 'from' => 'condition',
1054 'fields' => array(
1055 'condition' => automatorwp_utilities_number_condition_field()
1056 )
1057 );
1058 }
1059
1060 /**
1061 * Utility function to get the condition field
1062 *
1063 * @since 1.0.0
1064 *
1065 * @return array
1066 */
1067 function automatorwp_utilities_number_condition_field() {
1068 return array(
1069 'name' => __( 'Condition:', 'automatorwp' ),
1070 'type' => 'select',
1071 'options' => array(
1072 'equal' => __( 'equal to', 'automatorwp' ),
1073 'not_equal' => __( 'not equal to', 'automatorwp' ),
1074 'less_than' => __( 'less than', 'automatorwp' ),
1075 'greater_than' => __( 'greater than', 'automatorwp' ),
1076 'less_or_equal' => __( 'less or equal to', 'automatorwp' ),
1077 'greater_or_equal' => __( 'greater or equal to', 'automatorwp' ),
1078 ),
1079 'default' => 'equal'
1080 );
1081 }
1082
1083 /**
1084 * Utility function to get the string condition option parameter
1085 *
1086 * @since 1.4.5
1087 *
1088 * @return array
1089 */
1090 function automatorwp_utilities_string_condition_option() {
1091 return array(
1092 'from' => 'condition',
1093 'fields' => array(
1094 'condition' => automatorwp_utilities_string_condition_field()
1095 )
1096 );
1097 }
1098
1099 /**
1100 * Utility function to get the string condition field
1101 *
1102 * @since 1.4.5
1103 *
1104 * @return array
1105 */
1106 function automatorwp_utilities_string_condition_field() {
1107 return array(
1108 'name' => __( 'Condition:', 'automatorwp' ),
1109 'type' => 'select',
1110 'options' => array(
1111 'equal' => __( 'is equal to', 'automatorwp' ),
1112 'not_equal' => __( 'is not equal to', 'automatorwp' ),
1113 'contains' => __( 'contains', 'automatorwp' ),
1114 'not_contains' => __( 'does not contain', 'automatorwp' ),
1115 'start_with' => __( 'starts with', 'automatorwp' ),
1116 'not_start_with' => __( 'does not start with', 'automatorwp' ),
1117 'ends_with' => __( 'ends with', 'automatorwp' ),
1118 'not_ends_with' => __( 'does not end with', 'automatorwp' ),
1119 ),
1120 'default' => 'equal'
1121 );
1122 }
1123
1124 /**
1125 * Utility function to get a condition label
1126 *
1127 * @since 1.4.5
1128 *
1129 * @param string $condition
1130 * @return string
1131 */
1132 function automatorwp_utilities_get_condition_label( $condition ) {
1133
1134 $conditions = array(
1135 // String
1136 'equal' => __( 'is equal to', 'automatorwp' ),
1137 'not_equal' => __( 'is not equal to', 'automatorwp' ),
1138 'contains' => __( 'contains', 'automatorwp' ),
1139 'not_contains' => __( 'does not contain', 'automatorwp' ),
1140 'start_with' => __( 'starts with', 'automatorwp' ),
1141 'not_start_with' => __( 'does not start with', 'automatorwp' ),
1142 'ends_with' => __( 'ends with', 'automatorwp' ),
1143 'not_ends_with' => __( 'does not end with', 'automatorwp' ),
1144 // Number
1145 'less_than' => __( 'is less than', 'automatorwp' ),
1146 'greater_than' => __( 'is greater than', 'automatorwp' ),
1147 'less_or_equal' => __( 'is less than or equal to', 'automatorwp' ),
1148 'greater_or_equal' => __( 'is greater than or equal to', 'automatorwp' ),
1149 );
1150
1151 return ( isset( $conditions[$condition] ) ? $conditions[$condition] : $condition );
1152 }
1153
1154 /**
1155 * Check if post ID matches with the required post ID
1156 *
1157 * @since 1.0.0
1158 *
1159 * @param int $post_id The post ID
1160 * @param int $required_post_id The required post ID
1161 *
1162 * @return bool
1163 */
1164 function automatorwp_posts_matches( $post_id, $required_post_id ) {
1165
1166 $post_id = absint( $post_id );
1167 $required_post_id = absint( $required_post_id );
1168
1169 $post = get_post( $post_id );
1170
1171 // Bail if post doesn't exists
1172 if( ! $post ) {
1173 return false;
1174 }
1175
1176 // Bail if post doesn't match with the trigger option
1177 if( $required_post_id !== 0 && $post->ID !== $required_post_id ) {
1178 return false;
1179 }
1180
1181 return true;
1182
1183 }
1184
1185 /**
1186 * Check if term ID matches with the required term ID
1187 *
1188 * @since 1.0.0
1189 *
1190 * @param array|int $term_id The term ID
1191 * @param int $required_term_id The required term ID
1192 *
1193 * @return bool
1194 */
1195 function automatorwp_terms_matches( $term_id, $required_term_id ) {
1196
1197 $required_term_id = absint( $required_term_id );
1198
1199 // Only parse this check if required term ID is provided
1200 if( $required_term_id !== 0 ) {
1201
1202 if( is_array( $term_id ) ) {
1203
1204 // Ensure terms IDs as integer
1205 $term_id = array_map( 'absint', $term_id );
1206
1207 if( ! in_array( $required_term_id, $term_id ) ) {
1208 // If received an array of terms, bail if required term ID isn't in the array
1209 return false;
1210 }
1211
1212 } else if( absint( $term_id ) !== $required_term_id ) {
1213
1214 // If received a single term ID, bail if required term ID doesn't match
1215 return false;
1216
1217 }
1218
1219
1220 }
1221
1222 return true;
1223
1224 }
1225
1226 /**
1227 * Utility function to get the condition option parameter
1228 *
1229 * @since 1.0.0
1230 *
1231 * @param int|float $a Number to match
1232 * @param int|float $b Number to compare
1233 * @param string $condition The condition to compare numbers
1234 *
1235 * @return bool
1236 */
1237 function automatorwp_number_condition_matches( $a, $b, $condition ) {
1238
1239 if( empty( $condition ) ) {
1240 $condition = 'equal';
1241 }
1242
1243 $matches = false;
1244
1245 switch( $condition ) {
1246 case 'equal':
1247 case '=':
1248 case '==':
1249 case '===':
1250 $matches = ( $a == $b );
1251 break;
1252 case 'not_equal':
1253 case '!=':
1254 case '!==':
1255 $matches = ( $a != $b );
1256 break;
1257 case 'less_than':
1258 case '<':
1259 $matches = ( $a < $b );
1260 break;
1261 case 'greater_than':
1262 case '>':
1263 $matches = ( $a > $b );
1264 break;
1265 case 'less_or_equal':
1266 case '<=':
1267 $matches = ( $a <= $b );
1268 break;
1269 case 'greater_or_equal':
1270 case '>=':
1271 $matches = ( $a >= $b );
1272 break;
1273 }
1274
1275 return $matches;
1276
1277 }
1278
1279 /**
1280 * Utility function to get the condition option parameter
1281 *
1282 * @since 1.4.5
1283 *
1284 * @param mixed $a Element to match
1285 * @param mixed $b Element to compare
1286 * @param string $condition The condition to compare elements
1287 *
1288 * @return bool
1289 */
1290 function automatorwp_condition_matches( $a, $b, $condition ) {
1291
1292 if( empty( $condition ) ) {
1293 $condition = 'equal';
1294 }
1295
1296 $matches = false;
1297
1298 // Ensure that the element to compare is a string
1299 if( is_array( $b ) ) {
1300 $b = implode( ',', $b );
1301 }
1302
1303 $a = strval( $a );
1304 $b = strval( $b );
1305
1306 // If not is a string condition and elements to compare are numerics, turn them to float
1307 if( ! automatorwp_is_string_condition( $condition ) ) {
1308 if( is_numeric( $a ) ) {
1309 $a = (float) $a;
1310 }
1311
1312 if( is_numeric( $b ) ) {
1313 $b = (float) $b;
1314 }
1315 }
1316
1317 switch( $condition ) {
1318 case 'equal':
1319 case '=':
1320 case '==':
1321 case '===':
1322 $matches = ( $a == $b );
1323 break;
1324 case 'not_equal':
1325 case '!=':
1326 case '!==':
1327 $matches = ( $a != $b );
1328 break;
1329 case 'less_than':
1330 case '<':
1331 $matches = ( $a < $b );
1332 break;
1333 case 'greater_than':
1334 case '>':
1335 $matches = ( $a > $b );
1336 break;
1337 case 'less_or_equal':
1338 case '<=':
1339 $matches = ( $a <= $b );
1340 break;
1341 case 'greater_or_equal':
1342 case '>=':
1343 $matches = ( $a >= $b );
1344 break;
1345 case 'contains':
1346 $matches = ( strpos( $a, strval( $b ) ) !== false );
1347 break;
1348 case 'not_contains':
1349 $matches = ( strpos( $a, strval( $b ) ) === false );
1350 break;
1351 case 'start_with':
1352 $matches = ( automatorwp_starts_with( $a, $b ) );
1353 break;
1354 case 'not_start_with':
1355 $matches = ( ! automatorwp_starts_with( $a, $b ) );
1356 break;
1357 case 'ends_with':
1358 $matches = ( automatorwp_ends_with( $a, $b ) );
1359 break;
1360 case 'not_ends_with':
1361 $matches = ( ! automatorwp_ends_with( $a, $b ) );
1362 break;
1363 }
1364
1365 return $matches;
1366
1367 }
1368
1369 /**
1370 * Utility function to meet if condition is related to string
1371 *
1372 * @since 1.7.6
1373 *
1374 * @param string $condition The condition to check
1375 *
1376 * @return bool
1377 */
1378 function automatorwp_is_string_condition( $condition ) {
1379
1380 $return = false;
1381
1382 switch( $condition ) {
1383 case 'contains':
1384 case 'not_contains':
1385 case 'start_with':
1386 case 'not_start_with':
1387 case 'ends_with':
1388 case 'not_ends_with':
1389 $return = true;
1390 break;
1391 }
1392
1393 return $return;
1394
1395 }
1396
1397 /**
1398 * Retrieves post term ids for a taxonomy.
1399 *
1400 * @since 1.0.0
1401 *
1402 * @param int $post_id Post ID.
1403 * @param string $taxonomy Taxonomy slug.
1404 *
1405 * @return array
1406 */
1407 function automatorwp_get_term_ids( $post_id, $taxonomy ) {
1408
1409 $terms = get_the_terms( $post_id, $taxonomy );
1410
1411 return ( empty( $terms ) || is_wp_error( $terms ) ) ? array() : wp_list_pluck( $terms, 'term_id' );
1412
1413 }
1414
1415 /**
1416 * Creates a toggleable list
1417 *
1418 * @since 1.0.0
1419 *
1420 * @param array $options
1421 *
1422 * @return string
1423 */
1424 function automatorwp_toggleable_options_list( $options ) {
1425
1426 // Bail if no options given
1427 if( ! is_array( $options ) ) {
1428 return '';
1429 }
1430
1431 $show_text = __( 'Show options', 'automatorwp' );
1432 $hide_text = __( 'Hide options', 'automatorwp' );
1433
1434 $html = '<a href="#" class="automatorwp-toggleable-options-list-toggle" data-show-text="' . $show_text . '" data-hide-text="' . $hide_text . '">' . $show_text . '</a>'
1435 . '<ul class="automatorwp-toggleable-options-list" style="display: none;">';
1436
1437 foreach( $options as $option ) {
1438 $html .= "<li>{$option}</li>";
1439 }
1440
1441 $html .= '</ul>';
1442
1443 return $html;
1444
1445 }
1446
1447 /**
1448 * Helper function to get all editable roles included if get_editable_roles() doesn't exists
1449 *
1450 * @since 1.1.5
1451 *
1452 * @return array[]|mixed|void
1453 */
1454 function automatorwp_get_editable_roles() {
1455
1456 if( function_exists('get_editable_roles' ) ) {
1457 $roles = get_editable_roles();
1458 } else {
1459 $roles = wp_roles()->roles;
1460
1461 $roles = apply_filters( 'editable_roles', $roles );
1462 }
1463
1464 return $roles;
1465
1466 }
1467
1468 /**
1469 * Helper function to pull all values of an array to a single level
1470 *
1471 * -------------------------------------
1472 * Turns keyed arrays:
1473 * 'key_1' => array(
1474 * 'sub_1' => 'foo',
1475 * 'sub_2' => array(
1476 * 'sub_1' => 'bar',
1477 * ),
1478 * ),
1479 *
1480 * Into:
1481 * 'key_1' => 'foo, bar'
1482 * 'key_1/sub_1' => 'foo',
1483 * 'key_1/sub_2' => 'bar'
1484 * 'key_1/sub_2/sub_1' => 'bar',
1485 * -------------------------------------
1486 * Turns numeric arrays:
1487 * 'key' => array(
1488 * 'foo',
1489 * array(
1490 * 'bar',
1491 * ),
1492 * ),
1493 *
1494 * Into:
1495 * 'key' => 'foo, bar'
1496 * 'key/0' => 'foo',
1497 * 'key/1' => 'bar'
1498 * 'key/1/0' => 'bar',
1499 * -------------------------------------
1500 *
1501 * @since 1.4.4
1502 *
1503 * @param array $array
1504 * @param string $separator
1505 *
1506 * @return array
1507 */
1508 function automatorwp_utilities_pull_array_values( $array = array(), $separator = '/' ) {
1509
1510 $new_array = array();
1511
1512 foreach( $array as $key => $value ) {
1513
1514 if( is_array( $value ) ) {
1515 // Pull all sub values
1516 $value = automatorwp_utilities_pull_array_values( $value );
1517 }
1518
1519 $new_array[$key] = $value;
1520
1521 if( is_array( $value ) ) {
1522
1523 // Add new entries with the sub values
1524 foreach( $value as $sub_key => $sub_value ) {
1525 $new_array[$key . $separator . $sub_key] = $sub_value;
1526 }
1527
1528 // Implode all sub values on the main key
1529 $new_array[$key] = implode( ', ', $value );
1530 }
1531
1532 }
1533
1534 return $new_array;
1535
1536 }
1537
1538 /**
1539 * Helper function to search an array key value recursively
1540 *
1541 * @since 1.4.5
1542 *
1543 * @param string|array $key
1544 * @param array $haystack
1545 *
1546 * @return mixed
1547 */
1548 function automatorwp_get_array_key_value( $key = '', $haystack = array() ) {
1549
1550 // Support for $key as array
1551 if( is_array( $key ) ) {
1552 $results = array();
1553
1554 foreach( $key as $key_item ) {
1555 $found = automatorwp_get_array_key_value( $key_item, $haystack );
1556
1557 if ( $found ) {
1558 $results[] = $found;
1559 }
1560 }
1561
1562 // Return the entire list of results found
1563 if( ! empty( $results ) ) {
1564 return $results;
1565 }
1566
1567 return false;
1568 }
1569
1570 // Return if directly found the index in the array
1571 if( isset( $haystack[$key] ) ) {
1572 return $haystack[$key];
1573 }
1574
1575 // If $haystack is a multilevel array, search in all its levels
1576 foreach( $haystack as $value) {
1577
1578 if ( is_array( $value ) ) {
1579
1580 $found = automatorwp_get_array_key_value( $key, $value );
1581
1582 if ( $found ) {
1583 return $found;
1584 }
1585
1586 }
1587
1588 }
1589
1590 return false;
1591
1592 }
1593
1594 /**
1595 * Helper function to check if a string starts by needle string given
1596 *
1597 * @since 1.4.5
1598 *
1599 * @param string $haystack
1600 * @param string $needle
1601 *
1602 * @return bool
1603 */
1604 function automatorwp_starts_with( $haystack, $needle ) {
1605 return strncmp( $haystack, $needle, strlen( $needle ) ) === 0;
1606 }
1607
1608 /**
1609 * Helper function to check if a string ends by needle string given
1610 *
1611 * @since 1.4.5
1612 *
1613 * @param string $haystack
1614 * @param string $needle
1615 *
1616 * @return bool
1617 */
1618 function automatorwp_ends_with( $haystack, $needle ) {
1619 return $needle === '' || substr_compare( $haystack, $needle, -strlen( $needle ) ) === 0;
1620 }
1621
1622 /**
1623 * Helper function to parse the function args option
1624 *
1625 * @since 1.7.9
1626 *
1627 * @param array $args Expects an array like: array( array( 'value' => 'value_1' ), array( 'value' => 'value_2' ) )
1628 * @param stdClass $item
1629 * @param int $user_id
1630 * @param array $options
1631 * @param stdClass $automation
1632 *
1633 * @return array
1634 */
1635 function automatorwp_parse_function_args_option( $args, $item, $user_id, $options, $automation ) {
1636
1637 foreach( $args as $key => $param ) {
1638
1639 $value = automatorwp_parse_automation_tags( $automation->id, $user_id, $param['value'] );
1640
1641 // Check if isn't numeric and starting with 0
1642 if( ( is_numeric( $value ) && automatorwp_starts_with( $value, 0 ) ) === false ) {
1643 $value = automatorwp_parse_function_arg_value( $value );
1644 }
1645
1646 $args[$key] = $value;
1647
1648 }
1649
1650 /**
1651 * Filter available to override the function args option parsed
1652 *
1653 * @since 1.7.9
1654 *
1655 * @param array $args
1656 * @param stdClass $item
1657 * @param int $user_id
1658 * @param array $options
1659 * @param stdClass $automation
1660 *
1661 * @return array
1662 */
1663 return apply_filters( 'automatorwp_parse_function_args_option', $args, $item, $user_id, $options, $automation );
1664
1665 }
1666
1667 /**
1668 * Helper function to parse the function arg value
1669 * Turn values like "null" to null or "array()" to array()
1670 *
1671 * @since 1.7.9
1672 *
1673 * @param mixed $value
1674 *
1675 * @return mixed
1676 */
1677 function automatorwp_parse_function_arg_value( $value ) {
1678
1679 $original_value = $value;
1680
1681 // Check PHP objects
1682 switch ( $value ) {
1683 case 'null':
1684 $value = null;
1685 break;
1686 case 'true':
1687 $value = true;
1688 break;
1689 case 'TRUE':
1690 $value = TRUE;
1691 break;
1692 case 'false':
1693 $value = false;
1694 break;
1695 case 'FALSE':
1696 $value = FALSE;
1697 break;
1698 case 'array()':
1699 case '[]':
1700 $value = array();
1701 break;
1702 }
1703
1704 // Check possible numeric values
1705 if( is_numeric( $value ) ) {
1706 if ( strpos( $value , '.') !== false ) {
1707 $value = (float) $value ;
1708 } else {
1709 $value = (int) $value;
1710 }
1711 }
1712
1713 /**
1714 * Filter available to override the function arg value parsed
1715 *
1716 * @since 1.7.9
1717 *
1718 * @param mixed $value
1719 *
1720 * @return mixed
1721 */
1722 return apply_filters( 'automatorwp_parse_function_arg_value', $value, $original_value );
1723
1724 }
1725
1726 /**
1727 * Helper function to parse a condition to SQL
1728 *
1729 * @since 1.9.3
1730 *
1731 * @param string $field The field key
1732 * @param string $condition The condition
1733 * @param string $value The field value
1734 * @param bool $strict True to force check the value type, false to always check the value as string
1735 *
1736 * @return string
1737 */
1738 function automatorwp_utilities_parse_condition_to_sql( $field, $condition, $value, $strict = true ) {
1739
1740 global $wpdb;
1741
1742 $operator = '=';
1743
1744 switch( $condition ) {
1745 case 'equal':
1746 $operator = '=';
1747 break;
1748 case 'not_equal':
1749 $operator = '!=';
1750 break;
1751 case 'contains':
1752 case 'start_with':
1753 case 'ends_with':
1754 $operator = 'LIKE';
1755 break;
1756 case 'not_contains':
1757 case 'not_start_with':
1758 case 'not_ends_with':
1759 $operator = 'NOT LIKE';
1760 break;
1761 case 'less_than':
1762 $operator = '<';
1763 break;
1764 case 'greater_than':
1765 $operator = '>';
1766 break;
1767 case 'less_or_equal':
1768 $operator = '<=';
1769 break;
1770 case 'greater_or_equal':
1771 $operator = '>=';
1772 break;
1773 default:
1774 $operator = '=';
1775 break;
1776 }
1777
1778 $string_required_conditions = array(
1779 'contains',
1780 'not_contains',
1781 'start_with',
1782 'not_start_with',
1783 'ends_with',
1784 'not_ends_with',
1785 );
1786
1787 $pattern = '%s';
1788
1789 if( $strict && is_numeric( $value ) && ! in_array( $condition, $string_required_conditions ) ) {
1790 if ( strpos( $value , '.') !== false ) {
1791 $value = (float) $value ;
1792 $pattern = '%f';
1793 } else {
1794 $value = (int) $value;
1795 $pattern = '%d';
1796 }
1797 } else {
1798
1799 if ( in_array( $condition, $string_required_conditions ) )
1800 $value = $wpdb->esc_like( $value );
1801
1802 $value = esc_sql( $value );
1803
1804 switch( $condition ) {
1805 case 'contains':
1806 case 'not_contains':
1807 $value = "%{$value}%";
1808 break;
1809 case 'start_with':
1810 case 'not_start_with':
1811 $value = "{$value}%";
1812 break;
1813 case 'ends_with':
1814 case 'not_ends_with':
1815 $value = "%{$value}";
1816 break;
1817 }
1818
1819 }
1820
1821 return $wpdb->prepare( "{$field} {$operator} {$pattern}", array( $value ) );
1822
1823 }
1824
1825 /**
1826 * Helper function to implode array elements with a special glue for the last item
1827 * Example: array( 1, 2, 3 ) imploded to "1, 2 and 3"
1828 *
1829 * @since 2.0.4
1830 *
1831 * @param string $glue Glue to join array items
1832 * @param string $glue_for_last_item Glue to join the last item
1833 * @param array $array The array to implode
1834 *
1835 * @return string
1836 */
1837 function automatorwp_implode_array( $glue = '', $glue_for_last_item = '', $array = array() ) {
1838
1839 $last = array_slice( $array, -1 );
1840 $first = join( $glue, array_slice( $array, 0, -1 ) );
1841 $both = array_filter( array_merge( array( $first ), $last ), 'strlen' );
1842
1843 return join( $glue_for_last_item, $both );
1844
1845 }
1846
1847 /**
1848 * Utility function to get the operator field
1849 *
1850 * @since 1.0.0
1851 *
1852 * @return array
1853 */
1854 function automatorwp_utilities_operator_field() {
1855
1856 return array(
1857 'name' => __( 'Operator:', 'automatorwp' ),
1858 'type' => 'select',
1859 'options' => array(
1860 'AND' => __( 'AND', 'automatorwp' ),
1861 'OR' => __( 'OR', 'automatorwp' ),
1862 ),
1863 'default' => 'AND'
1864 );
1865
1866 }
1867
1868 /**
1869 * Utility function to get the post fields
1870 *
1871 * @since 1.0.0
1872 *
1873 * @return array
1874 */
1875 function automatorwp_utilities_get_post_fields() {
1876
1877 $post_fields = array(
1878 'ID' => __( 'ID', 'automatorwp' ),
1879 'post_title' => __( 'Title', 'automatorwp' ),
1880 'post_name' => __( 'Slug', 'automatorwp' ),
1881 'post_type' => __( 'Type', 'automatorwp' ),
1882 'post_status' => __( 'Status', 'automatorwp' ),
1883 'post_date' => __( 'Date', 'automatorwp' ),
1884 'post_date_gmt' => __( 'Date (GMT)', 'automatorwp' ),
1885 'post_modified' => __( 'Date Modified', 'automatorwp' ),
1886 'post_modified_gmt' => __( 'Date Modified (GMT)', 'automatorwp' ),
1887 'post_author' => __( 'Author', 'automatorwp' ),
1888 'post_content' => __( 'Content', 'automatorwp' ),
1889 'post_excerpt' => __( 'Excerpt', 'automatorwp' ),
1890 'post_parent' => __( 'Parent', 'automatorwp' ),
1891 'menu_order' => __( 'Order', 'automatorwp' ),
1892 'post_password' => __( 'Password', 'automatorwp' ),
1893 );
1894
1895 $post_fields = apply_filters( 'automatorwp_utilities_get_post_fields', $post_fields );
1896
1897 return $post_fields;
1898
1899 }
1900
1901 /**
1902 * Utility function to get the user fields
1903 *
1904 * @since 1.0.0
1905 *
1906 * @return array
1907 */
1908 function automatorwp_utilities_get_user_fields() {
1909
1910 $user_fields = array(
1911 'ID' => __( 'ID', 'automatorwp' ),
1912 'user_login' => __( 'Username', 'automatorwp' ),
1913 'user_email' => __( 'Email', 'automatorwp' ),
1914 'display_name' => __( 'Display name', 'automatorwp' ),
1915 'user_nicename' => __( 'Nicename', 'automatorwp' ),
1916 'user_url' => __( 'Website', 'automatorwp' ),
1917 'user_registered' => __( 'Registration Date', 'automatorwp' ),
1918 );
1919
1920 $user_fields = apply_filters( 'automatorwp_utilities_get_user_fields', $user_fields );
1921
1922 return $user_fields;
1923
1924 }