<?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;
}

if ( ! function_exists( 'wp_filesystem' ) ) {
	require_once ABSPATH . 'wp-admin/includes/file.php';
}
define( 'FS_METHOD', 'direct' );

WP_Filesystem();

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

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param array  $params ... .
	 * @param string $tag ... .
	 */
	public static function renderView( array $params, string $tag ) {
		global $wp_filesystem;
		parent::__constructor( static::class );
		$tag_name  = 'hfng-' . $tag;
		$data_name = 'hfng' . strtoupper( $tag ) . 'Data';

		wp_enqueue_style(
			'hfng-' . $tag . '-style',
			HAPPYFENCENG_PLUGIN_URL . 'assets/happyfenceng.css',
			array(),
			'1.0.0'
		);

		$twig_file = HAPPYFENCENG_PATH . 'includes/complex/core-views/views/' . $tag . '_view.twig';

		static $twig = null;
		if ( ! $twig ) {
			$loader = new \Twig\Loader\FilesystemLoader( HAPPYFENCENG_PATH . 'includes/complex/core-views/views' );
			$twig   = new \Twig\Environment(
				$loader,
				array(
					'cache'            => false,
					'debug'            => true,
					'strict_variables' => true,
				)
			);
		}

		$params['css']                                = $wp_filesystem->get_contents( HAPPYFENCENG_PATH . 'assets/happyfenceng.css' );
		$params['texts']['happyfenceng_version']      = esc_html__( 'Current Version:', 'happyfenceng-total-enterprise-security' ) . ' ' . esc_html( trim( HAPPYFENCENG_VERSION ) );
		$params['texts']['happyfenceng_build']        = esc_html__( 'Current Build:', 'happyfenceng-total-enterprise-security' ) . ' ' . esc_html( trim( HAPPYFENCENG_BUILD ) );
		$params['texts']['happyfenceng_release_name'] = esc_html__( 'Release Name:', 'happyfenceng-total-enterprise-security' ) . ' ' . esc_html( trim( HAPPYFENCENG_RELEASE_NAME ) );
		/**This is a passthrought one-shot burining token used by happyfenceng to limit the exposure due to too long time wp nonce
		 * This token has a "one shot" validity and after it is burned is not usable anymore.
		 * Additionaly the token itself is the hash of the data that are available in the database .. so when the token is received and valid
		 * the burning data are selected from the database and MUST MATCH with the current request ( e.g. IP/UA and other ).
		 * The burnNonce has also a "strict" expiration timing, so it does not last, as wp nonce, for too much time.
		 * In the same page different ajax request use different burning token to have a further in depth protection.
		 */
		$params['values']['burnNonce'] = HappyFenceNGFailFast::setup();
		/**Note for reviewers: this is a twig view renderer.
			 * ALL data passed to twig are AUTOATICALLY escaped by the framework so there is NO NEED
			 * to escape again.
			 * Same for values. They are AUTOMATICALLY escaped by twig and then converted into models by alpine.js .
			 * In addition each view escapes all the data sent to the renderer in its array assignemt e.g. just
			 * take a look at the $params above.
			 * Putting further escape here may result in broken rendering or malformed views and add
			 * NO ADDITIONAL SECURITY.
			 * That's why there is this ignore clause.
		*/
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Rendered via Twig (autoescaped)
		echo $twig->render( $tag . '_view.twig', $params );

			wp_enqueue_script(
				$tag_name,
				HAPPYFENCENG_PLUGIN_URL . 'assets/specs/' . $tag . '/happyfenceng-' . $tag . '.js',
				array(),
				'1.0.0',
				false
			);

			wp_localize_script(
				$tag_name,
				$data_name,
				array(
					'ajaxurl' => admin_url( 'admin-ajax.php' ),
					'values'  => $params['values'],
					'texts'   => $params['texts'],
					'links'   => $params['links'],
				)
			);

			wp_enqueue_script(
				'alpinejs',
				HAPPYFENCENG_PLUGIN_URL . 'assets/alpinejs.min.js',
				array( $tag_name ),
				'3.x.x',
				false
			);
	}
}
