<?php
/**
 * Dashboard class for Maintainn tools plugin.
 *
 * @version 2.0.0
 * @package Maintainn Tools
 */

/**
 * Class that holds functonality for the displaying the dashboard.
 */
class Maintainn_Tools_Dashboard {

	/**
	 * Maintainn_Tools obj
	 *
	 * @var Maintainn_Tools
	 */
	protected $plugin;

	/**
	 * Tabs to display.
	 *
	 * @var array
	 */
	protected $tabs = array();

	/**
	 * Page URL.
	 *
	 * @var string
	 */
	public $page_url = '';

	/**
	 * Option key, and option page slug
	 *
	 * @var string
	 * @since  0.1.0
	 */
	protected $key = 'maintainn-dashboard-widget';

	/**
	 * Options Page title
	 *
	 * @var string
	 * @since  0.1.0
	 */
	protected $title = '';

	/**
	 * The hook suffix for the options page.
	 *
	 * @var string
	 * @since 2.0.x
	 */
	protected $options_page = '';

	/**
	 * Keep track of current tab.
	 *
	 * @var string
	 * @since 2.0.x
	 */
	protected $current_tab = '';

	/**
	 * Maintainn_Tools_Dashboard constructor.
	 *
	 * @param Maintainn_Tools $plugin Parent plugin.
	 * @return void
	 */
	public function __construct( $plugin ) {
		$this->plugin = $plugin;

		// Set plugin title.
		$this->title = __( 'Maintainn', 'maintainn-tools' );
		$this->logo = "<img src='" . esc_url( plugins_url( 'assets/img/Maintainn-logo.png', __DIR__ ) ) . "' alt='Maintainn' height='18px' width='116px' />";

		add_action( 'plugins_loaded', array( $this, 'setup_globals' ) );
		$this->setup_actions();

		// Get the current tab.
		$this->current_tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'about'; // @codingStandardsIgnoreLine
	}

	/**
	 * Set up our class global objects.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function setup_globals() {

		$this->tabs = apply_filters( 'maintainn_tabs',
			array(
				'about'        => __( 'About', 'maintainn-tools' ),
				'site-scanner' => __( 'Site Scanner', 'maintainn-tools' ),
				'tools'        => __( 'Tools', 'maintainn-tools' ),
				'support'      => __( 'Support', 'maintainn-tools' ),
			)
		);

		$this->page_url = admin_url( 'admin.php?page=maintainn' );

	}

	/**
	 * Set up our actions.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	private function setup_actions() {
		add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
		add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) );

		// Add network admin menu page if this is multisite.
		add_action( 'network_admin_menu', array( $this, 'add_network_options_page' ) );
	}

	/**
	 * Add network options page
	 *
	 * @since  0.1.0
	 *
	 * @return  void
	 */
	public function add_network_options_page() {
		$this->options_page = add_menu_page(
			$this->title,
			$this->title,
			'manage_options',
			$this->key,
			array( $this, 'render_page' )
		);

		// Include CMB CSS in the head to avoid FOUC.
		add_action( "admin_print_styles-{$this->options_page}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) );
	}

	/**
	 * Add Dashboard page.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function add_admin_menu() {
		 add_menu_page( $this->title, $this->title, 'manage_options', 'maintainn', array( $this, 'render_page' ) );
	}

	/**
	 * Register Dashboard widget.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function add_dashboard_widget() {
		add_meta_box( $this->key, $this->logo, array( $this, 'dashboard_widget' ), 'dashboard', 'side', 'high' );
	}

	/**
	 * Render Dashboard widget.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function dashboard_widget() {
		$announcement = $this->plugin->helpers->fetch_latest_announcement();
		if ( false !== $announcement ) {
			?>
			<div class="maintainn-tools-announcement">
				<div class="announcement-date"><p class="month-day"><?php echo esc_html( $announcement->get_date( 'M j' ) ); ?></p><p class="year"><?php echo esc_html( $announcement->get_date( 'Y' ) ); ?></p></div>
				<div class="announcement-content"><h3> <strong><?php echo esc_html( $announcement->get_title() ); ?></strong></h3>
				<p>
					<?php echo wp_kses_post( $announcement->get_content() ); ?>
				</p></div>
			</div>
			<hr />
			<?php
		}
		?>
		<p>
			<?php esc_html_e( 'Maintainn Tools helps give peace of mind that your site is healthy and up to date. Important announcements from the team at Maintainn is displayed above. Use the buttons below to view your site information, scan your site, check out the tools, or get support.', 'maintainn-tools' ); ?>
		</p>
		<p>
			<?php
			printf(
				'<a href="%1$s" class="button">%2$s</a> ',
				esc_url( admin_url( 'site-health.php?tab=debug' ) ),
				esc_html__( 'Site health', 'maintainn-tools' )
			);
			foreach ( $this->tabs as $tab => $name ) {
				printf( '<a href="%1$s" class="button">%2$s</a> ',
					esc_url( add_query_arg( 'tab', $tab, $this->page_url ) ),
					esc_html( $name )
				);
			}
			do_action( 'maintainn_dashboard_widget_links' );
			?>
		</p>
		<?php

		// Use minified js if script debug isn't on.
		$suffix = $this->plugin->get_script_suffix();

		// Enqueue styles.
		wp_enqueue_style( 'maintainn-tools', $this->plugin->url( "assets/css/styles{$suffix}.css" ), array(), $this->plugin->version );

	}

	/**
	 * Render Maintainn Support page.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function render_page() {
		// Enqueue scripts and styles.
		$this->enqueue_scripts();

		$current_user = wp_get_current_user();
		?>
		<div class="wrap">
			<h1>
				<?php
				echo sprintf(
					// Translators: maintainn version number.
					esc_html__( 'Maintainn Tools %1$s', 'maintainn-tools' ),
					esc_attr( MAINTAINN_TOOLS_VERSION )
				); ?>
			</h1>

			<?php $this->option_tabs(); ?>

			<?php
			switch ( $this->current_tab ) {
				case 'about':
					$this->tab_about();
					break;
				case 'site-info':
					$this->tab_site_info();
					break;
				case 'site-scanner':
					$this->scan_my_site();
					break;
				case 'tools':
					$this->tab_tools();
					break;
				case 'support':
					$this->tab_support();
					break;
			}

			do_action( 'maintainn_tab_switch', $this->current_tab );
			?>
		</div>
		<?php
	}

	/**
	 * Render options.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function option_tabs() {
		echo '<h2 class="nav-tab-wrapper maintainn-tabs">';

		printf( '<a class="nav-tab" href="%s">%s</a>',
			esc_url( admin_url( 'site-health.php?tab=debug' ) ),
			esc_html__( 'Site health', 'maintainn-tools' )
		);
		foreach ( $this->tabs as $tab => $name ) {
			$active = ( $this->current_tab === $tab ) ? 'nav-tab-active' : '';
			printf( '<a class="nav-tab %s" href="%s">%s</a>',
				esc_attr( $active ),
				add_query_arg( 'tab', $tab, $this->page_url ),
				esc_html( $name )
			);
		}

		echo '</h2>';
	}

	/**
	 * Render about tab.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function tab_about() {
		?>
		<div class="wrap maintainn-audit-wrap">
			<?php echo $this->maintainn_about_page(); ?>
		</div>
		<?php
	}

	/**
	 * Render tools tab.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function tab_tools() {
		$this->plugin->extensions->render_tab();
	}

	/**
	 * Render support tab.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function tab_support() {
		?>
		<div class="wrap maintainn-audit-wrap">
			<?php echo $this->maintainn_support(); ?>
		</div>
		<?php
	}

	/**
	 * Render site info tab.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function tab_site_info() {
		?>

		<div class="wrap maintainn-audit-wrap">
			<?php echo $this->maintainn_site_info(); ?>
		</div>

		<?php
	}

	/**
	 * Render our Maintainn About Page.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	private function maintainn_about_page() {
		// Bail early if not the right permissions.
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		// Include browser class.
		if ( ! class_exists( 'Browser' ) ) {
			$this->plugin->include_vendor( 'browser' );
		}

		$this->plugin->include_view( 'about-page' );
	}

	/**
	 * Render our Maintainn site info.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	private function maintainn_site_info() {
		// Bail early if not the right permissions.
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		// Include browser class.
		if ( ! class_exists( 'Browser' ) ) {
			$this->plugin->include_vendor( 'browser' );
		}

		$this->plugin->include_view( 'site-information' );
	}

	/**
	 * Render our Maintainn support info.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	private function maintainn_support() {
		// Bail early if not the right permissions.
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		// Include browser class.
		if ( ! class_exists( 'Browser' ) ) {
			$this->plugin->include_vendor( 'browser' );
		}

		$this->plugin->include_view( 'support-info' );
	}

	/**
	 * Render our system status.
	 *
	 * @since 2.0.0
	 *
	 * @return void
	 */
	public function system_status() {
		// Bail early if not the right permissions.
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		// Include browser class.
		if ( ! class_exists( 'Browser' ) ) {
			$this->plugin->include_vendor( 'browser' );
		}

		$this->plugin->include_view( 'system-info' );
	}

	/**
	 * Scan my site tab
	 *
	 * @since 0.1.0
	 *
	 * @return void
	 */
	public function scan_my_site() {
		?>
		<p><?php esc_html_e( 'The "Site Scanner" tool compares the WordPress Core files on your website to the original files on wordpress.org and ensures that they have not been altered in any way.', 'maintainn-tools' ); ?></p>
		<a class="button button-primary" id="maintainn-scanner-check-core" href="javascript:void(0);"><?php esc_html_e( 'Check Core Files', 'maintainn-tools' ); ?></a>
		<div id="maintainn-scanner-log"></div>
		<?php
	}

	/**
	 * Enqueue admin scripts.
	 *
	 * @since 0.1.0
	 *
	 * @return void
	 */
	public function enqueue_scripts() {
		// Use minified js if script debug isn't on.
		$suffix = $this->plugin->get_script_suffix();

		// Enqueue styles.
		wp_enqueue_style( 'maintainn-tools', $this->plugin->url( "assets/css/styles{$suffix}.css" ), array(), $this->plugin->version );

		// Add Scanner JS if we're on the tab.
		if ( 'site-scanner' === $this->current_tab ) {
			wp_enqueue_script( 'maintainn-scanner-admin', $this->plugin->url( "assets/js/admin{$suffix}.js" ), array( 'jquery' ), $this->plugin->version() );

			$l10n = array(
				'checksum_nonce' => wp_create_nonce( 'verify_core_checksums' ),
				'checksums_error' => __( 'There was an error running checksums test.', 'maintainn-tools' ),
				'verifying' => __( 'Verifying', 'maintainn-tools' ),
			);

			wp_localize_script( 'maintainn-scanner-admin', 'maintainn_scanner_admin_config', $l10n );
		}
	}
}
