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/actions/update-option.php +273 -0 5.7.8 → 6.0.3 View file →
@@ -1,0 +1,273 @@
1 +<?php
2 +/**
3 + * Update Option
4 + *
5 + * @package AutomatorWP\Integrations\WordPress\Actions\Update_Option
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_Update_Option 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 . '_update_option';
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' => __( 'Update option', 'automatorwp' ),
38 + 'select_option' => __( 'Update <strong>option</strong>', 'automatorwp' ),
39 + /* translators: %1$s: Operation (Set, insert, increment or decrement). %2$s: Value. %3$s: Option. */
40 + 'edit_label' => sprintf( __( '%1$s %2$s to %3$s', 'automatorwp' ), '{operation}', '{value}', '{option}' ),
41 + /* translators: %1$s: Operation (Set, insert, increment or decrement). %2$s: Value. %3$s: Option. */
42 + 'log_label' => sprintf( __( '%1$s %2$s to %3$s', 'automatorwp' ), '{operation}', '{value}', '{option}' ),
43 + 'options' => array(
44 + 'operation' => array(
45 + 'from' => 'operation',
46 + 'fields' => array(
47 + 'operation' => array(
48 + 'name' => __( 'Operation:', 'automatorwp' ),
49 + 'desc' => __( 'Operation defines how the value will be applied. The available options are:', 'automatorwp' )
50 + . '<br><br>' . __( '<strong>Set:</strong> The new value will override the current value.', 'automatorwp' )
51 + . '<br>' . __( 'Example: If old value is "Word" and new value is "Press", the final value will be "Press".', 'automatorwp' )
52 + . '<br><br>' . __( '<strong>Insert:</strong> The new value will be inserted to the current value (for arrays, the new value will be inserted at the end, for other types, the new value will be appended).', 'automatorwp' )
53 + . '<br>' . __( 'Example for arrays: If old value is array( "Word" ) and new value is "Press", the final value will be array( "Word", "Press" ).', 'automatorwp' )
54 + . '<br>' . __( 'Example for other types: If old value is "Word" and new value is "Press", the final value will be "WordPress".', 'automatorwp' )
55 + . '<br><br>' . __( '<strong>Increment:</strong> For numeric values, the current value will be incremented the same amount as the new value.', 'automatorwp' )
56 + . '<br>' . __( 'Example: If old value is "5" and new value is "1", the final value will be "6".', 'automatorwp' )
57 + . '<br><br>' . __( '<strong>Decrement:</strong> For numeric values, the current value will be decremented the same amount as the new value.', 'automatorwp' )
58 + . '<br>' . __( 'Example: If old value is "5" and new value is "1", the final value will be "4".', 'automatorwp' ),
59 + 'type' => 'select',
60 + 'options' => array(
61 + 'set' => __( 'Set', 'automatorwp' ),
62 + 'insert' => __( 'Insert', 'automatorwp' ),
63 + 'increment' => __( 'Increment', 'automatorwp' ),
64 + 'decrement' => __( 'Decrement', 'automatorwp' ),
65 + ),
66 + 'default' => 'set'
67 + ),
68 + )
69 + ),
70 + 'value' => array(
71 + 'from' => 'value',
72 + 'default' => __( 'value', 'automatorwp' ),
73 + 'fields' => array(
74 + 'value' => array(
75 + 'name' => __( 'Value:', 'automatorwp' ),
76 + 'type' => 'text',
77 + 'default' => ''
78 + ),
79 + )
80 + ),
81 + 'option' => array(
82 + 'from' => 'option',
83 + /* translators: Refers to meta key */
84 + 'default' => __( 'option', 'automatorwp' ),
85 + 'fields' => array(
86 + 'option' => array(
87 + 'name' => __( 'Option:', 'automatorwp' ),
88 + 'type' => 'text',
89 + 'default' => ''
90 + ),
91 + 'autoload' => array(
92 + 'name' => __( 'Autoload:', 'automatorwp' ),
93 + 'type' => 'select',
94 + 'options' => array(
95 + '' => __( 'Yes', 'automatorwp' ),
96 + 'yes' => __( 'Yes', 'automatorwp' ),
97 + 'no' => __( 'No', 'automatorwp' ),
98 + ),
99 + 'default' => ''
100 + ),
101 + )
102 + )
103 + ),
104 + 'tags' => array(
105 +
106 + )
107 + ) );
108 +
109 + }
110 +
111 + /**
112 + * Register required hooks
113 + *
114 + * @since 1.0.0
115 + */
116 + public function hooks() {
117 +
118 + // Log meta data
119 + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
120 +
121 + // Log fields
122 + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
123 +
124 + parent::hooks();
125 +
126 + }
127 +
128 + /**
129 + * Action execution function
130 + *
131 + * @since 1.0.0
132 + *
133 + * @param stdClass $action The action object
134 + * @param int $user_id The user ID
135 + * @param array $action_options The action's stored options (with tags already passed)
136 + * @param stdClass $automation The action's automation object
137 + */
138 + public function execute( $action, $user_id, $action_options, $automation ) {
139 +
140 + // Shorthand
141 + $operation = $action_options['operation'];
142 + $option = sanitize_key( $action_options['option'] );
143 + $new_value = sanitize_text_field( $action_options['value'] );
144 + $autoload = $action_options['autoload'];
145 +
146 + // Bail if empty option name
147 + if( empty( $option ) ) {
148 + $this->result = __( 'Action not processed. Option name is empty.', 'automatorwp' );
149 + return;
150 + }
151 +
152 + // Bail if is a protected option
153 + if( automatorwp_is_protected_option( $option ) ) {
154 + $this->result = sprintf( __( 'Action not processed."%s" is a protected option. You can filter "automatorwp_protected_options" to modify this.', 'automatorwp' ), $option );
155 + return;
156 + }
157 +
158 + // Get the current meta value
159 + $value = get_option( $option, '' );
160 +
161 + // For increment and decrement, is required to turn values into a numeric value
162 + if( in_array( $operation, array( 'increment', 'decrement' ) ) ) {
163 +
164 + if( strpos( $new_value, '.' ) !== false ) {
165 + // Treat values as float
166 + $new_value = (float) $new_value;
167 + $value = (float) $value;
168 + } else {
169 + // Treat values as int
170 + $new_value = (int) $new_value;
171 + $value = (int) $value;
172 + }
173 +
174 + }
175 +
176 + switch ( $operation ) {
177 + case 'set':
178 + // Override old meta value
179 + $value = $new_value;
180 + break;
181 + case 'insert':
182 + if( is_array( $value ) ) {
183 + // If value is an array, append the new value
184 + $value[] = $new_value;
185 + } else {
186 + // If not, concat the new value
187 + $value .= $new_value;
188 + }
189 + break;
190 + case 'increment':
191 + // Increase meta value
192 + $value += $new_value;
193 + break;
194 + case 'decrement':
195 + // Decrease meta value
196 + $value -= $new_value;
197 + break;
198 + }
199 +
200 + switch ( $autoload ) {
201 + case 'yes': $autoload = true;
202 + break;
203 + case 'no': $autoload = false;
204 + break;
205 + default: $autoload = null;
206 + break;
207 + }
208 +
209 + // Update the user meta value
210 + update_option( $option, $value, $autoload );
211 +
212 + $this->result = sprintf( __( 'Option "%s" updated with value "%s".', 'automatorwp' ), $option, $value );
213 +
214 + }
215 +
216 + /**
217 + * Action custom log meta
218 + *
219 + * @since 1.0.0
220 + *
221 + * @param array $log_meta Log meta data
222 + * @param stdClass $action The action object
223 + * @param int $user_id The user ID
224 + * @param array $action_options The action's stored options (with tags already passed)
225 + * @param stdClass $automation The action's automation object
226 + *
227 + * @return array
228 + */
229 + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
230 +
231 + // Bail if action type don't match this action
232 + if( $action->type !== $this->action ) {
233 + return $log_meta;
234 + }
235 +
236 + $option = sanitize_key( $action_options['option'] );
237 + $new_value = sanitize_text_field( $action_options['value'] );
238 +
239 + $log_meta['option'] = $option;
240 + $log_meta['value'] = $new_value;
241 + $log_meta['result'] = $this->result;
242 +
243 + return $log_meta;
244 + }
245 +
246 + /**
247 + * Action custom log fields
248 + *
249 + * @since 1.0.0
250 + *
251 + * @param array $log_fields The log fields
252 + * @param stdClass $log The log object
253 + * @param stdClass $object The trigger/action/automation object attached to the log
254 + *
255 + * @return array
256 + */
257 + public function log_fields( $log_fields, $log, $object ) {
258 +
259 + // Bail if log is not assigned to an action
260 + if( $log->type !== 'action' || $object->type !== $this->action )
261 + return $log_fields;
262 +
263 + $log_fields['result'] = array(
264 + 'name' => __( 'Result:', 'automatorwp' ),
265 + 'type' => 'text',
266 + );
267 +
268 + return $log_fields;
269 + }
270 +
271 +}
272 +
273 +new AutomatorWP_WordPress_Update_Option( 'wordpress' );