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 / actions.php

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

382 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Actions
4 *
5 * @package AutomatorWP\Actions
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 * Registers a new action
14 *
15 * @since 1.0.0
16 *
17 * @param string $action The action key
18 * @param array $args The action arguments
19 */
20 function automatorwp_register_action( $action, $args ) {
21
22 $args = wp_parse_args( $args, array(
23 'integration' => '',
24 'label' => '',
25 'select_option' => '',
26 'show_in_selector' => true,
27 'edit_label' => '',
28 'options' => array(),
29 'tags' => array(),
30 ) );
31
32 /**
33 * Filter to extend registered action arguments
34 *
35 * @since 1.0.0
36 *
37 * @param array $args The action arguments
38 * @param string $action The action key
39 *
40 * @return array
41 */
42 $args = apply_filters( 'automatorwp_register_action_args', $args, $action );
43
44 // Sanitize options setup
45 foreach( $args['options'] as $option => $option_args ) {
46
47 if( in_array( $option, array( 'action', 'nonce', 'id', 'item_type', 'option_name' ) ) ) {
48 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Action "%s" has the option key "%s" that is not allowed', 'automatorwp' ), $action, $option ), null );
49 return;
50 }
51
52 }
53
54 $log_duplications = apply_filters( 'automatorwp_log_duplications', false );
55
56 if( $log_duplications && isset( AutomatorWP()->actions[$action] ) ) {
57 error_log( sprintf( __( 'Possible action duplication with the key "%s"', 'automatorwp' ), $action ) );
58 }
59
60 AutomatorWP()->actions[$action] = $args;
61
62 }
63
64 /**
65 * Get registered actions
66 *
67 * @since 1.0.0
68 *
69 * @return array
70 */
71 function automatorwp_get_actions() {
72
73 return AutomatorWP()->actions;
74
75 }
76
77 /**
78 * Get an action
79 *
80 * @since 1.0.0
81 *
82 * @param string $action
83 *
84 * @return array|false
85 */
86 function automatorwp_get_action( $action ) {
87
88 $action_args = ( isset( AutomatorWP()->actions[$action] ) ? AutomatorWP()->actions[$action] : false );
89
90 /**
91 * Available filter to override the action args
92 *
93 * @since 1.0.0
94 *
95 * @param array|false $action_args
96 * @param string $action
97 *
98 * @return array|false
99 */
100 return apply_filters( 'automatorwp_get_action', $action_args, $action );
101
102 }
103
104 /**
105 * Get an integration actions
106 *
107 * @since 1.0.0
108 *
109 * @param string $integration The integration key
110 * @param array $filters Filters to filter triggers by args
111 *
112 * @return array
113 */
114 function automatorwp_get_integration_actions( $integration, $filters = array() ) {
115
116 $actions = array();
117
118 foreach( AutomatorWP()->actions as $action => $args ) {
119
120 if( $args['integration'] !== $integration ) {
121 continue;
122 }
123
124 // If filters defined, apply them
125 if( is_array( $filters ) && ! empty( $filters ) ) {
126
127 $pass_filters = true;
128
129 foreach( $filters as $filter_key => $filter_value ) {
130
131 // Check if argument exists
132 if( ! isset( $args[$filter_key] ) ) {
133 $pass_filters = false;
134 }
135
136 // Check if argument value matches
137 if( $args[$filter_key] !== $filter_value ) {
138 $pass_filters = false;
139 }
140
141 }
142
143 // Skip this item if filters not passed
144 if( ! $pass_filters ) {
145 continue;
146 }
147 }
148
149 $actions[$action] = $args;
150
151 }
152
153 /**
154 * Available filter to extend integration actions
155 *
156 * @since 1.0.0
157 *
158 * @param array $actions
159 * @param string $integration
160 *
161 * @return array
162 */
163 return apply_filters( 'automatorwp_get_integration_actions', $actions, $integration );
164
165 }
166
167 /**
168 * Get the action object data
169 *
170 * @param int $action_id The action ID
171 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
172 * a object, an associative array, or a numeric array, respectively. Default OBJECT.
173 * @return array|stdClass|null
174 */
175 function automatorwp_get_action_object( $action_id, $output = OBJECT ) {
176
177 ct_setup_table( 'automatorwp_actions' );
178
179 $action = ct_get_object( $action_id );
180
181 ct_reset_setup_table();
182
183 return $action;
184
185 }
186
187 /**
188 * Get the action object data
189 *
190 * @param int $action_id The action ID
191 * @param string $meta_key Optional. The meta key to retrieve. By default, returns
192 * data for all keys. Default empty.
193 * @param bool $single Optional. Whether to return a single value. Default false.
194 *
195 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true.
196 */
197 function automatorwp_get_action_meta( $action_id, $meta_key = '', $single = false ) {
198
199 ct_setup_table( 'automatorwp_actions' );
200
201 $meta_value = ct_get_object_meta( $action_id, $meta_key, $single );
202
203 ct_reset_setup_table();
204
205 return $meta_value;
206
207 }
208
209 /**
210 * Get the action's automation object
211 *
212 * @since 1.0.0
213 *
214 * @param int $action_id The action ID
215 *
216 * @return stdClass|false The automation object or false
217 */
218 function automatorwp_get_action_automation( $action_id ) {
219
220 $action = automatorwp_get_action_object( $action_id );
221
222 if( ! $action ) {
223 return false;
224 }
225
226 $automation = automatorwp_get_automation_object( $action->automation_id );
227
228 return $automation;
229
230 }
231
232 /**
233 * Get the action's stored options
234 *
235 * @since 1.0.0
236 *
237 * @param int $action_id The action ID
238 * @param bool $single_level The level of options array, if is set to try, only option fields will be returned
239 *
240 * @return array The action options.
241 * Stored options format wit $single_level=true:
242 * array(
243 * 'field_id' => 'value'
244 * )
245 * Stored options format wit $single_level=false:
246 * array(
247 * 'option' => array(
248 * 'field_id' => 'value'
249 * )
250 * )
251 */
252 function automatorwp_get_action_stored_options( $action_id, $single_level = true ) {
253
254 $object = automatorwp_get_action_object( $action_id );
255
256 if( ! $object ) {
257 return array();
258 }
259
260 $action = automatorwp_get_action( $object->type );
261
262 if( ! $action ) {
263 return array();
264 }
265
266 ct_setup_table( 'automatorwp_actions' );
267
268 $options = array();
269
270 foreach( $action['options'] as $option => $option_args ) {
271
272 if( ! isset( $option_args['fields'] ) ) {
273 continue;
274 }
275
276 if( ! $single_level ) {
277 $options[$option] = array();
278 }
279
280 foreach( $option_args['fields'] as $field_id => $field ) {
281
282 $value = ct_get_object_meta( $object->id, $field_id, true );
283
284 // Fallback to default attribute if value is empty
285 if( ( $value === '' || $value === null ) && isset( $field['default'] ) ) {
286 $value = $field['default'];
287 }
288
289 if( isset( $field['option_custom'] ) && $field['option_custom'] // If option_custom is enabled
290 && $value === $field['option_custom_value'] // Value is setup to use the custom value
291 && isset( $option_args['fields'][$field_id . '_custom'] ) ) { // Isset the custom field
292 $value = ct_get_object_meta( $object->id, $field_id . '_custom', true );
293 }
294
295 if( $single_level ) {
296 $options[$field_id] = $value;
297 } else {
298 $options[$option][$field_id] = $value;
299 }
300
301
302 }
303
304 }
305
306 ct_reset_setup_table();
307
308 return $options;
309
310 }
311
312 /**
313 * Check if action is in use
314 *
315 * @since 1.4.3
316 *
317 * @param string $action
318 *
319 * @return bool
320 */
321 function automatorwp_is_action_in_use( $action ) {
322
323 $actions_in_user = automatorwp_get_actions_in_use();
324
325 // Check if this action is not in use
326 return (bool) in_array( $action, $actions_in_user );
327
328 }
329
330 /**
331 * Get all actions in use
332 *
333 * @since 1.4.3
334 *
335 * @return array
336 */
337 function automatorwp_get_actions_in_use() {
338
339 global $wpdb;
340
341 $ct_table = ct_setup_table( 'automatorwp_actions' );
342
343 // Check if table exists, just to avoid issues on first install
344 if( ! $ct_table->db->exists() ) {
345 ct_reset_setup_table();
346 return array();
347 }
348
349 $cache = automatorwp_get_cache( 'actions_in_use', false, false );
350
351 // If result already cached, return it
352 if( is_array( $cache ) ) {
353 ct_reset_setup_table();
354 return $cache;
355 }
356
357 $cache = get_transient( 'automatorwp_actions_in_use' );
358
359 // If result is in a transient, return it
360 if( is_array( $cache ) && count( $cache ) ) {
361 ct_reset_setup_table();
362 return $cache;
363 }
364
365 $actions_in_use = array();
366 $results = $wpdb->get_results( "SELECT t.type FROM {$ct_table->db->table_name} AS t GROUP BY t.type" );
367
368 ct_reset_setup_table();
369
370 if( is_array( $results ) && count( $results ) ) {
371 $actions_in_use = wp_list_pluck( $results, 'type' );
372 }
373
374 // Cache function result
375 automatorwp_set_cache( 'actions_in_use', $actions_in_use );
376
377 // Store the result for a day
378 set_transient( 'automatorwp_actions_in_use', $actions_in_use, HOUR_IN_SECONDS * 24 );
379
380 return $actions_in_use;
381
382 }