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
← All changes | integrations/automatorwp/actions/do-action.php +197 -0 5.6.1 → 6.0.3 View file →
@@ -1,0 +1,197 @@
1 +<?php
2 +/**
3 + * Do Action
4 + *
5 + * @package AutomatorWP\Integrations\WordPress\Actions\Do_Action
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_WordPress_Do_Action extends AutomatorWP_Integration_Action {
13 +
14 + /**
15 + * Initialize the trigger
16 + *
17 + * @since 1.0.0
18 + */
19 + public function __construct( $integration ) {
20 +
21 + $this->integration = $integration;
22 + $this->action = $integration . '_do_action';
23 +
24 + parent::__construct();
25 +
26 + }
27 +
28 + /**
29 + * Register the trigger
30 + *
31 + * @since 1.0.0
32 + */
33 + public function register() {
34 +
35 + automatorwp_register_action( $this->action, array(
36 + 'integration' => $this->integration,
37 + 'label' => __( 'Run a WordPress hook', 'automatorwp' ),
38 + 'select_option' => __( 'Run a <strong>WordPress hook</strong>', 'automatorwp' ),
39 + /* translators: %1$s: Hook name. */
40 + 'edit_label' => sprintf( __( 'Run %1$s', 'automatorwp' ), '{hook}' ),
41 + /* translators: %1$s: Hook name */
42 + 'log_label' => sprintf( __( 'Run %1$s', 'automatorwp' ), '{hook}' ),
43 + 'options' => array(
44 + 'hook' => array(
45 + 'from' => 'hook_name',
46 + 'default' => __( 'a WordPress hook', 'automatorwp' ),
47 + 'fields' => array(
48 + 'hook_name' => array(
49 + 'name' => __( 'Hook:', 'automatorwp' ),
50 + 'desc' => __( 'The hook to run.', 'automatorwp' ),
51 + 'type' => 'text',
52 + 'default' => ''
53 + ),
54 + 'hook_args' => array(
55 + 'name' => __( 'Variables:', 'automatorwp' ),
56 + 'desc' => __( 'The variables to pass to the hook.', 'automatorwp' ),
57 + 'type' => 'group',
58 + 'classes' => 'automatorwp-fields-table automatorwp-fields-table-col-1',
59 + 'options' => array(
60 + 'add_button' => __( 'Add variable', 'automatorwp' ),
61 + 'remove_button' => '<span class="dashicons dashicons-no-alt"></span>',
62 + ),
63 + 'fields' => array(
64 + 'value' => array(
65 + 'name' => __( 'Value:', 'automatorwp' ),
66 + 'type' => 'text',
67 + 'default' => ''
68 + ),
69 + ),
70 + ),
71 + )
72 + )
73 + ),
74 + ) );
75 +
76 + }
77 +
78 + /**
79 + * Action execution function
80 + *
81 + * @since 1.0.0
82 + *
83 + * @param stdClass $action The action object
84 + * @param int $user_id The user ID
85 + * @param array $action_options The action's stored options (with tags already passed)
86 + * @param stdClass $automation The action's automation object
87 + */
88 + public function execute( $action, $user_id, $action_options, $automation ) {
89 +
90 + // Shorthand
91 + $hook_name = $action_options['hook_name'];
92 + $hook_args = $action_options['hook_args'];
93 +
94 + // Bail if empty meta key
95 + if( empty( $hook_name ) ) {
96 + $this->result = __( 'Empty hook name.', 'automatorwp' );
97 + return;
98 + }
99 +
100 + if( ! is_array( $hook_args ) ) {
101 + $hook_args = array();
102 + }
103 +
104 + // Parse the hook args option
105 + $hook_args = automatorwp_parse_function_args_option( $hook_args, $action, $user_id, $action_options, $automation );
106 +
107 + // Place the hook name at start of the hook args
108 + array_unshift( $hook_args, $hook_name );
109 +
110 + try {
111 + // Try to call to the hook
112 + call_user_func_array( 'do_action', $hook_args );
113 +
114 + $this->result = sprintf( __( 'Hook "%s" run successfully.', 'automatorwp' ), $hook_name );
115 + } catch ( Error $e ) {
116 + // Notify about any error handled
117 + $this->result = sprintf( __( 'Hook "%s" throw the error: %s', 'automatorwp' ), $hook_name, $e->getMessage() );
118 + }
119 +
120 + }
121 +
122 + /**
123 + * Register required hooks
124 + *
125 + * @since 1.0.0
126 + */
127 + public function hooks() {
128 +
129 + // Log meta data
130 + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
131 +
132 + // Log fields
133 + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
134 +
135 + parent::hooks();
136 + }
137 +
138 + /**
139 + * Action custom log meta
140 + *
141 + * @since 1.0.0
142 + *
143 + * @param array $log_meta Log meta data
144 + * @param stdClass $action The action object
145 + * @param int $user_id The user ID
146 + * @param array $action_options The action's stored options (with tags already passed)
147 + * @param stdClass $automation The action's automation object
148 + *
149 + * @return array
150 + */
151 + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
152 +
153 + // Bail if action type don't match this action
154 + if( $action->type !== $this->action ) {
155 + return $log_meta;
156 + }
157 +
158 + $log_meta['result'] = $this->result;
159 +
160 + return $log_meta;
161 + }
162 +
163 + /**
164 + * Action custom log fields
165 + *
166 + * @since 1.0.0
167 + *
168 + * @param array $log_fields The log fields
169 + * @param stdClass $log The log object
170 + * @param stdClass $object The trigger/action/automation object attached to the log
171 + *
172 + * @return array
173 + */
174 + public function log_fields( $log_fields, $log, $object ) {
175 +
176 + // Bail if log is not assigned to an action
177 + if( $log->type !== 'action' ) {
178 + return $log_fields;
179 + }
180 +
181 + // Bail if action type don't match this action
182 + if( $object->type !== $this->action ) {
183 + return $log_fields;
184 + }
185 +
186 + $log_fields['result'] = array(
187 + 'name' => __( 'Result:', 'automatorwp' ),
188 + 'type' => 'text',
189 + );
190 +
191 + return $log_fields;
192 + }
193 +
194 +}
195 +
196 +new AutomatorWP_WordPress_Do_Action( 'automatorwp' );
197 +new AutomatorWP_WordPress_Do_Action( 'code' );