<?php
/**
 * Maintenance Mode — FREE
 *
 * Displays a maintenance page to all non-admin visitors when enabled.
 * Returns HTTP 503 and prevents caching.
 *
 * @package Panelra
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Pnlrdb_Maintenance_Mode
 */
class Pnlrdb_Maintenance_Mode {

	/**
	 * Register hooks.
	 */
	public function __construct() {
		add_action( 'template_redirect', array( $this, 'maybe_show' ) );
	}

	/**
	 * Show the maintenance page if maintenance mode is enabled
	 * and the current visitor is not an administrator.
	 *
	 * @return void
	 */
	public function maybe_show() {
		if ( ! get_option( 'pnlrdb_maintenance_enabled', 0 ) ) {
			return;
		}
		if ( current_user_can( 'manage_options' ) ) {
			return;
		}

		status_header( 503 );
		nocache_headers();

		/**
		 * Fires when a maintenance page should be rendered.
		 * Pro hooks here to render a custom design.
		 * If nothing is hooked, the default page is rendered below.
		 *
		 * @since 1.0.0
		 */
		if ( has_action( 'pnlrdb_maintenance_page' ) ) {
			do_action( 'pnlrdb_maintenance_page' );
		} else {
			$this->render_default_page();
		}

		exit;
	}

	/**
	 * Render a minimal built-in maintenance page.
	 *
	 * @return void
	 */
	private function render_default_page() {
		$title   = esc_html( get_option( 'pnlrdb_maintenance_title',   __( 'Site under maintenance', 'panelra-dashboard-builder' ) ) );
		$message = esc_html( get_option( 'pnlrdb_maintenance_message', __( "We'll be back soon!", 'panelra-dashboard-builder' ) ) );

		$inline_css = '*{box-sizing:border-box;margin:0;padding:0;}'
			. 'body{background:#0f1117;color:#fff;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;display:flex;align-items:center;justify-content:center;min-height:100vh;text-align:center;padding:2rem;}'
			. '.wrap{max-width:520px;}'
			. 'h1{font-size:clamp(1.8rem,4vw,2.6rem);font-weight:700;margin-bottom:1rem;letter-spacing:-0.02em;}'
			. 'p{font-size:1.1rem;line-height:1.7;opacity:.7;}';

		wp_register_style( 'pnlrdb-maintenance', false, array(), PNLRDB_VERSION );
		wp_enqueue_style( 'pnlrdb-maintenance' );
		wp_add_inline_style( 'pnlrdb-maintenance', $inline_css );

		echo '<!DOCTYPE html><html><head>';// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '<meta charset="UTF-8">';// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '<meta name="viewport" content="width=device-width,initial-scale=1">';// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '<title>' . esc_html( $title ) . '</title>';// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		wp_print_styles( array( 'pnlrdb-maintenance' ) );
		echo '</head><body>';// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '<div class="wrap"><h1>' . esc_html( $title ) . '</h1><p>' . esc_html( $message ) . '</p></div>';// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '</body></html>';// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}
}
