<?php
namespace Fabrica\Dashboard;

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

require_once('base.php');

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

	// Register
	public static function register() {
		add_meta_box('fd_site', 'Site', array(__CLASS__, 'display'), 'dashboard', 'normal', 'high');
	}

	// Display
	public static function display() {

		// Get last site activity
		$timestamps = array();
		global $wpdb;

		// Latest post creation
		$query = 'SELECT post_date_gmt, ID FROM ' . Base::$dbPrefix . 'posts WHERE post_date_gmt < NOW() ORDER BY post_date_gmt DESC LIMIT 1';
		$results = $wpdb->get_results($query, OBJECT_K);
		if ($results) {
			$timestamps[key($results)] = array(
				'type' => __("content created", Base::$textDomain),
				'link' => admin_url('post.php?action=edit&post=' . current($results)->ID)
			);
		}

		// Latest post modification
		$query = 'SELECT post_modified_gmt, ID FROM ' . Base::$dbPrefix . 'posts WHERE post_modified_gmt < NOW() ORDER BY post_modified_gmt DESC LIMIT 1';
		$results = $wpdb->get_results($query, OBJECT_K);
		if ($results) {
			$timestamps[key($results)] = array(
				'type' => __("content edited", Base::$textDomain),
				'link' => admin_url('post.php?action=edit&post=' . current($results)->ID)
			);
		}

		// Latest comment modification
		$query = 'SELECT comment_date_gmt, comment_ID FROM ' . Base::$dbPrefix . 'comments WHERE comment_date_gmt < NOW() ORDER BY comment_date_gmt DESC LIMIT 1';
		$results = $wpdb->get_results($query, OBJECT_K);
		if ($results) {
			$timestamps[key($results)] = array(
				'type' => __("comment submitted", Base::$textDomain),
				'link' => admin_url('comment.php?action=editcomment&c=' . current($results)->comment_ID)
			);
		}

		// Latest user registration
		$userstable = Base::$dbBasePrefix . 'users';
		$usermetatable = Base::$dbBasePrefix . 'usermeta';
		$query = $wpdb->prepare("SELECT {$userstable}.user_registered, {$userstable}.ID FROM {$userstable} INNER JOIN {$usermetatable} ON ({$userstable}.ID = {$usermetatable}.user_id) WHERE {$usermetatable}.meta_key = %s ORDER BY {$userstable}.user_registered DESC", Base::$dbPrefix . 'capabilities');
		$results = $wpdb->get_results($query, OBJECT_K);
		if ($results) {
			$timestamps[key($results)] = array(
				'type' => __("user created", Base::$textDomain),
				'link' => admin_url('user-edit.php?user_id=' . current($results)->ID)
			);
		}

		// Sort them so we have the newest
		krsort($timestamps);

		?><a class="fd-icon fd-util-inline" title="<?php esc_attr_e("You can change your site's favicon in Customizer", Base::$textDomain); ?>" href="<?php echo esc_html(admin_url('customize.php?autofocus[control]=site_icon')); ?>"><?php
			if (has_site_icon()) {
				?><img class="fd-util-block" src="<?php echo esc_attr(get_site_icon_url(48)); ?>" width="48" height="48"><?php
			} else {
				?><span class="fd-homeicon"></span><?php
			}
		?></a>
		<div class="fd-util-inline">
			<strong><?php echo esc_html(get_bloginfo('name')); ?></strong><?php
			if ($timestamps && Base::$isEditor) {
				$latest = current($timestamps);
				echo '<br>' . esc_html__("last activity", Base::$textDomain) . ': <a href="' . esc_html($latest['link']) . '">' . esc_html($latest['type']) . '</a> ' . esc_html(human_time_diff(strtotime(key($timestamps)))) . ' ' . esc_html__("ago", Base::$textDomain);;
			}
		?></div><?php
	}
}
Site::init();
