<?php

/**

 * The file that defines the core plugin class

 *

 * A class definition that includes attributes and functions used across both the

 * public-facing side of the site and the admin area.

 *

 * @link       https://sensfrx.ai

 * @since      1.0.1

 *

 * @package    SensFRX

 * @subpackage SensFRX/includes

 */

/**

 * The core plugin class.

 *

 * This is used to define internationalization, admin-specific hooks, and

 * public-facing site hooks.

 *

 * Also maintains the unique identifier of this plugin as well as the current

 * version of the plugin.

 *

 * @since      1.0.0

 * @package    SensFRX

 * @subpackage SensFRX/includes

 */

class SensFRX

{

    /**

     * The loader that's responsible for maintaining and registering all hooks that power

     * the plugin.

     *

     * @since    1.0.0

     * @var      SensFRX_Loader    $loader    Maintains and registers all hooks for the plugin.

     */

    protected $loader;

    /**

     * The unique identifier of this plugin.

     *

     * @since    1.0.0

     * @var      string    $SensFRX    The string used to uniquely identify this plugin.

     */

    protected $SensFRX;

    /**

     * The current version of the plugin.

     *

     * @since    1.0.0

     * @var      string    $version    The current version of the plugin.

     */

    protected $version;

    /**

     * The core functionality of the SensFRX plugin.

     *

     * @since    1.0.0

     */

    public function __construct()

    {

        $this->constants();

        if (defined('SENSFRX_VERSION')) {

            $this->version = SENSFRX_VERSION;

        } else {

            $this->version = '1.0.0';

        }

        $this->SensFRX = 'SensFRX';

        $this->load_dependencies();

        $this->set_locale();

        $this->define_admin_hooks();

        $this->define_public_hooks();

    }

    public function constants()

    {

        if (! defined('SENSFRX_VERSION')) {

            define('SENSFRX_VERSION', '1.0.0');

        }

        if (! defined('SENSFRX_AUTHOR')) {

            define('SENSFRX_AUTHOR', 'SensFRX');

        }

        if (! defined('SENSFRX_NAME')) {

            // Use plain string in constant - translate when displaying, not during plugin load

            define('SENSFRX_NAME', 'SensFRX');

        }

        if (! defined('SENSFRX_PATH')) {

            define('SENSFRX_PATH', 'admin.php?page=sensfrx_fpwoo');

        }

        if (! defined('SENSFRX_API_URL')) {

            define('SENSFRX_API_URL', 'https://a.sensfrx.ai/v1');

        }

    }



    public static function default_options()

    {

        $options = [

            'sensfrx_property_id'              => '',

            'sensfrx_property_secret'          => '',

            'default_options'                  => 0,

            // Core Fraud Detection

            'sensfrx_master_fraud'             => '1',

            'sensfrx_bot_fraud'                => '1',

            'sensfrx_new_account_fraud'        => '1',

            // User Activity Protection

            'sensfrx_login_fraud'              => '1',

            'sensfrx_reset_password_fraud'     => '1',

            // Payment Protection

            'sensfrx_before_payment_fraud'     => '1',

            'sensfrx_after_payment_fraud'      => '1',

            // Email Notifications

            'sensfrx_medium_email'             => '1',

            'sensfrx_challenge_email'          => '1',

            'sensfrx_deny_email'               => '1',

            // Payment Email Alerts

            'sensfrx_allow_payment_email'      => '1',

            'sensfrx_challenge_payment_email'  => '1',

            'sensfrx_deny_payment_email'       => '1',

            // Registration Email Alerts

            'sensfrx_allow_register_email'     => '1',

            'sensfrx_challenge_register_email' => '1',

            'sensfrx_deny_register_email'      => '1',

            // Advanced Features

            'sensfrx_webhook_allow'            => '0',

            'sensfrx_whitle_list_email'        => '0',

            // OFAC Compliance (PRO Feature)

            'sensfrx_ofac_enabled'             => '0',

            'sensfrx_ofac_block_countries'     => '0',

            'sensfrx_ofac_sdn_check'           => '0',

            'sensfrx_ofac_email_alerts'        => '1',

            'sensfrx_ofac_dashboard_alerts'    => '1',

            'sensfrx_ofac_audit_log'           => '1',

            'sensfrx_ofac_reports'             => '1',

            'sensfrx_ofac_auto_update'         => '1',

        ];

        return apply_filters('sensfrx_options', $options);

    }

    public static function default_policy_options()

    {

        $options = [

            'sensfrx_medium_email'             => '1',

            'sensfrx_challenge_email'          => '1',

            'sensfrx_deny_email'               => '1',

            'sensfrx_allow_payment_email'      => '1',

            'sensfrx_challenge_payment_email'  => '1',

            'sensfrx_deny_payment_email'       => '1',

            'sensfrx_allow_register_email'     => '1',

            'sensfrx_challenge_register_email' => '1',

            'sensfrx_deny_register_email'      => '1',

            'sensfrx_webhook_allow'            => '1',

            'sensfrx_whitle_list_email'        => '1',

            'sensfrx_master_fraud'             => '1',

            'sensfrx_new_account_fraud'        => '1',

            'sensfrx_bot_fraud'                => '1',

            'sensfrx_login_fraud'              => '1',

            'sensfrx_reset_password_fraud'     => '1',

            'sensfrx_before_payment_fraud'     => '1',

            'sensfrx_after_payment_fraud'      => '1',

            // Admin Alerts 

            'sensfrx_admin_alert_email'                => '',

            'sensfrx_admin_login_alerts'               => '1',

            'sensfrx_admin_payment_alerts'             => '1',

            'sensfrx_admin_registration_alerts'        => '1',

            'sensfrx_admin_challenge_login_email'      => '1',

            'sensfrx_admin_deny_login_email'           => '1',

            'sensfrx_admin_challenge_payment_email'    => '1',

            'sensfrx_admin_deny_payment_email'         => '1',

            'sensfrx_admin_challenge_register_email'   => '1',

            'sensfrx_admin_deny_register_email'        => '1',

        ];

        return apply_filters('sensfrx_policy_options', $options);

    }

    /**

     * Load the required dependencies for this plugin.

     *

     * Include the following files that make up the plugin:

     *

     * - SensFRX_Loader. Orchestrates the hooks of the plugin.

     * - SensFRXi18n. Defines internationalization functionality.

     * - SensFRX_Admin. Defines all hooks for the admin area.

     * - SensFRX_Public. Defines all hooks for the public side of the site.

     *

     * Create an instance of the loader which will be used to register the hooks

     * with WordPress.

     *

     * @since    1.0.0

     *

     */



    private function load_dependencies()

    {

        /**

         * The class responsible for orchestrating the actions and filters of the

         * core plugin.

         */

        require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-sensfrx-loader.php';

        /**

         * The class responsible for defining internationalization functionality

         * of the plugin.

         */

        require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-sensfrx-i18n.php';

        /**

         * The database security helper class for secure database operations.

         */

        require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-sensfrx-db-helper.php';

        /**

         * The demo data generator for first-time installations and empty states.

         */

        require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-sensfrx-demo-data.php';

        /**

         * The class responsible for defining all actions that occur in the admin area.

         */

        require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-sensfrx-admin.php';

        /**

         * The class responsible for defining all actions that occur in the public-facing

         * side of the site.

         */

        require_once plugin_dir_path(dirname(__FILE__)) . 'public/class-sensfrx-public.php';

        $this->loader = new SensFRX_Loader();

    }



    /**

     * Define the locale for this plugin for internationalization.

     *

     * Uses the Sensfrxi18n class in order to set the domain and to register the hook

     * with WordPress.

     *

     * @since    1.0.0

     */



    private function set_locale()

    {

        $plugin_i18n = new SensFRXi18n();

        $this->loader->add_action('init', $plugin_i18n, 'load_plugin_textdomain');

    }



    /**

     * Register all of the hooks related to the admin area functionality

     * of the plugin.

     *

     * @since    1.0.0

     *

     */

    private function define_admin_hooks()

    {



        $plugin_admin = new SensFRX_Admin($this->get_SensFRX(), $this->get_version());



        $this->loader->add_filter('admin_init', $plugin_admin, 'add_settings');



        $this->loader->add_filter('login_message', $plugin_admin, 'sensfrxLoginError');



        $this->loader->add_action('admin_menu', $plugin_admin, 'add_sensfrx_menu');



        // AJAX handler for OFAC settings save

        $this->loader->add_action('wp_ajax_sensfrx_save_ofac_settings', $plugin_admin, 'ajax_save_ofac_settings');



        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_react_app');



        $this->loader->add_action('login_enqueue_scripts', $plugin_admin, 'sensfrx_script_code');



        $this->loader->add_filter('plugin_action_links', $plugin_admin, 'action_links', 10, 2);



        $this->loader->add_action('wp_head', $plugin_admin->webhook, 'webhook_implementation');



		$this->loader->add_action('template_redirect', $plugin_admin->webhookAction, 'webhookAction');



        $this->loader->add_action('rest_api_init', $plugin_admin->api_callback, 'sensfrx_route_callback_handling');



        $this->loader->add_action('plugins_loaded', $plugin_admin, 'sensfrx_check_update');



        $this->loader->add_filter('registration_errors', $plugin_admin->registration_wp, 'registration_function_wordpress', 10, 3);



        $this->loader->add_action('woocommerce_registration_errors', $plugin_admin->registration_func, 'registration_function', 10, 3);



        $this->loader->add_filter('login_message', $plugin_admin, 'as_login_message', 10, 2);



        $this->loader->add_filter('woocommerce_save_account_details_errors', $plugin_admin->profile_update, 'user_profile_updates', 10, 1);



        $this->loader->add_action('admin_notices', $plugin_admin->dashboard_notification, 'add_dashboard_notification');



        $this->loader->add_action('wp_login', $plugin_admin->login_track, 'wp_login_track', 10, 2);



        $this->loader->add_filter('woocommerce_checkout_create_order', $plugin_admin, 'detect_shipping_on_order', 20, 2);



        $this->loader->add_action('woocommerce_checkout_create_order_line_item', $plugin_admin, 'check_product_type_in_order', 10, 4);



        $this->loader->add_action('woocommerce_checkout_order_processed', $plugin_admin->payment_attempt, 'payment_attempt', 10, 3);



        $this->loader->add_action('woocommerce_thankyou', $plugin_admin->payment, 'payment_complete', 10, 2);



        $this->loader->add_action('wp_login_failed', $plugin_admin->login_failed, 'wp_login_failed_track', 10, 2);



        $this->loader->add_action('password_reset', $plugin_admin->password_reset, 'password_reset_track_woo', 10, 2);



        $this->loader->add_action('lostpassword_post', $plugin_admin->password_reset_failed, 'password_reset_failed_track', 10, 2);



        $this->loader->add_action('preprocess_comment', $plugin_admin->comment_checker, 'sensfrx_check_comment', 10, 1);



        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');



        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');



        // AJAX actions for auto-save

        $this->loader->add_action('wp_ajax_sensfrx_autosave_settings', $plugin_admin, 'autosave_settings_ajax');



        // AJAX actions for auto-save

        $this->loader->add_action('wp_ajax_sensfrx_auto_save_settings', $plugin_admin, 'auto_save_settings_ajax');



        // AJAX action for manual sync from cloud

        $this->loader->add_action('wp_ajax_sensfrx_sync_from_cloud', $plugin_admin, 'sync_from_cloud_ajax');



        // AJAX actions for reset onboarding

        $this->loader->add_action('wp_ajax_sensfrx_reset_onboarding', $plugin_admin, 'reset_onboarding_ajax');



        // AJAX actions for demo mode

        $this->loader->add_action('wp_ajax_sensfrx_set_demo_mode', $plugin_admin, 'set_demo_mode_ajax');



        // Woocommerce Extension Development



        $this->loader->add_action('woocommerce_validation_rules_tab_content', $plugin_admin, 'woocommerce_validation_rules_tab_content');



        $this->loader->add_action('woocommerce_policies_tab_content', $plugin_admin, 'woocommerce_policies_tab_content');



        $this->loader->add_action('woocommerce_notifications_alerts_tab_content', $plugin_admin, 'woocommerce_notifications_alerts_tab_content');



        $this->loader->add_action('woocommerce_license_information_tab_content', $plugin_admin, 'woocommerce_license_information_tab_content');



        $this->loader->add_action('account_privacy_tab_content', $plugin_admin, 'account_privacy_tab_content');



        $this->loader->add_action('woocommerce_profile_tab_content', $plugin_admin, 'woocommerce_profile_tab_content');



        $this->loader->add_action('woocommerce_process_shop_order_meta', $plugin_admin->order_update_checker, 'sensfrx_check_order_update_changes', 10, 2);



    }



    /**

     * Register all of the hooks related to the public-facing functionality

     * of the plugin.

     *

     * @since    1.0.0

     *

     */



    private function define_public_hooks()

    {



        $plugin_public = new SensFRX_Public($this->get_SensFRX(), $this->get_version());



        $this->loader->add_action('init', $plugin_public, 'get_user_verify');



        $this->loader->add_action('wp_head', $plugin_public, 'sensfrx_nonce');



        $this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_styles');



        $this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts');



        $this->loader->add_action('woocommerce_login_form', $plugin_public, 'add_field_login_form');



        $this->loader->add_action('woocommerce_register_form', $plugin_public, 'add_field_registration_form');



        $this->loader->add_action('woocommerce_edit_account_form', $plugin_public, 'add_field_edit_password_form');



        $this->loader->add_action('woocommerce_billing_fields', $plugin_public, 'sensfrx_woocommerce_billing_fields');



        $this->loader->add_action('woocommerce_cart_collaterals', $plugin_public, 'custom_cart_collaterals');



        $this->loader->add_action('woocommerce_checkout_before_order_review', $plugin_public, 'add_custom_checkout_field');



        $this->loader->add_action('woocommerce_edit_account_form', $plugin_public, 'profile_update');



        $this->loader->add_action('woocommerce_save_account_details', $plugin_public, 'save_custom_input_field');



        $this->loader->add_action('woocommerce_checkout_update_order_meta', $plugin_public, 'save_custom_field_value_to_session');



        $this->loader->add_filter('template_redirect', $plugin_public, 'as_logout', 10, 2);



        $this->loader->add_action('login_message', $plugin_public, 'as_login_message');



        // Add shipping phone hooks

        $this->loader->add_filter('woocommerce_shipping_fields', $plugin_public, 'custom_shipping_phone_field');

        $this->loader->add_action('woocommerce_checkout_update_order_meta', $plugin_public, 'save_shipping_phone_field');

        $this->loader->add_action('woocommerce_customer_save_address', $plugin_public, 'save_shipping_phone_address', 10, 2);

    }



    /**

     * Run the loader to execute all of the hooks with WordPress.

     *

     * @since    1.0.0

     */

    public function run()

    {

        $this->loader->run();

    }



    /**

     * The name of the plugin used to uniquely identify it within the context of

     * WordPress and to define internationalization functionality.

     *

     * @since     1.0.0

     * @return    string    The name of the plugin.

     */



    public function get_SensFRX()

    {

        return $this->SensFRX;

    }



    /**

     * The reference to the class that orchestrates the hooks with the plugin.

     *

     * @since     1.0.0

     * @return    SensFRX_Loader    Orchestrates the hooks of the plugin.

     */



    public function get_loader()

    {

        return $this->loader;

    }



    /**

     * Retrieve the version number of the plugin.

     *

     * @since     1.0.0

     * @return    string    The version number of the plugin.

     */



    public function get_version()

    {

        return $this->version;

    }



}

