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 / triggers / user-field-update.php

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

298 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * User Field Update
4 *
5 * @package AutomatorWP\Integrations\WordPress\Triggers\User_Field_Update
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_Field_Update 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 . '_user_field_update';
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 profile field gets updated', 'automatorwp' ),
38 'select_option' => __( 'User <strong>profile field</strong> gets updated', 'automatorwp' ),
39 /* translators: %1$s: Profile Field. %2$s: Number of times. */
40 'edit_label' => sprintf( __( 'User %1$s gets updated %2$s time(s)', 'automatorwp' ), '{profile_field}', '{times}' ),
41 /* translators: %1$s: Profile Field. */
42 'log_label' => sprintf( __( 'User %1$s gets updated', 'automatorwp' ), '{profile_field}' ),
43 'action' => 'profile_update',
44 'function' => array( $this, 'listener' ),
45 'priority' => 10,
46 'accepted_args' => 2,
47 'options' => array(
48 'profile_field' => array(
49 'from' => 'profile_field',
50 'default' => __( 'profile field', 'automatorwp' ),
51 'fields' => array(
52 'profile_field' => array(
53 'name' => __( 'Profile field:', 'automatorwp' ),
54 'type' => 'select',
55 'options' => array(
56 '' => __( 'any profile field', 'automatorwp' ),
57 'user_login' => __( 'Username', 'automatorwp' ),
58 'user_email' => __( 'Email', 'automatorwp' ),
59 'display_name' => __( 'Display name', 'automatorwp' ),
60 'user_nicename' => __( 'Nicename', 'automatorwp' ),
61 'user_pass' => __( 'Password', 'automatorwp' ),
62 'user_url' => __( 'Website', 'automatorwp' ),
63 ),
64 'default' => ''
65 ),
66 )
67 ),
68 'times' => automatorwp_utilities_times_option(),
69 ),
70 'tags' => array_merge(
71 array(
72 'new_field_value' => array(
73 'label' => __( 'New field value', 'automatorwp' ),
74 'type' => 'text',
75 'preview' => __( 'New value of the updated field', 'automatorwp' ),
76 ),
77 'old_field_value' => array(
78 'label' => __( 'Old field value', 'automatorwp' ),
79 'type' => 'text',
80 'preview' => __( 'Old value of the updated field', 'automatorwp' ),
81 ),
82 ),
83 automatorwp_utilities_times_tag()
84 )
85 ) );
86
87 }
88
89 /**
90 * Trigger listener
91 *
92 * @since 1.0.0
93 *
94 * @param int $user_id User ID.
95 * @param WP_User $old_user_data Object containing user's data prior to update.
96 */
97 public function listener( $user_id, $old_user_data ) {
98
99 // Setup vars
100 $user_fields = array(
101 'user_login',
102 'user_email',
103 'display_name',
104 'user_nicename',
105 'user_pass',
106 'user_url',
107 );
108
109 $new_user_data = get_userdata( $user_id );
110
111 foreach( $user_fields as $user_field ) {
112
113 // Skip field if not updated
114 if( $new_user_data->$user_field === $old_user_data->$user_field ) {
115 continue;
116 }
117
118 automatorwp_trigger_event( array(
119 'trigger' => $this->trigger,
120 'user_id' => $user_id,
121 'profile_field' => $user_field,
122 'new_field_value' => $new_user_data->$user_field,
123 'old_field_value' => $old_user_data->$user_field,
124 ) );
125
126 }
127
128 }
129
130 /**
131 * User deserves check
132 *
133 * @since 1.0.0
134 *
135 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
136 * @param stdClass $trigger The trigger object
137 * @param int $user_id The user ID
138 * @param array $event Event information
139 * @param array $trigger_options The trigger's stored options
140 * @param stdClass $automation The trigger's automation object
141 *
142 * @return bool True if user deserves trigger, false otherwise
143 */
144 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
145
146 // Don't deserve if post is not received
147 if( ! isset( $event['profile_field'] ) ) {
148 return false;
149 }
150
151 // Don't deserve if profile field doesn't matches with the trigger option
152 if( $trigger_options['profile_field'] !== '' && $trigger_options['profile_field'] !== $event['profile_field'] ) {
153 return false;
154 }
155
156 return $deserves_trigger;
157
158 }
159
160 /**
161 * Register the required hooks
162 *
163 * @since 1.0.0
164 */
165 public function hooks() {
166
167 // Tags replacement
168 add_filter( 'automatorwp_get_trigger_tag_replacement', array( $this, 'tags_replacement' ), 10, 6 );
169
170 // Log meta data
171 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
172
173 // Log fields
174 add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
175
176 parent::hooks();
177 }
178
179 /**
180 * Trigger custom tags replacement
181 *
182 * @since 1.0.0
183 *
184 * @param string $replacement The tag replacement
185 * @param string $tag_name The tag name (without "{}")
186 * @param stdClass $trigger The trigger object
187 * @param int $user_id The user ID
188 * @param string $content The content to parse
189 * @param stdClass $log The last trigger log object
190 *
191 * @return string
192 */
193 function tags_replacement( $replacement, $tag_name, $trigger, $user_id, $content, $log ) {
194
195 // Bail if action type don't match this action
196 if( $trigger->type !== $this->trigger ) {
197 return $replacement;
198 }
199
200 switch( $tag_name ) {
201 case 'new_field_value':
202 $replacement = automatorwp_get_log_meta( $log->id, 'new_field_value', true );
203 break;
204 case 'old_field_value':
205 $replacement = automatorwp_get_log_meta( $log->id, 'old_field_value', true );
206 break;
207 }
208
209 return $replacement;
210
211 }
212
213 /**
214 * Trigger custom log meta
215 *
216 * @since 1.0.0
217 *
218 * @param array $log_meta Log meta data
219 * @param stdClass $trigger The trigger object
220 * @param int $user_id The user ID
221 * @param array $event Event information
222 * @param array $trigger_options The trigger's stored options
223 * @param stdClass $automation The trigger's automation object
224 *
225 * @return array
226 */
227 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
228
229 // Bail if action type don't match this action
230 if( $trigger->type !== $this->trigger ) {
231 return $log_meta;
232 }
233
234 $log_meta['profile_field'] = ( isset( $event['profile_field'] ) ? $event['profile_field'] : '' );
235 $log_meta['new_field_value'] = ( isset( $event['new_field_value'] ) ? $event['new_field_value'] : '' );
236 $log_meta['old_field_value'] = ( isset( $event['old_field_value'] ) ? $event['old_field_value'] : '' );
237
238 return $log_meta;
239
240 }
241
242 /**
243 * Action custom log fields
244 *
245 * @since 1.0.0
246 *
247 * @param array $log_fields The log fields
248 * @param stdClass $log The log object
249 * @param stdClass $object The trigger/action/automation object attached to the log
250 *
251 * @return array
252 */
253 public function log_fields( $log_fields, $log, $object ) {
254
255 // Bail if log is not assigned to an trigger
256 if( $log->type !== 'trigger' ) {
257 return $log_fields;
258 }
259
260 // Bail if trigger type don't match this trigger
261 if( $object->type !== $this->trigger ) {
262 return $log_fields;
263 }
264
265 $log_fields['profile_field'] = array(
266 'name' => __( 'Profile field', 'automatorwp' ),
267 'desc' => __( 'Profile field updated.', 'automatorwp' ),
268 'type' => 'select',
269 'options' => array(
270 'user_login' => __( 'Username', 'automatorwp' ),
271 'user_email' => __( 'Email', 'automatorwp' ),
272 'display_name' => __( 'Display name', 'automatorwp' ),
273 'user_nicename' => __( 'Nicename', 'automatorwp' ),
274 'user_pass' => __( 'Password', 'automatorwp' ),
275 'user_url' => __( 'Website', 'automatorwp' ),
276 ),
277 );
278
279 $log_fields['new_field_value'] = array(
280 'name' => __( 'New field value', 'automatorwp' ),
281 'desc' => __( 'New value of the updated field.', 'automatorwp' ),
282 'type' => 'text',
283 );
284
285 $log_fields['old_field_value'] = array(
286 'name' => __( 'Old field value', 'automatorwp' ),
287 'desc' => __( 'Old value of the updated field.', 'automatorwp' ),
288 'type' => 'text',
289 );
290
291 return $log_fields;
292
293 }
294
295 }
296
297 new AutomatorWP_WordPress_User_Field_Update( 'wordpress' );
298 new AutomatorWP_WordPress_User_Field_Update( 'users' );