<?php
/**
 * Pro Admin features.
 *
 * Adds pro admin features like live refreshing data.
 *
 * @since 6.0.0
 *
 * @package MonsterInsights Dimensions
 * @subpackage Reports
 * @author  Chris Christoff
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class MonsterInsights_Admin_Pro_Reports {

	/**
	 * Primary class constructor.
	 *
	 * @access public
	 * @since 6.0.0
	 */
	public function __construct() {
		add_action( 'admin_print_scripts', array( $this, 'report_scripts' ) );
		add_action( 'wp_ajax_monsterinsights_refresh_data', array( $this, 'refresh_data' ) );
		add_action( 'monsterinsights_dashboard_actions', array( $this, 'dashboard_refresh_button' ) );
		add_action( 'monsterinsights_tab_reports_actions', array( $this, 'dashboard_refresh_button' ) );
		add_action( 'monsterinsights_tab_reports_notices', array( $this, 'notice_refresh_dashboard' ) );
	}

	/**
	 * Enqueue premium scripts
	 */
	public function report_scripts() {
		if ( ( filter_input( INPUT_GET, 'page' ) === 'monsterinsights_reports' || filter_input( INPUT_GET, 'page' ) === 'monsterinsights_dashboard' ) && current_user_can( 'manage_options' ) ) {

			wp_enqueue_script( 'monsterinsights_pro_reports', MONSTERINSIGHTS_PLUGIN_URL . 'pro/assets/js/dashboard.js' );

			wp_localize_script( 'monsterinsights_pro_reports', 'monsterinsights_pro_reports', array(
				'monsterinsights_loading'      => esc_html__( 'Loading', 'ga-premium' ),
				'monsterinsights_reports_nonce' => wp_create_nonce( 'monsterinsights_reports_ajax_nonce' ),
			) );
		}
	}

	/**
	 * Ajax call: Fetch new data from Google Analytics
	 */
	public function refresh_data() {
		check_ajax_referer( 'monsterinsights_reports_ajax_nonce', 'security' );
		if ( get_transient( 'monsterinsights-pro-ajax-refresh' ) !== true ) {
			$base = MonsterInsights();

			// Destroy the data
			$base->reports->delete_aggregate_data();
			
			$base->reports->run_cron();

			set_transient( 'monsterinsights-pro-ajax-refresh', true, ( MINUTE_IN_SECONDS * 5 ) );
			set_transient( 'monsterinsights-pro-ajax-refresh-message', true, ( MINUTE_IN_SECONDS * 5 ) );
		}

		die( 'done' );
	}

	/**
	 * Show the dashboard refresh button for premium users
	 */
	public function dashboard_refresh_button() {
		echo '<a class="button" id="monsterinsights_refresh" style="padding-top: 5px; float: right;" alt="' . esc_attr__( 'Click to fetch the latest data from Google Analytics.', 'ga-premium' ) . '" title="' . esc_attr__( 'Click to fetch the latest data from Google Analytics.', 'ga-premium' ) . '"><span class="dashicons dashicons-update"></span></a>';
	}

	/**
	 * Check if we need to show an admin message when we refreshed the dashboar data
	 */
	public function notice_refresh_dashboard() {
		if ( get_transient( 'monsterinsights-pro-ajax-refresh-message' ) !== false ) {
			$description = esc_html__( 'The Google Analytics data was updated successfully.', 'ga-premium' );
			echo monsterinsights_get_message('success', $description );
			delete_transient( 'monsterinsights-pro-ajax-refresh-message' );
		}
	}

}
new MonsterInsights_Admin_Pro_Reports();