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/automatorwp/actions/anonymous-user.php +909 -0 5.7.2 → 6.0.3 View file →
@@ -1,0 +1,909 @@
1 +<?php
2 +/**
3 + * Anonymous User
4 + *
5 + * @package AutomatorWP\Integrations\WordPress\Actions\Anonymous_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_Anonymous_User extends AutomatorWP_Integration_Action {
13 +
14 + public $integration = 'automatorwp';
15 + public $action = 'automatorwp_anonymous_user';
16 +
17 + /**
18 + * The new inserted user ID
19 + *
20 + * @since 1.0.0
21 + *
22 + * @var int|WP_Error $user_id
23 + */
24 + public $user_id = 0;
25 +
26 + /**
27 + * The post meta
28 + *
29 + * @since 1.0.0
30 + *
31 + * @var array $user_meta
32 + */
33 + public $user_meta = array();
34 +
35 + /**
36 + * Defines if new user has been created by this action
37 + *
38 + * @since 1.0.0
39 + *
40 + * @var bool $new_user
41 + */
42 + public $new_user = false;
43 +
44 + /**
45 + * The action result
46 + *
47 + * @since 1.0.0
48 + *
49 + * @var string $result
50 + */
51 + public $result = '';
52 +
53 + /**
54 + * The action result (detailed)
55 + *
56 + * @since 1.0.0
57 + *
58 + * @var array $detailed_result
59 + */
60 + public $detailed_result = '';
61 +
62 + /**
63 + * Register the trigger
64 + *
65 + * @since 1.0.0
66 + */
67 + public function register() {
68 +
69 + automatorwp_register_action( $this->action, array(
70 + 'integration' => $this->integration,
71 + 'label' => '',
72 + 'select_option' => '',
73 + /* translators: %1$s: User. */
74 + 'edit_label' => sprintf( __( 'Actions will be run on %1$s', 'automatorwp' ), '{user}' ),
75 + /* translators: %1$s: User. */
76 + 'log_label' => sprintf( __( 'Actions will be run on %1$s', 'automatorwp' ), '{user}' ),
77 + 'options' => array(
78 + 'user' => array(
79 + 'from' => 'run_actions_on',
80 + 'fields' => array(
81 + 'run_actions_on' => array(
82 + 'name' => __( 'Run actions on:', 'automatorwp' ),
83 + 'type' => 'select',
84 + 'required' => true,
85 + 'options' => array(
86 + '' => '',
87 + 'existing_user' => __( 'Existing user', 'automatorwp' ),
88 + 'new_user' => __( 'New user', 'automatorwp' ),
89 + ),
90 + 'default' => ''
91 + ),
92 +
93 + // Existing user fields
94 +
95 + 'search_field' => array(
96 + 'name' => __( 'Field to search the user:', 'automatorwp' ),
97 + 'desc' => __( 'The field by which to search the user.', 'automatorwp' ),
98 + 'type' => 'select',
99 + 'required' => true,
100 + 'options' => array(
101 + 'id' => __( 'ID', 'automatorwp' ),
102 + 'email' => __( 'Email', 'automatorwp' ),
103 + 'login' => __( 'Username', 'automatorwp' ),
104 + ),
105 + 'default' => 'id'
106 + ),
107 + 'search_field_value' => array(
108 + 'name' => __( 'Field value:', 'automatorwp' ),
109 + 'desc' => __( 'Value of the field to search the user.', 'automatorwp' ),
110 + 'type' => 'text',
111 + 'required' => true,
112 + 'default' => ''
113 + ),
114 + 'existing_user_not_exists' => array(
115 + 'name' => __( 'What to do if the user doesn\'t exist:', 'automatorwp' ),
116 + 'type' => 'radio',
117 + 'required' => true,
118 + 'options' => array(
119 + 'abort' => __( 'Do not run the actions', 'automatorwp' ),
120 + 'new_user' => __( 'Create a new user', 'automatorwp' ),
121 + ),
122 + 'default_cb' => array( $this, 'return_abort_default_cb' ),
123 + ),
124 +
125 + // New user fields
126 +
127 + 'user_login' => array(
128 + 'name' => __( 'Username:', 'automatorwp' ),
129 + 'desc' => __( 'The user\'s login username.', 'automatorwp' ),
130 + 'type' => 'text',
131 + 'required' => true,
132 + 'default' => ''
133 + ),
134 + 'user_email' => array(
135 + 'name' => __( 'Email:', 'automatorwp' ),
136 + 'desc' => __( 'The user email address.', 'automatorwp' ),
137 + 'type' => 'text',
138 + 'required' => true,
139 + 'default' => ''
140 + ),
141 + 'first_name' => array(
142 + 'name' => __( 'First Name:', 'automatorwp' ),
143 + 'desc' => __( 'The user\'s first name.', 'automatorwp' ),
144 + 'type' => 'text',
145 + 'default' => ''
146 + ),
147 + 'last_name' => array(
148 + 'name' => __( 'Last Name:', 'automatorwp' ),
149 + 'desc' => __( 'The user\'s last name.', 'automatorwp' ),
150 + 'type' => 'text',
151 + 'default' => ''
152 + ),
153 + 'user_url' => array(
154 + 'name' => __( 'Website:', 'automatorwp' ),
155 + 'desc' => __( 'The user URL.', 'automatorwp' ),
156 + 'type' => 'text',
157 + 'default' => ''
158 + ),
159 + 'user_pass' => array(
160 + 'name' => __( 'Password:', 'automatorwp' ),
161 + 'desc' => __( 'The user password. Leave blank to get password will get automatically generated.', 'automatorwp' ),
162 + 'type' => 'text',
163 + 'default' => ''
164 + ),
165 + 'role' => automatorwp_utilities_role_field( array(
166 + 'option_custom' => true,
167 + 'desc' => __( 'The user\'s role. By default, "subscriber".', 'automatorwp' ),
168 + ) ),
169 + 'role_custom' => automatorwp_utilities_custom_field( array(
170 + 'option_custom_desc' => __( 'Role name.', 'automatorwp' )
171 + ) ),
172 + 'send_user_notification' => array(
173 + 'name' => __( 'Send User Notification:', 'automatorwp' ),
174 + 'desc' => __( 'Send the new user an email about their account.', 'automatorwp' ),
175 + 'type' => 'checkbox',
176 + 'classes' => 'cmb2-switch'
177 + ),
178 + 'user_meta' => array(
179 + 'name' => __( 'User Meta:', 'automatorwp' ),
180 + 'desc' => __( 'The user meta values keyed by their user meta key.', 'automatorwp' ),
181 + 'type' => 'group',
182 + 'classes' => 'automatorwp-fields-table',
183 + 'options' => array(
184 + 'add_button' => __( 'Add meta', 'automatorwp' ),
185 + 'remove_button' => '<span class="dashicons dashicons-no-alt"></span>',
186 + ),
187 + 'fields' => array(
188 + 'meta_key' => array(
189 + 'name' => __( 'Meta Key:', 'automatorwp' ),
190 + 'type' => 'text',
191 + 'default' => ''
192 + ),
193 + 'meta_value' => array(
194 + 'name' => __( 'Meta Value:', 'automatorwp' ),
195 + 'type' => 'text',
196 + 'default' => ''
197 + ),
198 + ),
199 + ),
200 + 'new_user_exists' => array(
201 + 'name' => __( 'What to do if the user already exists:', 'automatorwp' ),
202 + 'type' => 'radio',
203 + 'required' => true,
204 + 'options' => array(
205 + 'abort' => __( 'Do not run the actions', 'automatorwp' ),
206 + 'existing_user' => __( 'Select existing user', 'automatorwp' ),
207 + ),
208 + 'default_cb' => array( $this, 'return_abort_default_cb' ),
209 + ),
210 + 'new_user_search_field' => array(
211 + 'name' => __( 'Field to search the user:', 'automatorwp' ),
212 + 'desc' => __( 'The field by which to search the user if during creation is detected that the user already exists.', 'automatorwp' ),
213 + 'type' => 'select',
214 + 'required' => true,
215 + 'options' => array(
216 + 'email' => __( 'Email', 'automatorwp' ),
217 + 'login' => __( 'Username', 'automatorwp' ),
218 + ),
219 + 'default' => 'email',
220 + ),
221 + )
222 + )
223 + ),
224 + ) );
225 +
226 + }
227 +
228 + /**
229 + * Register required hooks
230 + *
231 + * @since 1.0.0
232 + */
233 + public function hooks() {
234 +
235 + // Dynamic edit label
236 + add_filter( 'automatorwp_get_anonymous_automation_user_id', array( $this, 'get_user_id' ), 10, 4 );
237 +
238 + // Dynamic edit label
239 + add_filter( 'automatorwp_parse_automation_item_edit_label', array( $this, 'dynamic_edit_label' ), 10, 5 );
240 +
241 + // Dynamic log label
242 + add_filter( 'automatorwp_parse_automation_item_log_label', array( $this, 'dynamic_log_label' ), 10, 5 );
243 +
244 + // Log meta data
245 + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
246 +
247 + // Log fields
248 + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
249 + add_filter( 'automatorwp_log_fields', array( $this, 'anonymous_log_fields' ), 10, 5 );
250 +
251 + parent::hooks();
252 + }
253 +
254 + /**
255 + * Decide to which user ID will be assigned to the automation
256 + *
257 + * @since 1.0.0
258 + *
259 + * @param int $user_id The user ID
260 + * @param stdClass $action The action object
261 + * @param array $action_options The action's stored options (with tags already passed)
262 + * @param stdClass $automation The action's automation object
263 + *
264 + * @return int|false The user ID, false otherwise
265 + */
266 + public function get_user_id( $user_id, $action, $action_options, $automation ) {
267 +
268 + $run_actions_on = $action_options['run_actions_on'];
269 + $this->user_id = $user_id;
270 + $this->result = '';
271 + $this->detailed_result = '';
272 + $this->new_user = false;
273 +
274 + $user_fields_labels = array(
275 + 'id' => __( 'ID', 'automatorwp' ),
276 + 'email' => __( 'email', 'automatorwp' ),
277 + 'login' => __( 'username', 'automatorwp' ),
278 + );
279 +
280 + // Bail if not defined where run the actions on
281 + if( ! in_array( $run_actions_on, array( 'existing_user', 'new_user' ) ) ) {
282 + return $this->user_id;
283 + }
284 +
285 + if( $run_actions_on === 'existing_user' ) {
286 + // Existing user
287 +
288 + // Shorthand
289 + $search_field = $action_options['search_field'];
290 + $search_field_value = $action_options['search_field_value'];
291 + $existing_user_not_exists = $action_options['existing_user_not_exists'];
292 +
293 + // Check that the search field is a correct one
294 + if( in_array( $search_field, array( 'id', 'email', 'login' ) ) ) {
295 +
296 + $user = get_user_by( $search_field, $search_field_value );
297 +
298 + // If user found, update the user ID and result
299 + if( $user ) {
300 +
301 + /* translators: %1$s: User field (id, email or username). */
302 + $this->result = sprintf( __( 'Existing user found by %1$s', 'automatorwp' ), $user_fields_labels[$search_field] );
303 +
304 + /* translators: %1$s: User field (id, email or username). %2$s: User field value. */
305 + $this->detailed_result = sprintf( __( 'Existing user found by the %1$s %2$s', 'automatorwp' ), $user_fields_labels[$search_field], $search_field_value );
306 +
307 + // Assign the user found
308 + $this->user_id = $user->ID;
309 +
310 + }
311 +
312 + }
313 +
314 + if( ! $this->user_id ) {
315 +
316 + // If user not found, decide if abort or create a new one
317 + switch( $existing_user_not_exists ) {
318 + case 'new_user':
319 + // Try to create a new user from the action options
320 + $new_user_id = $this->create_user( $action, $action_options, $automation );
321 +
322 + if( is_wp_error( $new_user_id ) ) {
323 +
324 + /* translators: %1$s: User field (id, email or username). */
325 + $this->result = sprintf( __( 'Existing user not found by %1$s, could not create user', 'automatorwp' ), $user_fields_labels[$search_field] );
326 +
327 + /* translators: %1$s: User field (id, email or username). %2$s: User field value. */
328 + $this->detailed_result = sprintf( __( 'Existing user not found by the %1$s %2$s, could not create user, reason: %3$s', 'automatorwp' ), $user_fields_labels[$search_field], $search_field_value, $new_user_id->get_error_message() );
329 +
330 + } else{
331 + /* translators: %1$s: User field (id, email or username). */
332 + $this->result = sprintf( __( 'Existing user not found by %1$s, new user created', 'automatorwp' ), $user_fields_labels[$search_field] );
333 +
334 + /* translators: %1$s: User field (id, email or username). %2$s: User field value. */
335 + $this->detailed_result = sprintf( __( 'Existing user not found by the %1$s %2$s, new user created', 'automatorwp' ), $user_fields_labels[$search_field], $search_field_value );
336 +
337 + // Assign the new created user
338 + $this->user_id = $new_user_id;
339 + $this->new_user = true;
340 + }
341 + break;
342 + case 'abort':
343 + default:
344 + // Abort
345 +
346 + /* translators: %1$s: User field (id, email or username). */
347 + $this->result = sprintf( __( 'Existing user not found by %1$s, execution aborted', 'automatorwp' ), $user_fields_labels[$search_field] );
348 +
349 + /* translators: %1$s: User field (id, email or username). %2$s: User field value. */
350 + $this->detailed_result = sprintf( __( 'Existing user not found by the %1$s %2$s, execution aborted', 'automatorwp' ), $user_fields_labels[$search_field], $search_field_value );
351 + break;
352 + }
353 +
354 + }
355 +
356 + }else if( $run_actions_on === 'new_user' ) {
357 + // New user action
358 +
359 + // Shorthand
360 + $new_user_exists = $action_options['new_user_exists'];
361 + $new_user_search_field = $action_options['new_user_search_field'];
362 + $found_by = 'email';
363 +
364 + // Check if the new user exists
365 + switch( $new_user_search_field ) {
366 + case 'login':
367 + $user_id = username_exists( $action_options['user_login'] );
368 + $found_by = 'login';
369 +
370 + if( ! $user_id ) {
371 + $user_id = email_exists( $action_options['user_email'] );
372 + $found_by = 'email';
373 + }
374 + break;
375 + case 'email':
376 + default:
377 + $user_id = email_exists( $action_options['user_email'] );
378 + $found_by = 'email';
379 +
380 + if( ! $user_id ) {
381 + $user_id = username_exists( $action_options['user_login'] );
382 + $found_by = 'login';
383 + }
384 + break;
385 + }
386 +
387 + if( $user_id ) {
388 +
389 + // If user found, decide if abort or use this one
390 + switch( $new_user_exists ) {
391 + case 'existing_user':
392 + /* translators: %1$s: User field (id, email or username). */
393 + $this->result = sprintf( __( 'New user found by %1$s, selecting user', 'automatorwp' ), $user_fields_labels[$found_by] );
394 +
395 + /* translators: %1$s: User field (id, email or username). %2$s: User field value. */
396 + $this->detailed_result = sprintf( __( 'New user found by the %1$s %2$s, selecting user', 'automatorwp' ), $user_fields_labels[$found_by], $action_options['user_' . $found_by] );
397 +
398 + // Assign the user found
399 + $this->user_id = $user_id;
400 + break;
401 + case 'abort':
402 + default:
403 + // Abort
404 +
405 + /* translators: %1$s: User field (id, email or username). */
406 + $this->result = sprintf( __( 'New user found by %1$s, execution aborted', 'automatorwp' ), $user_fields_labels[$found_by] );
407 +
408 + /* translators: %1$s: User field (id, email or username). %2$s: User field value. */
409 + $this->detailed_result = sprintf( __( 'New user found by the %1$s %2$s, execution aborted', 'automatorwp' ), $user_fields_labels[$found_by], $action_options['user_' . $found_by] );
410 +
411 + break;
412 + }
413 +
414 + } else {
415 +
416 + // Try to create a new user from the action options
417 + $new_user_id = $this->create_user( $action, $action_options, $automation );
418 +
419 + if( is_wp_error( $new_user_id ) ) {
420 +
421 + $this->result = __( 'Could not create user', 'automatorwp' );
422 +
423 + /* translators: %1$s: Error message. */
424 + $this->detailed_result = sprintf( __( 'Could not create user, reason: %1$s', 'automatorwp' ), $new_user_id->get_error_message() );
425 +
426 + } else {
427 +
428 + $this->result = __( 'New user created', 'automatorwp' );
429 + $this->detailed_result = __( 'New user created', 'automatorwp' );
430 +
431 + // Assign the new created user
432 + $this->user_id = $new_user_id;
433 + $this->new_user = true;
434 +
435 + }
436 +
437 + }
438 +
439 + }
440 +
441 + // Only insert a log entry if the user doesn't gets created
442 + if( ! $this->user_id ) {
443 + automatorwp_insert_log(
444 + array(
445 + 'title' => $this->result,
446 + 'type' => 'anonymous',
447 + 'object_id' => $automation->id,
448 + 'user_id' => ( $this->user_id ? $this->user_id : 0 ),
449 + 'post_id' => 0,
450 + 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
451 + ),
452 + array(
453 + 'new_user' => ( $this->new_user ? 1 : 0 ),
454 + 'result' => $this->detailed_result,
455 + ) );
456 + }
457 +
458 + return $this->user_id;
459 +
460 + }
461 +
462 + /**
463 + * Action execution function
464 + *
465 + * @since 1.0.0
466 + *
467 + * @param stdClass $action The action object
468 + * @param array $action_options The action's stored options (with tags already passed)
469 + * @param stdClass $automation The action's automation object
470 + *
471 + * @return int|WP_Error The new user ID, false otherwise
472 + */
473 + public function create_user( $action, $action_options, $automation ) {
474 +
475 + // Setup user fields
476 + $user_data = wp_parse_args( $action_options, array(
477 + 'user_login' => '',
478 + 'user_email' => '',
479 + 'first_name' => '',
480 + 'last_name' => '',
481 + 'user_url' => '',
482 + 'user_pass' => '',
483 + 'role' => 'subscriber',
484 + ) );
485 +
486 + // Check the user role
487 + $roles = automatorwp_get_editable_roles();
488 +
489 + // Bail if empty role to assign
490 + if( ! isset( $roles[$user_data['role']] ) ) {
491 + $user_data['role'] = 'subscriber';
492 + }
493 +
494 + // Generate the user password
495 + if( empty( $user_data['user_pass'] ) ) {
496 + $user_data['user_pass'] = wp_generate_password( 24 );
497 + }
498 +
499 + // Insert the user
500 + $user_id = wp_insert_user( $user_data );
501 +
502 + if( $user_id ) {
503 +
504 + if( is_array( $action_options['user_meta'] ) ) {
505 +
506 + foreach( $action_options['user_meta'] as $i => $meta ) {
507 +
508 + // Parse automation tags replacements to both, key and value
509 + $meta_key = automatorwp_parse_automation_tags( $automation->id, 0, $meta['meta_key'] );
510 + $meta_value = automatorwp_parse_automation_tags( $automation->id, 0, $meta['meta_value'] );
511 +
512 + // Sanitize
513 + $meta_key = sanitize_text_field( $meta_key );
514 + $meta_value = sanitize_text_field( $meta_value );
515 +
516 + // Update user meta
517 + update_user_meta( $user_id, $meta_key, $meta_value );
518 +
519 + $this->user_meta[$meta_key] = $meta_value;
520 +
521 + // Update action options to be passed on upcoming hooks
522 + $action_options['user_meta'][$i] = array(
523 + 'meta_key' => $meta_key,
524 + 'meta_value' => $meta_value,
525 + );
526 +
527 + }
528 +
529 + }
530 +
531 + // Send notification
532 + $notify = 'admin';
533 +
534 + if( (bool) $action_options['send_user_notification'] ) {
535 + $notify = 'both';
536 + }
537 +
538 + wp_send_new_user_notifications( $user_id, $notify );
539 +
540 + /**
541 + * Action triggered before create the new user for an anonymous automation
542 + *
543 + * @since 1.3.0
544 + *
545 + * @param int $user_id The new user ID
546 + * @param stdClass $action The action object
547 + * @param array $action_options The action's stored options (with tags already passed, included on meta keys and values)
548 + * @param stdClass $automation The action's automation object
549 + */
550 + do_action( 'automatorwp_anonymous_user_created', $user_id, $action, $action_options, $automation );
551 +
552 + }
553 +
554 + return $user_id;
555 +
556 + }
557 +
558 + /**
559 + * Default option abort
560 + *
561 + * @since 1.0.0
562 + *
563 + * @return string
564 + */
565 + public function return_abort_default_cb() {
566 + return 'abort';
567 + }
568 +
569 + /**
570 + * Custom edit/log label
571 + *
572 + * @since 1.0.0
573 + *
574 + * @param string $label The edit label
575 + * @param stdClass $object The trigger/action object
576 + * @param string $item_type The item type (trigger|action)
577 + * @param string $context The context this function is executed
578 + * @param array $type_args The type parameters
579 + *
580 + * @return string
581 + */
582 + public function dynamic_edit_label( $label, $object, $item_type, $context, $type_args ) {
583 +
584 + // Bail if action type don't match this action
585 + if( $object->type !== $this->action ) {
586 + return $label;
587 + }
588 +
589 + // Get the operation value
590 + ct_setup_table( "automatorwp_{$item_type}s" );
591 +
592 + // Update the edit label
593 + if( $context === 'edit' ) {
594 +
595 + // Get the action info
596 + $run_actions_on = ct_get_object_meta( $object->id, 'run_actions_on', true );
597 + $search_field = ct_get_object_meta( $object->id, 'search_field', true );
598 + $search_field_value = ct_get_object_meta( $object->id, 'search_field_value', true );
599 + $existing_user_not_exists = ct_get_object_meta( $object->id, 'existing_user_not_exists', true );
600 + $user_email = ct_get_object_meta( $object->id, 'user_email', true );
601 + $new_user_exists = ct_get_object_meta( $object->id, 'new_user_exists', true );
602 +
603 + // Setup labels
604 + $run_actions_on_labels = array(
605 + '' => '',
606 + 'existing_user' => __( 'Existing user', 'automatorwp' ),
607 + 'new_user' => __( 'New user', 'automatorwp' ),
608 + );
609 +
610 + $run_actions_on_images = array(
611 + '' => '',
612 + 'existing_user' => AUTOMATORWP_URL . 'assets/img/existing-user.svg',
613 + 'new_user' => AUTOMATORWP_URL . 'assets/img/new-user.svg',
614 + );
615 +
616 + $run_actions_on_label = $run_actions_on_labels[$run_actions_on];
617 +
618 + $existing_user_no_exists_labels = array(
619 + '' => '',
620 + 'abort' => __( 'Do not run the actions', 'automatorwp' ),
621 + 'new_user' => __( 'Create a new user', 'automatorwp' ),
622 + );
623 +
624 + $existing_user_no_exists_label = strtolower( $existing_user_no_exists_labels[$existing_user_not_exists] );
625 +
626 + $new_user_exists_labels = array(
627 + '' => '',
628 + 'abort' => __( 'Do not run the actions', 'automatorwp' ),
629 + 'existing_user' => __( 'Select existing user', 'automatorwp' ),
630 + );
631 +
632 + $new_user_exists_label = strtolower( $new_user_exists_labels[$new_user_exists] );
633 +
634 + ob_start(); ?>
635 + <div class="automatorwp-anonymous-user-label"><?php _e( 'Actions will be run on...', 'automatorwp' ); ?></div>
636 +
637 + <div class="automatorwp-anonymous-user-resume" <?php if( $run_actions_on === '' ) : ?>style="display: none"<?php endif; ?>>
638 +
639 + <div><img src="<?php echo $run_actions_on_images[$run_actions_on]; ?>" alt="<?php echo $run_actions_on_label; ?>"><strong><?php echo $run_actions_on_label; ?></strong></div>
640 +
641 + <?php if( $run_actions_on === 'existing_user' ) : ?>
642 +
643 + <div><?php printf( __( 'Where %1$s matches with %2$s', 'automatorwp' ), "<strong>{$search_field}</strong>", "<strong>{$search_field_value}</strong>" ); ?></div>
644 + <div><?php printf( __( 'If user not found then %1$s', 'automatorwp' ), "<strong>{$existing_user_no_exists_label}</strong>" ); ?></div>
645 +
646 + <?php elseif( $run_actions_on === 'new_user' ) : ?>
647 +
648 + <div><?php printf( __( 'With the email %1$s', 'automatorwp' ), "<strong>{$user_email}</strong>" ); ?></div>
649 + <div><?php printf( __( 'If user already exists then %1$s', 'automatorwp' ), "<strong>{$new_user_exists_label}</strong>" ); ?></div>
650 +
651 + <?php endif; ?>
652 +
653 + <button type="button" class="button button-primary automatorwp-anonymous-user-change-button"><?php _e( 'Change', 'automatorwp' ); ?></button>
654 + </div>
655 +
656 + <div class="automatorwp-anonymous-user-choices" <?php if( $run_actions_on !== '' ) : ?>style="display: none"<?php endif; ?>>
657 +
658 + <div class="automatorwp-anonymous-user-choice automatorwp-anonymous-user-choice-existing-user" data-choice="existing_user">
659 + <img src="<?php echo $run_actions_on_images['existing_user']; ?>" alt="">
660 + <strong><?php _e( 'Existing user', 'automatorwp' ); ?></strong>
661 + </div>
662 +
663 + <div class="automatorwp-anonymous-user-choice automatorwp-anonymous-user-choice-new-user" data-choice="new_user">
664 + <img src="<?php echo $run_actions_on_images['new_user']; ?>" alt="">
665 + <strong><?php _e( 'New user', 'automatorwp' ); ?></strong>
666 + </div>
667 +
668 + </div>
669 +
670 + <?php $choices_html = ob_get_clean();
671 +
672 + return $choices_html;
673 + }
674 +
675 + ct_reset_setup_table();
676 +
677 + return $label;
678 +
679 + }
680 +
681 + /**
682 + * Custom log label
683 + *
684 + * @since 1.0.0
685 + *
686 + * @param string $label The edit label
687 + * @param stdClass $object The trigger/action object
688 + * @param string $item_type The item type (trigger|action)
689 + * @param string $context The context this function is executed
690 + * @param array $type_args The type parameters
691 + *
692 + * @return string
693 + */
694 + public function dynamic_log_label( $label, $object, $item_type, $context, $type_args ) {
695 +
696 + // Bail if action type don't match this action
697 + if( $object->type !== $this->action ) {
698 + return $label;
699 + }
700 +
701 + return $this->result;
702 +
703 + }
704 +
705 + /**
706 + * Action custom log meta
707 + *
708 + * @since 1.0.0
709 + *
710 + * @param array $log_meta Log meta data
711 + * @param stdClass $action The action object
712 + * @param int $user_id The user ID
713 + * @param array $action_options The action's stored options (with tags already passed)
714 + * @param stdClass $automation The action's automation object
715 + *
716 + * @return array
717 + */
718 + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
719 +
720 + // Bail if action type don't match this action
721 + if( $action->type !== $this->action ) {
722 + return $log_meta;
723 + }
724 +
725 + // Store all actions options
726 + foreach( $action_options as $option_key => $option_value ) {
727 +
728 + // Skip user meta
729 + if( $option_key === 'user_meta' ) {
730 + continue;
731 + }
732 +
733 + $log_meta[$option_key] = $option_value;
734 + }
735 +
736 + // Store user meta
737 + $log_meta['user_meta'] = $this->user_meta;
738 +
739 + // Store result
740 + $log_meta['user_id'] = ( $this->user_id ? $this->user_id : 0 );
741 + $log_meta['new_user'] = ( $this->new_user ? 1 : 0 );
742 + $log_meta['result'] = $this->detailed_result;
743 +
744 + return $log_meta;
745 + }
746 +
747 + /**
748 + * Action custom log fields
749 + *
750 + * @since 1.0.0
751 + *
752 + * @param array $log_fields The log fields
753 + * @param stdClass $log The log object
754 + * @param stdClass $object The trigger/action/automation object attached to the log
755 + *
756 + * @return array
757 + */
758 + public function log_fields( $log_fields, $log, $object ) {
759 +
760 + // Bail if log is not assigned to an action
761 + if( $log->type !== 'action' ) {
762 + return $log_fields;
763 + }
764 +
765 + // Bail if action type don't match this action
766 + if( $object->type !== $this->action ) {
767 + return $log_fields;
768 + }
769 +
770 + $log_fields['run_actions_on'] = array(
771 + 'name' => __( 'Run actions on:', 'automatorwp' ),
772 + 'type' => 'select',
773 + 'options' => array(
774 + '' => '',
775 + 'existing_user' => __( 'Existing user', 'automatorwp' ),
776 + 'new_user' => __( 'New user', 'automatorwp' ),
777 + ),
778 + );
779 +
780 + $run_actions_on = automatorwp_get_log_meta( $log->id, 'run_actions_on', true );
781 +
782 + if( $run_actions_on === 'existing_user' ) {
783 +
784 + $log_fields['search_field'] = array(
785 + 'name' => __( 'Field to search the user:', 'automatorwp' ),
786 + 'desc' => __( 'The field by which to search the user.', 'automatorwp' ),
787 + 'type' => 'text',
788 + );
789 +
790 + $log_fields['search_field_value'] = array(
791 + 'name' => __( 'Field value:', 'automatorwp' ),
792 + 'desc' => __( 'Value of the field to search the user.', 'automatorwp' ),
793 + 'type' => 'text',
794 + );
795 +
796 + $log_fields['existing_user_not_exists'] = array(
797 + 'name' => __( 'What to do if the user doesn\'t exist:', 'automatorwp' ),
798 + 'type' => 'select',
799 + 'options' => array(
800 + 'abort' => __( 'Do not run the actions', 'automatorwp' ),
801 + 'new_user' => __( 'Create a new user', 'automatorwp' ),
802 + ),
803 + );
804 +
805 + }
806 +
807 + $new_user = automatorwp_get_log_meta( $log->id, 'new_user', true );
808 +
809 + if( $new_user ) {
810 +
811 + $log_fields['user_info'] = array(
812 + 'name' => __( 'User Information', 'automatorwp' ),
813 + 'desc' => __( 'Information about the user created.', 'automatorwp' ),
814 + 'type' => 'title',
815 + );
816 +
817 + $log_fields['user_login'] = array(
818 + 'name' => __( 'Username:', 'automatorwp' ),
819 + 'desc' => __( 'The user\'s login username.', 'automatorwp' ),
820 + 'type' => 'text',
821 + );
822 +
823 + $log_fields['user_email'] = array(
824 + 'name' => __( 'Email:', 'automatorwp' ),
825 + 'desc' => __( 'The user email address.', 'automatorwp' ),
826 + 'type' => 'text',
827 + 'default' => ''
828 + );
829 +
830 + $log_fields['first_name'] = array(
831 + 'name' => __( 'First Name:', 'automatorwp' ),
832 + 'desc' => __( 'The user\'s first name.', 'automatorwp' ),
833 + 'type' => 'text',
834 + 'default' => ''
835 + );
836 +
837 + $log_fields['last_name'] = array(
838 + 'name' => __( 'Last Name:', 'automatorwp' ),
839 + 'desc' => __( 'The user\'s last name.', 'automatorwp' ),
840 + 'type' => 'text',
841 + 'default' => ''
842 + );
843 +
844 + $log_fields['user_url'] = array(
845 + 'name' => __( 'Website:', 'automatorwp' ),
846 + 'desc' => __( 'The user URL.', 'automatorwp' ),
847 + 'type' => 'text',
848 + 'default' => ''
849 + );
850 +
851 + $log_fields['user_pass'] = array(
852 + 'name' => __( 'Password:', 'automatorwp' ),
853 + 'desc' => __( 'The user password.', 'automatorwp' ),
854 + 'type' => 'text',
855 + 'default' => ''
856 + );
857 +
858 + $log_fields['role'] = array(
859 + 'name' => __( 'Role:', 'automatorwp' ),
860 + 'desc' => __( 'The user\'s role.', 'automatorwp' ),
861 + 'type' => 'text',
862 + );
863 +
864 + $log_fields['user_meta'] = array(
865 + 'name' => __( 'User Meta:', 'automatorwp' ),
866 + 'desc' => __( 'The user meta values keyed by their user meta key.', 'automatorwp' ),
867 + 'type' => 'text',
868 + );
869 +
870 + }
871 +
872 + $log_fields['result'] = array(
873 + 'name' => __( 'Result:', 'automatorwp' ),
874 + 'type' => 'text',
875 + );
876 +
877 + return $log_fields;
878 + }
879 +
880 + /**
881 + * Action custom log fields
882 + *
883 + * @since 1.0.0
884 + *
885 + * @param array $log_fields The log fields
886 + * @param stdClass $log The log object
887 + * @param stdClass $object The trigger/action/automation object attached to the log
888 + *
889 + * @return array
890 + */
891 + public function anonymous_log_fields( $log_fields, $log, $object ) {
892 +
893 + // Bail if log is not assigned to an anonymous entry
894 + if( $log->type !== 'anonymous' ) {
895 + return $log_fields;
896 + }
897 +
898 + $log_fields['result'] = array(
899 + 'name' => __( 'Result:', 'automatorwp' ),
900 + 'type' => 'text',
901 + );
902 +
903 + return $log_fields;
904 +
905 + }
906 +
907 +}
908 +
909 +new AutomatorWP_WordPress_Anonymous_User();