PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.4
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.4
6.0.4 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 All 35 releases
← All changes | integrations/wordpress/actions/create-user.php +409 -0 5.7.3 → 6.0.4 View file →
@@ -1,0 +1,409 @@
1 +<?php
2 +/**
3 + * Create User
4 + *
5 + * @package AutomatorWP\Integrations\WordPress\Actions\Create_User
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_Create_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 . '_create_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 + * Register the trigger
48 + *
49 + * @since 1.0.0
50 + */
51 + public function register() {
52 +
53 + automatorwp_register_action( $this->action, array(
54 + 'integration' => $this->integration,
55 + 'label' => __( 'Create a user', 'automatorwp' ),
56 + 'select_option' => __( 'Create <strong>a user</strong>', 'automatorwp' ),
57 + /* translators: %1$s: User. */
58 + 'edit_label' => sprintf( __( 'Create a %1$s', 'automatorwp' ), '{user}' ),
59 + /* translators: %1$s: User. */
60 + 'log_label' => sprintf( __( 'Create a %1$s', 'automatorwp' ), '{user}' ),
61 + 'options' => array(
62 + 'user' => array(
63 + 'default' => __( 'user', 'automatorwp' ),
64 + 'fields' => array(
65 + 'user_login' => array(
66 + 'name' => __( 'Username:', 'automatorwp' ),
67 + 'desc' => __( 'The user\'s login username.', 'automatorwp' ),
68 + 'type' => 'text',
69 + 'required' => true,
70 + 'default' => ''
71 + ),
72 + 'user_email' => array(
73 + 'name' => __( 'Email:', 'automatorwp' ),
74 + 'desc' => __( 'The user email address.', 'automatorwp' ),
75 + 'type' => 'text',
76 + 'required' => true,
77 + 'default' => ''
78 + ),
79 + 'first_name' => array(
80 + 'name' => __( 'First Name:', 'automatorwp' ),
81 + 'desc' => __( 'The user\'s first name.', 'automatorwp' ),
82 + 'type' => 'text',
83 + 'default' => ''
84 + ),
85 + 'last_name' => array(
86 + 'name' => __( 'Last Name:', 'automatorwp' ),
87 + 'desc' => __( 'The user\'s last name.', 'automatorwp' ),
88 + 'type' => 'text',
89 + 'default' => ''
90 + ),
91 + 'user_url' => array(
92 + 'name' => __( 'Website:', 'automatorwp' ),
93 + 'desc' => __( 'The user\'s website URL.', 'automatorwp' ),
94 + 'type' => 'text',
95 + 'default' => ''
96 + ),
97 + 'user_pass' => array(
98 + 'name' => __( 'Password:', 'automatorwp' ),
99 + 'desc' => __( 'The user password. Leave blank to get password will get automatically generated.', 'automatorwp' ),
100 + 'type' => 'text',
101 + 'default' => ''
102 + ),
103 + 'role' => automatorwp_utilities_role_field( array(
104 + 'option_custom' => true,
105 + 'desc' => __( 'The user\'s role. By default, "subscriber".', 'automatorwp' ),
106 + ) ),
107 + 'role_custom' => automatorwp_utilities_custom_field( array(
108 + 'option_custom_desc' => __( 'Role name.', 'automatorwp' )
109 + ) ),
110 + 'send_user_notification' => array(
111 + 'name' => __( 'Send User Notification:', 'automatorwp' ),
112 + 'desc' => __( 'Send the new user an email about their account.', 'automatorwp' ),
113 + 'type' => 'checkbox',
114 + 'classes' => 'cmb2-switch'
115 + ),
116 + 'user_meta' => array(
117 + 'name' => __( 'User Meta:', 'automatorwp' ),
118 + 'desc' => __( 'The user meta values keyed by their user meta key.', 'automatorwp' ),
119 + 'type' => 'group',
120 + 'classes' => 'automatorwp-fields-table',
121 + 'options' => array(
122 + 'add_button' => __( 'Add meta', 'automatorwp' ),
123 + 'remove_button' => '<span class="dashicons dashicons-no-alt"></span>',
124 + ),
125 + 'fields' => array(
126 + 'meta_key' => array(
127 + 'name' => __( 'Meta Key:', 'automatorwp' ),
128 + 'type' => 'text',
129 + 'default' => ''
130 + ),
131 + 'meta_value' => array(
132 + 'name' => __( 'Meta Value:', 'automatorwp' ),
133 + 'type' => 'text',
134 + 'default' => ''
135 + ),
136 + ),
137 + ),
138 + )
139 + )
140 + ),
141 + 'tags' => array_merge(
142 + automatorwp_utilities_user_tags()
143 + )
144 + ) );
145 +
146 + }
147 +
148 + /**
149 + * Action execution function
150 + *
151 + * @since 1.0.0
152 + *
153 + * @param stdClass $action The action object
154 + * @param int $user_id The user ID
155 + * @param array $action_options The action's stored options (with tags already passed)
156 + * @param stdClass $automation The action's automation object
157 + */
158 + public function execute( $action, $user_id, $action_options, $automation ) {
159 +
160 + // Setup user fields
161 + $user_data = wp_parse_args( $action_options, array(
162 + 'user_login' => '',
163 + 'user_email' => '',
164 + 'first_name' => '',
165 + 'last_name' => '',
166 + 'user_url' => '',
167 + 'user_pass' => '',
168 + 'role' => 'subscriber',
169 + ) );
170 +
171 + // Check the user role
172 + $roles = automatorwp_get_editable_roles();
173 +
174 + if( ! isset( $roles[$user_data['role']] ) )
175 + $user_data['role'] = 'subscriber';
176 +
177 + // Check if role comes from role_custom (which could come from a tag)
178 + if( $action_options['role_custom'] === 'administrator' ) {
179 + // Security filter to allows through code create administrator users if not desired
180 + $allow_admin = apply_filters( 'automatorwp_allow_create_admin_user_from_custom_role', false, $action, $user_id, $action_options, $automation );
181 +
182 + if( ! $allow_admin )
183 + $user_data['role'] = 'subscriber';
184 + }
185 +
186 + // Generate the user password
187 + if( empty( $user_data['user_pass'] ) )
188 + $user_data['user_pass'] = wp_generate_password( 24 );
189 +
190 + // Insert the user
191 + $this->user_id = wp_insert_user( $user_data );
192 +
193 + if( $this->user_id ) {
194 +
195 + if( isset( $action_options['user_meta'] ) && is_array( $action_options['user_meta'] ) ) {
196 +
197 + foreach( $action_options['user_meta'] as $i => $meta ) {
198 +
199 + // Parse automation tags replacements to both, key and value
200 + $meta_key = automatorwp_parse_automation_tags( $automation->id, $user_id, $meta['meta_key'] );
201 + $meta_value = automatorwp_parse_automation_tags( $automation->id, $user_id, $meta['meta_value'] );
202 +
203 + // Sanitize
204 + $meta_key = sanitize_text_field( $meta_key );
205 + $meta_value = sanitize_text_field( $meta_value );
206 +
207 + // Update user meta
208 + update_user_meta( $this->user_id, $meta_key, $meta_value );
209 +
210 + $this->user_meta[$meta_key] = $meta_value;
211 +
212 + // Update action options to be passed on upcoming hooks
213 + $action_options['user_meta'][$i] = array(
214 + 'meta_key' => $meta_key,
215 + 'meta_value' => $meta_value,
216 + );
217 +
218 + }
219 +
220 + }
221 +
222 + // Send notification
223 + $notify = 'admin';
224 +
225 + if( isset( $action_options['send_user_notification'] ) && (bool) $action_options['send_user_notification'] ) {
226 + $notify = 'both';
227 + }
228 +
229 + wp_send_new_user_notifications( $this->user_id, $notify );
230 +
231 + /**
232 + * Action triggered before the create new user action gets executed
233 + *
234 + * @since 1.2.6
235 + *
236 + * @param int $new_user_id The new user ID
237 + * @param stdClass $action The action object
238 + * @param int $user_id The user ID (user who triggered the automation)
239 + * @param array $action_options The action's stored options (with tags already passed, included on meta keys and values)
240 + * @param stdClass $automation The action's automation object
241 + */
242 + do_action( 'automatorwp_wordpress_create_user_executed', $this->user_id, $action, $user_id, $action_options, $automation );
243 +
244 + }
245 +
246 + }
247 +
248 + /**
249 + * Register required hooks
250 + *
251 + * @since 1.0.0
252 + */
253 + public function hooks() {
254 +
255 + // Log meta data
256 + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
257 +
258 + // Log fields
259 + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
260 +
261 + parent::hooks();
262 + }
263 +
264 + /**
265 + * Action custom log meta
266 + *
267 + * @since 1.0.0
268 + *
269 + * @param array $log_meta Log meta data
270 + * @param stdClass $action The action object
271 + * @param int $user_id The user ID
272 + * @param array $action_options The action's stored options (with tags already passed)
273 + * @param stdClass $automation The action's automation object
274 + *
275 + * @return array
276 + */
277 + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
278 +
279 + // Bail if action type don't match this action
280 + if( $action->type !== $this->action )
281 + return $log_meta;
282 +
283 + // Store user fields
284 + $user_fields = array(
285 + 'user_login',
286 + 'user_email',
287 + 'first_name',
288 + 'last_name',
289 + 'user_url',
290 + 'user_pass',
291 + 'role',
292 + );
293 +
294 + foreach( $user_fields as $user_field ) {
295 + $log_meta[$user_field] = $action_options[$user_field];
296 + }
297 +
298 + // Store user ID
299 + $log_meta['user_id'] = $this->user_id;
300 +
301 + // Store user meta
302 + $log_meta['user_meta'] = $this->user_meta;
303 +
304 + // Store result
305 + if( is_wp_error( $this->user_id ) ) {
306 + $log_meta['result'] = $this->user_id->get_error_message();
307 + } else if( $this->user_id ) {
308 + $log_meta['result'] = __( 'User created correctly', 'automatorwp' );
309 + } else {
310 + $log_meta['result'] = __( 'Could not create user', 'automatorwp' );
311 + }
312 +
313 + return $log_meta;
314 + }
315 +
316 + /**
317 + * Action custom log fields
318 + *
319 + * @since 1.0.0
320 + *
321 + * @param array $log_fields The log fields
322 + * @param stdClass $log The log object
323 + * @param stdClass $object The trigger/action/automation object attached to the log
324 + *
325 + * @return array
326 + */
327 + public function log_fields( $log_fields, $log, $object ) {
328 +
329 + // Bail if log is not assigned to this action
330 + if( $log->type !== 'action' || $object->type !== $this->action )
331 + return $log_fields;
332 +
333 + $log_fields['user_info'] = array(
334 + 'name' => __( 'User Information', 'automatorwp' ),
335 + 'desc' => __( 'Information about the user created.', 'automatorwp' ),
336 + 'type' => 'title',
337 + );
338 +
339 + $log_fields['user_id_meta'] = array(
340 + 'name' => __( 'User:', 'automatorwp' ),
341 + 'desc' => __( 'The user the action runs for.', 'automatorwp' ),
342 + 'type' => 'text',
343 + );
344 +
345 + $log_fields['user_login'] = array(
346 + 'name' => __( 'Username:', 'automatorwp' ),
347 + 'desc' => __( 'The user\'s login username.', 'automatorwp' ),
348 + 'type' => 'text',
349 + );
350 +
351 + $log_fields['user_email'] = array(
352 + 'name' => __( 'Email:', 'automatorwp' ),
353 + 'desc' => __( 'The user email address.', 'automatorwp' ),
354 + 'type' => 'text',
355 + 'default' => ''
356 + );
357 +
358 + $log_fields['first_name'] = array(
359 + 'name' => __( 'First Name:', 'automatorwp' ),
360 + 'desc' => __( 'The user\'s first name.', 'automatorwp' ),
361 + 'type' => 'text',
362 + 'default' => ''
363 + );
364 +
365 + $log_fields['last_name'] = array(
366 + 'name' => __( 'Last Name:', 'automatorwp' ),
367 + 'desc' => __( 'The user\'s last name.', 'automatorwp' ),
368 + 'type' => 'text',
369 + 'default' => ''
370 + );
371 +
372 + $log_fields['user_url'] = array(
373 + 'name' => __( 'Website:', 'automatorwp' ),
374 + 'desc' => __( 'The user URL.', 'automatorwp' ),
375 + 'type' => 'text',
376 + 'default' => ''
377 + );
378 +
379 + $log_fields['user_pass'] = array(
380 + 'name' => __( 'Password:', 'automatorwp' ),
381 + 'desc' => __( 'The user password.', 'automatorwp' ),
382 + 'type' => 'text',
383 + 'default' => ''
384 + );
385 +
386 + $log_fields['role'] = array(
387 + 'name' => __( 'Role:', 'automatorwp' ),
388 + 'desc' => __( 'The user\'s role.', 'automatorwp' ),
389 + 'type' => 'text',
390 + );
391 +
392 + $log_fields['user_meta'] = array(
393 + 'name' => __( 'User Meta:', 'automatorwp' ),
394 + 'desc' => __( 'The user meta values keyed by their user meta key.', 'automatorwp' ),
395 + 'type' => 'text',
396 + );
397 +
398 + $log_fields['result'] = array(
399 + 'name' => __( 'Result:', 'automatorwp' ),
400 + 'type' => 'text',
401 + );
402 +
403 + return $log_fields;
404 + }
405 +
406 +}
407 +
408 +new AutomatorWP_WordPress_Create_User( 'wordpress' );
409 +new AutomatorWP_WordPress_Create_User( 'users' );