WP_CREATEUSER::create_user( $user_id,  $action_data,  $recipe_id,  $args )

Validation function when the trigger action is hit


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)

$args

(Required)


Source Source

File: src/integrations/wp/actions/wp-createuser.php

	public function create_user( $user_id, $action_data, $recipe_id, $args ) {




		// Username is mandatory. Return error its not valid.
		if ( isset( $action_data['meta']['USERNAME'] ) ) {
			$username = Automator()->parse->text( $action_data['meta']['USERNAME'], $recipe_id, $user_id, $args );
			if ( ! validate_username( $username ) ) {
				Automator()->complete->action( $user_id, $action_data, $recipe_id, sprintf(
				/* translators: Create a {{user}} - Error while creating a new user */
					esc_attr__( 'Invalid username: %1$s', 'uncanny-automator' ),
					$username ) );
			}
		} else {
			Automator()->complete->action( $user_id, $action_data, $recipe_id,
				/* translators: Create a {{user}} - Error while creating a new user */
				esc_attr__( 'Username was not set', 'uncanny-automator' )
			);

			return;
		}

		// Email is mandatory. Return error its not valid.
		if ( isset( $action_data['meta']['EMAIL'] ) ) {
			$email = Automator()->parse->text( $action_data['meta']['EMAIL'], $recipe_id, $user_id, $args );
			if ( ! is_email( $email ) ) {
				Automator()->complete->action( $user_id, $action_data, $recipe_id, sprintf(
				/* translators: Create a {{user}} - Error while creating a new user */
					esc_attr__( 'Invalid email: %1$s', 'uncanny-automator' )
					, $email ) );
			}
		} else {
			Automator()->complete->action( $user_id, $action_data, $recipe_id, esc_attr__( 'Username was not set', 'uncanny-automator' ) );

			return;
		}

		$userdata = array(
			'user_login' => $username,   //(string) The user's login username.
			'user_email' => $email,   //(string) The user email address.
		);

		if ( isset( $action_data['meta']['PASSWORD'] ) && ! empty( $action_data['meta']['PASSWORD'] ) ) {
			$userdata['user_pass'] = Automator()->parse->text( $action_data['meta']['PASSWORD'], $recipe_id, $user_id, $args );
		} else {
			$userdata['user_pass'] = wp_generate_password();
		}

		if ( isset( $action_data['meta']['WEBSITE'] ) && ! empty( $action_data['meta']['WEBSITE'] ) ) {
			$userdata['user_url'] = Automator()->parse->text( $action_data['meta']['WEBSITE'], $recipe_id, $user_id, $args );
		}

		if ( isset( $action_data['meta']['FIRSTNAME'] ) && ! empty( $action_data['meta']['FIRSTNAME'] ) ) {
			$userdata['first_name'] = Automator()->parse->text( $action_data['meta']['FIRSTNAME'], $recipe_id, $user_id, $args );
		}

		if ( isset( $action_data['meta']['LASTNAME'] ) && ! empty( $action_data['meta']['LASTNAME'] ) ) {
			$userdata['last_name'] = Automator()->parse->text( $action_data['meta']['LASTNAME'], $recipe_id, $user_id, $args );
		}

		if ( isset( $action_data['meta']['WPROLE'] ) && ! empty( $action_data['meta']['WPROLE'] ) ) {
			$userdata['role'] = $action_data['meta']['WPROLE'];
		}

		$user_id = wp_insert_user( $userdata );

		if ( is_wp_error( $user_id ) ) {
			Automator()->complete->action( $user_id, $action_data, $recipe_id,
				/* translators: Create a {{user}} - Error while creating a new user */
				esc_attr__( 'Failed to create a user', 'uncanny-automator' )
			);

			return;
		}

		$failed_meta_updates = array();


		if ( isset( $action_data['meta']['USERMETA_PAIRS'] ) && ! empty( $action_data['meta']['USERMETA_PAIRS'] ) ) {
			$fields = json_decode( $action_data['meta']['USERMETA_PAIRS'], true );

			foreach ( $fields as $meta ) {
				if ( isset( $meta['meta_key'] ) && ! empty( $meta['meta_key'] ) && isset( $meta['meta_value'] ) && ! empty( $meta['meta_value'] ) ) {
					$key   = Automator()->parse->text( $meta['meta_key'], $recipe_id, $user_id, $args );
					$value = Automator()->parse->text( $meta['meta_value'], $recipe_id, $user_id, $args );
					update_user_meta( $user_id, $key, $value );
				} else {
					$failed_meta_updates[ $meta['meta_key'] ] = $meta['meta_value'];
				}
			}
		}

		if ( ! empty( $failed_meta_updates ) ) {
			$failed_keys = "'" . implode( "','", array_keys( $failed_meta_updates ) ) . "'";
			Automator()->complete->action( $user_id, $action_data, $recipe_id, sprintf(
			/* translators: Create a {{user}} - Error while creating a new user */
				esc_attr__( 'Meta keys failed to update: %1$s', 'uncanny-automator' ),
				$failed_keys ) );
		}

		wp_new_user_notification( $user_id, null, 'both' );

		Automator()->complete->action( $user_id, $action_data, $recipe_id );
	}