<?php
/**
 * Cache module — Page Cache tab view
 *
 * Variables in scope:
 *   @var Blaminhor_Essentials_Module_Cache $module
 *   @var array                             $settings
 *
 * @package BlaminhorEssentials
 */

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

$dropin_installed = $module->dropin_installer->is_installed();
$wp_cache_const   = defined( 'WP_CACHE' ) && WP_CACHE;
// When another cache plugin / foreign drop-in is in charge, we don't try to
// install our own drop-in — surfacing "drop-in is missing" + Reinstall button
// here would only invite a futile click that returns an English error in an
// alert() dialog. The orchestrator-level admin notice already explains the
// suspension; the rest of the tab stays informational.
$is_suspended = $module->conflict_detector->should_suspend_optimizations();

// Auto-detect mobile-specific theme rendering (uses wp_is_mobile()) so we can
// nudge the user to enable the mobile variant if it would matter.
$mobile_theme_detected = false;
$theme_dir = get_template_directory();
if ( is_dir( $theme_dir ) ) {
	$it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $theme_dir, FilesystemIterator::SKIP_DOTS ) );
	$scanned = 0;
	foreach ( $it as $f ) {
		if ( $scanned++ > 200 ) { break; } // Cap scan effort.
		if ( ! $f->isFile() ) { continue; }
		if ( substr( $f->getFilename(), -4 ) !== '.php' ) { continue; }
		$head = @file_get_contents( $f->getPathname(), false, null, 0, 8192 );
		if ( $head && false !== strpos( $head, 'wp_is_mobile' ) ) {
			$mobile_theme_detected = true;
			break;
		}
	}
}
?>

<h2><?php esc_html_e( 'Page cache', 'blaminhor-essentials' ); ?></h2>

<?php if ( $settings['page_cache_enabled'] && ! $dropin_installed && ! $is_suspended ) : ?>
	<div class="notice notice-warning inline">
		<p>
			<strong><?php esc_html_e( 'The advanced-cache.php drop-in is missing.', 'blaminhor-essentials' ); ?></strong>
			<?php esc_html_e( 'Page caching falls back to a slower PHP path until the drop-in is reinstalled.', 'blaminhor-essentials' ); ?>
			<button type="button" class="button button-secondary be-cache-resync-dropin" style="margin-left: 8px;">
				<?php esc_html_e( 'Reinstall drop-in', 'blaminhor-essentials' ); ?>
			</button>
		</p>
	</div>
<?php endif; ?>

<?php if ( $settings['page_cache_enabled'] && ! $wp_cache_const && ! $is_suspended ) : ?>
	<div class="notice notice-warning inline">
		<p>
			<strong><?php esc_html_e( 'WP_CACHE is not enabled in wp-config.php.', 'blaminhor-essentials' ); ?></strong>
			<?php esc_html_e( 'Without it, WordPress will not load the drop-in. If your host blocks programmatic edits to wp-config.php, paste the line below near the top of the file:', 'blaminhor-essentials' ); ?>
		</p>
		<pre style="margin: 0 0 0 28px;"><code>define( 'WP_CACHE', true );</code></pre>
	</div>
<?php endif; ?>

<table class="form-table">
	<tr>
		<th scope="row">
			<label for="be-cache-page-cache-enabled"><?php esc_html_e( 'Enable Cache &amp; Performance', 'blaminhor-essentials' ); ?></label>
		</th>
		<td>
			<label class="blaminhor-essentials-switch">
				<input
					type="checkbox"
					id="be-cache-page-cache-enabled"
					name="page_cache_enabled"
					value="1"
					<?php checked( $settings['page_cache_enabled'] ); ?>>
				<span class="slider"></span>
			</label>
			<p class="description">
				<?php esc_html_e( 'Master switch for the whole module. When ON, the page cache serves a ready-made copy of each page to anonymous visitors AND every individual optimisation below (Defer JS, Critical CSS, lazy-load, image dimensions, self-hosted Google Fonts, etc.) takes effect according to its own toggle. When OFF, none of these optimisations run — the site is served exactly as WordPress and your theme produce it.', 'blaminhor-essentials' ); ?>
			</p>
			<p class="description">
				<?php esc_html_e( 'Logged-in users and shoppers with a WooCommerce cart always see the live version, never a cached one.', 'blaminhor-essentials' ); ?>
			</p>
		</td>
	</tr>
	<tr>
		<th scope="row">
			<label for="be-cache-ttl"><?php esc_html_e( 'Cache lifetime (seconds)', 'blaminhor-essentials' ); ?></label>
		</th>
		<td>
			<input
				type="number"
				id="be-cache-ttl"
				name="cache_ttl"
				min="60"
				max="31536000"
				value="<?php echo esc_attr( (int) $settings['cache_ttl'] ); ?>"
				class="small-text">
			<p class="description">
				<?php esc_html_e( 'How long a cached page stays valid before being regenerated. The cache is also automatically cleared whenever you edit an article, theme or comment — no need to wait for the lifetime to expire.', 'blaminhor-essentials' ); ?>
				<?php esc_html_e( 'Recommended: 1 day (86400 seconds) for a regular site. Use a shorter value if you publish multiple times per day.', 'blaminhor-essentials' ); ?>
			</p>
		</td>
	</tr>
	<tr>
		<th scope="row"><?php esc_html_e( 'Mobile variant', 'blaminhor-essentials' ); ?></th>
		<td>
			<label>
				<input type="checkbox" name="cache_mobile_separate" value="1" <?php checked( $settings['cache_mobile_separate'] ); ?>>
				<?php esc_html_e( 'Generate separate cache files for mobile visitors', 'blaminhor-essentials' ); ?>
			</label>
			<?php if ( $mobile_theme_detected ) : ?>
				<p class="description" style="color:#b35900;">
					<?php esc_html_e( 'Enable this if your site shows different content on mobile vs desktop (e.g. a custom hamburger menu). Otherwise leave it off.', 'blaminhor-essentials' ); ?>
				</p>
			<?php else : ?>
				<p class="description">
					<?php esc_html_e( 'Most recent themes adapt to mobile via CSS — you probably do not need to enable anything here. Only useful if you use AMP or a dedicated mobile plugin.', 'blaminhor-essentials' ); ?>
				</p>
			<?php endif; ?>
		</td>
	</tr>
	<tr>
		<th scope="row"><?php esc_html_e( 'Logged-in users', 'blaminhor-essentials' ); ?></th>
		<td>
			<label>
				<input type="checkbox" name="cache_logged_in" value="1" <?php checked( $settings['cache_logged_in'] ); ?>>
				<?php esc_html_e( 'Cache pages for logged-in visitors (advanced)', 'blaminhor-essentials' ); ?>
			</label>
			<p class="description" style="color:#b35900;">
				<?php esc_html_e( 'Not recommended for WooCommerce, membership or forum sites: a logged-in user could accidentally see another user\'s page. Leave off unless you really know what you are doing.', 'blaminhor-essentials' ); ?>
			</p>
		</td>
	</tr>
</table>

<h2><?php esc_html_e( 'Browser cache & GZIP', 'blaminhor-essentials' ); ?></h2>
<p class="description">
	<?php esc_html_e( 'Tells the browser to keep your images, fonts, CSS and JS in memory for longer, and compresses text responses to send them faster. Works automatically on most Apache hosts; on Nginx, your server config must do it.', 'blaminhor-essentials' ); ?>
</p>

<table class="form-table">
	<tr>
		<th scope="row"><?php esc_html_e( 'Browser cache', 'blaminhor-essentials' ); ?></th>
		<td>
			<label>
				<input type="checkbox" name="browser_cache_enabled" value="1" <?php checked( $settings['browser_cache_enabled'] ); ?>>
				<?php esc_html_e( 'Add Expires + Cache-Control headers via .htaccess', 'blaminhor-essentials' ); ?>
			</label>
		</td>
	</tr>
	<tr>
		<th scope="row"><?php esc_html_e( 'GZIP compression', 'blaminhor-essentials' ); ?></th>
		<td>
			<label>
				<input type="checkbox" name="gzip_enabled" value="1" <?php checked( $settings['gzip_enabled'] ); ?>>
				<?php esc_html_e( 'Compress text responses (mod_deflate)', 'blaminhor-essentials' ); ?>
			</label>
		</td>
	</tr>
</table>

<h2><?php esc_html_e( 'Heartbeat & HTML', 'blaminhor-essentials' ); ?></h2>

<table class="form-table">
	<tr>
		<th scope="row"><?php esc_html_e( 'Heartbeat (frontend)', 'blaminhor-essentials' ); ?></th>
		<td>
			<select name="heartbeat_frontend">
				<option value="disabled"  <?php selected( $settings['heartbeat_frontend'], 'disabled' ); ?>><?php esc_html_e( 'Disabled', 'blaminhor-essentials' ); ?></option>
				<option value="throttle" <?php selected( $settings['heartbeat_frontend'], 'throttle' ); ?>><?php esc_html_e( 'Throttle (60s)', 'blaminhor-essentials' ); ?></option>
				<option value="enabled"  <?php selected( $settings['heartbeat_frontend'], 'enabled' ); ?>><?php esc_html_e( 'Default', 'blaminhor-essentials' ); ?></option>
			</select>
			<p class="description"><?php esc_html_e( 'WordPress automatically pings the server every 15 seconds (autosave, notifications). On the admin: throttle to save server resources while keeping autosave working. On the visitor frontend: turn it off, it is useless there.', 'blaminhor-essentials' ); ?></p>
		</td>
	</tr>
	<tr>
		<th scope="row"><?php esc_html_e( 'Heartbeat (admin)', 'blaminhor-essentials' ); ?></th>
		<td>
			<select name="heartbeat_admin">
				<option value="disabled"  <?php selected( $settings['heartbeat_admin'], 'disabled' ); ?>><?php esc_html_e( 'Disabled', 'blaminhor-essentials' ); ?></option>
				<option value="throttle" <?php selected( $settings['heartbeat_admin'], 'throttle' ); ?>><?php esc_html_e( 'Throttle (60s)', 'blaminhor-essentials' ); ?></option>
				<option value="enabled"  <?php selected( $settings['heartbeat_admin'], 'enabled' ); ?>><?php esc_html_e( 'Default (15s)', 'blaminhor-essentials' ); ?></option>
			</select>
		</td>
	</tr>
	<tr>
		<th scope="row"><?php esc_html_e( 'Heartbeat (post editor)', 'blaminhor-essentials' ); ?></th>
		<td>
			<select name="heartbeat_edit">
				<option value="enabled"  <?php selected( $settings['heartbeat_edit'], 'enabled' ); ?>><?php esc_html_e( 'Default', 'blaminhor-essentials' ); ?></option>
				<option value="throttle" <?php selected( $settings['heartbeat_edit'], 'throttle' ); ?>><?php esc_html_e( 'Throttle (60s)', 'blaminhor-essentials' ); ?></option>
				<option value="disabled" <?php selected( $settings['heartbeat_edit'], 'disabled' ); ?>><?php esc_html_e( 'Disabled', 'blaminhor-essentials' ); ?></option>
			</select>
			<p class="description"><?php esc_html_e( 'Do not disable while you are editing an article: you will lose autosave and the revisions history.', 'blaminhor-essentials' ); ?></p>
		</td>
	</tr>
	<tr>
		<th scope="row"><?php esc_html_e( 'Minify HTML', 'blaminhor-essentials' ); ?></th>
		<td>
			<label>
				<input type="checkbox" name="minify_html" value="1" <?php checked( $settings['minify_html'] ); ?>>
				<?php esc_html_e( 'Strip unnecessary whitespace and HTML comments before serving', 'blaminhor-essentials' ); ?>
			</label>
		</td>
	</tr>
</table>
