<?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 HappyFenceNGEDPFUN extends HappyFenceNGAEndpoint {
	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function init() {
		add_action( 'wp_ajax_happyfenceng_FUN_save_settings', array( $this, 'doCallback' ) );
	}
	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function doCallback() {
		try {
			$burn = parent::checkBurn( 'happyfenceng_FUN_save_settings' );
			if ( HappyFenceNGPROC::not_equals( $burn, HappyFenceNGPlaceholder::KO ) ) {
				if ( check_ajax_referer( 'happyfenceng_FUN_save_settings', 'security' ) ) {

					HappyFenceNGWhoAmI::get_instance()->XMLRPC->block_xmlrpc = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['block_xmlrpc'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
					parent::logChange( 'Block xmlrpc', HappyFenceNGWhoAmI::get_instance()->XMLRPC->block_xmlrpc );

					HappyFenceNGWhoAmI::get_instance()->META->remove_meta_generator = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['remove_meta_generator'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
					parent::logChange( 'Remove meta generator', HappyFenceNGWhoAmI::get_instance()->META->remove_meta_generator );

					HappyFenceNGWhoAmI::get_instance()->EMBED->remove_embed_script = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['remove_embed_script'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
					parent::logChange( 'Remove embed script', HappyFenceNGWhoAmI::get_instance()->EMBED->remove_embed_script );

					HappyFenceNGWhoAmI::get_instance()->PCORE->enable_custom_login = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['enable_custom_login'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
					parent::logChange( 'Enable custom login', HappyFenceNGWhoAmI::get_instance()->PCORE->enable_custom_login );

					if ( HappyFenceNGWhoAmI::get_instance()->PCORE->enable_custom_login ) {
						$tmp_slug = HappyFenceNGCleaner::must_be_slug( sanitize_text_field( wp_unslash( $_POST['custom_login_slug'] ?? HappyFenceNGPlaceholder::NOT_A_VALID_SLUG ) ) );
						if ( HappyFenceNGPROC::equals( $tmp_slug, HappyFenceNGPlaceholder::NOT_A_VALID_SLUG ) ) {
							parent::answerKo( esc_html__( 'Invalid slug specified', 'happyfenceng-total-enterprise-security' ) );
							exit;
						}
						HappyFenceNGWhoAmI::get_instance()->PCORE->custom_login_slug = str_starts_with( $tmp_slug, '/' ) ? $tmp_slug : '/' . $tmp_slug;
					}

					if ( HappyFenceNGCleaner::must_be_bool(
						sanitize_text_field( wp_unslash( $_POST['generate_random_slug'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) )
					) ) {

						$new_slug = HappyFenceNGUtils::generateStrongSlug( HappyFenceNGPcoreProps::_SLUG_LEN );
						HappyFenceNGWhoAmI::get_instance()->PCORE->custom_login_slug = '/' . sanitize_title( $new_slug );
					}

					HappyFenceNGWhoAmI::get_instance()->PCORE->disable_rss_feed = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['disable_rss_feed'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
					parent::logChange( 'Disable RSS feed', HappyFenceNGWhoAmI::get_instance()->PCORE->disable_rss_feed );
					HappyFenceNGWhoAmI::get_instance()->PCORE->disable_app_passwords = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['disable_app_passwords'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
					parent::logChange( 'Disable App Password', HappyFenceNGWhoAmI::get_instance()->PCORE->disable_app_passwords );

					parent::audit( 'Basic Security Shields 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' ) );
		}
	}
}
