<?php
namespace Fabrica\Dashboard;

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

require_once('base.php');

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

	// Register
	public static function register() {

		// Only proceed if user has relevant privileges, and if there's at least one comment in the database
		if (!Base::$isModerator || !Base::$hasComments) {
			return;
		}
		add_meta_box('fd_discussion_overview', __("Discussion Overview", Base::$textDomain), array(__CLASS__, 'display'), 'dashboard', 'side', 'high');
	}

	// Display
	public static function display() {

		global $wpdb;
		$counts = array();
		$baseQuery = 'SELECT COUNT(*) FROM ' . Base::$dbPrefix . 'comments WHERE comment_type = \'comment\'';

		// Total published comments
		$commentsCount = 0;
		$query = $baseQuery . ' AND comment_approved = \'1\'';
		$results = $wpdb->get_results($query, OBJECT_K);
		if ($results) {
			$commentsCount = key($results);
		}

		// Number of commenters
		$commentersCount = 0;
		$query = $baseQuery . ' AND comment_approved = \'1\' GROUP BY comment_author_email';
		$results = $wpdb->get_results($query, ARRAY_N);
		if ($results) {
			$commentersCount = count($results);
		}

		// Repeat commenters
		$repeatCommentersCount = 0;
		$query = $baseQuery . ' AND comment_approved = \'1\' GROUP BY comment_author_email HAVING COUNT(*) > 1';
		$results = $wpdb->get_results($query, ARRAY_N);
		if ($results) {
			$repeatCommentersCount = count($results);
		}

		if (Base::$lastLogin && Base::$currentLogin) {

			// Responses to your posts and comments – doesn't use the base query because of the JOIN
			$responsesBaseQuery = 'SELECT COUNT(*) FROM ' . Base::$dbPrefix . 'comments ';
			$responsesBaseQuery .= ' LEFT JOIN ' . Base::$dbPrefix . 'comments AS comment_parents ON (' . Base::$dbPrefix . 'comments.comment_ID = comment_parents.comment_parent)';
			$responsesBaseQuery .= ' LEFT JOIN ' . Base::$dbPrefix . 'posts ON (' . Base::$dbPrefix . 'comments.comment_post_ID = ' . Base::$dbPrefix . 'posts.ID)';
			$responsesBaseQuery .= ' WHERE ' . Base::$dbPrefix . 'comments.comment_type = \'comment\'';
			$responsesBaseQuery .= ' AND ' . Base::$dbPrefix . 'comments.comment_date_gmt > \'' . date('Y-m-d H:i:s', Base::$lastLogin) . '\' AND ' . Base::$dbPrefix . 'comments.comment_date_gmt < \'' . date('Y-m-d H:i:s', Base::$currentLogin) . '\'';
			$query = $wpdb->prepare($responsesBaseQuery . ' AND (comment_parents.user_ID = %s OR ' . Base::$dbPrefix . 'posts.post_author = %s)', Base::$currentUser->ID, Base::$currentUser->ID);
			$results = $wpdb->get_results($query, OBJECT_K);
			if ($results && key($results) > 0) {
				$counts[] = array(
					'label' => __("Responses to your posts / comments", Base::$textDomain),
					'number' => key($results)
				);
			}

			// Responses to other site authors
			$query = $wpdb->prepare($responsesBaseQuery . ' AND ((comment_parents.user_ID != %s AND comment_parents.user_ID IS NOT NULL)', Base::$currentUser->ID);
			$query .= $wpdb->prepare(' OR (' . Base::$dbPrefix . 'posts.post_author != %s AND ' . Base::$dbPrefix . 'posts.post_author IS NOT NULL))', Base::$currentUser->ID);
			$results = $wpdb->get_results($query, OBJECT_K);
			if ($results && key($results) > 0) {
				$counts[] = array(
					'label' => __("Responses to other authors", Base::$textDomain),
					'number' => key($results)
				);
			}

			// Approved comments since last login
			$query = $wpdb->prepare($baseQuery . ' AND comment_approved = \'1\' AND comment_date_gmt > %s', date('Y-m-d H:i:s', Base::$lastLogin));
			$results = $wpdb->get_results($query, OBJECT_K);
			if ($results && key($results) > 0) {
				$counts[] = array(
					'label' => __("Approved comments", Base::$textDomain),
					'number' => key($results),
					'trafficlight' => 'green'
				);
			}

			// Pending comments since last login
			$query = $wpdb->prepare($baseQuery . ' AND comment_approved = \'0\' AND comment_date_gmt > %s', date('Y-m-d H:i:s', Base::$lastLogin));
			$results = $wpdb->get_results($query, OBJECT_K);
			if ($results && key($results) > 0) {
				$counts[] = array(
					'label' => __("Comments pending approval", Base::$textDomain),
					'number' => key($results),
					'trafficlight' => 'orange'
				);
			}

			// Spam
			$query = $wpdb->prepare($baseQuery . ' AND comment_approved = \'spam\' AND comment_date_gmt > %s', date('Y-m-d H:i:s', Base::$lastLogin));
			$results = $wpdb->get_results($query, OBJECT_K);
			if ($results && key($results) > 0) {
				$counts[] = array(
					'label' => __("Spam comments", Base::$textDomain),
					'number' => key($results),
					'trafficlight' => 'red'
				);
			}

			// Trashed
			$query = $wpdb->prepare($baseQuery . ' AND comment_approved = \'trash\' AND comment_date_gmt > %s', date('Y-m-d H:i:s', Base::$lastLogin));
			$results = $wpdb->get_results($query, OBJECT_K);
			if ($results && key($results) > 0) {
				$counts[] = array(
					'label' => __("Trashed comments", Base::$textDomain),
					'number' => key($results)
				);
			}
		}

		// Render
		?><div class="fd-key-metrics">
			<div class="fd-key-metrics__metric">
				<div class="fd-key-metrics__head">
					<span class="fd-key-metrics__icon fd-util-grey dashicons dashicons-admin-comments"></span>
					<span class="fd-util-large fd-util-light"><?php echo esc_html($commentsCount); ?></span>
				</div>
				<span class="fd-util-medium"><?php esc_html_e("published comments", Base::$textDomain); ?></span>
			</div>
			<div class="fd-key-metrics__metric">
				<div class="fd-key-metrics__head">
					<span class="fd-key-metrics__icon fd-util-grey dashicons dashicons-groups"></span>
					<span class="fd-util-large fd-util-light"><?php echo esc_html($commentersCount); ?></span>
				</div>
				<span class="fd-util-medium"><?php esc_html_e("commenters", Base::$textDomain); ?></span>
			</div>
			<div class="fd-key-metrics__metric">
				<div class="fd-key-metrics__head">
					<span class="fd-key-metrics__icon fd-util-grey dashicons dashicons-megaphone"></span>
					<span class="fd-util-large fd-util-light"><?php echo esc_html($repeatCommentersCount); ?></span>
				</div>
				<span class="fd-util-medium"><?php esc_html_e("repeat commenters", Base::$textDomain); ?></span>
			</div>
		</div><?php
		if ($counts) {
			?><table class="wp-list-table widefat striped fd-util-normal-table fd-util-margin-top">
				<thead>
					<th class="fd-util-center" colspan="4"><?php esc_html_e("Since your previous login", Base::$textDomain); ?></th>
				</thead>
				<tbody><?php echo esc_html(Base::renderStatsTable($counts)); ?></tbody>
			</table><?php
		}
	}
}
DiscussionOverview::init();
