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 / create-automation.php

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

173 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Create Automation Ability
4 *
5 * @package AutomatorWP\AI_Assistant\Ability\Create_Automation
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_Create_Automation extends AutomatorWP_AI_Assistant_Ability {
13
14 public $ability = 'create-automation';
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__( 'Create an automation in AutomatorWP', 'automatorwp' ),
25 'description' => esc_html__( 'Design and insert a new automation into the database.', 'automatorwp' ),
26 'input_schema' => array(
27 'type' => 'object',
28 'properties' => array(
29 'title' => array(
30 'description' => esc_html__( 'A clean, professional, and descriptive title for the automation.', 'automatorwp' ),
31 'type' => 'string',
32 ),
33 'type' => array(
34 'description' => __( 'The automation type. By default, "user" (for logged in users).', 'automatorwp' ),
35 'type' => 'string',
36 'enum' => array_keys( automatorwp_get_automation_types() ),
37 ),
38 'expiration' => array(
39 'description' => __( 'The automation expiration datetime (YYYY-MM-DD HH:MM:SS).', 'automatorwp' ),
40 'type' => 'string',
41 'format' => 'date-time',
42 ),
43 'triggers' => array(
44 'description' => esc_html__( 'List of triggers that launches the automation.', 'automatorwp' ),
45 'type' => 'array',
46 'items' => array(
47 'type' => 'object',
48 'properties' => automatorwp_ai_assistant_get_items_properties( 'trigger' ),
49 ),
50 ),
51 'actions' => array(
52 'description' => esc_html__( 'List of action that should be executed when the triggers are meet.', 'automatorwp' ),
53 'type' => 'array',
54 'items' => array(
55 'type' => 'object',
56 'properties' => automatorwp_ai_assistant_get_items_properties( 'action' ),
57 ),
58 ),
59 ),
60 'required' => array( 'title', 'type' ),
61 'additionalProperties' => false,
62 ),
63 );
64
65 }
66
67 /**
68 * Ability execute callback
69 *
70 * @since 1.0.0
71 *
72 * @param array $args
73 *
74 * @return array array( 'success' => true|false, 'message' => '' )
75 */
76 public function execute( $args ) {
77
78 $args = wp_parse_args( (array) $args, array(
79 'title' => '',
80 'type' => 'user',
81 'expiration' => '',
82 'triggers' => array(),
83 'actions' => array(),
84 ) );
85
86 $title = sanitize_text_field( $args['title'] );
87 $type = sanitize_key( strtolower( $args['type'] ) );
88 $expiration = sanitize_text_field( $args['expiration'] );
89 $triggers = $args['triggers'];
90 $actions = $args['actions'];
91
92 // Sanitize automation type
93 $type = automatorwp_validate_from_array( $type, array_keys( automatorwp_get_automation_types() ), 'user' );
94
95 // Check the expiration date
96 $expiration_date = '';
97
98 if( $expiration !== '' ) {
99 $expiration_time = strtotime( $expiration );
100
101 if( $expiration_time )
102 $expiration_date = date( 'Y-m-d H:i:s', $expiration_time );
103 }
104
105 $automation = array(
106 'title' => ! empty( $title ) ? $title : __( '(no title)', 'automatorwp' ),
107 'type' => $type,
108 'user_id' => get_current_user_id(),
109 'sequential' => 0,
110 'times_per_user' => 1,
111 'times' => 0,
112 'status' => 'inactive',
113 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
114 'expiration' => $expiration_date,
115 );
116
117 ct_setup_table( "automatorwp_automations" );
118 $automation_id = ct_insert_object( $automation );
119 ct_reset_setup_table();
120
121 if( is_wp_error( $automation_id ) ) {
122 // translators: %1$s: Points type singular %2$s: Error messages
123 return $this->response_error( sprintf( __( 'I couldn\'t create the %1$s. Reason: %2$s.', 'automatorwp' ), $title, $automation_id->get_error_message() ) );
124 }
125
126 // Triggers & Actions
127 $updated_triggers = automatorwp_ai_assistant_process_items(
128 $triggers,
129 $automation_id,
130 'trigger'
131 );
132
133 $updated_actions = automatorwp_ai_assistant_process_items(
134 $actions,
135 $automation_id,
136 'action'
137 );
138
139 // Final Response
140 $triggers_count = count( $updated_triggers );
141 $actions_count = count( $updated_actions );
142
143 $triggers_answer = automatorwp_ai_assistant_get_items_answer( $updated_triggers, 'trigger' );
144 $actions_answer = automatorwp_ai_assistant_get_items_answer( $updated_actions, 'action' );
145
146 $automation_display = $automation['title'];
147
148 if ( current_user_can( automatorwp_get_manager_capability() ) )
149 $automation_display = '[' . $automation_display . '](' . ct_get_edit_link( 'automatorwp_automations', $automation_id ) . ')';
150
151 if( $triggers_count > 0 && $actions_count > 0 ) {
152 $message = sprintf( __( 'I created the %s automation with:', 'automatorwp' ), $automation_display )
153 . "\n"
154 . $triggers_answer
155 . $actions_answer;
156 } else if( $triggers_count > 0 && $actions_count === 0 ) {
157 $message = sprintf( __( 'I created the %s automation with:', 'automatorwp' ), $automation_display )
158 . "\n"
159 . $triggers_answer;
160 } else if( $triggers_count === 0 && $actions_count > 0 ) {
161 $message = sprintf( __( 'I created the %s automation with:', 'automatorwp' ), $automation_display )
162 . "\n"
163 . $actions_answer;
164 } else {
165 $message = sprintf( __( 'I created the %s automation.', 'automatorwp' ), $automation_display );
166 }
167
168 return $this->response_success( $message );
169
170 }
171
172 }
173 new AutomatorWP_AI_Assistant_Ability_Create_Automation();