<?php
/**
 * Plugin Name: AIEO SEO Redirects (lean front-end serve)
 * Description: Serves AIEO 301/302/307/308/410 redirects on EVERY front-end
 *   request, loading ONLY the self-contained AIEO_SEO_Redirects class — never the
 *   full AIEO plugin. As a must-use plugin it is immune to the active_plugins
 *   load-optimisation that trims the AIEO runtime/core on storefront + 404 paths
 *   (exactly where retired URLs land), so PHP serves redirects whenever the nginx
 *   edge map isn't installed. Core-independent: resolves the class via the shared
 *   autoloader, falling back to the bundled copy on disk (runtime first, then core).
 *
 * @package ai-eshop-optimizer
 */

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

add_action(
	'plugins_loaded',
	static function () {
		if ( ! class_exists( 'AIEO_SEO_Redirects' ) ) {
			// The aieo-autoload.php mu-plugin (loads alphabetically before this
			// one) normally resolves the class. Fall back to the bundled file on
			// disk so a fully-trimmed active_plugins set — where neither AIEO
			// plugin booted — still serves redirects. Runtime copy wins.
			if ( defined( 'WP_PLUGIN_DIR' ) ) {
				foreach ( array( 'ai-eshop-optimizer-runtime', 'ai-eshop-optimizer' ) as $dir ) {
					$file = WP_PLUGIN_DIR . '/' . $dir . '/includes/seo/class-aieo-seo-redirects.php';
					if ( is_readable( $file ) ) {
						require_once $file;
						break;
					}
				}
			}
		}
		if ( class_exists( 'AIEO_SEO_Redirects' ) ) {
			// register() hooks template_redirect@1 → maybe_redirect, which is
			// self-gated on seo.enabled and does nothing but an indexed rule
			// lookup + wp_redirect. The shutdown edge-map export only fires on a
			// bust() (admin add/delete), never on a front-end serve.
			AIEO_SEO_Redirects::register();
		}
	},
	1
);
