<?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 HappyFenceNGEDPDSEC extends HappyFenceNGAEndpoint {
	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function init() {
		add_action( 'wp_ajax_happyfenceng_DSEC_save_settings', array( $this, 'doCallback' ) );
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function doCallback() {
		try {
			$burn = parent::checkBurn( 'happyfenceng_DSEC_save_settings' );
			if ( HappyFenceNGPROC::not_equals( $burn, HappyFenceNGPlaceholder::KO ) ) {
				if ( check_ajax_referer( 'happyfenceng_DSEC_save_settings', 'security' ) ) {
					if ( isset( $_POST['delete_tables_on_uninstall'] ) ) {
						HappyFenceNGWhoAmI::get_instance()->PCORE->delete_tables_on_uninstall = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['delete_tables_on_uninstall'] ) ) );
					} else {
						HappyFenceNGWhoAmI::get_instance()->PCORE->delete_tables_on_uninstall = false;
					}
					parent::logChange( 'Delete tables on uninstall', HappyFenceNGWhoAmI::get_instance()->PCORE->delete_tables_on_uninstall );

					parent::audit( 'Data Security options changed successfully' );
					parent::answerOk( $burn, esc_html__( 'Saved successfully', 'happyfenceng-total-enterprise-security' ) );
				}
			}
			HappyFenceNGAlog::get_instance()->log(
				static::class . ' ->Unauthorized call to endpoint, invalid nonce or burn token. (BLOCKED) ',
				HappyFenceNGSyslogFacility::SECURITY,
				HappyFenceNGSyslogSeverity::WARNING
			);
			parent::answerKo( esc_html__( 'Not Authorized', 'happyfenceng-total-enterprise-security' ) );
		} catch ( Throwable $t ) {
			HappyFenceNGLOGCORE::logcore(
				static::class . $t->getMessage(),
				HappyFenceNGSyslogFacility::DAEMON,
				HappyFenceNGSyslogSeverity::ERROR
			);
			parent::answerKo( esc_html__( 'Denied', 'happyfenceng-total-enterprise-security' ) );
		}
	}
}
