<?php
namespace Fabrica\Dashboard;

if (!defined('WPINC')) { die(); }

abstract class LoginsCommon {
	public static function init() {
		add_action('wp_login', array(__CLASS__, 'saveLoginTimestamps'), 10, 2);
	}

	// Save current login timestamp / IP, and move previous timestamp / IP to penultimate
	public static function saveLoginTimestamps($userLogin, $user) {
		$timeNow = time();
		$lastLogin = get_user_meta($user->ID, '_fd_current_login', true);
		if ($lastLogin && $lastLogin != $timeNow) {
			update_user_meta($user->ID, '_fd_last_login', $lastLogin);
			update_user_meta($user->ID, '_fd_last_ip', get_user_meta($user->ID, '_fd_current_ip', true));
		}
		update_user_meta($user->ID, '_fd_current_login', $timeNow);
		update_user_meta($user->ID, '_fd_current_ip', $_SERVER['REMOTE_ADDR']);
	}
}
