<?php
if (!defined('ABSPATH')) exit;

$reeid_loader_base = dirname(__DIR__); // .../includes

// 1) Load standard includes if present (idempotent)
foreach (['rt-wc-store.php'] as $reeid_loader_file) {
    $reeid_loader_path = $reeid_loader_base . '/' . $reeid_loader_file;
    if (is_readable($reeid_loader_path)) require_once $reeid_loader_path;
}

// 2) Load migrated files (natural order, quarantine first)
$reeid_loader_mu_dir = $reeid_loader_base . '/compat';

if (is_dir($reeid_loader_mu_dir)) {

    $reeid_loader_files = glob($reeid_loader_mu_dir . '/*.php') ?: [];

    usort($reeid_loader_files, function($a, $b) {

        $qa = (int)preg_match('/quarantine/i', basename($a));
        $qb = (int)preg_match('/quarantine/i', basename($b));

        if ($qa !== $qb) {
            return $qb <=> $qa; // quarantine first
        }

        return strnatcasecmp(basename($a), basename($b));
    });

    foreach ($reeid_loader_files as $reeid_loader_file) {
        require_once $reeid_loader_file;
    }
}

// 3) Early hook space
add_action('plugins_loaded', static function(){}, 1);