<?php
/**
 * Cache module — Preload tab view
 *
 * @var Blaminhor_Essentials_Module_Cache $module
 * @var array                             $settings
 *
 * @package BlaminhorEssentials
 */

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

/**
 * Resolve a sensible default sitemap URL when the user hasn't set one.
 * Priority order:
 *   1. The plugin's own SEO/GEO module if it's active (it publishes a custom
 *      sitemap that knows every public post type the user has indexed).
 *   2. WordPress core's /wp-sitemap.xml when the WP_Sitemaps class is loaded.
 *   3. /sitemap.xml as a last-resort guess.
 *
 * Returned as the input's value (not just a placeholder) so the user sees
 * what the preloader will actually crawl.
 */
$be_sitemap_default = '';
$be_sitemap_source  = '';
if ( function_exists( 'blaminhor_essentials' ) ) {
	$be_plugin_root = blaminhor_essentials();
	if ( is_object( $be_plugin_root ) && method_exists( $be_plugin_root, 'is_module_active' ) && $be_plugin_root->is_module_active( 'seo-manager' ) ) {
		$be_sitemap_default = home_url( '/sitemap.xml' ); // SEO/GEO publishes its custom sitemap at /sitemap.xml
		$be_sitemap_source  = 'seo-manager';
	}
}
if ( '' === $be_sitemap_default ) {
	if ( class_exists( 'WP_Sitemaps' ) ) {
		$be_sitemap_default = home_url( '/wp-sitemap.xml' );
		$be_sitemap_source  = 'wp-core';
	} else {
		$be_sitemap_default = home_url( '/sitemap.xml' );
		$be_sitemap_source  = 'guess';
	}
}
$be_sitemap_value = '' !== (string) $settings['preload_sitemap_url']
	? (string) $settings['preload_sitemap_url']
	: $be_sitemap_default;
?>

<h2><?php esc_html_e( 'Sitemap warmup', 'blaminhor-essentials' ); ?></h2>
<p class="description">
	<?php esc_html_e( 'Automatically visits the pages of your site (from your sitemap) to put them in cache ahead of time. That way your visitors always get a ready-made page, never one being generated. Runs every hour, 10 pages at a time.', 'blaminhor-essentials' ); ?>
</p>

<table class="form-table">
	<tr>
		<th scope="row"><?php esc_html_e( 'Enable', 'blaminhor-essentials' ); ?></th>
		<td>
			<label>
				<input type="checkbox" name="preload_sitemap_enabled" value="1" <?php checked( $settings['preload_sitemap_enabled'] ); ?>>
				<?php esc_html_e( 'Warm up the cache automatically from the sitemap', 'blaminhor-essentials' ); ?>
			</label>
		</td>
	</tr>
	<tr>
		<th scope="row">
			<label for="be-sitemap-url"><?php esc_html_e( 'Sitemap URL', 'blaminhor-essentials' ); ?></label>
		</th>
		<td>
			<input
				type="url"
				id="be-sitemap-url"
				name="preload_sitemap_url"
				value="<?php echo esc_attr( $be_sitemap_value ); ?>"
				class="regular-text code"
				placeholder="<?php echo esc_attr( $be_sitemap_default ); ?>">
			<?php if ( '' === (string) $settings['preload_sitemap_url'] && 'seo-manager' === $be_sitemap_source ) : ?>
				<p class="description" style="color:#3a8a4a;">
					<span class="dashicons dashicons-yes-alt" style="vertical-align:text-bottom;"></span>
					<?php esc_html_e( 'Pre-filled from your active SEO/GEO module — covers every post type you indexed there.', 'blaminhor-essentials' ); ?>
				</p>
			<?php elseif ( '' === (string) $settings['preload_sitemap_url'] && 'wp-core' === $be_sitemap_source ) : ?>
				<p class="description"><?php esc_html_e( 'Pre-filled with the WordPress core sitemap.', 'blaminhor-essentials' ); ?></p>
			<?php else : ?>
				<p class="description"><?php esc_html_e( 'Sitemap indexes are walked one level deep, capped at 5 children. Edit the URL or clear the field to fall back to auto-detection.', 'blaminhor-essentials' ); ?></p>
			<?php endif; ?>
		</td>
	</tr>
	<tr>
		<th scope="row">
			<label for="be-preload-batch-size"><?php esc_html_e( 'Pages per run', 'blaminhor-essentials' ); ?></label>
		</th>
		<td>
			<input
				type="number"
				id="be-preload-batch-size"
				name="preload_batch_size"
				value="<?php echo esc_attr( (int) $settings['preload_batch_size'] ); ?>"
				min="1"
				max="200"
				step="1"
				class="small-text">
			<p class="description">
				<?php esc_html_e( 'Number of pages warmed in a single batch (range 1–200, default 30). Once a batch finishes the next one starts straight away, so a 30-URL sitemap fits in one run. Lower the number on a small or shared-hosting site if you see timeouts; raise it on a beefy server to drain a long sitemap faster.', 'blaminhor-essentials' ); ?>
			</p>
		</td>
	</tr>
	<tr>
		<th scope="row"><?php esc_html_e( 'Run preload now', 'blaminhor-essentials' ); ?></th>
		<td>
			<p>
				<button type="button" class="button button-primary" id="be-preload-now"><?php esc_html_e( 'Preload all pages now', 'blaminhor-essentials' ); ?></button>
				<button type="button" class="button" id="be-preload-stop" style="display:none;"><?php esc_html_e( 'Stop', 'blaminhor-essentials' ); ?></button>
			</p>
			<p class="description"><?php esc_html_e( 'Goes through every page in your sitemap one by one, showing live in the table whether each page responds correctly and how long it took to generate. Safe to click at any time, does not interfere with the automatic preloading.', 'blaminhor-essentials' ); ?></p>

			<div id="be-preload-progress-wrap" class="ap-mt-lg" style="display:none;">
				<div class="ap-progress-header">
					<span id="be-preload-now-status"></span>
					<span id="be-preload-now-count">0 / 0</span>
				</div>
				<div class="ap-progress-bar-wrapper">
					<div class="ap-progress-bar" id="be-preload-progress-bar" style="width:0;"></div>
				</div>
			</div>

			<table class="widefat striped ap-sortable-table ap-mt-lg" id="be-preload-table" style="display:none;">
				<thead>
					<tr>
						<th style="width:60%;"><?php esc_html_e( 'URL', 'blaminhor-essentials' ); ?></th>
						<th style="width:25%;"><?php esc_html_e( 'Status', 'blaminhor-essentials' ); ?></th>
						<th style="width:15%;"><?php esc_html_e( 'Generation time', 'blaminhor-essentials' ); ?> <span title="<?php esc_attr_e( 'Time the server took to generate the page during preloading. NOT the visitor\'s total page load time (which also includes network, asset downloads, and browser rendering). It is the only step the cache eliminates for the next visitor — that is why it is shown here. A page above ~2000 ms usually points to a slow plugin, a heavy database query, or a slow external API call.', 'blaminhor-essentials' ); ?>" style="cursor:help;color:#888;font-weight:normal;">ⓘ</span></th>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
		</td>
	</tr>
</table>

<div id="be-cached-pages" class="be-cached-pages">
	<div class="be-cached-pages-header">
		<h3><?php esc_html_e( 'Pages currently in cache', 'blaminhor-essentials' ); ?></h3>
		<input
			type="search"
			class="be-cached-pages-search regular-text"
			placeholder="<?php esc_attr_e( 'Filter by URL…', 'blaminhor-essentials' ); ?>"
		>
	</div>
	<p class="be-cached-pages-count"></p>
	<p class="description">
		<?php esc_html_e( 'Live list of pages saved on disk by the cache. The list updates automatically as new pages are warmed up. Use the Purge button to drop a single page without touching the rest of the cache.', 'blaminhor-essentials' ); ?>
	</p>
	<table class="widefat striped">
		<thead>
			<tr>
				<th><?php esc_html_e( 'URL', 'blaminhor-essentials' ); ?></th>
				<th style="width:14%;"><?php esc_html_e( 'Variant', 'blaminhor-essentials' ); ?></th>
				<th style="width:10%;"><?php esc_html_e( 'Size', 'blaminhor-essentials' ); ?></th>
				<th style="width:14%;"><?php esc_html_e( 'Generated', 'blaminhor-essentials' ); ?></th>
				<th style="width:10%;"></th>
			</tr>
		</thead>
		<tbody>
			<tr><td colspan="5"><em><?php esc_html_e( 'Loading…', 'blaminhor-essentials' ); ?></em></td></tr>
		</tbody>
	</table>
	<div class="be-cached-pages-pager">
		<button type="button" class="button button-small be-cached-pages-prev" disabled>
			<?php esc_html_e( 'Previous', 'blaminhor-essentials' ); ?>
		</button>
		<button type="button" class="button button-small be-cached-pages-next" disabled>
			<?php esc_html_e( 'Next', 'blaminhor-essentials' ); ?>
		</button>
		<span class="be-cached-pages-position">1 / 1</span>
	</div>
</div>

<h2><?php esc_html_e( 'Hover prefetch', 'blaminhor-essentials' ); ?></h2>

<table class="form-table">
	<tr>
		<th scope="row"><?php esc_html_e( 'Prefetch on hover', 'blaminhor-essentials' ); ?></th>
		<td>
			<label>
				<input type="checkbox" name="preload_links_hover" value="1" <?php checked( $settings['preload_links_hover'] ); ?>>
				<?php esc_html_e( 'When the visitor hovers (or tabs into) an internal link, prefetch the target page in the background', 'blaminhor-essentials' ); ?>
			</label>
			<p class="description"><?php esc_html_e( 'Respects the browser\'s data-saver mode. Skips admin URLs, download links, and external domains.', 'blaminhor-essentials' ); ?></p>
		</td>
	</tr>
</table>

<h2><?php esc_html_e( 'Resource hints', 'blaminhor-essentials' ); ?></h2>
<p class="description">
	<?php esc_html_e( 'Warn the browser ahead of time about external sites the page will contact (Google Fonts, YouTube, Google Maps…) so it can prepare the connection before the resource is actually requested. A few milliseconds saved per external resource.', 'blaminhor-essentials' ); ?>
</p>

<table class="form-table">
	<tr>
		<th scope="row"><?php esc_html_e( 'Auto-detect 3rd-party origins', 'blaminhor-essentials' ); ?></th>
		<td>
			<label>
				<input type="checkbox" name="auto_resource_hints_enabled" value="1" <?php checked( ! empty( $settings['auto_resource_hints_enabled'] ) ); ?>>
				<?php esc_html_e( 'Inject dns-prefetch and preconnect hints automatically for the most-used 3rd-party hosts on each page', 'blaminhor-essentials' ); ?>
			</label>
			<p class="description"><?php esc_html_e( 'Automatically detects the 5 external sites used the most on each page (image CDNs, fonts, third-party scripts…) and prepares the connection to them as soon as the page starts loading. If a site is already prepared by your theme, it is not duplicated.', 'blaminhor-essentials' ); ?></p>
		</td>
	</tr>
	<tr>
		<th scope="row">
			<label for="be-preconnect-domains"><?php esc_html_e( 'Preconnect', 'blaminhor-essentials' ); ?></label>
		</th>
		<td>
			<textarea id="be-preconnect-domains" name="preconnect_domains" rows="4" class="large-text code" placeholder="https://fonts.gstatic.com&#10;https://www.googletagmanager.com"><?php echo esc_textarea( $settings['preconnect_domains'] ); ?></textarea>
			<p class="description"><?php esc_html_e( 'Maximum 6 external sites. Each one triggers an immediate secure connection. Only use for genuinely critical sites — too many open connections slow things down rather than speed them up.', 'blaminhor-essentials' ); ?></p>
		</td>
	</tr>
	<tr>
		<th scope="row">
			<label for="be-dns-prefetch-domains"><?php esc_html_e( 'DNS prefetch', 'blaminhor-essentials' ); ?></label>
		</th>
		<td>
			<textarea id="be-dns-prefetch-domains" name="dns_prefetch_domains" rows="4" class="large-text code" placeholder="//cdn.example.com&#10;//analytics.example.com"><?php echo esc_textarea( $settings['dns_prefetch_domains'] ); ?></textarea>
			<p class="description"><?php esc_html_e( 'Lightweight version: only resolves the domain name in advance, not the full connection. You can list many entries without penalty.', 'blaminhor-essentials' ); ?></p>
		</td>
	</tr>
	<tr>
		<th scope="row">
			<label for="be-preload-fonts"><?php esc_html_e( 'Preload fonts', 'blaminhor-essentials' ); ?></label>
		</th>
		<td>
			<textarea id="be-preload-fonts" name="preload_fonts" rows="4" class="large-text code" placeholder="https://example.com/wp-content/themes/your-theme/fonts/Inter-Variable.woff2"><?php echo esc_textarea( $settings['preload_fonts'] ); ?></textarea>
			<p class="description"><?php esc_html_e( 'One font URL per line. Only preload fonts that are visible right when a visitor lands on the page (header, first heading…). Preloading fonts used further down actually slows the page down instead of speeding it up.', 'blaminhor-essentials' ); ?></p>
		</td>
	</tr>
	<tr>
		<th scope="row"><?php esc_html_e( 'Google Fonts display=swap', 'blaminhor-essentials' ); ?></th>
		<td>
			<label>
				<input type="checkbox" name="fonts_display_swap" value="1" <?php checked( $settings['fonts_display_swap'] ); ?>>
				<?php esc_html_e( 'Mirrored from the Files tab — set there.', 'blaminhor-essentials' ); ?>
			</label>
			<p class="description"><?php esc_html_e( 'Configured under Files → Web fonts. Repeated here for convenience.', 'blaminhor-essentials' ); ?></p>
		</td>
	</tr>
</table>
