<?php
/**
 * Plugin Name: WP Total Audit
 * Plugin URI: https://example.com/wp-total-audit
 * Description: Technical SEO, performance, security, accessibility, ecommerce, AEO and WordPress health audit plugin with extensible audit modules.
 * Version: 1.0.0
 * Author: Pentanik IT
 * Text Domain: wp-total-audit
 * Requires at least: 6.0
 * Requires PHP: 7.4
 */

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

define('WPTA_VERSION', '1.0.0');
define('WPTA_FILE', __FILE__);
define('WPTA_PATH', plugin_dir_path(__FILE__));
define('WPTA_URL', plugin_dir_url(__FILE__));

require_once WPTA_PATH . 'includes/class-wpta-utils.php';
require_once WPTA_PATH . 'includes/class-wpta-crawler.php';
require_once WPTA_PATH . 'includes/class-wpta-audit-engine.php';
require_once WPTA_PATH . 'includes/class-wpta-admin.php';
require_once WPTA_PATH . 'includes/class-wpta-rest.php';

final class WP_Total_Audit {
    private static $instance = null;

    public static function instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        register_activation_hook(WPTA_FILE, array($this, 'activate'));
        add_filter('cron_schedules', array($this, 'cron_schedules'));
        add_action('plugins_loaded', array($this, 'load'));
    }

    public function activate() {
        WPTA_Utils::create_tables();
        if (!wp_next_scheduled('wpta_weekly_scheduled_audit')) {
            wp_schedule_event(time() + HOUR_IN_SECONDS, 'weekly', 'wpta_weekly_scheduled_audit');
        }
    }

    public function cron_schedules($schedules) {
        if (!isset($schedules['weekly'])) {
            $schedules['weekly'] = array('interval' => WEEK_IN_SECONDS, 'display' => 'Once Weekly');
        }
        return $schedules;
    }

    public function load() {
        new WPTA_Admin();
        new WPTA_REST();
        add_action('wpta_weekly_scheduled_audit', array($this, 'scheduled_audit'));
    }

    public function scheduled_audit() {
        $settings = get_option('wpta_settings', array());
        if (empty($settings['scheduled_enabled'])) {
            return;
        }
        $limit = isset($settings['crawl_limit']) ? absint($settings['crawl_limit']) : 25;
        $engine = new WPTA_Audit_Engine();
        $engine->run(array('limit' => max(5, min($limit, 200)), 'mode' => 'scheduled'));
    }
}

WP_Total_Audit::instance();
