<?php
/**
 * Autoloader for SEO Pro Stack plugin.
 *
 * @package SEO_Pro_Stack
 * @subpackage SEO_Pro_Stack/Core
 */

// If this file is called directly, abort.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Class SEOProStack_Autoloader
 * 
 * Handles autoloading of plugin classes.
 */
class SEOProStack_Autoloader {
    /**
     * Register the autoloader
     */
    public static function register() {
        spl_autoload_register(array(self::class, 'autoload'));
    }

    /**
     * Autoload a class file based on its name
     *
     * @param string $class_name The name of the class to load.
     */
    public static function autoload($class_name) {
        // Check if the class should be loaded by this autoloader
        if (false === strpos($class_name, 'SEOProStack')) {
            return;
        }

        // Convert class name to filename
        $file_path = str_replace('SEOProStack_', '', $class_name);
        $file_path = str_replace('_', '-', $file_path);
        $file_path = strtolower($file_path);
        
        // Add 'class-' prefix
        $file_path = 'class-' . $file_path . '.php';

        // Base paths to check for classes
        $base_paths = array(
            SEOPROSTACK_PLUGIN_DIR . 'includes/',
            SEOPROSTACK_PLUGIN_DIR . 'includes/core/',
            SEOPROSTACK_PLUGIN_DIR . 'includes/features/',
            SEOPROSTACK_PLUGIN_DIR . 'includes/features/auto-upload/',
            SEOPROSTACK_PLUGIN_DIR . 'admin/',
            SEOPROSTACK_PLUGIN_DIR . 'admin/settings/',
            SEOPROSTACK_PLUGIN_DIR . 'admin/settings/ajax/',
            SEOPROSTACK_PLUGIN_DIR . 'admin/settings/tabs/',
        );

        // Try to find and load the file
        foreach ($base_paths as $base_path) {
            $full_path = $base_path . $file_path;
            if (file_exists($full_path)) {
                require_once $full_path;
                return;
            }
        }
    }
}

SEOProStack_Autoloader::register();