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/wordpress/triggers/option-updated.php +294 -0 5.8.4 → 6.0.3 View file →
@@ -1,0 +1,294 @@
1 +<?php
2 +/**
3 + * Option Updated
4 + *
5 + * @package AutomatorWP\Integrations\WordPress\Triggers\Option_Updated
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_Option_Updated extends AutomatorWP_Integration_Trigger {
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->trigger = $integration . '_option_updated';
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_trigger( $this->trigger, array(
36 + 'integration' => $this->integration,
37 + 'label' => __( 'Option gets updated with a value', 'automatorwp' ),
38 + 'select_option' => __( 'Option <strong>gets updated</strong> with a value', 'automatorwp' ),
39 + /* translators: %1$s: Post title. %2$s: Key. %3$s: Condition. %4$s: Value. %5$s: Number of times. */
40 + 'edit_label' => sprintf( __( '%1$s option gets updated with a value %2$s %3$s %4$s time(s)', 'automatorwp' ), '{option}', '{condition}', '{value}', '{times}' ),
41 + /* translators: %1$s: Post title. %2$s: Key. %3$s: Condition. %4$s: Value. */
42 + 'log_label' => sprintf( __( '%1$s option gets updated with a value %2$s %3$s', 'automatorwp' ), '{option}', '{condition}', '{value}' ),
43 + 'action' => 'update_option',
44 + 'function' => array( $this, 'listener' ),
45 + 'priority' => 10,
46 + 'accepted_args' => 3,
47 + 'options' => array(
48 + 'condition' => automatorwp_utilities_condition_option(),
49 + 'option' => array(
50 + 'from' => 'option',
51 + /* translators: Refers to meta key */
52 + 'default' => __( 'Any', 'automatorwp' ),
53 + 'fields' => array(
54 + 'option' => array(
55 + 'name' => __( 'Option:', 'automatorwp' ),
56 + 'type' => 'text',
57 + 'default' => ''
58 + ),
59 + )
60 + ),
61 + 'value' => array(
62 + 'from' => 'value',
63 + 'default' => __( 'any value', 'automatorwp' ),
64 + 'fields' => array(
65 + 'value' => array(
66 + 'name' => __( 'Value:', 'automatorwp' ),
67 + 'type' => 'text',
68 + 'default' => ''
69 + ),
70 + )
71 + ),
72 + 'times' => automatorwp_utilities_times_option(),
73 + ),
74 + 'tags' => array_merge(
75 + array(
76 + 'option_name' => array(
77 + 'label' => __( 'Updated option name', 'automatorwp' ),
78 + 'type' => 'text',
79 + 'preview' => __( 'Name of the updated option', 'automatorwp' ),
80 + ),
81 + 'option_new_value' => array(
82 + 'label' => __( 'Option new value', 'automatorwp' ),
83 + 'type' => 'text',
84 + 'preview' => __( 'The option\'s new value', 'automatorwp' ),
85 + ),
86 + 'option_old_value' => array(
87 + 'label' => __( 'Option old value', 'automatorwp' ),
88 + 'type' => 'text',
89 + 'preview' => __( 'The option\'s old value', 'automatorwp' ),
90 + ),
91 + ),
92 + automatorwp_utilities_times_tag()
93 + )
94 + ) );
95 +
96 + }
97 +
98 + /**
99 + * Trigger listener
100 + *
101 + * @param string $option Name of the option to update.
102 + * @param mixed $old_value The old option value.
103 + * @param mixed $value The new option value.
104 + * @since 1.0.0
105 + *
106 + */
107 + public function listener( $option, $old_value, $value ) {
108 +
109 + $user_id = get_current_user_id();
110 +
111 + // Bail if user is not logged in
112 + if( $user_id === 0 ) {
113 + return;
114 + }
115 +
116 + automatorwp_trigger_event( array(
117 + 'trigger' => $this->trigger,
118 + 'user_id' => $user_id,
119 + 'option' => $option,
120 + 'value' => $value,
121 + 'old_value' => $old_value,
122 +
123 + ) );
124 +
125 + }
126 +
127 + /**
128 + * User deserves check
129 + *
130 + * @since 1.0.0
131 + *
132 + * @param bool $deserves_trigger True if user deserves trigger, false otherwise
133 + * @param stdClass $trigger The trigger object
134 + * @param int $user_id The user ID
135 + * @param array $event Event information
136 + * @param array $trigger_options The trigger's stored options
137 + * @param stdClass $automation The trigger's automation object
138 + *
139 + * @return bool True if user deserves trigger, false otherwise
140 + */
141 + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
142 +
143 + // Don't deserve if event vars not received
144 + if( ! isset( $event['option'] ) && ! isset( $event['value'] ) ) {
145 + return false;
146 + }
147 +
148 + // Don't deserve if option doesn't match with the trigger option
149 + if( $trigger_options['option'] !== '' && $trigger_options['option'] !== $event['option'] ) {
150 + return false;
151 + }
152 +
153 + // Don't deserve if value doesn't match with the trigger option
154 + if ( $trigger_options['value'] !== '' && ! automatorwp_condition_matches( $event['value'], $trigger_options['value'], $trigger_options['condition'] ) ) {
155 + return false;
156 + }
157 +
158 + return $deserves_trigger;
159 +
160 + }
161 +
162 + /**
163 + * Register the required hooks
164 + *
165 + * @since 1.0.0
166 + */
167 + public function hooks() {
168 +
169 + // Tags replacement
170 + add_filter( 'automatorwp_get_trigger_tag_replacement', array( $this, 'tags_replacement' ), 10, 6 );
171 +
172 + // Log meta data
173 + add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
174 +
175 + // Log fields
176 + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
177 +
178 + parent::hooks();
179 + }
180 +
181 + /**
182 + * Trigger custom tags replacement
183 + *
184 + * @since 1.0.0
185 + *
186 + * @param string $replacement The tag replacement
187 + * @param string $tag_name The tag name (without "{}")
188 + * @param stdClass $trigger The trigger object
189 + * @param int $user_id The user ID
190 + * @param string $content The content to parse
191 + * @param stdClass $log The last trigger log object
192 + *
193 + * @return string
194 + */
195 + function tags_replacement( $replacement, $tag_name, $trigger, $user_id, $content, $log ) {
196 +
197 + // Bail if action type don't match this action
198 + if( $trigger->type !== $this->trigger ) {
199 + return $replacement;
200 + }
201 +
202 + switch( $tag_name ) {
203 + case 'option_name':
204 + $replacement = automatorwp_get_log_meta( $log->id, 'option_name', true );
205 + break;
206 + case 'option_new_value':
207 + $replacement = automatorwp_get_log_meta( $log->id, 'option_new_value', true );
208 + break;
209 + case 'option_old_value':
210 + $replacement = automatorwp_get_log_meta( $log->id, 'option_old_value', true );
211 + break;
212 + }
213 +
214 + return $replacement;
215 +
216 + }
217 +
218 + /**
219 + * Trigger custom log meta
220 + *
221 + * @since 1.0.0
222 + *
223 + * @param array $log_meta Log meta data
224 + * @param stdClass $trigger The trigger object
225 + * @param int $user_id The user ID
226 + * @param array $event Event information
227 + * @param array $trigger_options The trigger's stored options
228 + * @param stdClass $automation The trigger's automation object
229 + *
230 + * @return array
231 + */
232 + function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
233 +
234 + // Bail if action type don't match this action
235 + if( $trigger->type !== $this->trigger ) {
236 + return $log_meta;
237 + }
238 +
239 + $log_meta['option_name'] = ( isset( $event['option'] ) ? $event['option'] : '' );
240 + $log_meta['option_new_value'] = ( isset( $event['value'] ) ? $event['value'] : '' );
241 + $log_meta['option_old_value'] = ( isset( $event['old_value'] ) ? $event['old_value'] : '' );
242 +
243 + return $log_meta;
244 +
245 + }
246 +
247 + /**
248 + * Action custom log fields
249 + *
250 + * @since 1.0.0
251 + *
252 + * @param array $log_fields The log fields
253 + * @param stdClass $log The log object
254 + * @param stdClass $object The trigger/action/automation object attached to the log
255 + *
256 + * @return array
257 + */
258 + public function log_fields( $log_fields, $log, $object ) {
259 +
260 + // Bail if log is not assigned to a trigger
261 + if( $log->type !== 'trigger' ) {
262 + return $log_fields;
263 + }
264 +
265 + // Bail if trigger type don't match this trigger
266 + if( $object->type !== $this->trigger ) {
267 + return $log_fields;
268 + }
269 +
270 + $log_fields['option_name'] = array(
271 + 'name' => __( 'Option', 'automatorwp' ),
272 + 'desc' => __( 'Name of the updated option.', 'automatorwp' ),
273 + 'type' => 'text',
274 + );
275 +
276 + $log_fields['option_new_value'] = array(
277 + 'name' => __( 'Option new value', 'automatorwp' ),
278 + 'desc' => __( 'The option\'s new value.', 'automatorwp' ),
279 + 'type' => 'text',
280 + );
281 +
282 + $log_fields['option_old_value'] = array(
283 + 'name' => __( 'Option old value', 'automatorwp' ),
284 + 'desc' => __( 'The option\'s old value.', 'automatorwp' ),
285 + 'type' => 'text',
286 + );
287 +
288 + return $log_fields;
289 +
290 + }
291 +
292 +}
293 +
294 +new AutomatorWP_WordPress_Option_Updated( 'wordpress' );