<?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.. */
abstract class HappyFenceNGSystem extends HappyFenceNGRuntimeExceptions {
    /**PHPCS -> code compliance: we put useful comments where needed..
     *
     * @var array $instances .
     */
    private static array $instances = HappyFenceNGPlaceholder::EMPTY_ARRAY;

    protected const HF_ALLOWED_PREFIXES = array(
        HappyFenceNGStStored::MAIN_SLUG,
        'HappyFence',
        'HappyFenceNG',
        'HappyPrivacy',
        'happyprivacy',
        'wpdb'
    );

    protected const HF_ALLOWED_HOOKS = array();

    /**PHPCS -> code compliance: we put useful comments where needed.. */
    public static function get_instance() : ?static {
        try {
            $cls = static::class;
            if ( isset( self::$instances[$cls] ) ) {
                self::$instances[$cls]::__constructor( $cls );
                return self::$instances[$cls];
            } else {
                self::$instances[$cls] = new $cls();
                self::$instances[$cls]::__constructor( $cls );
            }
            return self::$instances[$cls];
        } catch ( \Throwable $e ) {
            if ( is_admin() ) {
                set_transient( 'happyfenceng_last_error', "FATAL::[HappyFenceNGSystem][{$cls}] " . $e->getMessage(), 14400 );
            }
            // lasciare null.
            return null;
        }
    }

    /**PHPCS -> code compliance: we put useful comments where needed..
     *
     * @param string $c .
     * @param array  $p .
     * @param string $s .
     */
    protected function exc( $c, array $p, $s ) {
        if ( !$this->regs( $c, $p ) ) {
            HappyFenceNGLOGCORE::logcore( HappyFenceNGSym::err( $s, static::class ), HappyFenceNGSyslogFacility::MCONTROL, HappyFenceNGSyslogSeverity::ERROR );
            return false;
        }
        return true;
    }

    /**PHPCS -> code compliance: we put useful comments where needed..
     *
     * @param string $sr .
     * @param array  $dt .
     */
    public function regs( string $sr, array $dt ) : bool {
        try {
            if ( !$sr ) {
                self::raiseException( 'SEVERE::nothing to call.' );
            }
            if ( !is_string( $sr ) || !class_exists( $sr ) ) {
                self::raiseException( "NOT-FOUND::{$sr}" );
            }
            $obj = $sr::get_instance();
            if ( $obj ) {
                if ( empty( $dt[0] ) ) {
                    $obj->init();
                } else {
                    $obj->{$dt[0]}( HappyFenceNGPlaceholder::EMPTY_STRING, HappyFenceNGPlaceholder::EMPTY_ARRAY );
                }
            } else {
                self::raiseException( "Invalid object for {$sr} cannot proceed" );
            }
            return true;
        } catch ( \Throwable $e ) {
            HappyFenceNGLOGCORE::logcore( sprintf(
                'This should never happen: %s in %s on line %d. Referer: [%s]',
                $e->getMessage(),
                $e->getFile(),
                (int) $e->getLine(),
                sanitize_text_field( wp_unslash( $_SERVER['HTTP_REFERER'] ?? HappyFenceNGPlaceholder::UNKNOWN ) )
            ), HappyFenceNGSyslogFacility::SYSTEM, HappyFenceNGSyslogSeverity::ALERT );
            return false;
        }
    }

}
