<?php
/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */

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

/**PHPCS -> code compliance: we put useful comments where needed.. */
class HappyFenceNGVRGEO extends HappyFenceNGAVRoute {

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function doRoute() {
		try {

			$nonce_save_settings = wp_create_nonce( 'happyfenceng_GEO_save_settings' );
			$burn                = HappyFenceNGFailFast::setup();

			$params = array(
				'texts'  => array(
					'title'                         => esc_html__( 'GeoIP Fence', 'happyfenceng-total-enterprise-security' ),
					'note_title'                    => esc_html__( 'IMPORTANT NOTE:', 'happyfenceng-total-enterprise-security' ),
					'note_line_1'                   => esc_html__( 'HappyFenceNG is a WordPress security plugin with enterprise-grade features.', 'happyfenceng-total-enterprise-security' ),
					'note_line_2'                   => esc_html__( 'Our mission is to help keep your WordPress site safe and secure.', 'happyfenceng-total-enterprise-security' ),
					'note_line_3'                   => esc_html__( 'Neither HappyWPress nor Razorvue collect, read, or store any user data.', 'happyfenceng-total-enterprise-security' ),
					'note_line_4'                   => esc_html__( 'When an endpoint is provided (e.g., SIEM integration or this GeoIP integration),', 'happyfenceng-total-enterprise-security' ),
					'note_line_5'                   => esc_html__( 'it refers exclusively to external software or services owned or licensed by the end user.', 'happyfenceng-total-enterprise-security' ),
					'note_line_6'                   => esc_html__( 'Razorvue and its brands have no technical or commercial connection with these endpoints.', 'happyfenceng-total-enterprise-security' ),
					'note_line_7'                   => esc_html__( 'No data is transmitted to any external service unless explicitly enabled by the user via the plugin settings, and only the strictly necessary data is transmitted.', 'happyfenceng-total-enterprise-security' ),
					'note_line_8'                   => esc_html__( 'All features and their purposes are clearly documented in the plugin menu.', 'happyfenceng-total-enterprise-security' ),
					'note_line_9'                   => esc_html__( 'If anything is unclear, we recommend not activating the option and contacting support for clarification.', 'happyfenceng-total-enterprise-security' ),
					'note_line_10'                  => esc_html__( 'There are no “call home”, “external callbacks”, hidden features, or obfuscated code — the plugin source is fully visible and auditable.', 'happyfenceng-total-enterprise-security' ),
					'note_line_11'                  => esc_html__( 'To avoid misunderstandings, features requiring external integrations (e.g., SIEM, GeoIP) are available only in premium editions.', 'happyfenceng-total-enterprise-security' ),
					'note_line_12'                  => esc_html__( 'These editions include direct premium support as outlined in the license agreement.', 'happyfenceng-total-enterprise-security' ),
					'note_line_13'                  => esc_html__( 'By enabling this option, you confirm that you have read and understood these terms.', 'happyfenceng-total-enterprise-security' ),
					'enable_geoip'                  => esc_html__( 'I have read and understood the terms above', 'happyfenceng-total-enterprise-security' ),
					'feature_requires'              => esc_html__( 'THIS FEATURE REQUIRES PRO OR BETTER', 'happyfenceng-total-enterprise-security' ),
					'save_settings'                 => esc_html__( 'Save Settings', 'happyfenceng-total-enterprise-security' ),
					'settings_saved'                => esc_html__( 'Settings Saved', 'happyfenceng-total-enterprise-security' ),
					'error_saving_settings'         => esc_html__( 'Error saving settings', 'happyfenceng-total-enterprise-security' ),
					'select_provider'               => esc_html__( 'GeoIP Provider', 'happyfenceng-total-enterprise-security' ),
					'api_key_title'                 => esc_html__( 'API Key', 'happyfenceng-total-enterprise-security' ),
					'api_key_description'           => esc_html__( 'Enter your API key for the selected GeoIP provider.', 'happyfenceng-total-enterprise-security' ),
					'api_key_tooltip'               => esc_html__( 'You can get your own private key on the site of the service you choose.', 'happyfenceng-total-enterprise-security' ),
					'blocked_countries_title'       => esc_html__( 'Blocked Countries', 'happyfenceng-total-enterprise-security' ),
					'blocked_countries_description' => esc_html__( 'Select one or more countries to block.', 'happyfenceng-total-enterprise-security' ),
				),
				'links'  => array(
					'happyfenceng_logo_url' => esc_url( HAPPYFENCENG_PLUGIN_URL . 'assets/images/happyfenceng-small.png' ),

				),
				'values' => array(
					'geoip_block_enable'      => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->GEOIP->geoip_block_enable, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'geoip_provider'          => esc_attr( HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->GEOIP->geoip_provider, HappyFenceNGPlaceholder::EMPTY_STRING ) ),
					'geoip_api_key'           => esc_attr( HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->GEOIP->geoip_api_key, HappyFenceNGPlaceholder::EMPTY_STRING ) ),
					'geoip_blocked_countries' => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->GEOIP->geoip_blocked_countries, HappyFenceNGPlaceholder::EMPTY_STRING ),
					'all_countries'           => HappyFenceNGStStored::COUNTRIES,
					'checkbox_on'             => HappyFenceNGPlaceholder::CHECKBOX_ON,
					'checkbox_off'            => HappyFenceNGPlaceholder::CHECKBOX_OFF,
					'nonce'                   => $nonce_save_settings,
					'burnNonce'               => $burn,
					'feature_enabled'         => ( function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array( HappyFenceNGTR::PREAMBLE ) ) ),
				),
			);

			ob_start();
			HappyFenceNGVControl::renderView( $params, 'geo' );
			// phpcs:ignore WordPress.Security.EscapeOutput
			echo ob_get_clean();

		} catch ( Throwable $e ) {
			HappyFenceNGLOGCORE::logcore(
				'HappyFenceNGVRGEO - cannot render UI ' . $e->getMessage(),
				HappyFenceNGSyslogFacility::UI,
				HappyFenceNGSyslogSeverity::ERROR
			);
			ob_end_clean();
			HappyFenceNGCleaner::cleaner_oops();
		}
	}
}
