<?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;
}
