| @@ -1,0 +1,402 @@ | ||
| 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 | + | |
| 178 | + // Generate the user password | |
| 179 | + if( empty( $user_data['user_pass'] ) ) { | |
| 180 | + $user_data['user_pass'] = wp_generate_password( 24 ); | |
| 181 | + } | |
| 182 | + | |
| 183 | + // Insert the user | |
| 184 | + $this->user_id = wp_insert_user( $user_data ); | |
| 185 | + | |
| 186 | + if( $this->user_id ) { | |
| 187 | + | |
| 188 | + if( isset( $action_options['user_meta'] ) && is_array( $action_options['user_meta'] ) ) { | |
| 189 | + | |
| 190 | + foreach( $action_options['user_meta'] as $i => $meta ) { | |
| 191 | + | |
| 192 | + // Parse automation tags replacements to both, key and value | |
| 193 | + $meta_key = automatorwp_parse_automation_tags( $automation->id, $user_id, $meta['meta_key'] ); | |
| 194 | + $meta_value = automatorwp_parse_automation_tags( $automation->id, $user_id, $meta['meta_value'] ); | |
| 195 | + | |
| 196 | + // Sanitize | |
| 197 | + $meta_key = sanitize_text_field( $meta_key ); | |
| 198 | + $meta_value = sanitize_text_field( $meta_value ); | |
| 199 | + | |
| 200 | + // Update user meta | |
| 201 | + update_user_meta( $this->user_id, $meta_key, $meta_value ); | |
| 202 | + | |
| 203 | + $this->user_meta[$meta_key] = $meta_value; | |
| 204 | + | |
| 205 | + // Update action options to be passed on upcoming hooks | |
| 206 | + $action_options['user_meta'][$i] = array( | |
| 207 | + 'meta_key' => $meta_key, | |
| 208 | + 'meta_value' => $meta_value, | |
| 209 | + ); | |
| 210 | + | |
| 211 | + } | |
| 212 | + | |
| 213 | + } | |
| 214 | + | |
| 215 | + // Send notification | |
| 216 | + $notify = 'admin'; | |
| 217 | + | |
| 218 | + if( isset( $action_options['send_user_notification'] ) && (bool) $action_options['send_user_notification'] ) { | |
| 219 | + $notify = 'both'; | |
| 220 | + } | |
| 221 | + | |
| 222 | + wp_send_new_user_notifications( $this->user_id, $notify ); | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Action triggered before the create new user action gets executed | |
| 226 | + * | |
| 227 | + * @since 1.2.6 | |
| 228 | + * | |
| 229 | + * @param int $new_user_id The new user ID | |
| 230 | + * @param stdClass $action The action object | |
| 231 | + * @param int $user_id The user ID (user who triggered the automation) | |
| 232 | + * @param array $action_options The action's stored options (with tags already passed, included on meta keys and values) | |
| 233 | + * @param stdClass $automation The action's automation object | |
| 234 | + */ | |
| 235 | + do_action( 'automatorwp_wordpress_create_user_executed', $this->user_id, $action, $user_id, $action_options, $automation ); | |
| 236 | + | |
| 237 | + } | |
| 238 | + | |
| 239 | + } | |
| 240 | + | |
| 241 | + /** | |
| 242 | + * Register required hooks | |
| 243 | + * | |
| 244 | + * @since 1.0.0 | |
| 245 | + */ | |
| 246 | + public function hooks() { | |
| 247 | + | |
| 248 | + // Log meta data | |
| 249 | + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); | |
| 250 | + | |
| 251 | + // Log fields | |
| 252 | + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); | |
| 253 | + | |
| 254 | + parent::hooks(); | |
| 255 | + } | |
| 256 | + | |
| 257 | + /** | |
| 258 | + * Action custom log meta | |
| 259 | + * | |
| 260 | + * @since 1.0.0 | |
| 261 | + * | |
| 262 | + * @param array $log_meta Log meta data | |
| 263 | + * @param stdClass $action The action object | |
| 264 | + * @param int $user_id The user ID | |
| 265 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 266 | + * @param stdClass $automation The action's automation object | |
| 267 | + * | |
| 268 | + * @return array | |
| 269 | + */ | |
| 270 | + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { | |
| 271 | + | |
| 272 | + // Bail if action type don't match this action | |
| 273 | + if( $action->type !== $this->action ) | |
| 274 | + return $log_meta; | |
| 275 | + | |
| 276 | + // Store user fields | |
| 277 | + $user_fields = array( | |
| 278 | + 'user_login', | |
| 279 | + 'user_email', | |
| 280 | + 'first_name', | |
| 281 | + 'last_name', | |
| 282 | + 'user_url', | |
| 283 | + 'user_pass', | |
| 284 | + 'role', | |
| 285 | + ); | |
| 286 | + | |
| 287 | + foreach( $user_fields as $user_field ) { | |
| 288 | + $log_meta[$user_field] = $action_options[$user_field]; | |
| 289 | + } | |
| 290 | + | |
| 291 | + // Store user ID | |
| 292 | + $log_meta['user_id'] = $this->user_id; | |
| 293 | + | |
| 294 | + // Store user meta | |
| 295 | + $log_meta['user_meta'] = $this->user_meta; | |
| 296 | + | |
| 297 | + // Store result | |
| 298 | + if( is_wp_error( $this->user_id ) ) { | |
| 299 | + $log_meta['result'] = $this->user_id->get_error_message(); | |
| 300 | + } else if( $this->user_id ) { | |
| 301 | + $log_meta['result'] = __( 'User created correctly', 'automatorwp' ); | |
| 302 | + } else { | |
| 303 | + $log_meta['result'] = __( 'Could not create user', 'automatorwp' ); | |
| 304 | + } | |
| 305 | + | |
| 306 | + return $log_meta; | |
| 307 | + } | |
| 308 | + | |
| 309 | + /** | |
| 310 | + * Action custom log fields | |
| 311 | + * | |
| 312 | + * @since 1.0.0 | |
| 313 | + * | |
| 314 | + * @param array $log_fields The log fields | |
| 315 | + * @param stdClass $log The log object | |
| 316 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 317 | + * | |
| 318 | + * @return array | |
| 319 | + */ | |
| 320 | + public function log_fields( $log_fields, $log, $object ) { | |
| 321 | + | |
| 322 | + // Bail if log is not assigned to this action | |
| 323 | + if( $log->type !== 'action' || $object->type !== $this->action ) | |
| 324 | + return $log_fields; | |
| 325 | + | |
| 326 | + $log_fields['user_info'] = array( | |
| 327 | + 'name' => __( 'User Information', 'automatorwp' ), | |
| 328 | + 'desc' => __( 'Information about the user created.', 'automatorwp' ), | |
| 329 | + 'type' => 'title', | |
| 330 | + ); | |
| 331 | + | |
| 332 | + $log_fields['user_id_meta'] = array( | |
| 333 | + 'name' => __( 'User:', 'automatorwp' ), | |
| 334 | + 'desc' => __( 'The user the action runs for.', 'automatorwp' ), | |
| 335 | + 'type' => 'text', | |
| 336 | + ); | |
| 337 | + | |
| 338 | + $log_fields['user_login'] = array( | |
| 339 | + 'name' => __( 'Username:', 'automatorwp' ), | |
| 340 | + 'desc' => __( 'The user\'s login username.', 'automatorwp' ), | |
| 341 | + 'type' => 'text', | |
| 342 | + ); | |
| 343 | + | |
| 344 | + $log_fields['user_email'] = array( | |
| 345 | + 'name' => __( 'Email:', 'automatorwp' ), | |
| 346 | + 'desc' => __( 'The user email address.', 'automatorwp' ), | |
| 347 | + 'type' => 'text', | |
| 348 | + 'default' => '' | |
| 349 | + ); | |
| 350 | + | |
| 351 | + $log_fields['first_name'] = array( | |
| 352 | + 'name' => __( 'First Name:', 'automatorwp' ), | |
| 353 | + 'desc' => __( 'The user\'s first name.', 'automatorwp' ), | |
| 354 | + 'type' => 'text', | |
| 355 | + 'default' => '' | |
| 356 | + ); | |
| 357 | + | |
| 358 | + $log_fields['last_name'] = array( | |
| 359 | + 'name' => __( 'Last Name:', 'automatorwp' ), | |
| 360 | + 'desc' => __( 'The user\'s last name.', 'automatorwp' ), | |
| 361 | + 'type' => 'text', | |
| 362 | + 'default' => '' | |
| 363 | + ); | |
| 364 | + | |
| 365 | + $log_fields['user_url'] = array( | |
| 366 | + 'name' => __( 'Website:', 'automatorwp' ), | |
| 367 | + 'desc' => __( 'The user URL.', 'automatorwp' ), | |
| 368 | + 'type' => 'text', | |
| 369 | + 'default' => '' | |
| 370 | + ); | |
| 371 | + | |
| 372 | + $log_fields['user_pass'] = array( | |
| 373 | + 'name' => __( 'Password:', 'automatorwp' ), | |
| 374 | + 'desc' => __( 'The user password.', 'automatorwp' ), | |
| 375 | + 'type' => 'text', | |
| 376 | + 'default' => '' | |
| 377 | + ); | |
| 378 | + | |
| 379 | + $log_fields['role'] = array( | |
| 380 | + 'name' => __( 'Role:', 'automatorwp' ), | |
| 381 | + 'desc' => __( 'The user\'s role.', 'automatorwp' ), | |
| 382 | + 'type' => 'text', | |
| 383 | + ); | |
| 384 | + | |
| 385 | + $log_fields['user_meta'] = array( | |
| 386 | + 'name' => __( 'User Meta:', 'automatorwp' ), | |
| 387 | + 'desc' => __( 'The user meta values keyed by their user meta key.', 'automatorwp' ), | |
| 388 | + 'type' => 'text', | |
| 389 | + ); | |
| 390 | + | |
| 391 | + $log_fields['result'] = array( | |
| 392 | + 'name' => __( 'Result:', 'automatorwp' ), | |
| 393 | + 'type' => 'text', | |
| 394 | + ); | |
| 395 | + | |
| 396 | + return $log_fields; | |
| 397 | + } | |
| 398 | + | |
| 399 | +} | |
| 400 | + | |
| 401 | +new AutomatorWP_WordPress_Create_User( 'wordpress' ); | |
| 402 | +new AutomatorWP_WordPress_Create_User( 'users' ); | |