<?php
/**
 * Plugin Name: Guard Dog
 * Plugin URI: https://guarddog.cc/
 * Description: Security plugin for login protection, custom login URL, 2FA, and more.
 * Version: 1.9.50
 * Author: Adam Greenwell
 * Author URI: https://adamgreenwell.com
 * Text Domain: guard-dog
 * Domain Path: /languages
 * Requires at least: 5.9
 * Requires PHP: 8.1
 * License: GPL-2.0+
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

if (!defined('ABSPATH')) exit; // Exit if accessed directly

// Define GUARD_DOG_PLUGIN_FILE
if (!defined('GUARD_DOG_PLUGIN_FILE')) {
    define('GUARD_DOG_PLUGIN_FILE', __FILE__);
}

// Include the Composer autoloader.
// Production builds use scoper-autoload.php (prefixed vendor namespaces).
// Development uses autoload.php + shim that aliases GuardDog\Vendor\X → X.
if ( file_exists( dirname( __FILE__ ) . '/vendor/scoper-autoload.php' ) ) {
    require_once dirname( __FILE__ ) . '/vendor/scoper-autoload.php';
} elseif ( file_exists( dirname( __FILE__ ) . '/vendor/autoload.php' ) ) {
    require_once dirname( __FILE__ ) . '/vendor/autoload.php';
    require_once dirname( __FILE__ ) . '/includes/guard-dog-scoped-autoload.php';
}

// Include the main Guard_Dog class.
if (!class_exists('Guard_Dog')) {
    include_once dirname(__FILE__) . '/includes/class-guard-dog.php';
}

// Include the View Loader class.
if (!class_exists('Guard_Dog_View_Loader')) {
    include_once dirname(__FILE__) . '/includes/utils/class-view-loader.php';
}

/**
 * Returns the main instance of Guard_Dog.
 *
 * @since  1.1.0
 * @return Guard_Dog
 */
function guard_dog_get_instance() {
    return Guard_Dog::instance();
}

/**
 * Convenience function for accessing the main Guard_Dog instance.
 * Provides shorter syntax for accessing features and debug systems.
 *
 * @since  1.1.0
 * @return Guard_Dog
 */
function Guard_Dog() {
    return Guard_Dog::instance();
}

/**
 * Check whether Guard Dog's custom-login 2FA API is available.
 *
 * @since 1.9.40
 * @return bool
 */
function guard_dog_is_login_2fa_available() {
    return guard_dog_get_instance()->is_login_2fa_available();
}

/**
 * Get custom-login 2FA status for a user.
 *
 * @since 1.9.40
 * @param int|WP_User|string $user User ID, user object, username, or email.
 * @return array
 */
function guard_dog_get_login_2fa_status( $user ) {
    return guard_dog_get_instance()->get_login_2fa_status( $user );
}

/**
 * Send an email 2FA code for a custom login flow.
 *
 * @since 1.9.40
 * @param int|WP_User|string $user User ID, user object, username, or email.
 * @return array|WP_Error
 */
function guard_dog_send_login_email_2fa_code( $user ) {
    return guard_dog_get_instance()->send_login_email_2fa_code( $user );
}

/**
 * Verify a custom-login 2FA code.
 *
 * @since 1.9.40
 * @param int|WP_User|string $user   User ID, user object, username, or email.
 * @param string             $code   Verification or recovery code.
 * @param string             $method 2FA method: app, email, or recovery.
 * @return array|WP_Error
 */
function guard_dog_verify_login_2fa_code( $user, $code, $method = 'app' ) {
    return guard_dog_get_instance()->verify_login_2fa_code( $user, $code, $method );
}

/**
 * Check whether passkey login is available.
 *
 * @since 1.9.40
 * @return bool
 */
function guard_dog_is_passkey_login_available() {
    return guard_dog_get_instance()->is_passkey_login_available();
}

/**
 * Get passkey login button markup.
 *
 * @since 1.9.40
 * @param array $args Optional template args.
 * @return string
 */
function guard_dog_get_passkey_login_button_markup( $args = array() ) {
    return guard_dog_get_instance()->get_passkey_login_button_markup( $args );
}

/**
 * Enqueue passkey login assets for a custom login surface.
 *
 * @since 1.9.40
 * @param array $args Optional asset configuration.
 * @return void
 */
function guard_dog_enqueue_passkey_login_assets( $args = array() ) {
    guard_dog_get_instance()->enqueue_passkey_login_assets( $args );
}

/**
 * Enqueue public login assets needed by Guard Dog 2FA forms.
 *
 * @since 1.9.40
 * @return void
 */
function guard_dog_enqueue_login_2fa_assets() {
    guard_dog_get_instance()->enqueue_login_2fa_assets();
}

/**
 * Enqueue frontend account-security assets before rendering the shortcode.
 *
 * @since 1.9.40
 * @param array $args Optional account-security configuration.
 * @return void
 */
function guard_dog_enqueue_account_security_assets( $args = array() ) {
    guard_dog_get_instance()->enqueue_account_security_assets( $args );
}

/**
 * Write a Guard Dog Activity Log entry.
 *
 * Register custom event types with the guard_dog_activity_log_event_types filter
 * before writing custom events.
 *
 * @since 1.9.45
 * @param string $event_type Registered activity-log event slug.
 * @param array  $args       Optional log context: username, user_id, ip_address, object_id, object_type, details.
 * @return bool True when the log entry was accepted.
 */
function guard_dog_log_activity( $event_type, $args = array() ) {
    if ( ! is_array( $args ) ) {
        $args = array();
    }

    return guard_dog_get_instance()->log_activity( $event_type, $args );
}

// Global for backwards compatibility.
$GLOBALS['guard_dog'] = guard_dog_get_instance();

// Activation/Deactivation Hooks
register_activation_hook(__FILE__, ['Guard_Dog', 'activate']);
register_deactivation_hook(__FILE__, ['Guard_Dog', 'deactivate']);

// Register admin-post handler for activity log export
add_action('admin_post_guard_dog_export_logs', ['Guard_Dog_Activity_Log_Page', 'handle_export_logs']);
