User::reset_password()


Source

File: classes/User.php

	public function reset_password() {
		dozent_checking_nonce();

		$config = apply_filters( 'dozent_reset_password_form_validation_rules', [
			[
				'field' => 'current_password',
				'label' => __( 'Current Password', 'dozent' ),
				'rules' => 'required',
			],
			[
				'field' => 'password',
				'label' => __( 'New Password', 'dozent' ),
				'rules' => 'required|confirm',
			],
			[
				'field' => 'password_confirmation',
				'label' => __( 'Confirm New Password', 'dozent' ),
				'rules' => 'required',
			],
		] );

		$validator = dozent_form_validate( $config );

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


		$current_password = dozent_input_text( 'current_password' );
		$new_password     = dozent_input_text( 'password' );

		//Check old password
		$user = wp_get_current_user();

		$valid_current_password = wp_check_password( $current_password, $user->user_pass, $user->ID );

		if ( $valid_current_password ) {

			//Change the password immediately
			$user_id = get_current_user_id();
			wp_set_password( $new_password, $user->ID );
			dozent_set_flash_message( __( 'Password has been reset successfully', 'dozent' ) );
		} else {
			dozent_set_flash_message( __( 'Invalid current password', 'dozent' ), 'warning' );
		}

		dozent_redirect();
	}