<?php
/**
 * Plugin Name: Cache Autopilot
 * Description: Keeps WordPress cache fresh by purging affected pages, then preloading them safely in the background.
 * Requires at least: 6.5
 * Requires PHP: 8.0
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Author: ekesto
 * Author URI: https://ekesto.com
 * Version: 1.1.14
 * Text Domain: cache-autopilot
 * Domain Path: /languages
 */

declare(strict_types=1);

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

if (!defined('EKESTO_CA_PLUGIN_FILE')) {
    define('EKESTO_CA_PLUGIN_FILE', __FILE__);
}

$ekesto_ca_edition_conflict_guard = __DIR__ . '/src/CacheAutopilot/EditionConflictGuard.php';
$ekesto_ca_admin_notice_screens = __DIR__ . '/src/CacheAutopilot/AdminNoticeScreens.php';
if (is_file($ekesto_ca_admin_notice_screens)) {
    require_once $ekesto_ca_admin_notice_screens;
}

if (is_file($ekesto_ca_edition_conflict_guard)) {
    require_once $ekesto_ca_edition_conflict_guard;

    if (
        function_exists('ekesto_ca_edition_conflict_guard_run')
        && ekesto_ca_edition_conflict_guard_run(__FILE__)
    ) {
        return;
    }
} else {
    // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Early bootstrap failure before the project logger or Composer autoload is available.
    error_log('Cache Autopilot edition conflict guard unavailable during load.');
}

$ekesto_ca_composer_autoload = __DIR__ . '/vendor/autoload.php';
if (!is_file($ekesto_ca_composer_autoload)) {
    // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Early bootstrap failure before the project logger or Composer autoload is available.
    error_log('Cache Autopilot bootstrap skipped: missing vendor/autoload.php during load.');
    return;
}

require_once $ekesto_ca_composer_autoload;

if (!class_exists(\Ekesto\CacheAutopilot\Bootstrap::class, true)) {
    // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Early bootstrap failure before the project logger is guaranteed available.
    error_log('Cache Autopilot bootstrap skipped: Bootstrap class unavailable during load.');
    return;
}

try {
    \Ekesto\CacheAutopilot\Bootstrap::init(EKESTO_CA_PLUGIN_FILE);
} catch (\Throwable $ekesto_ca_bootstrap_error) {
    // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Last-resort bootstrap failure logging before plugin services are reliable.
    error_log(
        sprintf(
            'Cache Autopilot bootstrap skipped: %s',
            $ekesto_ca_bootstrap_error->getMessage()
        )
    );
    return;
}
