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/triggers/set-role-from-to.php +139 -0 5.7.2 → 6.0.3 View file →
@@ -1,0 +1,139 @@
1 +<?php
2 +/**
3 + * Set Role From/To
4 + *
5 + * @package AutomatorWP\Integrations\WordPress\Triggers\Set_Role_From_To
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_Set_Role_From_To extends AutomatorWP_Integration_Trigger {
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->trigger = $integration . '_set_role_from_to';
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_trigger( $this->trigger, array(
36 + 'integration' => $this->integration,
37 + 'label' => __( 'User role changes from role to role', 'automatorwp' ),
38 + 'select_option' => __( 'User role <strong>changes</strong> from role to role', 'automatorwp' ),
39 + /* translators: %1$s: Role From. %2$s: Role To. %2$s: Number of times. */
40 + 'edit_label' => sprintf( __( 'User role changes from %1$s to %2$s %3$s time(s)', 'automatorwp' ), '{role_from}', '{role_to}', '{times}' ),
41 + /* translators: %1$s: Role From. %2$s: Role To. */
42 + 'log_label' => sprintf( __( 'User role changes from %1$s to %2$s', 'automatorwp' ), '{role_from}', '{role_to}' ),
43 + 'action' => 'set_user_role',
44 + 'function' => array( $this, 'listener' ),
45 + 'priority' => 10,
46 + 'accepted_args' => 3,
47 + 'options' => array(
48 + 'role_from' => array(
49 + 'from' => 'role_from',
50 + 'default' => 'any',
51 + 'fields' => array(
52 + 'role_from' => automatorwp_utilities_role_field( array(
53 + 'option_custom' => true,
54 + ) ),
55 + 'role_from_custom' => automatorwp_utilities_custom_field( array(
56 + 'option_custom_desc' => __( 'Role name.', 'automatorwp' )
57 + ) ),
58 + )
59 + ),
60 + 'role_to' => array(
61 + 'from' => 'role_to',
62 + 'default' => 'any',
63 + 'fields' => array(
64 + 'role_to' => automatorwp_utilities_role_field( array(
65 + 'option_custom' => true,
66 + ) ),
67 + 'role_to_custom' => automatorwp_utilities_custom_field( array(
68 + 'option_custom_desc' => __( 'Role name.', 'automatorwp' )
69 + ) ),
70 + )
71 + ),
72 + 'times' => automatorwp_utilities_times_option(),
73 + ),
74 + 'tags' => array_merge(
75 + automatorwp_utilities_times_tag()
76 + )
77 + ) );
78 +
79 + }
80 +
81 + /**
82 + * Trigger listener
83 + *
84 + * @since 1.0.0
85 + *
86 + * @param int $user_id The user ID.
87 + * @param string $role The new role.
88 + * @param string[] $old_roles An array of the user's previous roles.
89 + */
90 + public function listener( $user_id, $role, $old_roles ) {
91 +
92 + automatorwp_trigger_event( array(
93 + 'trigger' => $this->trigger,
94 + 'user_id' => $user_id,
95 + 'role_from' => $old_roles[0],
96 + 'role_to' => $role,
97 + ) );
98 +
99 + }
100 +
101 + /**
102 + * User deserves check
103 + *
104 + * @since 1.0.0
105 + *
106 + * @param bool $deserves_trigger True if user deserves trigger, false otherwise
107 + * @param stdClass $trigger The trigger object
108 + * @param int $user_id The user ID
109 + * @param array $event Event information
110 + * @param array $trigger_options The trigger's stored options
111 + * @param stdClass $automation The trigger's automation object
112 + *
113 + * @return bool True if user deserves trigger, false otherwise
114 + */
115 + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
116 +
117 + // Don't deserve if role is not received
118 + if( ! isset( $event['role_from'] ) && ! isset( $event['role_to'] ) ) {
119 + return false;
120 + }
121 +
122 + // Don't deserve if role doesn't match with the trigger option
123 + if( $trigger_options['role_from'] !== 'any' && $trigger_options['role_from'] !== $event['role_from'] ) {
124 + return false;
125 + }
126 +
127 + // Don't deserve if role to change doesn't match with the trigger option
128 + if( $trigger_options['role_to'] !== 'any' && $trigger_options['role_to'] !== $event['role_to'] ) {
129 + return false;
130 + }
131 +
132 + return $deserves_trigger;
133 +
134 + }
135 +
136 +}
137 +
138 +new AutomatorWP_WordPress_Set_Role_From_To( 'wordpress' );
139 +new AutomatorWP_WordPress_Set_Role_From_To( 'users' );