<?php

namespace TotalTheme\Admin;

defined( 'ABSPATH' ) || exit;

final class Site_Health {

	/**
	 * Static-only class.
	 */
	private function __construct() {}

	/**
	 * Init.
	 */
	public static function init() {
		add_filter( 'site_status_tests', [ self::class, '__register_tests' ] );
	}

	/**
	 * Register tests.
	 */
	public static function __register_tests( $tests ) {
		if ( ! isset( $tests['direct'] ) ) {
			return $tests;
		}

		// Theme Updates Test
		$tests['direct']['total-theme-license-registered'] = [
			'test' => [ self::class, '_license_test' ],
		];

		// Single theme installed check
		$tests['direct']['total-single-theme-installed'] = [
			'test' => [ self::class, '_single_theme_installed_check' ],
		];

		// No Fonts added in the Font Manager
		$tests['direct']['total-font-manager'] = [
			'test' => [ self::class, '_font_manager_test' ],
		];

		// Superfish Menu Test
		$tests['direct']['total-header-menu-dropdown-superfish'] = [
			'test' => [ self::class, '_dropdown_superfish_test' ],
		];

		// jQuery Easing Test
		$tests['direct']['total-jquery-easing'] = [
			'test' => [ self::class, '_jquery_easing_test' ],
		];

		return $tests;
	}

	/**
	 * Checks if theme updates are enabled and working.
	 */
	public static function _license_test() {
		if ( ! totaltheme_is_license_active() ) {
			return [
				'test'        => 'total-theme-license-registered',
				'label'       => esc_html__( 'Theme license not registered', 'total' ),
				'status'      => 'critical',
				'description' => esc_html__( 'Registering a valid theme license is required for each site the theme is used on.', 'total' ),
				'actions'     => '<p><a href="' . esc_url( admin_url( 'admin.php?page=wpex-panel-theme-license' ) ) . '" target="_blank">' . esc_html( 'Register your theme license', 'total' )  . '</a></p>',
				'badge'       => [
					'color' => 'red',
					'label' => sprintf(
						esc_html_x( '%1$s Theme', '*theme name* Theme', 'total' ),
						'Total'
					),
				],
			];
		}
	}

	/**
	 * Checks if there is only a single theme installed which prevents update checks.
	 */
	public static function _single_theme_installed_check() {
		if ( 1 === count( (array) wp_get_themes() ) && totaltheme_is_license_active() ) {
			return [
				'test'        => 'total-single-theme-installed',
				'label'       => esc_html__( 'Backup theme required for updates', 'total' ),
				'status'      => 'critical',
				'description' => esc_html__( 'It appears you only have one theme installed. In order for theme updates to display in your dashboard you must have at least one of the default WP themes active to serve as a backup incase an update fails. This is a WordPress requirement.', 'total' ),
				'actions'     => '<p><a href="' . esc_url( admin_url( 'themes.php' ) ) . '" target="_blank">' . esc_html( 'Manage your themes.', 'total' )  . '</a></p>',
				'badge'       => [
					'color' => 'red',
					'label' => sprintf(
						esc_html_x( '%1$s Theme', '*theme name* Theme', 'total' ),
						'Total'
					),
				],
			];
		}
	}

	/**
	 * Checks if there are fonts registered in the font manager.
	 */
	public static function _font_manager_test() {
		if ( ! wpex_has_registered_fonts() ) {
			return [
				'test'        => 'total-font-manager',
				'label'       => esc_html__( 'No fonts registered', 'total' ),
				'status'      => 'recommended',
				'description' => esc_html__( 'You haven\'t registered any fonts in the Font Manager. Registering your site\'s fonts streamlines the font family dropdowns in the Customizer and builder, improving backend performance and ensuring consistent typography across your site.', 'total' ),
				'actions'     => '<p><a href="' . esc_url( admin_url( 'edit.php?post_type=wpex_font' ) ) . '" target="_blank">' . esc_html( 'Register your fonts', 'total' )  . '</a></p>',
				'badge'       => [
					'color' => 'blue',
					'label' => sprintf(
						esc_html_x( '%1$s Theme', '*theme name* Theme', 'total' ),
						'Total'
					),
				],
			];
		}
	}

	/**
	 * Checks if the site is using the outdated superfish JS script.
	 */
	public static function _dropdown_superfish_test() {
		if ( 'sfhover' === get_theme_mod( 'menu_dropdown_method' ) ) {
			return [
				'test'        => 'total-header-menu-dropdown-superfish',
				'label'       => esc_html__( 'Header menu is using an older dropdown method', 'total' ),
				'status'      => 'recommended',
				'description' => esc_html__( 'Switching to the newer dropdown methods ("CSS Hover" or "On Click") will eliminate the need for SuperFish scripts and jQuery, improving page performance.', 'total' ),
				'actions'     => '<p><a href="' . esc_url( admin_url( '/customize.php?autofocus[section]=wpex_header_menu_dropdowns' ) ) . '" target="_blank">' . esc_html( 'Visit the customizer to update your settings', 'total' )  . '</a></p>',
				'badge'       => [
					'color' => 'blue',
					'label' => sprintf(
						esc_html_x( '%1$s Theme', '*theme name* Theme', 'total' ),
						'Total'
					),
				],
			];
		}
	}

	/**
	 * Checks if the site is using jQuery Easing.
	 */
	public static function _jquery_easing_test() {
		if ( get_theme_mod( 'scroll_to_easing', false ) ) {
			return [
				'test'        => 'total-jquery-easing',
				'label'       => esc_html__( 'jQuery easing is enabled for local scroll links', 'total' ),
				'status'      => 'recommended',
				'description' => esc_html__( 'jQuery Easing is an optional feature added to the theme for users who prefer the older scrolling style for links that jump to sections on the same page. On modern sites, the browser handles smooth scrolling natively, so this feature is usually not needed. We recommend disabling this feature to prevent extra scripts from loading and improve site performance.', 'total' ),
				'actions'     => '<p><a href="' . esc_url( admin_url( '/customize.php?autofocus[section]=wpex_local_scroll' ) ) . '" target="_blank">' . esc_html( 'Visit the customizer to update your settings', 'total' )  . '</a></p>',
				'badge'       => [
					'color' => 'blue',
					'label' => sprintf(
						esc_html_x( '%1$s Theme', '*theme name* Theme', 'total' ),
						'Total'
					),
				],
			];
		}
	}

}
