<?php

/**
 * Plugin Name: HappyFenceNG Total Enterprise Security
 * Plugin URI: https://happywpress.com
 * Description: Next-generation enterprise security for WordPress with advanced threat protection.
 * Version: 11.8.6
 * Build: 1755
 * Release Name: Slim Lighter
 * Author: RazorVue di Scillitani Simonluca
 * Author URI: https://razorvue.it
 * Trademark: As stated in the HappyWpress site ... HappyWpress is a Razorvue trademark.
 * Text Domain: happyfenceng-total-enterprise-security
 * Domain Path: /languages
 * License: GPLv3
 * Requires at least: 5.0
 * Tested up to: 6.8
 * Requires PHP: 8.1
 * @set all
 * 
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
global $wpdb;
define( 'HAPPYFENCENG_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'HAPPYFENCENG_PATH', plugin_dir_path( __FILE__ ) );
define( 'HAPPYFENCENG_URL', plugin_dir_url( __FILE__ ) );
$plugin_data = get_file_data( __FILE__, [
    'Version'     => 'Version',
    'Build'       => 'Build',
    'ReleaseName' => 'Release Name',
] );
define( 'HAPPYFENCENG_VERSION', $plugin_data['Version'] ?? '0.0.0.0' );
define( 'HAPPYFENCENG_BUILD', $plugin_data['Build'] ?? '0' );
define( 'HAPPYFENCENG_RELEASE_NAME', $plugin_data['ReleaseName'] ?? 'No release' );
define( 'HAPPYFENCENG_DB', $wpdb );
define( 'HAPPYFENCENG_DB_PREFIX', HAPPYFENCENG_DB->prefix . 'happyfenceng_' );
require_once __DIR__ . '/vendor/autoload.php';
if ( function_exists( 'htes_fs' ) ) {
    htes_fs()->set_basename( false, __FILE__ );
} else {
    if ( !function_exists( 'htes_fs' ) ) {
        function htes_fs() {
            global $htes_fs;
            if ( !isset( $htes_fs ) ) {
                require_once dirname( __FILE__ ) . '/vendor/freemius/wordpress-sdk/start.php';
                $htes_fs = fs_dynamic_init( array(
                    'id'               => '19537',
                    'slug'             => HappyFenceNGStStored::MAIN_SLUG,
                    'premium_slug'     => HappyFenceNGStStored::MAIN_SLUG_PREMIUM,
                    'type'             => 'plugin',
                    'public_key'       => 'pk_c17621af3f5d42c62ac751b30b86c',
                    'is_premium'       => false,
                    'premium_suffix'   => HappyFenceNGStStored::PREMIUM_SUFFIX,
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'trial'            => array(
                        'days'               => 7,
                        'is_require_payment' => true,
                    ),
                    'menu'             => array(
                        'slug'       => HappyFenceNGStStored::MAIN_SLUG,
                        'first-path' => HappyFenceNGStStored::MAIN_FIRST_PATH,
                        'contact'    => false,
                        'support'    => false,
                    ),
                    'is_live'          => true,
                ) );
            }
            return $htes_fs;
        }

    }
    htes_fs();
    do_action( 'htes_fs_loaded' );
}
/**PHPCS -> code compliance: we put useful comments where needed.. */
function htes_fs_uninstall_cleanup() {
    HappyFenceNGLOGCORE::logcore( "Requested HappyFenceNG plugin uninstall ", HappyFenceNGSyslogFacility::SYSTEM, HappyFenceNGSyslogSeverity::WARNING );
    delete_option( 'happyfenceng_already_initialized' );
    if ( HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PCORE->delete_tables_on_uninstall, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ) ) {
        HappyFenceNGLOGCORE::logcore( "Uninstall tables marked true .. now removing HappyFenceNG tables!", HappyFenceNGSyslogFacility::SYSTEM, HappyFenceNGSyslogSeverity::WARNING );
        HappyFenceNGBStore::uninstall();
        HappyFenceNGCStore::uninstall();
        delete_option( 'happyfenceng_options' );
        delete_option( 'happyfenceng_purge_on_uninstall' );
        delete_transient( 'happyfenceng_last_error' );
        wp_clear_scheduled_hook( 'happyfenceng_daily_scan' );
    }
}

register_uninstall_hook( __FILE__, 'htes_fs_uninstall_cleanup' );
if ( function_exists( 'htes_fs' ) ) {
    htes_fs()->add_action( 'after_uninstall', 'htes_fs_uninstall_cleanup' );
}
/**PHPCS -> code compliance: we put useful comments where needed.. */
function happyfenceng_activate() {
    try {
        if ( !get_option( 'happyfenceng_options' ) ) {
            update_option( 'happyfenceng_options', HappyFenceNGPlaceholder::EMPTY_ARRAY );
        }
        HappyFenceNGCStoreBuilder::checkAndBuild();
        flush_rewrite_rules();
    } catch ( Throwable $e ) {
        HappyFenceNGLOGCORE::logcore( " HappyFenceNG Activation Error" . $e->getMessage(), HappyFenceNGSyslogFacility::INIT, HappyFenceNGSyslogSeverity::ERROR );
        set_transient( 'happyfenceng_last_error', 'Activation failed: ' . $e->getMessage(), 60 );
        HappyFenceNGCleaner::fatal( esc_html__( 'Please check your database configuration', 'happyfenceng-total-enterprise-security' ), esc_html__( 'Unable to activate HappyFenceNG: an error occurred while creating the required database tables. Please check your database permissions.', 'happyfenceng-total-enterprise-security' ) . e->getMessage(), esc_html__( 'This incident is logged under uploads/happyfence-logs directory, if you need help, please send us the log file.', 'happyfenceng-total-enterprise-security' ) );
    }
}

/**PHPCS -> code compliance: we put useful comments where needed.. */
register_activation_hook( __FILE__, 'happyfenceng_activate' );
/**PHPCS -> code compliance: we put useful comments where needed.. */
function happyfenceng_deactivate() {
    wp_clear_scheduled_hook( 'happyfenceng_daily_scan' );
    flush_rewrite_rules();
}

/**PHPCS -> code compliance: we put useful comments where needed.. */
register_deactivation_hook( __FILE__, 'happyfenceng_deactivate' );
/**PHPCS -> code compliance: we put useful comments where needed.. */
add_action( 'admin_init', 'happyfenceng_admin_init' );
/**PHPCS -> code compliance: we put useful comments where needed.. */
function happyfenceng_admin_init() {
    register_setting( 'happyfenceng_logs_settings', 'happyfenceng_log_type', [
        'sanitize_callback' => 'sanitize_text_field',
    ] );
    register_setting( 'happyfenceng_logs_settings', 'happyfenceng_siem_endpoint', [
        'sanitize_callback' => ['HappyFenceNGAlog', 'sanitize_siem_endpoint'],
    ] );
    register_setting( 'happyfenceng_options_group', 'happyfenceng_options', [
        'sanitize_callback' => function ( $input ) {
            if ( is_array( $input ) ) {
                return array_map( 'sanitize_text_field', $input );
            }
            return sanitize_text_field( $input );
        },
    ] );
}

/**PHPCS -> code compliance: we put useful comments where needed.. */
require_once dirname( __FILE__ ) . '/happyfenceng-bootstrap.php';