<?php

defined( 'ABSPATH' ) || exit;

class ZACLS_Branding {

	public function __construct() {
		$this->register_hooks();
	}

	private function register_hooks() {
		add_filter( 'login_headerurl', array( $this, 'custom_logo_url' ) );
		add_filter( 'login_headertext', array( $this, 'custom_logo_title' ) );
		add_action( 'login_enqueue_scripts', array( $this, 'custom_login_styles' ) );
		add_action( 'login_head', array( $this, 'custom_login_html' ) );
	}

	public function custom_logo_url() {
		$url = get_option( 'zacls_login_logo_url', '' );
		return ! empty( $url ) ? esc_url( $url ) : home_url();
	}

	public function custom_logo_title() {
		$title = get_option( 'zacls_login_logo_title', '' );
		return ! empty( $title ) ? esc_html( $title ) : get_bloginfo( 'name' );
	}

	public function custom_login_styles() {
		$bg_color     = get_option( 'zacls_login_bg_color', '' );
		$form_bg      = get_option( 'zacls_login_form_bg', '' );
		$text_color   = get_option( 'zacls_login_text_color', '' );
		$primary_color = get_option( 'zacls_login_primary_color', '' );
		$logo_url     = get_option( 'zacls_login_logo_image', '' );
		$custom_css   = get_option( 'zacls_login_custom_css', '' );

		echo '<style>' . "\n";

		if ( ! empty( $bg_color ) ) {
			echo 'body.login { background-color: ' . esc_attr( $bg_color ) . ' !important; }' . "\n";
		}
		if ( ! empty( $form_bg ) ) {
			echo '#loginform, .zacls-2fa-form { background-color: ' . esc_attr( $form_bg ) . ' !important; }' . "\n";
		}
		if ( ! empty( $text_color ) ) {
			echo 'body.login, body.login label, body.login .message { color: ' . esc_attr( $text_color ) . ' !important; }' . "\n";
		}
		if ( ! empty( $primary_color ) ) {
			echo 'body.login .button-primary { background: ' . esc_attr( $primary_color ) . ' !important; border-color: ' . esc_attr( $primary_color ) . ' !important; }' . "\n";
			echo 'body.login .button-primary:hover { opacity: 0.85; }' . "\n";
		}
		if ( ! empty( $logo_url ) ) {
			echo 'body.login h1 a { background-image: url(' . esc_url( $logo_url ) . ') !important; background-size: contain !important; width: auto !important; }' . "\n";
		}
		if ( ! empty( $custom_css ) ) {
			echo self::sanitize_css( $custom_css ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}

		echo '</style>' . "\n";
	}

	/**
	 * Strip dangerous constructs from user-supplied CSS.
	 *
	 * @param string $css Raw CSS.
	 * @return string Sanitized CSS.
	 */
	public static function sanitize_css( $css ) {
		$css = (string) $css;

		$css = str_replace( array( '<', '>' ), '', $css );

		$css = preg_replace(
			'/@import\s+|expression\s*\(|behavior\s*:|-moz-binding\s*:|javascript\s*:|vbscript\s*:|data\s*:/i',
			'',
			$css
		);

		return trim( (string) $css );
	}

	public function custom_login_html() {
		$header_html = get_option( 'zacls_login_header_html', '' );
		$footer_html = get_option( 'zacls_login_footer_html', '' );

		if ( ! empty( $header_html ) ) {
			echo '<div class="zacls-custom-login-header">' . wp_kses_post( $header_html ) . '</div>';
		}
		if ( ! empty( $footer_html ) ) {
			echo '<div class="zacls-custom-login-footer">' . wp_kses_post( $footer_html ) . '</div>';
		}
	}
}
