PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.0
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.0
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 / abilities / run-actions.php

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

143 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Run Actions Ability
4 *
5 * @package AutomatorWP\AI_Assistant\Ability\Run_Actions
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 class AutomatorWP_AI_Assistant_Ability_Run_Actions extends AutomatorWP_AI_Assistant_Ability {
13
14 public $ability = 'run-actions';
15
16 /**
17 * Register the ability
18 *
19 * @since 1.0.0
20 */
21 public function register() {
22
23 $this->args = array(
24 'label' => esc_html__( 'Run or execute AutomatorWP actions', 'automatorwp' ),
25 'description' => esc_html__( 'Runs actions directly, like a function call.', 'automatorwp' ),
26 'input_schema' => array(
27 'type' => 'object',
28 'properties' => array(
29 'actions' => array(
30 'description' => esc_html__( 'List of action that should be executed.', 'automatorwp' ),
31 'type' => 'array',
32 'items' => array(
33 'type' => 'object',
34 'properties' => automatorwp_ai_assistant_get_items_properties( 'action' ),
35 ),
36 ),
37 ),
38 'required' => array( 'actions' ),
39 'additionalProperties' => false,
40 ),
41 );
42
43 }
44
45 /**
46 * Ability execute callback
47 *
48 * @since 1.0.0
49 *
50 * @param array $args
51 *
52 * @return array array( 'success' => true|false, 'message' => '' )
53 */
54 public function execute( $args ) {
55
56 $args = wp_parse_args( (array) $args, array(
57 'actions' => array(),
58 ) );
59
60 $actions = $args['actions'];
61 $run = array();
62
63 if( is_array( $actions ) && count( $actions ) ) {
64
65 foreach( $actions as $k => $i ) {
66
67 $i = wp_parse_args( $i, array(
68 'type' => '',
69 'options' => array(),
70 ) );
71
72 $i['type'] = sanitize_key( trim( $i['type'] ) );
73
74 $type_args = automatorwp_get_action( $i['type'] );
75
76 // Bail if not is a valid type
77 if( ! $type_args ) continue;
78
79 $action = ( object ) array( 'type' => $i['type'] );
80 $user_id = 0;
81 $event = array(
82 'trigger' => 'automatorwp_ai_assistant_run_action',
83 'anonymous' => true,
84 'user_id' => $user_id,
85 );
86 $options = automatorwp_ai_assistant_build_options( $i['options'], $type_args );
87 $automation = ( object ) array( 'id' => 0 );
88
89 //do_action( 'automatorwp_execute_action', $action, $user_id, $event, $options, $automation );
90 do_action( 'automatorwp_execute_action_' . $i['type'], $action, $user_id, $event, $options, $automation );
91
92 $log_meta = apply_filters( 'automatorwp_user_completed_action_log_meta', array(), $action, $user_id, $options, $automation );
93 $post_id = apply_filters( 'automatorwp_user_completed_action_post_id', 0, $action, $user_id, $event, $options, $automation );
94
95 $log_meta['run_action_type'] = $i['type'];
96 $log_meta['run_action_options'] = automatorwp_ai_assistant_sanitize_options( $i['options'] );
97 $log_meta['run_action_parsed_options'] = $options;
98 $log_meta['ai_assistant_prompt'] = isset( $_POST['prompt'] ) ? sanitize_text_field( $_POST['prompt'] ) : '';
99
100 // Insert a new log entry to register the action run completion
101 $log_id = automatorwp_insert_log( array(
102 'title' => $type_args['label'],
103 'type' => 'run_action',
104 'object_id' => 0,
105 'user_id' => get_current_user_id(),
106 'post_id' => $post_id,
107 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) + count( $run ) ),
108 ), $log_meta );
109
110 $run[] = array(
111 'action' => $type_args['label'],
112 'options' => $options,
113 'log_id' => $log_id,
114 'log_meta' => $log_meta,
115 );
116 }
117 }
118
119 // Final response
120 $count = count( $run );
121 $label = _n( 'action', 'actions', $count, 'automatorwp' );
122
123 // translators: %1$s: Actions label.
124 $message = sprintf( __( 'I executed the following %1$s:', 'automatorwp' ), $label );
125 $message .= "\n";
126
127 $can_manage = current_user_can( automatorwp_get_manager_capability() );
128
129 foreach( $run as $r ) {
130 $message .= '- ' . esc_html( $r['action'] );
131
132 if( $can_manage && $r['log_id'] )
133 $message .= ' ([' . esc_html__( 'View details' ) . '](' . ct_get_edit_link( 'automatorwp_logs', $r['log_id'] ) . '))';
134
135 $message .= "\n";
136 }
137
138 return $this->response_success( $message );
139
140 }
141
142 }
143 new AutomatorWP_AI_Assistant_Ability_Run_Actions();