<?php
/**
 * Site Health test: surfaces the last audit verdict where admins already look.
 *
 * @package Upload_Vault
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Uvgf_Site_Health
 */
class Uvgf_Site_Health {

	/**
	 * Wire.
	 */
	public static function init() {
		add_filter( 'site_status_tests', array( __CLASS__, 'register' ) );
	}

	/**
	 * Register the test.
	 *
	 * @param array $tests Tests.
	 * @return array
	 */
	public static function register( $tests ) {
		$tests['direct']['uvgf_uploads_exposure'] = array(
			'label' => __( 'Gravity Forms upload exposure', 'buildsbyluke-upload-vault-for-gravity-forms' ),
			'test'  => array( __CLASS__, 'run' ),
		);
		return $tests;
	}

	/**
	 * The test.
	 *
	 * @return array
	 */
	public static function run() {
		$audit   = get_option( 'uvgf_audit', array() );
		$verdict = isset( $audit['verdict'] ) ? (string) $audit['verdict'] : '';
		$labels  = Uvgf_Auditor::verdict_labels();
		$label   = isset( $labels[ $verdict ] ) ? $labels[ $verdict ] : __( 'No audit has run yet.', 'buildsbyluke-upload-vault-for-gravity-forms' );

		$status = 'recommended';
		if ( in_array( $verdict, array( 'protected', 'no_uploads' ), true ) ) {
			$status = 'good';
		} elseif ( 'exposed' === $verdict ) {
			$status = 'critical';
		}

		return array(
			'label'       => __( 'Gravity Forms upload exposure', 'buildsbyluke-upload-vault-for-gravity-forms' ),
			'status'      => $status,
			'badge'       => array(
				'label' => __( 'Security', 'buildsbyluke-upload-vault-for-gravity-forms' ),
				'color' => 'critical' === $status ? 'red' : 'blue',
			),
			'description' => '<p>' . esc_html( $label ) . '</p>',
			'actions'     => '<a href="' . esc_url( admin_url( 'admin.php?page=upload-vault-for-gravity-forms' ) ) . '">' . esc_html__( 'Open the Upload Vault audit', 'buildsbyluke-upload-vault-for-gravity-forms' ) . '</a>',
			'test'        => 'uvgf_uploads_exposure',
		);
	}
}
