<?php

/**
 * Automatically log in WordPress
 * @category Functions
 * @author Krik
 */

add_action('login_init', 'krik_auto_login');

function krik_auto_login()
{
	if (WP_ENV != 'production' && WP_LOGIN != false && WP_LOGIN != '') {
		add_filter('authenticate', 'krik_auto_login_user', 10, 3);
	}
}

function krik_auto_login_user($user, $username, $password)
{
	$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'login';
	if ('login' === $action
		&& isset($_POST['wp-submit'])
		&& empty($username)
		&& empty($password)) {
		$auto_user = get_user_by('slug', WP_LOGIN);
		if ($auto_user !== false) {
			$user = $auto_user;
		}
	}
	return $user;
}

// Remove required attribute from login inputs
if ( $GLOBALS['pagenow'] === 'wp-login.php' ) {
  ob_start();
}

add_action('login_form', 'krik_remove_required_from_login_form', 9999);

function krik_remove_required_from_login_form($args) {
	$login = ob_get_contents();
	ob_clean();
	$login = str_replace('required="required"', '', $login);
	echo $login;
}
