<?php
namespace Fabrica\Dashboard;

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

require_once('base.php');

class You {
	public static function init() {
		add_action('wp_dashboard_setup', array(__CLASS__, 'register'));
	}

	// Register
	public static function register() {
		add_meta_box('fd_you', 'You', array(__CLASS__, 'display'), 'dashboard', 'side', 'high');
	}

	// Display
	public static function display() {
		// suffix `_safe` tells WP the variable doesn't require escaping
		$avatar = get_avatar(Base::$currentUser->ID, 48, null, null, array('class' => 'fd-util-block'));
		if ($avatar) {
			?><a class="fd-icon fd-util-inline" title="<?php esc_attr_e("You can change your user icon in Gravatar", Base::$textDomain); ?>" href="https://en.gravatar.com/"><?php echo esc_html($avatar); ?></a><?php
		} else {
			?><span class="fd-icon fd-util-inline fd-usericon"></span><?php
		}
		?><div class="fd-util-inline">
			<strong><?php echo esc_html(Base::$currentUser->display_name) . ' (' . esc_html(ucfirst(Base::$currentUser->roles[0])) . ')'; ?></strong>
			at IP <?php echo esc_url($_SERVER['REMOTE_ADDR']); ?>
			<?php if (Base::$lastLogin) {
				echo '<br>' . esc_html__("last login", Base::$textDomain) . ': ' . esc_html(human_time_diff(Base::$lastLogin)) . ' ' . esc_html__("ago", Base::$textDomain);
				$lastIP = get_user_meta(Base::$currentUser->ID, '_fd_last_ip', true);
				if ($lastIP) {
					echo ' from IP ' . esc_url($lastIP);
				}
			} ?>
		</div><?php
	}
}
You::init();
