<?php
/**
 * Plugin Name: Capsquery Facial Authentication
 * Plugin URI:  https://capsquery.com/
 * Description: Capsquery Facial Authentication. Users can enroll their face and log in with face recognition plus a PIN. Where PIN is a security add on layer along with face.
 * Version:     1.1.4
 * Author:      Arijit
 * Author URI:  https://www.linkedin.com/in/developer-arijit/
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: capsquery-facial-authentication
 * Requires at least: 6.0
 * Tested up to: 7.0
 * Requires PHP: 7.4
 */

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

define( 'CAPSQ_VERSION', '1.1.0' );
define( 'CAPSQ_PLUGIN_FILE', __FILE__ );
define( 'CAPSQ_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'CAPSQ_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

if ( ! defined( 'CAPSQ_MATCH_THRESHOLD' ) ) {
	define( 'CAPSQ_MATCH_THRESHOLD', 0.55 );
}

if ( ! defined( 'CAPSQ_MAX_PIN_ATTEMPTS' ) ) {
	define( 'CAPSQ_MAX_PIN_ATTEMPTS', 5 );
}

/**
 * Cache-busting asset version.
 *
 * @param string $file Relative path under plugin root.
 */
function capsq_asset_version( $file ) {
	$path = CAPSQ_PLUGIN_DIR . $file;
	if ( file_exists( $path ) ) {
		return (string) filemtime( $path );
	}
	return CAPSQ_VERSION;
}

require_once CAPSQ_PLUGIN_DIR . 'includes/class-capsq-helpers.php';
require_once CAPSQ_PLUGIN_DIR . 'includes/class-capsq-crypto.php';
require_once CAPSQ_PLUGIN_DIR . 'includes/class-capsq-admin.php';
require_once CAPSQ_PLUGIN_DIR . 'includes/class-capsq-profile.php';
require_once CAPSQ_PLUGIN_DIR . 'includes/class-capsq-rest.php';
require_once CAPSQ_PLUGIN_DIR . 'includes/class-capsq-login.php';
require_once CAPSQ_PLUGIN_DIR . 'includes/class-capsq-shortcode.php';
require_once CAPSQ_PLUGIN_DIR . 'includes/class-capsq-privacy.php';

/**
 * Activation: encryption key and default options.
 */
function capsq_activate() {
	if ( ! get_option( 'capsq_encryption_key' ) ) {
		update_option( 'capsq_encryption_key', bin2hex( random_bytes( 32 ) ), false );
	}
	if ( null === get_option( Capsq_Helpers::OPTION_ENABLED, null ) ) {
		update_option( Capsq_Helpers::OPTION_ENABLED, true );
	}
	if ( null === get_option( Capsq_Helpers::OPTION_THRESHOLD, null ) ) {
		update_option( Capsq_Helpers::OPTION_THRESHOLD, CAPSQ_MATCH_THRESHOLD );
	}
}
register_activation_hook( __FILE__, 'capsq_activate' );


/**
 * Boot the plugin.
 */
function capsq_init() {
	if ( is_admin() ) {
		new Capsq_Admin();
	}
	new Capsq_Profile();
	new Capsq_REST();
	new Capsq_Login();
	new Capsq_Shortcode();
	new Capsq_Privacy();
}
add_action( 'plugins_loaded', 'capsq_init' );

/**
 * Warn when HTTPS is not enabled (camera requires a secure context).
 */
function capsq_https_admin_notice() {
	if ( is_ssl() ) {
		return;
	}
	$host = wp_parse_url( home_url(), PHP_URL_HOST );
	if ( in_array( $host, array( 'localhost', '127.0.0.1' ), true ) ) {
		return;
	}
	echo '<div class="notice notice-warning"><p><strong>' . esc_html__( 'Capsquery Facial Authentication:', 'capsquery-facial-authentication' ) . '</strong> ';
	echo esc_html__( 'Your site is not using HTTPS. Browsers block camera access on non-secure origins. Capsquery Facial Authentication requires SSL.', 'capsquery-facial-authentication' );
	echo '</p></div>';
}
add_action( 'admin_notices', 'capsq_https_admin_notice' );

/**
 * Plugin action links on Plugins screen.
 *
 * @param string[] $links Existing links.
 */
function capsq_plugin_action_links( $links ) {
	$settings = '<a href="' . esc_url( admin_url( 'options-general.php?page=capsquery-facial-authentication' ) ) . '">' . esc_html__( 'Settings', 'capsquery-facial-authentication' ) . '</a>';
	array_unshift( $links, $settings );
	return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'capsq_plugin_action_links' );
