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/user-meta.php +292 -0 5.6.1 → 6.0.3 View file →
@@ -1,0 +1,292 @@
1 +<?php
2 +/**
3 + * User Meta
4 + *
5 + * @package AutomatorWP\Integrations\WordPress\Actions\User_Meta
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_User_Meta 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 . '_user_meta';
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 user meta', 'automatorwp' ),
38 + 'select_option' => __( 'Update <strong>user meta</strong>', 'automatorwp' ),
39 + /* translators: %1$s: Operation (Set, insert, increment or decrement). %2$s: Meta value. %3$s: Meta key. */
40 + 'edit_label' => sprintf( __( '%1$s user meta value %2$s for meta key %3$s', 'automatorwp' ), '{operation}', '{meta_value}', '{meta_key}' ),
41 + /* translators: %1$s: Operation (Set, insert, increment or decrement). %2$s: Meta value. %3$s: Meta key. */
42 + 'log_label' => sprintf( __( '%1$s user meta value %2$s for meta key %3$s', 'automatorwp' ), '{operation}', '{meta_value}', '{meta_key}' ),
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 meta value will be applied. The available options are:', 'automatorwp' )
50 + . '<br><br>' . __( '<strong>Set:</strong> The new value will be set as the meta value.', 'automatorwp' )
51 + . '<br>' . __( 'Example: If old value is "Word" and new value is "Press", the final meta value will be "Press".', 'automatorwp' )
52 + . '<br><br>' . __( '<strong>Insert:</strong> The new value will be inserted to the current meta 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 meta 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 meta value will be "WordPress".', 'automatorwp' )
55 + . '<br><br>' . __( '<strong>Increment:</strong> For numeric values, the current meta 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 meta value will be "6".', 'automatorwp' )
57 + . '<br><br>' . __( '<strong>Decrement:</strong> For numeric values, the current meta 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 meta 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 + 'meta_value' => array(
71 + 'from' => 'meta_value',
72 + 'default' => __( 'value', 'automatorwp' ),
73 + 'fields' => array(
74 + 'meta_value' => array(
75 + 'name' => __( 'Meta value:', 'automatorwp' ),
76 + 'type' => 'text',
77 + 'default' => ''
78 + ),
79 + )
80 + ),
81 + 'meta_key' => array(
82 + 'from' => 'meta_key',
83 + /* translators: Refers to meta key */
84 + 'default' => __( 'key', 'automatorwp' ),
85 + 'fields' => array(
86 + 'meta_key' => array(
87 + 'name' => __( 'Meta key:', 'automatorwp' ),
88 + 'type' => 'text',
89 + 'default' => ''
90 + ),
91 + )
92 + )
93 + ),
94 + 'tags' => array_merge(
95 + automatorwp_utilities_user_tags()
96 + )
97 + ) );
98 +
99 + }
100 +
101 + /**
102 + * Register required hooks
103 + *
104 + * @since 1.0.0
105 + */
106 + public function hooks() {
107 +
108 + // Dynamic edit and log labels
109 + add_filter( 'automatorwp_parse_automation_item_edit_label', array( $this, 'dynamic_label' ), 10, 5 );
110 + add_filter( 'automatorwp_parse_automation_item_log_label', array( $this, 'dynamic_label' ), 10, 5 );
111 +
112 + // Log meta data
113 + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
114 +
115 + parent::hooks();
116 +
117 + }
118 +
119 + /**
120 + * Custom edit/log label
121 + *
122 + * @since 1.0.0
123 + *
124 + * @param string $label The edit label
125 + * @param stdClass $object The trigger/action object
126 + * @param string $item_type The item type (trigger|action)
127 + * @param string $context The context this function is executed
128 + * @param array $type_args The type parameters
129 + *
130 + * @return string
131 + */
132 + public function dynamic_label( $label, $object, $item_type, $context, $type_args ) {
133 +
134 + // Bail if action type don't match this action
135 + if( $object->type !== $this->action ) {
136 + return $label;
137 + }
138 +
139 + // Get the operation value
140 + ct_setup_table( "automatorwp_{$item_type}s" );
141 + $operation = ct_get_object_meta( $object->id, 'operation', true );
142 + ct_reset_setup_table();
143 +
144 + // Update the edit and log labels
145 + if( in_array( $operation, array( 'increment', 'decrement' ) ) ) {
146 + /* translators: %1$s: Operation (Set, insert, increment or decrement). %2$s: Meta value. %3$s: Meta key. */
147 + return sprintf( __( '%1$s user meta value by %2$s for meta key %3$s', 'automatorwp' ), '{operation}', '{meta_value}', '{meta_key}' );
148 + }
149 +
150 + return $label;
151 +
152 + }
153 +
154 + /**
155 + * Action execution function
156 + *
157 + * @since 1.0.0
158 + *
159 + * @param stdClass $action The action object
160 + * @param int $user_id The user ID
161 + * @param array $action_options The action's stored options (with tags already passed)
162 + * @param stdClass $automation The action's automation object
163 + */
164 + public function execute( $action, $user_id, $action_options, $automation ) {
165 +
166 + // Shorthand
167 + $operation = $action_options['operation'];
168 + $meta_key = sanitize_title( $action_options['meta_key'] );
169 + $meta_value = sanitize_text_field( $action_options['meta_value'] );
170 + $this->user_data = array();
171 +
172 + // Bail if empty meta key
173 + if( empty( $meta_key ) ) {
174 + return;
175 + }
176 +
177 + // Get the current meta value
178 + $value = get_user_meta( $user_id, $meta_key, true );
179 +
180 + // For increment and decrement, is required to turn values into a numeric value
181 + if( in_array( $operation, array( 'increment', 'decrement' ) ) ) {
182 +
183 + if( strpos( $meta_value, '.' ) !== false ) {
184 + // Treat values as float
185 + $value = (float) $value;
186 + $meta_value = (float) $meta_value;
187 + } else {
188 + // Treat values as int
189 + $value = (int) $value;
190 + $meta_value = (int) $meta_value;
191 + }
192 +
193 + }
194 +
195 + switch ( $operation ) {
196 + case 'set':
197 + // Override old meta value
198 + $value = $meta_value;
199 + break;
200 + case 'insert':
201 + if( is_array( $value ) ) {
202 + // If value is an array, append the new value
203 + $value[] = $meta_value;
204 + } else {
205 + // If not, concat the new value
206 + $value .= $meta_value;
207 + }
208 + break;
209 + case 'increment':
210 + // Increase meta value
211 + $value += $meta_value;
212 + break;
213 + case 'decrement':
214 + // Decrease meta value
215 + $value -= $meta_value;
216 + break;
217 + }
218 +
219 + // Update the user meta value
220 + update_user_meta( $user_id, $meta_key, $value );
221 +
222 + $this->user_meta[$meta_key] = $value;
223 +
224 + $user = get_user_by( 'id', $user_id );
225 +
226 + // The user fields
227 + $user_fields = array(
228 + 'user_login',
229 + 'user_email',
230 + 'first_name',
231 + 'last_name',
232 + 'user_url',
233 + 'user_pass',
234 + 'role',
235 + 'display_name',
236 + );
237 +
238 + foreach( $user_fields as $user_field ) {
239 + $this->user_data[$user_field] = $user->$user_field;
240 + }
241 +
242 + }
243 +
244 + /**
245 + * Action custom log meta
246 + *
247 + * @since 1.0.0
248 + *
249 + * @param array $log_meta Log meta data
250 + * @param stdClass $action The action object
251 + * @param int $user_id The user ID
252 + * @param array $action_options The action's stored options (with tags already passed)
253 + * @param stdClass $automation The action's automation object
254 + *
255 + * @return array
256 + */
257 + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
258 +
259 + // Bail if action type don't match this action
260 + if( $action->type !== $this->action ) {
261 + return $log_meta;
262 + }
263 +
264 + // Store user fields
265 + $user_fields = array(
266 + 'user_login',
267 + 'user_email',
268 + 'first_name',
269 + 'last_name',
270 + 'user_url',
271 + 'user_pass',
272 + 'role',
273 + 'display_name',
274 + );
275 +
276 + foreach( $user_fields as $user_field ) {
277 + $log_meta[$user_field] = $this->user_data[$user_field];
278 + }
279 +
280 + // Store user ID
281 + $log_meta['user_id'] = $user_id;
282 +
283 + // Store user meta
284 + $log_meta['user_meta'] = $this->user_meta;
285 +
286 + return $log_meta;
287 + }
288 +
289 +}
290 +
291 +new AutomatorWP_WordPress_User_Meta( 'wordpress' );
292 +new AutomatorWP_WordPress_User_Meta( 'users' );