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 / integrations / wordpress / actions / update-user.php

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

454 lines 16.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Update User
4 *
5 * @package AutomatorWP\Integrations\WordPress\Actions\Update_User
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 class AutomatorWP_WordPress_Update_User 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_user';
23
24 parent::__construct();
25
26 }
27
28 /**
29 * The new inserted user ID
30 *
31 * @since 1.0.0
32 *
33 * @var int|WP_Error $user_id
34 */
35 public $user_id = 0;
36
37 /**
38 * The post meta
39 *
40 * @since 1.0.0
41 *
42 * @var array $user_meta
43 */
44 public $user_meta = array();
45
46 /**
47 * The action result
48 *
49 * @since 1.0.0
50 *
51 * @var array $result
52 */
53 public $result = array();
54
55 /**
56 * Register the trigger
57 *
58 * @since 1.0.0
59 */
60 public function register() {
61
62 automatorwp_register_action( $this->action, array(
63 'integration' => $this->integration,
64 'label' => __( 'Update a user', 'automatorwp' ),
65 'select_option' => __( 'Update a <strong>user</strong>', 'automatorwp' ),
66 /* translators: %1$s: User. */
67 'edit_label' => sprintf( __( 'Update a %1$s', 'automatorwp' ), '{user}' ),
68 /* translators: %1$s: User. */
69 'log_label' => sprintf( __( 'Update a %1$s', 'automatorwp' ), '{user}' ),
70 'options' => array(
71 'user' => array(
72 'default' => __( 'user', 'automatorwp' ),
73 'fields' => array(
74 'user_id' => array(
75 'name' => __( 'User ID:', 'automatorwp' ),
76 'desc' => __( 'The user\'s ID to update.', 'automatorwp' )
77 . ' ' . __( 'Leave empty to use the ID of the user that completes the automation.', 'automatorwp' ),
78 'type' => 'text',
79 'default' => ''
80 ),
81 'user_login' => array(
82 'name' => __( 'Username:', 'automatorwp' ),
83 'desc' => __( 'The user\'s login username.', 'automatorwp' )
84 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
85 'type' => 'text',
86 'default' => ''
87 ),
88 'user_email' => array(
89 'name' => __( 'Email:', 'automatorwp' ),
90 'desc' => __( 'The user email address.', 'automatorwp' )
91 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
92 'type' => 'text',
93 'default' => ''
94 ),
95 'first_name' => array(
96 'name' => __( 'First Name:', 'automatorwp' ),
97 'desc' => __( 'The user\'s first name.', 'automatorwp' )
98 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
99 'type' => 'text',
100 'default' => ''
101 ),
102 'last_name' => array(
103 'name' => __( 'Last Name:', 'automatorwp' ),
104 'desc' => __( 'The user\'s last name.', 'automatorwp' )
105 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
106 'type' => 'text',
107 'default' => ''
108 ),
109 'display_name' => array(
110 'name' => __( 'Display Name:', 'automatorwp' ),
111 'desc' => __( 'The user\'s display name.', 'automatorwp' )
112 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
113 'type' => 'text',
114 'default' => ''
115 ),
116 'user_url' => array(
117 'name' => __( 'Website:', 'automatorwp' ),
118 'desc' => __( 'The user\'s website URL.', 'automatorwp' )
119 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
120 'type' => 'text',
121 'default' => ''
122 ),
123 'user_pass' => array(
124 'name' => __( 'Password:', 'automatorwp' ),
125 'desc' => __( 'The user password.', 'automatorwp' )
126 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
127 'type' => 'text',
128 'default' => ''
129 ),
130 'role' => automatorwp_utilities_role_field( array(
131 'option_custom' => true,
132 'desc' => __( 'The user\'s role. By default, "subscriber".', 'automatorwp' )
133 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
134 ) ),
135 'role_custom' => automatorwp_utilities_custom_field( array(
136 'option_custom_desc' => __( 'Role name.', 'automatorwp' )
137 ) ),
138 'user_meta' => array(
139 'name' => __( 'User Meta:', 'automatorwp' ),
140 'desc' => __( 'The user meta values keyed by their user meta key.', 'automatorwp' ),
141 'type' => 'group',
142 'classes' => 'automatorwp-fields-table',
143 'options' => array(
144 'add_button' => __( 'Add meta', 'automatorwp' ),
145 'remove_button' => '<span class="dashicons dashicons-no-alt"></span>',
146 ),
147 'fields' => array(
148 'meta_key' => array(
149 'name' => __( 'Meta Key:', 'automatorwp' ),
150 'type' => 'text',
151 'default' => ''
152 ),
153 'meta_value' => array(
154 'name' => __( 'Meta Value:', 'automatorwp' ),
155 'type' => 'text',
156 'default' => ''
157 ),
158 ),
159 ),
160 )
161 )
162 ),
163 'tags' => array_merge(
164 automatorwp_utilities_user_tags()
165 )
166 ) );
167
168 }
169
170 /**
171 * Action execution function
172 *
173 * @since 1.0.0
174 *
175 * @param stdClass $action The action object
176 * @param int $user_id The user ID
177 * @param array $action_options The action's stored options (with tags already passed)
178 * @param stdClass $automation The action's automation object
179 */
180 public function execute( $action, $user_id, $action_options, $automation ) {
181
182 global $wpdb;
183
184 // Shorthand
185 $user_login = $action_options['user_login'];
186 $user_email = $action_options['user_email'];
187 $role = $action_options['role'];
188
189 $user_data = array();
190 $this->result = array();
191
192 // User ID
193 if( empty( $action_options['user_id'] ) ) {
194 $action_options['user_id'] = $user_id;
195 }
196
197 $this->user_id = absint( $action_options['user_id'] );
198 $user = get_userdata( $this->user_id );
199
200 // Bail if could not find the user
201 if( ! $user ) {
202 $this->result[] = sprintf( __( 'User not found by the ID %1$s.', 'automatorwp' ), $this->user_id );
203 return;
204 }
205
206 $user_data['ID'] = $this->user_id;
207
208 // User login
209 if( ! empty( $user_login ) ) {
210 if ( ! validate_username( $user_login ) ) {
211 $this->result[] = sprintf( __( 'Invalid username: %1$s.', 'automatorwp' ), $user_login );
212 } elseif ( username_exists( $user_login ) !== false && username_exists( $user_login ) !== $this->user_id ) {
213 $this->result[] = sprintf( __( 'Username "%1$s" already exists.', 'automatorwp' ), $user_login );
214 } else {
215 // Update the user login
216 $wpdb->update( $wpdb->users, array( 'user_login' => $user_login ), array( 'ID' => $this->user_id ) );
217 }
218 }
219
220 // User email
221 if( ! empty( $user_email ) ) {
222 if ( ! is_email( $user_email ) ) {
223 $this->result[] = sprintf( __( 'Invalid email address: %1$s.', 'automatorwp' ), $user_email );
224 } elseif ( email_exists( $user_email ) !== false && email_exists( $user_email ) !== $this->user_id ) {
225 $this->result[] = sprintf( __( 'Email address "%1$s" already exists.', 'automatorwp' ), $user_email );
226 } else {
227 $user_data['user_email'] = $user_email;
228 }
229 }
230
231 // Role
232 $roles = automatorwp_get_editable_roles();
233
234 if( ! isset( $roles[$role] ) ) {
235 $this->result[] = sprintf( __( 'Invalid role: %1$s.', 'automatorwp' ), $role );
236 } else {
237 $user_data['role'] = $role;
238 }
239
240 // The rest fields
241 $user_fields = array(
242 'first_name',
243 'last_name',
244 'user_url',
245 'user_pass',
246 'display_name',
247 );
248
249 foreach( $user_fields as $user_field ) {
250 if( ! empty( $action_options[$user_field] ) ) {
251 $user_data[$user_field] = $action_options[$user_field];
252 }
253 }
254
255 // User meta
256 if( is_array( $action_options['user_meta'] ) ) {
257
258 foreach( $action_options['user_meta'] as $i => $meta ) {
259
260 // Parse automation tags replacements to both, key and value
261 $meta_key = automatorwp_parse_automation_tags( $automation->id, $user_id, $meta['meta_key'] );
262 $meta_value = automatorwp_parse_automation_tags( $automation->id, $user_id, $meta['meta_value'] );
263
264 // Sanitize
265 $meta_key = sanitize_text_field( $meta_key );
266 $meta_value = sanitize_text_field( $meta_value );
267
268 // Update user meta
269 update_user_meta( $this->user_id, $meta_key, $meta_value );
270
271 $this->user_meta[$meta_key] = $meta_value;
272
273 // Update action options to be passed on upcoming hooks
274 $action_options['user_meta'][$i] = array(
275 'meta_key' => $meta_key,
276 'meta_value' => $meta_value,
277 );
278
279 }
280
281 }
282
283 // Update the user
284 $update_user = wp_update_user( $user_data );
285
286 if ( is_wp_error( $update_user ) ) {
287 $this->result[] = sprintf( __( 'Failed to update user, reason: %1$s', 'automatorwp' ), $update_user->get_error_message() );
288 }
289
290 }
291
292 /**
293 * Register required hooks
294 *
295 * @since 1.0.0
296 */
297 public function hooks() {
298
299 // Log meta data
300 add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
301
302 // Log fields
303 add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
304
305 parent::hooks();
306 }
307
308 /**
309 * Action custom log meta
310 *
311 * @since 1.0.0
312 *
313 * @param array $log_meta Log meta data
314 * @param stdClass $action The action object
315 * @param int $user_id The user ID
316 * @param array $action_options The action's stored options (with tags already passed)
317 * @param stdClass $automation The action's automation object
318 *
319 * @return array
320 */
321 public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
322
323 // Bail if action type don't match this action
324 if( $action->type !== $this->action ) {
325 return $log_meta;
326 }
327
328 // Store user fields
329 $user_fields = array(
330 'user_login',
331 'user_email',
332 'first_name',
333 'last_name',
334 'user_url',
335 'user_pass',
336 'role',
337 );
338
339 foreach( $user_fields as $user_field ) {
340 $log_meta[$user_field] = $action_options[$user_field];
341 }
342
343 // Store user ID
344 $log_meta['user_id'] = $this->user_id;
345
346 // Store user meta
347 $log_meta['user_meta'] = $this->user_meta;
348
349 // Store result
350 $log_meta['result'] = $this->result;
351
352 return $log_meta;
353 }
354
355 /**
356 * Action custom log fields
357 *
358 * @since 1.0.0
359 *
360 * @param array $log_fields The log fields
361 * @param stdClass $log The log object
362 * @param stdClass $object The trigger/action/automation object attached to the log
363 *
364 * @return array
365 */
366 public function log_fields( $log_fields, $log, $object ) {
367
368 // Bail if log is not assigned to an action
369 if( $log->type !== 'action' || $object->type !== $this->action )
370 return $log_fields;
371
372 $log_fields['user_info'] = array(
373 'name' => __( 'User Information', 'automatorwp' ),
374 'desc' => __( 'Information about the user created.', 'automatorwp' ),
375 'type' => 'title',
376 );
377
378 $log_fields['user_id_meta'] = array(
379 'name' => __( 'User:', 'automatorwp' ),
380 'desc' => __( 'The user the action runs for.', 'automatorwp' ),
381 'type' => 'text',
382 );
383
384 $log_fields['user_login'] = array(
385 'name' => __( 'User ID:', 'automatorwp' ),
386 'desc' => __( 'The user\'s ID updated.', 'automatorwp' ),
387 'type' => 'text',
388 );
389
390 $log_fields['user_login'] = array(
391 'name' => __( 'Username:', 'automatorwp' ),
392 'desc' => __( 'The user\'s login username.', 'automatorwp' ),
393 'type' => 'text',
394 );
395
396 $log_fields['user_email'] = array(
397 'name' => __( 'Email:', 'automatorwp' ),
398 'desc' => __( 'The user email address.', 'automatorwp' ),
399 'type' => 'text',
400 'default' => ''
401 );
402
403 $log_fields['first_name'] = array(
404 'name' => __( 'First Name:', 'automatorwp' ),
405 'desc' => __( 'The user\'s first name.', 'automatorwp' ),
406 'type' => 'text',
407 'default' => ''
408 );
409
410 $log_fields['last_name'] = array(
411 'name' => __( 'Last Name:', 'automatorwp' ),
412 'desc' => __( 'The user\'s last name.', 'automatorwp' ),
413 'type' => 'text',
414 'default' => ''
415 );
416
417 $log_fields['user_url'] = array(
418 'name' => __( 'Website:', 'automatorwp' ),
419 'desc' => __( 'The user URL.', 'automatorwp' ),
420 'type' => 'text',
421 'default' => ''
422 );
423
424 $log_fields['user_pass'] = array(
425 'name' => __( 'Password:', 'automatorwp' ),
426 'desc' => __( 'The user password.', 'automatorwp' ),
427 'type' => 'text',
428 'default' => ''
429 );
430
431 $log_fields['role'] = array(
432 'name' => __( 'Role:', 'automatorwp' ),
433 'desc' => __( 'The user\'s role.', 'automatorwp' ),
434 'type' => 'text',
435 );
436
437 $log_fields['user_meta'] = array(
438 'name' => __( 'User Meta:', 'automatorwp' ),
439 'desc' => __( 'The user meta values keyed by their user meta key.', 'automatorwp' ),
440 'type' => 'text',
441 );
442
443 $log_fields['result'] = array(
444 'name' => __( 'Result:', 'automatorwp' ),
445 'type' => 'text',
446 );
447
448 return $log_fields;
449 }
450
451 }
452
453 new AutomatorWP_WordPress_Update_User( 'wordpress' );
454 new AutomatorWP_WordPress_Update_User( 'users' );