User::user_signup()


Source

File: classes/User.php

	public function user_signup() {
		dozent_checking_nonce();

		$config = apply_filters( 'dozent_user_signup_form_validation_rules', [
			[
				'field' => 'first_name',
				'label' => __( 'First Name', 'dozent' ),
				'rules' => 'required',
			],
			[
				'field' => 'last_name',
				'label' => __( 'Last Name', 'dozent' ),
				'rules' => 'required',
			],
			[
				'field'  => 'email',
				'label'  => __( 'E-Mail', 'dozent' ),
				'rules'  => 'required|email',
				'errors' => [
					'email' => __( 'You must provide a valid %s.', 'dozent' ),
				],
			],
			[
				'field' => 'user_login',
				'label' => __( 'User Login', 'dozent' ),
				'rules' => 'required',
			],
			[
				'field' => 'password',
				'label' => __( 'Password', 'dozent' ),
				'rules' => 'required|confirm',
			],
			[
				'field' => 'password_confirmation',
				'label' => __( 'Password Confirmation', 'dozent' ),
				'rules' => 'required',
			],
		] );

		$validator = dozent_form_validate( $config );

		/**
		 * If validator fail, stop script
		 */
		if ( ! $validator->success ) {
			return;
		}

		$first_name = dozent_input_text( 'first_name' );
		$last_name  = dozent_input_text( 'last_name' );
		$email      = dozent_input_text( 'email' );
		$user_login = dozent_input_text( 'user_login' );
		$password   = dozent_input_text( 'password' );

		$userdata = apply_filters( 'dozent_user_signup_data', [
			'user_login' => $user_login,
			'user_email' => $email,
			'first_name' => $first_name,
			'last_name'  => $last_name,
			'role'       => 'subscriber',
			'user_pass'  => $password,
		] );


		do_action( 'dozent_user_signup_before', $userdata );


		$user_id = wp_insert_user( $userdata );

		if ( ! is_wp_error( $user_id ) ) {
			do_action( 'dozent_user_signup_after', $user_id );

			/**
			 * Should user login right after signup
			 */

			$login_after_signup = apply_filters( 'dozent_login_after_signup', true );

			if ( $login_after_signup && ! is_admin() ) {
				$user = get_user_by( 'id', $user_id );
				if ( $user ) {
					wp_set_current_user( $user_id, $user->user_login );
					wp_set_auth_cookie( $user_id );
				}
			}

		} else {

			dozent_add_form_errors( $user_id->get_error_messages() );

			return;
		}

		dozent_set_flash_message( __( 'Signup done', 'dozent' ) );

		if ( is_admin() ) {
			$this->upgrade_to_instructor( $user_id );
			dozent_redirect();
		} else {
			dozent_redirect( dozent_dashboard_uri() );
		}
	}