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

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function doCallback() {
		try {
			$burn = parent::checkBurn( 'happyfenceng_PRI_save_settings' );
			if ( HappyFenceNGPROC::not_equals( $burn, HappyFenceNGPlaceholder::KO ) ) {

				$privacy_notice_warnings = (
													! (
														HappyFenceNGPROC::equals( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_policy_banner, true ) ) ||
														(
															( HappyFenceNGPROC::equals( isset( HappyFenceNGWhoAmI::get_instance()->PRIVACY->privacy_url ), true ) &&
															HappyFenceNGPROC::equals( empty( HappyFenceNGWhoAmI::get_instance()->PRIVACY->privacy_url ), false )
														)
													)
														);
				$terms_notice_warnings   = (
													! (
														HappyFenceNGPROC::equals( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_terms_banner, true ) ) ||
														(
															( HappyFenceNGPROC::equals( isset( HappyFenceNGWhoAmI::get_instance()->PRIVACY->terms_url ), true ) &&
															HappyFenceNGPROC::equals( empty( HappyFenceNGWhoAmI::get_instance()->PRIVACY->terms_url ), false )
														)
													)
														);

				$cookie_notice_warnings = (
													! (
														HappyFenceNGPROC::equals( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_cookie_banner, true ) ) ||
														(
															( HappyFenceNGPROC::equals( isset( HappyFenceNGWhoAmI::get_instance()->PRIVACY->cookie_url ), true ) &&
															HappyFenceNGPROC::equals( empty( HappyFenceNGWhoAmI::get_instance()->PRIVACY->cookie_url ), false )
														)
													)
														);

				$options = array(
					'privacy_notice_warnings' => $privacy_notice_warnings,
					'cookie_notice_warnings'  => $cookie_notice_warnings,
					'terms_notice_warnings'   => $terms_notice_warnings,
				);

				if ( check_ajax_referer( 'happyfenceng_PRI_save_settings', 'security' ) ) {
					HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_privacy_management = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['enable_privacy_management'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
					parent::logChange( 'Privacy management', HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_privacy_management );

					if ( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_privacy_management ) {
						// we must disable the rest guest protecion in order to allow the banner appear correctly .
						HappyFenceNGWhoAmI::get_instance()->REST->block_rest_guests = HappyFenceNGPlaceholder::CHECKBOX_OFF;

						if ( isset( $_POST['top_banner_text'] ) && ( ! empty( $_POST['top_banner_text'] ) ) ) {
							HappyFenceNGWhoAmI::get_instance()->PRIVACY->top_banner_text = HappyFenceNGCleaner::must_be_string( sanitize_text_field( wp_unslash( $_POST['top_banner_text'] ) ) );
						} else {
							parent::answerOk( $burn, esc_html__( 'Please enter a top banner text.', 'happyfenceng-total-enterprise-security' ), true, $options );
						}

						HappyFenceNGWhoAmI::get_instance()->PRIVACY->color      = HappyFenceNGCleaner::must_be_color( sanitize_text_field( wp_unslash( $_POST['color'] ?? HappyFenceNGPrivacyProps::_COLOR ) ) );
						HappyFenceNGWhoAmI::get_instance()->PRIVACY->text_color = HappyFenceNGCleaner::must_be_color( sanitize_text_field( wp_unslash( $_POST['text_color'] ?? HappyFenceNGPrivacyProps::_TEXT_COLOR ) ) );

						HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_cookie_banner = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['enable_cookie_banner'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
						parent::logChange( 'Cookie banner', HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_cookie_banner );

						if ( ! HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_cookie_banner, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ) ) {
							if ( isset( $_POST['cookie_url'] ) && ( ! empty( $_POST['cookie_url'] ) ) ) {
								$the_url = HappyFenceNGCleaner::must_be_url( esc_url_raw( wp_unslash( $_POST['cookie_url'] ?? '' ) ) );
								if ( HappyFenceNGPROC::equals( $the_url, HappyFenceNGPlaceholder::NOT_A_VALID_URL ) ) {
									parent::answerOk( $burn, esc_html__( 'Invalid cookie url specified', 'happyfenceng-total-enterprise-security' ), true, $options );
									exit;
								}
								HappyFenceNGWhoAmI::get_instance()->PRIVACY->cookie_url = $the_url;
								$cookie_notice_warnings                                 = false;
							} else {
								parent::answerOk( $burn, esc_html__( 'Fill the cookie url or enable the default cookie banner.', 'happyfenceng-total-enterprise-security' ), true, $options );
								exit;
							}
						} else {
							HappyFenceNGWhoAmI::get_instance()->PRIVACY->cookie_url = HappyFenceNGPlaceholder::EMPTY_STRING;
							$cookie_notice_warnings                                 = false;
						}

						HappyFenceNGWhoAmI::get_instance()->PRIVACY->show_manage_link = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['show_manage_link'] ?? HappyFenceNGPlaceholder::EMPTY_STRING ) ) );

						HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_policy_banner = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['enable_policy_banner'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
						parent::logChange( 'Policy banner', HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_policy_banner );

						if ( ! HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_policy_banner, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ) ) {
							if ( isset( $_POST['privacy_url'] ) && ( ! empty( $_POST['privacy_url'] ) ) ) {
								$the_url = HappyFenceNGCleaner::must_be_url( esc_url_raw( wp_unslash( $_POST['privacy_url'] ?? '' ) ) );
								if ( HappyFenceNGPROC::equals( $the_url, HappyFenceNGPlaceholder::NOT_A_VALID_URL ) ) {
									parent::answerOk( $burn, esc_html__( 'Invalid privacy url specified', 'happyfenceng-total-enterprise-security' ), true, $options );
									exit;
								}
								HappyFenceNGWhoAmI::get_instance()->PRIVACY->privacy_url = $the_url;
								$privacy_notice_warnings                                 = false;
							} else {
								parent::answerOk( $burn, esc_html__( 'Fill the privacy url or enable the default privacy banner.', 'happyfenceng-total-enterprise-security' ), true, $options );
								exit;
							}
						} else {
							HappyFenceNGWhoAmI::get_instance()->PRIVACY->privacy_url = HappyFenceNGPlaceholder::EMPTY_STRING;
							$privacy_notice_warnings                                 = false;
						}

						HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_terms_banner = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['enable_terms_banner'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
						parent::logChange( 'Terms banner', HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_terms_banner );

						if ( ! HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_terms_banner, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ) ) {
							if ( isset( $_POST['terms_url'] ) && ( ! empty( $_POST['terms_url'] ) ) ) {
								$the_url = HappyFenceNGCleaner::must_be_url( esc_url_raw( wp_unslash( $_POST['terms_url'] ?? '' ) ) );
								if ( HappyFenceNGPROC::equals( $the_url, HappyFenceNGPlaceholder::NOT_A_VALID_URL ) ) {
									parent::answerOk( $burn, esc_html__( 'Invalid terms url specified', 'happyfenceng-total-enterprise-security' ), true, $options );
									exit;
								}
								HappyFenceNGWhoAmI::get_instance()->PRIVACY->terms_url = $the_url;
								$terms_notice_warnings                                 = false;
							} else {
								parent::answerOk( $burn, esc_html__( 'Fill the terms url or enable the default terms banner.', 'happyfenceng-total-enterprise-security' ), true, $options );
								exit;
							}
						} else {
							HappyFenceNGWhoAmI::get_instance()->PRIVACY->terms_url = HappyFenceNGPlaceholder::EMPTY_STRING;
							$terms_notice_warnings                                 = false;
						}

						if ( isset( $_POST['company_name'] ) && ! empty( $_POST['company_name'] ) ) {
							HappyFenceNGWhoAmI::get_instance()->PRIVACY->company_name = HappyFenceNGCleaner::must_be_string( sanitize_text_field( wp_unslash( $_POST['company_name'] ) ) );
						} else {
							parent::answerOk( $burn, esc_html__( 'Company name cannot be empty.', 'happyfenceng-total-enterprise-security' ), true, $options );
							exit;
						}

						if ( isset( $_POST['owner_email'] ) && ! empty( $_POST['owner_email'] ) ) {
							$the_email = HappyFenceNGCleaner::must_be_email( sanitize_email( wp_unslash( $_POST['owner_email'] ) ) );
							if ( HappyFenceNGPROC::equals( $the_email, HappyFenceNGPlaceholder::NOT_A_VALID_EMAIL ) ) {
								parent::answerOk( $burn, esc_html__( 'Invalid email address', 'happyfenceng-total-enterprise-security' ), true, $options );
								exit;
							}
							HappyFenceNGWhoAmI::get_instance()->PRIVACY->owner_email = $the_email;
						} else {
							parent::answerOk( $burn, esc_html__( 'Owner email cannot be empty.', 'happyfenceng-total-enterprise-security' ), true, $options );
							exit;
						}

						if ( isset( $_POST['treatment_location'] ) && ! empty( $_POST['treatment_location'] ) ) {
							HappyFenceNGWhoAmI::get_instance()->PRIVACY->treatment_location = HappyFenceNGCleaner::must_be_string( sanitize_text_field( wp_unslash( $_POST['treatment_location'] ?? HappyFenceNGPlaceholder::EMPTY_STRING ) ) );
						} else {
							parent::answerOk( $burn, esc_html__( 'Threatment location cannot be empty.', 'happyfenceng-total-enterprise-security' ), true, $options );
							exit;
						}
					} else {
						parent::answerOk( $burn, esc_html__( 'Banner disabled, other changes will not be saved.', 'happyfenceng-total-enterprise-security' ), true, $options );
						exit;
					}

					parent::audit( 'Privacy Shields options changed successfully' );
					parent::answerOk(
						$burn,
						esc_html__( 'Saved successfully', 'happyfenceng-total-enterprise-security' ),
						false,
						$options
					);
				}
			}
			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' ) );
		}
	}
}
