PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
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 / modules / ai-assistant / includes / functions.php

functions.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.3, at modules/ai-assistant/includes/functions.php

499 lines 12.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions
4 *
5 * @package AutomatorWP\AI_Assistant\Functions
6 * @since 1.0.0
7 * @version 1.0.0
8 */
9 // Exit if accessed directly
10 if ( ! defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Check if is a AutomatorWP page to render/enqueue our assets
14 *
15 * @since 1.0.0
16 */
17 function automatorwp_ai_assistant_is_valid_page() {
18
19 if ( ( isset( $_GET['page'] ) && (
20 $_GET['page'] === 'automatorwp'
21 || $_GET['page'] === 'automatorwp_automations'
22 || $_GET['page'] === 'edit_automatorwp_automations'
23 || $_GET['page'] === 'automatorwp_logs'
24 || $_GET['page'] === 'edit_automatorwp_logs'
25 || $_GET['page'] === 'automatorwp_settings'
26 || $_GET['page'] === 'automatorwp_licenses'
27 )
28 ) ) {
29 return true;
30 }
31
32 return false;
33 }
34
35 /**
36 * Get all triggers slugs
37 *
38 * @since 1.0.0
39 *
40 * @return array
41 */
42 function automatorwp_ai_assistant_get_triggers_slugs() {
43
44 $slugs = array();
45
46 $excluded = array( 'filter' );
47
48 foreach( AutomatorWP()->triggers as $trigger => $args ) {
49 if( in_array( $trigger, $excluded ) ) continue;
50
51 $slugs[] = $trigger;
52 }
53
54 return $slugs;
55
56 }
57
58 /**
59 * Get all actions slugs
60 *
61 * @since 1.0.0
62 *
63 * @return array
64 */
65 function automatorwp_ai_assistant_get_actions_slugs() {
66
67 $slugs = array();
68
69 $excluded = array( 'filter' );
70
71 foreach( AutomatorWP()->actions as $action => $args ) {
72 if( in_array( $action, $excluded ) ) continue;
73
74 $slugs[] = $action;
75 }
76
77 return $slugs;
78
79 }
80
81 /**
82 * Get all actions slugs
83 *
84 * @since 1.0.0
85 *
86 * @return string
87 */
88 function automatorwp_ai_assistant_get_tags_list() {
89
90 $tags = '';
91
92 foreach( automatorwp_get_tags() as $group => $group_data ) {
93 if( isset( $group_data['tags'] ) && is_array( $group_data['tags'] ) ) {
94 foreach( $group_data['tags'] as $tag => $data ) {
95 $tags .= '{' . $tag . '} ';
96 }
97 }
98 }
99
100 $tags .= '{times} ';
101
102 foreach( automatorwp_utilities_post_tags() as $tag => $data ) {
103 $tags .= '{' . $tag . '} ';
104 }
105
106 return $tags;
107
108 }
109
110 /**
111 * Get triggers options list
112 *
113 * @since 1.0.0
114 *
115 * @return string
116 */
117 function automatorwp_ai_assistant_get_triggers_options_list() {
118
119 $output = '';
120
121 $excluded = array( 'filter' );
122
123 $output .= 'All triggers support the "times" option.' . "\n";
124
125 foreach( AutomatorWP()->triggers as $trigger => $type_args ) {
126 if( in_array( $trigger, $excluded ) ) continue;
127
128 $output .= $trigger . ': ';
129
130 foreach( $type_args['options'] as $option => $args ) {
131 foreach( $args['fields'] as $field_id => $field_args ) {
132 if( $field_id === 'times' ) continue;
133
134 if( isset( $field_args['fields'] ) ) {
135 $output .= $field_id . ' [' . implode( ', ', array_keys( $field_args['fields'] ) ) . '], ';
136 } else {
137 $output .= $field_id . ', ';
138 }
139
140
141 }
142 }
143
144 $output .= "\n";
145 }
146
147 return $output;
148
149 }
150
151 /**
152 * Get actions options list
153 *
154 * @since 1.0.0
155 *
156 * @return string
157 */
158 function automatorwp_ai_assistant_get_actions_options_list() {
159
160 $output = '';
161
162 $excluded = array( 'filter', 'automatorwp_anonymous_user' );
163
164 foreach( AutomatorWP()->actions as $action => $type_args ) {
165 if( in_array( $action, $excluded ) ) continue;
166
167 $output .= $action . ': ';
168
169 foreach( $type_args['options'] as $option => $args ) {
170 foreach( $args['fields'] as $field_id => $field_args ) {
171 $output .= $field_id . ', ';
172 }
173 }
174
175 $output .= "\n";
176 }
177
178 return $output;
179
180 }
181
182 /**
183 * Get item ability properties
184 *
185 * @since 1.0.0
186 *
187 * @return array
188 */
189 function automatorwp_ai_assistant_get_items_properties( $item_type = '' ) {
190
191
192 if( $item_type === 'trigger' ) {
193 $label = __( 'trigger', 'automatorwp' );
194 $types = automatorwp_ai_assistant_get_triggers_slugs();
195 $options = automatorwp_ai_assistant_get_triggers_options_list();
196 } else {
197 $label = __( 'action', 'automatorwp' );
198 $types = automatorwp_ai_assistant_get_actions_slugs();
199 $options = automatorwp_ai_assistant_get_actions_options_list();
200 }
201
202 $tags_info = ' ' . esc_html__( 'You can use dynamic data named tags.', 'automatorwp' ) . ' ' . $options
203 . ' ' . esc_html__( 'List of tags:', 'automatorwp' ) . ' ' . automatorwp_ai_assistant_get_tags_list();
204
205 $properties = array(
206 'type' => array(
207 'description' => sprintf( esc_html__( 'The %s type.', 'automatorwp' ), $label ),
208 'type' => 'string',
209 'enum' => $types,
210 ),
211 'options' => array(
212 'description' => sprintf( esc_html__( 'The %s options.', 'automatorwp' ), $label )
213 . ' ' . esc_html__( 'List of options by type:', 'automatorwp' ) . ' ' . $options
214 . ( $item_type === 'action' ? $tags_info : '' ),
215 'type' => 'array',
216 'items' => array(
217 'type' => 'object',
218 'properties' => array(
219 'name' => array(
220 'description' => esc_html__( 'The option name.', 'automatorwp' ),
221 'type' => 'string',
222 ),
223 'value' => array(
224 'description' => esc_html__( 'The option value.', 'automatorwp' ),
225 'type' => 'string',
226 ),
227 ),
228 ),
229 ),
230 );
231
232
233
234 return $properties;
235
236 }
237
238 /**
239 * Process requirements
240 *
241 * @since 1.0.0
242 */
243 function automatorwp_ai_assistant_process_items( $items = array(), $automation_id = 0, $item_type = '' ) {
244
245 // Used to access ability helper functions
246 $ability = new AutomatorWP_AI_Assistant_Ability();
247
248 $updated_items = array();
249
250 ct_setup_table( "automatorwp_{$item_type}s" );
251
252 // Requirements
253 if( is_array( $items ) && count( $items ) ) {
254
255 foreach( $items as $k => $i ) {
256
257 $i = wp_parse_args( $i, array(
258 'type' => '',
259 'options' => array(),
260 ) );
261
262 $i['type'] = sanitize_key( trim( $i['type'] ) );
263
264 if( $item_type === 'trigger' ) {
265 $type_args = automatorwp_get_trigger( $i['type'] );
266 } else if( $item_type === 'action' ) {
267 $type_args = automatorwp_get_action( $i['type'] );
268 }
269
270 // Bail if not is a valid type
271 if( ! $type_args ) continue;
272
273 $object = array(
274 'automation_id' => $automation_id,
275 'title' => '',
276 'type' => $i['type'],
277 'status' => 'active',
278 'position' => $k,
279 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
280 );
281
282 // Create the new trigger/action
283 $id = ct_insert_object( $object );
284
285 // Couldn't insert the requirement
286 if( is_wp_error( $id ) ) continue;
287
288 // Append the id to the trigger/action data
289 $object['id'] = $id;
290
291 // Parse options given
292 $options = automatorwp_ai_assistant_build_options( $i['options'], $type_args );
293
294 // Update object metas
295 foreach( $options as $name => $value )
296 ct_update_object_meta( $id, $name, $value );
297
298
299 // Flush cache to ensure that option replacement gets the newest value
300 wp_cache_flush();
301
302 $title = automatorwp_parse_automation_item_edit_label( (object) $object, $item_type, 'view' );
303
304 // Update the trigger title
305 ct_update_object( array(
306 'id' => $id,
307 'title' => $title
308 ) );
309
310 $updated_items[] = $title;
311 }
312 }
313
314 ct_reset_setup_table();
315
316 return $updated_items;
317
318 }
319
320 /**
321 * Build item options
322 *
323 * @since 1.0.0
324 *
325 * @param array $options
326 * @param array $type_args
327 *
328 * @return array
329 */
330 function automatorwp_ai_assistant_build_options( $options, $type_args ) {
331
332 $options = automatorwp_ai_assistant_sanitize_options( $options );
333
334 // Loop all options looking for their fields
335 foreach( $type_args['options'] as $option => $args ) {
336
337 // Loop all option fields to initialize them
338 foreach( $args['fields'] as $field_id => $field_args ) {
339
340 $options[$field_id] = automatorwp_ai_assistant_get_option_value( $field_id, $field_args, $options );
341
342 }
343
344 }
345
346 return $options;
347
348 }
349
350 /**
351 * Sanitize items options
352 *
353 * @since 1.0.0
354 *
355 * @param array $options
356 *
357 * @return array
358 */
359 function automatorwp_ai_assistant_sanitize_options( $options ) {
360
361 $new = array();
362
363 foreach( $options as $option ) {
364 $option['name'] = sanitize_key( trim( $option['name'] ) );
365 $option['value'] = sanitize_text_field( $option['value'] );
366
367 $new[$option['name']] = $option['value'];
368 }
369
370 return $new;
371
372 }
373
374 /**
375 * Update option value
376 *
377 * @since 1.0.0
378 *
379 * @param string $field_id
380 * @param array $field_args
381 * @param array $options
382 */
383 function automatorwp_ai_assistant_get_option_value( $field_id, $field_args, $options ) {
384
385 $ability = new AutomatorWP_AI_Assistant_Ability();
386
387 if( ! isset( $options[$field_id] ) && isset( $field_args['default'] ) ) {
388 return $field_args['default'];
389 }
390
391 if( isset( $field_args['classes'] ) ) {
392 if( $field_args['classes'] === 'automatorwp-post-selector' ) {
393
394 $post_type = isset( $field_args['attributes'] ) && isset( $field_args['attributes']['data-post-type'] ) ? $field_args['attributes']['data-post-type'] : '';
395
396 $post = $ability->get_post( $options[$field_id], array( 'post_type' => $post_type ) );
397
398 if( ! $ability->is_response_error( $post ) ) {
399 $options[$field_id] = $post->ID;
400 }
401 } else if( $field_args['classes'] === 'automatorwp-ajax-selector' ) {
402
403 $action_cb = '';
404
405 if( isset( $field_args['attributes'] ) && isset( $field_args['attributes']['data-action'] ) ) {
406 $action_cb = $field_args['attributes']['data-action'];
407 }
408
409 if( $action_cb === '' ) return;
410
411 $result = null;
412
413 // Try to call to action_cb if defined
414 if( is_callable( $action_cb ) ) {
415 $result = call_user_func( $action_cb, $options[$field_id] );
416
417 // Expected result is an array of id & text keys
418 if( is_array( $result ) && isset( $result[0] ) && isset( $result[0]['id'] ) )
419 $result = $result[0]['id'];
420 }
421
422 /**
423 * Custom ajax selector search for AI (without ajax)
424 *
425 * @param mixed $result
426 * @param string $search
427 *
428 * @return mixed null if no results found, can return a response error
429 */
430 $result = apply_filters( 'automatorwp_ai_' . $action_cb, $result, $options[$field_id] );
431
432 if( $result === null ) return;
433
434 if( ! $ability->is_response_error( $result ) )
435 $options[$field_id] = $result;
436 else
437 $options[$field_id] = '';
438
439 }
440 }
441
442 if( $field_id === 'user' || $field_id === 'user_id' ) {
443 $user = $ability->get_user( $options[$field_id] );
444
445 if( ! $ability->is_response_error( $user ) ) {
446 $options[$field_id] = $user->ID;
447 }
448 }
449
450 if( isset( $options[$field_id] ) )
451 return $options[$field_id];
452
453 return '';
454
455 }
456
457 /**
458 * Get items answer
459 *
460 * @since 1.0.0
461 *
462 * @param array $items
463 * @param string $items_type
464 *
465 * @return string
466 */
467 function automatorwp_ai_assistant_get_items_answer( $items = array(), $items_type = '', $title = true ) {
468
469 // Bail if not requirements
470 if ( empty( $items ) ) {
471 return '';
472 }
473
474 $text = '';
475
476 $count = count( $items );
477
478 if( $title ) {
479 if( $items_type === 'trigger' ) {
480 $title = _n( 'Trigger', 'Triggers', $count, 'automatorwp' );
481 } else {
482 $title = _n( 'Action', 'Actions', $count, 'automatorwp' );
483 }
484
485 $text .= '####' . $title . "\n";
486 $text .= "\n";
487 }
488
489 foreach( $items as $item ) {
490
491 $title = $item;
492 $text .= '- ' . esc_html( $title ) . "\n";
493
494 }
495
496 $text .= "\n";
497
498 return $text;
499 }