<?php

/**
 * Plugin Name: ClaimPrism
 * Description: Detects environmental marketing claims that may need review under EU consumer rules. A risk-screening tool, not legal advice.
 * Version: 0.10.1
 * Requires at least: 6.4
 * Requires PHP: 7.4
 * Author: ClaimPrism
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: claimprism
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'cp_fs' ) ) {
    cp_fs()->set_basename( false, __FILE__ );
} else {
    require_once __DIR__ . '/vendor/autoload.php';
    /**
     * Return the ClaimPrism Freemius SDK instance.
     *
     * @return Freemius
     */
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Freemius bootstrap function name is part of the SDK integration contract.
    function cp_fs() {
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Freemius bootstrap global is part of the SDK integration contract.
        global $cp_fs;
        if ( !isset( $cp_fs ) ) {
            // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Freemius bootstrap global is part of the SDK integration contract.
            $cp_fs = fs_dynamic_init( array(
                'id'               => '36754',
                'slug'             => 'claimprism',
                'premium_slug'     => 'claimprism-pro',
                'type'             => 'plugin',
                'public_key'       => 'pk_90538f9854efbf04c2f806503e29b',
                'is_premium'       => false,
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'slug'    => 'claimprism',
                    'parent'  => array(
                        'slug' => 'tools.php',
                    ),
                    'support' => false,
                ),
                'is_live'          => true,
            ) );
        }
        return $cp_fs;
    }

    function claimprism_default_freemius_currency(  $currency  ) {
        return 'eur';
    }

    cp_fs();
    cp_fs()->add_filter( 'default_currency', 'claimprism_default_freemius_currency' );
    cp_fs()->add_filter( 'pricing/show_annual_in_monthly', '__return_false' );
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Freemius bootstrap hook follows the SDK integration contract.
    do_action( 'cp_fs_loaded' );
    define( 'CLAIMPRISM_VERSION', '0.10.1' );
    define( 'CLAIMPRISM_FILE', __FILE__ );
    define( 'CLAIMPRISM_DIR', plugin_dir_path( __FILE__ ) );
    define( 'CLAIMPRISM_URL', plugin_dir_url( __FILE__ ) );
    require_once CLAIMPRISM_DIR . 'includes/class-claimprism-rules.php';
    require_once CLAIMPRISM_DIR . 'includes/class-claimprism-scanner.php';
    require_once CLAIMPRISM_DIR . 'includes/class-claimprism-background-scanner.php';
    require_once CLAIMPRISM_DIR . 'includes/class-claimprism-editor.php';
    require_once CLAIMPRISM_DIR . 'includes/class-claimprism-admin.php';
    require_once CLAIMPRISM_DIR . 'includes/class-claimprism-pro-value-path.php';
    final class ClaimPrism {
        private static $instance = null;

        private $scanner;

        private $background;

        private $team_roles = null;

        private $review_workflow = null;

        private $review_export = null;

        private $monitoring = null;

        private $monitoring_repair = null;

        private $monitoring_run_guard = null;

        private $monitoring_drilldown = null;

        private $evidence_repository = null;

        private $evidence_alerts = null;

        private $pro_scheduler = null;

        private $periodic_reports = null;

        private $bulk_review = null;

        private $scalable_review_queue = null;

        private $governed_claims = null;

        private $governance_workspace = null;

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

        private function __construct() {
            if ( ClaimPrism_Scanner::TABLE_VERSION !== get_option( 'claimprism_table_version' ) ) {
                ClaimPrism_Scanner::create_table();
            }
            $this->scanner = new ClaimPrism_Scanner();
            $this->background = new ClaimPrism_Background_Scanner($this->scanner);
            new ClaimPrism_Editor($this->scanner);
            if ( !wp_next_scheduled( 'claimprism_weekly_scan' ) ) {
                wp_schedule_event( time() + DAY_IN_SECONDS, 'weekly', 'claimprism_weekly_scan' );
            }
            if ( is_admin() ) {
                new ClaimPrism_Admin($this->scanner, $this->background);
            }
            add_action(
                'save_post',
                array($this, 'scan_saved_content'),
                20,
                3
            );
            add_action( 'claimprism_weekly_scan', array($this, 'run_scheduled_scan') );
        }

        public static function activate() {
            ClaimPrism_Scanner::create_table();
            if ( !wp_next_scheduled( 'claimprism_weekly_scan' ) ) {
                wp_schedule_event( time() + DAY_IN_SECONDS, 'weekly', 'claimprism_weekly_scan' );
            }
            $background_state = get_option( ClaimPrism_Background_Scanner::OPTION, array() );
            if ( is_array( $background_state ) && 'running' === ($background_state['status'] ?? '') && !wp_next_scheduled( ClaimPrism_Background_Scanner::HOOK ) ) {
                wp_schedule_single_event( time() + 10, ClaimPrism_Background_Scanner::HOOK );
            }
        }

        public static function deactivate() {
            $timestamp = wp_next_scheduled( 'claimprism_weekly_scan' );
            if ( $timestamp ) {
                wp_unschedule_event( $timestamp, 'claimprism_weekly_scan' );
            }
            wp_clear_scheduled_hook( 'claimprism_pro_scheduled_scan' );
            wp_clear_scheduled_hook( 'claimprism_evidence_alert_check' );
            wp_clear_scheduled_hook( ClaimPrism_Background_Scanner::HOOK );
            delete_transient( ClaimPrism_Background_Scanner::LOCK );
        }

        public function scan_saved_content( $post_id, $post, $update ) {
            if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
                return;
            }
            if ( !in_array( $post->post_type, ClaimPrism_Scanner::supported_post_types(), true ) ) {
                return;
            }
            if ( !in_array( $post->post_status, array('publish', 'draft', 'private'), true ) ) {
                $this->scanner->delete_findings_for_post( $post_id );
                return;
            }
            $this->scanner->scan_post( $post );
        }

        public function run_scheduled_scan() {
            $state = $this->background->get_state();
            if ( 'running' !== $state['status'] ) {
                $this->background->start_scan();
            }
            $this->background->run_batch();
        }

    }

    register_activation_hook( __FILE__, array('ClaimPrism', 'activate') );
    register_deactivation_hook( __FILE__, array('ClaimPrism', 'deactivate') );
    add_action( 'plugins_loaded', array('ClaimPrism', 'instance') );
}