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 / includes / utilities.php

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

2,011 lines 61.3 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 <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com>
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' => 'contact@automatorwp.com',
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' => 'contact@automatorwp.com',
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' => 'contact@automatorwp.com',
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' => automatorwp_number_condition_options(),
1072 'default' => 'equal'
1073 );
1074 }
1075
1076 /**
1077 * Utility function to get the number condition options
1078 *
1079 * @since 6.0.0
1080 *
1081 * @return array
1082 */
1083 function automatorwp_number_condition_options() {
1084 return array(
1085 'equal' => __( 'equal to', 'automatorwp' ),
1086 'not_equal' => __( 'not equal to', 'automatorwp' ),
1087 'less_than' => __( 'less than', 'automatorwp' ),
1088 'greater_than' => __( 'greater than', 'automatorwp' ),
1089 'less_or_equal' => __( 'less or equal to', 'automatorwp' ),
1090 'greater_or_equal' => __( 'greater or equal to', 'automatorwp' ),
1091 );
1092 }
1093
1094 /**
1095 * Utility function to get the string condition option parameter
1096 *
1097 * @since 1.4.5
1098 *
1099 * @return array
1100 */
1101 function automatorwp_utilities_string_condition_option() {
1102 return array(
1103 'from' => 'condition',
1104 'fields' => array(
1105 'condition' => automatorwp_utilities_string_condition_field()
1106 )
1107 );
1108 }
1109
1110 /**
1111 * Utility function to get the string condition field
1112 *
1113 * @since 1.4.5
1114 *
1115 * @return array
1116 */
1117 function automatorwp_utilities_string_condition_field() {
1118 return array(
1119 'name' => __( 'Condition:', 'automatorwp' ),
1120 'type' => 'select',
1121 'options' => array(
1122 'equal' => __( 'is equal to', 'automatorwp' ),
1123 'not_equal' => __( 'is not equal to', 'automatorwp' ),
1124 'contains' => __( 'contains', 'automatorwp' ),
1125 'not_contains' => __( 'does not contain', 'automatorwp' ),
1126 'start_with' => __( 'starts with', 'automatorwp' ),
1127 'not_start_with' => __( 'does not start with', 'automatorwp' ),
1128 'ends_with' => __( 'ends with', 'automatorwp' ),
1129 'not_ends_with' => __( 'does not end with', 'automatorwp' ),
1130 ),
1131 'default' => 'equal'
1132 );
1133 }
1134
1135 /**
1136 * Utility function to get a condition label
1137 *
1138 * @since 1.4.5
1139 *
1140 * @param string $condition
1141 * @return string
1142 */
1143 function automatorwp_utilities_get_condition_label( $condition ) {
1144
1145 $conditions = array(
1146 // String
1147 'equal' => __( 'is equal to', 'automatorwp' ),
1148 'not_equal' => __( 'is not equal to', 'automatorwp' ),
1149 'contains' => __( 'contains', 'automatorwp' ),
1150 'not_contains' => __( 'does not contain', 'automatorwp' ),
1151 'start_with' => __( 'starts with', 'automatorwp' ),
1152 'not_start_with' => __( 'does not start with', 'automatorwp' ),
1153 'ends_with' => __( 'ends with', 'automatorwp' ),
1154 'not_ends_with' => __( 'does not end with', 'automatorwp' ),
1155 // Number
1156 'less_than' => __( 'is less than', 'automatorwp' ),
1157 'greater_than' => __( 'is greater than', 'automatorwp' ),
1158 'less_or_equal' => __( 'is less than or equal to', 'automatorwp' ),
1159 'greater_or_equal' => __( 'is greater than or equal to', 'automatorwp' ),
1160 );
1161
1162 return ( isset( $conditions[$condition] ) ? $conditions[$condition] : $condition );
1163 }
1164
1165 /**
1166 * Check if post ID matches with the required post ID
1167 *
1168 * @since 1.0.0
1169 *
1170 * @param int $post_id The post ID
1171 * @param int $required_post_id The required post ID
1172 *
1173 * @return bool
1174 */
1175 function automatorwp_posts_matches( $post_id, $required_post_id ) {
1176
1177 $post_id = absint( $post_id );
1178 $required_post_id = absint( $required_post_id );
1179
1180 $post = get_post( $post_id );
1181
1182 // Bail if post doesn't exists
1183 if( ! $post ) {
1184 return false;
1185 }
1186
1187 // Bail if post doesn't match with the trigger option
1188 if( $required_post_id !== 0 && $post->ID !== $required_post_id ) {
1189 return false;
1190 }
1191
1192 return true;
1193
1194 }
1195
1196 /**
1197 * Check if term ID matches with the required term ID
1198 *
1199 * @since 1.0.0
1200 *
1201 * @param array|int $term_id The term ID
1202 * @param int $required_term_id The required term ID
1203 *
1204 * @return bool
1205 */
1206 function automatorwp_terms_matches( $term_id, $required_term_id ) {
1207
1208 $required_term_id = absint( $required_term_id );
1209
1210 // Only parse this check if required term ID is provided
1211 if( $required_term_id !== 0 ) {
1212
1213 if( is_array( $term_id ) ) {
1214
1215 // Ensure terms IDs as integer
1216 $term_id = array_map( 'absint', $term_id );
1217
1218 if( ! in_array( $required_term_id, $term_id ) ) {
1219 // If received an array of terms, bail if required term ID isn't in the array
1220 return false;
1221 }
1222
1223 } else if( absint( $term_id ) !== $required_term_id ) {
1224
1225 // If received a single term ID, bail if required term ID doesn't match
1226 return false;
1227
1228 }
1229
1230
1231 }
1232
1233 return true;
1234
1235 }
1236
1237 /**
1238 * Utility function to get the condition option parameter
1239 *
1240 * @since 1.0.0
1241 *
1242 * @param int|float $a Number to match
1243 * @param int|float $b Number to compare
1244 * @param string $condition The condition to compare numbers
1245 *
1246 * @return bool
1247 */
1248 function automatorwp_number_condition_matches( $a, $b, $condition ) {
1249
1250 if( empty( $condition ) ) {
1251 $condition = 'equal';
1252 }
1253
1254 $matches = false;
1255
1256 switch( $condition ) {
1257 case 'equal':
1258 case '=':
1259 case '==':
1260 case '===':
1261 $matches = ( $a == $b );
1262 break;
1263 case 'not_equal':
1264 case '!=':
1265 case '!==':
1266 $matches = ( $a != $b );
1267 break;
1268 case 'less_than':
1269 case '<':
1270 $matches = ( $a < $b );
1271 break;
1272 case 'greater_than':
1273 case '>':
1274 $matches = ( $a > $b );
1275 break;
1276 case 'less_or_equal':
1277 case '<=':
1278 $matches = ( $a <= $b );
1279 break;
1280 case 'greater_or_equal':
1281 case '>=':
1282 $matches = ( $a >= $b );
1283 break;
1284 }
1285
1286 return $matches;
1287
1288 }
1289
1290 /**
1291 * Utility function to get the condition option parameter
1292 *
1293 * @since 1.4.5
1294 *
1295 * @param mixed $a Element to match
1296 * @param mixed $b Element to compare
1297 * @param string $condition The condition to compare elements
1298 *
1299 * @return bool
1300 */
1301 function automatorwp_condition_matches( $a, $b, $condition ) {
1302
1303 if( empty( $condition ) ) {
1304 $condition = 'equal';
1305 }
1306
1307 $matches = false;
1308
1309 // Ensure that the element to compare is a string
1310 if( is_array( $b ) ) {
1311 $b = implode( ',', $b );
1312 }
1313
1314 $a = strval( $a );
1315 $b = strval( $b );
1316
1317 // If not is a string condition and elements to compare are numerics, turn them to float
1318 if( ! automatorwp_is_string_condition( $condition ) ) {
1319 if( is_numeric( $a ) ) {
1320 $a = (float) $a;
1321 }
1322
1323 if( is_numeric( $b ) ) {
1324 $b = (float) $b;
1325 }
1326 }
1327
1328 switch( $condition ) {
1329 case 'equal':
1330 case '=':
1331 case '==':
1332 case '===':
1333 $matches = ( $a == $b );
1334 break;
1335 case 'not_equal':
1336 case '!=':
1337 case '!==':
1338 $matches = ( $a != $b );
1339 break;
1340 case 'less_than':
1341 case '<':
1342 $matches = ( $a < $b );
1343 break;
1344 case 'greater_than':
1345 case '>':
1346 $matches = ( $a > $b );
1347 break;
1348 case 'less_or_equal':
1349 case '<=':
1350 $matches = ( $a <= $b );
1351 break;
1352 case 'greater_or_equal':
1353 case '>=':
1354 $matches = ( $a >= $b );
1355 break;
1356 case 'contains':
1357 $matches = ( strpos( $a, strval( $b ) ) !== false );
1358 break;
1359 case 'not_contains':
1360 $matches = ( strpos( $a, strval( $b ) ) === false );
1361 break;
1362 case 'start_with':
1363 $matches = ( automatorwp_starts_with( $a, $b ) );
1364 break;
1365 case 'not_start_with':
1366 $matches = ( ! automatorwp_starts_with( $a, $b ) );
1367 break;
1368 case 'ends_with':
1369 $matches = ( automatorwp_ends_with( $a, $b ) );
1370 break;
1371 case 'not_ends_with':
1372 $matches = ( ! automatorwp_ends_with( $a, $b ) );
1373 break;
1374 }
1375
1376 return $matches;
1377
1378 }
1379
1380 /**
1381 * Utility function to meet if condition is related to string
1382 *
1383 * @since 1.7.6
1384 *
1385 * @param string $condition The condition to check
1386 *
1387 * @return bool
1388 */
1389 function automatorwp_is_string_condition( $condition ) {
1390
1391 $return = false;
1392
1393 switch( $condition ) {
1394 case 'contains':
1395 case 'not_contains':
1396 case 'start_with':
1397 case 'not_start_with':
1398 case 'ends_with':
1399 case 'not_ends_with':
1400 $return = true;
1401 break;
1402 }
1403
1404 return $return;
1405
1406 }
1407
1408 /**
1409 * Retrieves post term ids for a taxonomy.
1410 *
1411 * @since 1.0.0
1412 *
1413 * @param int $post_id Post ID.
1414 * @param string $taxonomy Taxonomy slug.
1415 *
1416 * @return array
1417 */
1418 function automatorwp_get_term_ids( $post_id, $taxonomy ) {
1419
1420 $terms = get_the_terms( $post_id, $taxonomy );
1421
1422 return ( empty( $terms ) || is_wp_error( $terms ) ) ? array() : wp_list_pluck( $terms, 'term_id' );
1423
1424 }
1425
1426 /**
1427 * Creates a toggleable list
1428 *
1429 * @since 1.0.0
1430 *
1431 * @param array $options
1432 *
1433 * @return string
1434 */
1435 function automatorwp_toggleable_options_list( $options ) {
1436
1437 // Bail if no options given
1438 if( ! is_array( $options ) ) {
1439 return '';
1440 }
1441
1442 $show_text = __( 'Show options', 'automatorwp' );
1443 $hide_text = __( 'Hide options', 'automatorwp' );
1444
1445 $html = '<a href="#" class="automatorwp-toggleable-options-list-toggle" data-show-text="' . $show_text . '" data-hide-text="' . $hide_text . '">' . $show_text . '</a>'
1446 . '<ul class="automatorwp-toggleable-options-list" style="display: none;">';
1447
1448 foreach( $options as $option ) {
1449 $html .= "<li>{$option}</li>";
1450 }
1451
1452 $html .= '</ul>';
1453
1454 return $html;
1455
1456 }
1457
1458 /**
1459 * Helper function to get all editable roles included if get_editable_roles() doesn't exists
1460 *
1461 * @since 1.1.5
1462 *
1463 * @return array[]|mixed|void
1464 */
1465 function automatorwp_get_editable_roles() {
1466
1467 if( function_exists('get_editable_roles' ) ) {
1468 $roles = get_editable_roles();
1469 } else {
1470 $roles = wp_roles()->roles;
1471
1472 $roles = apply_filters( 'editable_roles', $roles );
1473 }
1474
1475 return $roles;
1476
1477 }
1478
1479 /**
1480 * Helper function to pull all values of an array to a single level
1481 *
1482 * -------------------------------------
1483 * Turns keyed arrays:
1484 * 'key_1' => array(
1485 * 'sub_1' => 'foo',
1486 * 'sub_2' => array(
1487 * 'sub_1' => 'bar',
1488 * ),
1489 * ),
1490 *
1491 * Into:
1492 * 'key_1' => 'foo, bar'
1493 * 'key_1/sub_1' => 'foo',
1494 * 'key_1/sub_2' => 'bar'
1495 * 'key_1/sub_2/sub_1' => 'bar',
1496 * -------------------------------------
1497 * Turns numeric arrays:
1498 * 'key' => array(
1499 * 'foo',
1500 * array(
1501 * 'bar',
1502 * ),
1503 * ),
1504 *
1505 * Into:
1506 * 'key' => 'foo, bar'
1507 * 'key/0' => 'foo',
1508 * 'key/1' => 'bar'
1509 * 'key/1/0' => 'bar',
1510 * -------------------------------------
1511 *
1512 * @since 1.4.4
1513 *
1514 * @param array $array
1515 * @param string $separator
1516 *
1517 * @return array
1518 */
1519 function automatorwp_utilities_pull_array_values( $array = array(), $separator = '/' ) {
1520
1521 $new_array = array();
1522
1523 foreach( $array as $key => $value ) {
1524
1525 if( is_array( $value ) ) {
1526 // Pull all sub values
1527 $value = automatorwp_utilities_pull_array_values( $value );
1528 }
1529
1530 $new_array[$key] = $value;
1531
1532 if( is_array( $value ) ) {
1533
1534 // Add new entries with the sub values
1535 foreach( $value as $sub_key => $sub_value ) {
1536 $new_array[$key . $separator . $sub_key] = $sub_value;
1537 }
1538
1539 // Implode all sub values on the main key
1540 $new_array[$key] = implode( ', ', $value );
1541 }
1542
1543 }
1544
1545 return $new_array;
1546
1547 }
1548
1549 /**
1550 * Helper function to search an array key value recursively
1551 *
1552 * @since 1.4.5
1553 *
1554 * @param string|array $key
1555 * @param array $haystack
1556 *
1557 * @return mixed
1558 */
1559 function automatorwp_get_array_key_value( $key = '', $haystack = array() ) {
1560
1561 // Support for $key as array
1562 if( is_array( $key ) ) {
1563 $results = array();
1564
1565 foreach( $key as $key_item ) {
1566 $found = automatorwp_get_array_key_value( $key_item, $haystack );
1567
1568 if ( $found ) {
1569 $results[] = $found;
1570 }
1571 }
1572
1573 // Return the entire list of results found
1574 if( ! empty( $results ) ) {
1575 return $results;
1576 }
1577
1578 return false;
1579 }
1580
1581 // Return if directly found the index in the array
1582 if( isset( $haystack[$key] ) ) {
1583 return $haystack[$key];
1584 }
1585
1586 // If $haystack is a multilevel array, search in all its levels
1587 foreach( $haystack as $value) {
1588
1589 if ( is_array( $value ) ) {
1590
1591 $found = automatorwp_get_array_key_value( $key, $value );
1592
1593 if ( $found ) {
1594 return $found;
1595 }
1596
1597 }
1598
1599 }
1600
1601 return false;
1602
1603 }
1604
1605 /**
1606 * Helper function to check if a string starts by needle string given
1607 *
1608 * @since 1.4.5
1609 *
1610 * @param string $haystack
1611 * @param string $needle
1612 *
1613 * @return bool
1614 */
1615 function automatorwp_starts_with( $haystack, $needle ) {
1616 return strncmp( $haystack, $needle, strlen( $needle ) ) === 0;
1617 }
1618
1619 /**
1620 * Helper function to check if a string ends by needle string given
1621 *
1622 * @since 1.4.5
1623 *
1624 * @param string $haystack
1625 * @param string $needle
1626 *
1627 * @return bool
1628 */
1629 function automatorwp_ends_with( $haystack, $needle ) {
1630 return $needle === '' || substr_compare( $haystack, $needle, -strlen( $needle ) ) === 0;
1631 }
1632
1633 /**
1634 * Helper function to parse the function args option
1635 *
1636 * @since 1.7.9
1637 *
1638 * @param array $args Expects an array like: array( array( 'value' => 'value_1' ), array( 'value' => 'value_2' ) )
1639 * @param stdClass $item
1640 * @param int $user_id
1641 * @param array $options
1642 * @param stdClass $automation
1643 *
1644 * @return array
1645 */
1646 function automatorwp_parse_function_args_option( $args, $item, $user_id, $options, $automation ) {
1647
1648 foreach( $args as $key => $param ) {
1649
1650 $value = automatorwp_parse_automation_tags( $automation->id, $user_id, $param['value'] );
1651
1652 // Check if isn't numeric and starting with 0
1653 if( ( is_numeric( $value ) && automatorwp_starts_with( $value, 0 ) ) === false ) {
1654 $value = automatorwp_parse_function_arg_value( $value );
1655 }
1656
1657 $args[$key] = $value;
1658
1659 }
1660
1661 /**
1662 * Filter available to override the function args option parsed
1663 *
1664 * @since 1.7.9
1665 *
1666 * @param array $args
1667 * @param stdClass $item
1668 * @param int $user_id
1669 * @param array $options
1670 * @param stdClass $automation
1671 *
1672 * @return array
1673 */
1674 return apply_filters( 'automatorwp_parse_function_args_option', $args, $item, $user_id, $options, $automation );
1675
1676 }
1677
1678 /**
1679 * Helper function to parse the function arg value
1680 * Turn values like "null" to null or "array()" to array()
1681 *
1682 * @since 1.7.9
1683 *
1684 * @param mixed $value
1685 *
1686 * @return mixed
1687 */
1688 function automatorwp_parse_function_arg_value( $value ) {
1689
1690 $original_value = $value;
1691
1692 // Check PHP objects
1693 switch ( $value ) {
1694 case 'null':
1695 $value = null;
1696 break;
1697 case 'true':
1698 $value = true;
1699 break;
1700 case 'TRUE':
1701 $value = TRUE;
1702 break;
1703 case 'false':
1704 $value = false;
1705 break;
1706 case 'FALSE':
1707 $value = FALSE;
1708 break;
1709 case 'array()':
1710 case '[]':
1711 $value = array();
1712 break;
1713 }
1714
1715 // Check possible numeric values
1716 if( is_numeric( $value ) ) {
1717 if ( strpos( $value , '.') !== false ) {
1718 $value = (float) $value ;
1719 } else {
1720 $value = (int) $value;
1721 }
1722 }
1723
1724 /**
1725 * Filter available to override the function arg value parsed
1726 *
1727 * @since 1.7.9
1728 *
1729 * @param mixed $value
1730 *
1731 * @return mixed
1732 */
1733 return apply_filters( 'automatorwp_parse_function_arg_value', $value, $original_value );
1734
1735 }
1736
1737 /**
1738 * Helper function to parse a condition to SQL
1739 *
1740 * @since 1.9.3
1741 *
1742 * @param string $field The field key
1743 * @param string $condition The condition
1744 * @param string $value The field value
1745 * @param bool $strict True to force check the value type, false to always check the value as string
1746 *
1747 * @return string
1748 */
1749 function automatorwp_utilities_parse_condition_to_sql( $field, $condition, $value, $strict = true ) {
1750
1751 global $wpdb;
1752
1753 $operator = '=';
1754
1755 switch( $condition ) {
1756 case 'equal':
1757 $operator = '=';
1758 break;
1759 case 'not_equal':
1760 $operator = '!=';
1761 break;
1762 case 'contains':
1763 case 'start_with':
1764 case 'ends_with':
1765 $operator = 'LIKE';
1766 break;
1767 case 'not_contains':
1768 case 'not_start_with':
1769 case 'not_ends_with':
1770 $operator = 'NOT LIKE';
1771 break;
1772 case 'less_than':
1773 $operator = '<';
1774 break;
1775 case 'greater_than':
1776 $operator = '>';
1777 break;
1778 case 'less_or_equal':
1779 $operator = '<=';
1780 break;
1781 case 'greater_or_equal':
1782 $operator = '>=';
1783 break;
1784 default:
1785 $operator = '=';
1786 break;
1787 }
1788
1789 $string_required_conditions = array(
1790 'contains',
1791 'not_contains',
1792 'start_with',
1793 'not_start_with',
1794 'ends_with',
1795 'not_ends_with',
1796 );
1797
1798 $pattern = '%s';
1799
1800 if( $strict && is_numeric( $value ) && ! in_array( $condition, $string_required_conditions ) ) {
1801 if ( strpos( $value , '.') !== false ) {
1802 $value = (float) $value ;
1803 $pattern = '%f';
1804 } else {
1805 $value = (int) $value;
1806 $pattern = '%d';
1807 }
1808 } else {
1809
1810 if ( in_array( $condition, $string_required_conditions ) )
1811 $value = $wpdb->esc_like( $value );
1812
1813 $value = esc_sql( $value );
1814
1815 switch( $condition ) {
1816 case 'contains':
1817 case 'not_contains':
1818 $value = "%{$value}%";
1819 break;
1820 case 'start_with':
1821 case 'not_start_with':
1822 $value = "{$value}%";
1823 break;
1824 case 'ends_with':
1825 case 'not_ends_with':
1826 $value = "%{$value}";
1827 break;
1828 }
1829
1830 }
1831
1832 return $wpdb->prepare( "{$field} {$operator} {$pattern}", array( $value ) );
1833
1834 }
1835
1836 /**
1837 * Helper function to implode array elements with a special glue for the last item
1838 * Example: array( 1, 2, 3 ) imploded to "1, 2 and 3"
1839 *
1840 * @since 2.0.4
1841 *
1842 * @param string $glue Glue to join array items
1843 * @param string $glue_for_last_item Glue to join the last item
1844 * @param array $array The array to implode
1845 *
1846 * @return string
1847 */
1848 function automatorwp_implode_array( $glue = '', $glue_for_last_item = '', $array = array() ) {
1849
1850 $last = array_slice( $array, -1 );
1851 $first = join( $glue, array_slice( $array, 0, -1 ) );
1852 $both = array_filter( array_merge( array( $first ), $last ), 'strlen' );
1853
1854 return join( $glue_for_last_item, $both );
1855
1856 }
1857
1858 /**
1859 * Utility function to get the operator field
1860 *
1861 * @since 1.0.0
1862 *
1863 * @return array
1864 */
1865 function automatorwp_utilities_operator_field() {
1866
1867 return array(
1868 'name' => __( 'Operator:', 'automatorwp' ),
1869 'type' => 'select',
1870 'options' => array(
1871 'AND' => __( 'AND', 'automatorwp' ),
1872 'OR' => __( 'OR', 'automatorwp' ),
1873 ),
1874 'default' => 'AND'
1875 );
1876
1877 }
1878
1879 /**
1880 * Utility function to get the post fields
1881 *
1882 * @since 1.0.0
1883 *
1884 * @return array
1885 */
1886 function automatorwp_utilities_get_post_fields() {
1887
1888 $post_fields = array(
1889 'ID' => __( 'ID', 'automatorwp' ),
1890 'post_title' => __( 'Title', 'automatorwp' ),
1891 'post_name' => __( 'Slug', 'automatorwp' ),
1892 'post_type' => __( 'Type', 'automatorwp' ),
1893 'post_status' => __( 'Status', 'automatorwp' ),
1894 'post_date' => __( 'Date', 'automatorwp' ),
1895 'post_date_gmt' => __( 'Date (GMT)', 'automatorwp' ),
1896 'post_modified' => __( 'Date Modified', 'automatorwp' ),
1897 'post_modified_gmt' => __( 'Date Modified (GMT)', 'automatorwp' ),
1898 'post_author' => __( 'Author', 'automatorwp' ),
1899 'post_content' => __( 'Content', 'automatorwp' ),
1900 'post_excerpt' => __( 'Excerpt', 'automatorwp' ),
1901 'post_parent' => __( 'Parent', 'automatorwp' ),
1902 'menu_order' => __( 'Order', 'automatorwp' ),
1903 'post_password' => __( 'Password', 'automatorwp' ),
1904 );
1905
1906 $post_fields = apply_filters( 'automatorwp_utilities_get_post_fields', $post_fields );
1907
1908 return $post_fields;
1909
1910 }
1911
1912 /**
1913 * Utility function to get the user fields
1914 *
1915 * @since 1.0.0
1916 *
1917 * @return array
1918 */
1919 function automatorwp_utilities_get_user_fields() {
1920
1921 $user_fields = array(
1922 'ID' => __( 'ID', 'automatorwp' ),
1923 'user_login' => __( 'Username', 'automatorwp' ),
1924 'user_email' => __( 'Email', 'automatorwp' ),
1925 'display_name' => __( 'Display name', 'automatorwp' ),
1926 'user_nicename' => __( 'Nicename', 'automatorwp' ),
1927 'user_url' => __( 'Website', 'automatorwp' ),
1928 'user_registered' => __( 'Registration Date', 'automatorwp' ),
1929 );
1930
1931 $user_fields = apply_filters( 'automatorwp_utilities_get_user_fields', $user_fields );
1932
1933 return $user_fields;
1934
1935 }
1936
1937 /**
1938 * Get all public post types slugs
1939 *
1940 * @since 6.0.0
1941 *
1942 * @return array
1943 */
1944 function automatorwp_get_post_types_slugs() {
1945 $post_types = get_post_types( array( 'public' => true ), 'objects' );
1946
1947 return is_array( $post_types ) ? array_keys( $post_types ) : array();
1948 }
1949
1950 /**
1951 * Validates if value is in array ensuring to return a valid entry from the array
1952 *
1953 * @since 6.0.0
1954 *
1955 * @param string $value
1956 * @param array $array
1957 * @param mixed $default
1958 *
1959 * @return mixed
1960 */
1961 function automatorwp_validate_from_array( $value, $array, $default = '' ) {
1962 return is_array( $array ) && in_array( $value, $array ) ? $value : $default;
1963 }
1964
1965 /**
1966 * Get a random array entry for associative arrays
1967 * For arrays without keys, use PHP array_rand() instead
1968 *
1969 * @since 6.0.0
1970 *
1971 * @param array $array
1972 * @param mixed $default
1973 *
1974 * @return mixed
1975 */
1976 function automatorwp_assoc_array_rand( $array, $default = false ) {
1977
1978 if( is_array( $array ) && count( $array ) ) {
1979 $keys = array_keys( $array );
1980
1981 if( count( $keys ) === 1 ) {
1982 return $array[ $keys[0] ];
1983 } else {
1984 return $array[ $keys[ array_rand( $keys ) ] ];
1985 }
1986 }
1987
1988 return $default;
1989 }
1990
1991 /**
1992 * Join a string with a natural language conjunction at the end (one, two, three or four).
1993 *
1994 * @since 6.0.0
1995 *
1996 * @param array $array Array of words
1997 * @param string $join and|or
1998 *
1999 * @return string
2000 */
2001 function automatorwp_join_words( $array, $join = '' ) {
2002
2003 if( $join === '' ) $join = __( 'and', 'automatorwp' );
2004
2005 $last = array_pop($array);
2006
2007 if ($array) return implode(', ', $array) . ' ' . $join . ' ' . $last;
2008
2009 return $last;
2010
2011 }