robotstxt-2fa/includes/class-plugin.php
2026-06-05 18:39:14 +00:00

168 lines
3.3 KiB
PHP

<?php
/**
* Core plugin bootstrap.
*
* @package Robotstxt_2FA
*/
namespace Robotstxt\TwoFA;
use Robotstxt\TwoFA\Admin\Settings_Page;
use Robotstxt\TwoFA\Cli\Cli_Command;
use Robotstxt\TwoFA\Login\Login_Form_Manager;
use Robotstxt\TwoFA\User\Frontend_Profile;
use Robotstxt\TwoFA\User\Grace_Period;
use Robotstxt\TwoFA\User\Profile_Settings;
use Robotstxt\TwoFA\User\Recovery_Codes;
use Robotstxt\TwoFA\User\Trusted_Devices;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Main plugin controller.
*/
class Plugin {
/**
* Plugin instance.
*
* @var Plugin|null
*/
private static ?Plugin $instance = null;
/**
* Login form manager.
*
* @var Login_Form_Manager
*/
private Login_Form_Manager $login_manager;
/**
* Profile settings manager.
*
* @var Profile_Settings
*/
private Profile_Settings $profile_settings;
/**
* Recovery codes manager.
*
* @var Recovery_Codes
*/
private Recovery_Codes $recovery_codes;
/**
* Settings page handler.
*
* @var Settings_Page
*/
private Settings_Page $settings_page;
/**
* Frontend profile shortcode handler.
*
* @var Frontend_Profile
*/
private Frontend_Profile $frontend_profile;
/**
* Trusted devices manager.
*
* @var Trusted_Devices
*/
private Trusted_Devices $trusted_devices;
/**
* Grace period manager.
*
* @var Grace_Period
*/
private Grace_Period $grace_period;
/**
* Get singleton instance.
*
* @return Plugin
*/
public static function get_instance(): Plugin {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
public function __construct() {
$this->login_manager = new Login_Form_Manager();
$this->profile_settings = new Profile_Settings();
$this->recovery_codes = new Recovery_Codes();
$this->settings_page = new Settings_Page();
$this->frontend_profile = new Frontend_Profile();
$this->trusted_devices = new Trusted_Devices();
$this->grace_period = new Grace_Period();
}
/**
* Plugin initialization.
*
* @return void
*/
public function init(): void {
$this->maybe_load_textdomain();
$this->login_manager->register_hooks();
$this->profile_settings->register_hooks();
$this->recovery_codes->register_hooks();
$this->settings_page->register_hooks();
$this->frontend_profile->register_hooks();
$this->trusted_devices->register_hooks();
$this->grace_period->register_hooks();
if ( defined( 'WP_CLI' ) && WP_CLI ) {
add_action( 'cli_init', array( $this, 'register_cli_commands' ) );
}
}
/**
* Register WP-CLI commands.
*
* Called only when WP_CLI is active.
*
* @since 1.3.0
*
* @return void
*/
public function register_cli_commands(): void {
\WP_CLI::add_command( '2fa', new Cli_Command() );
}
/**
* Runs on activation.
*
* @return void
*/
public static function activate(): void {
// Placeholder for activation tasks (option creation, migrations, etc.).
}
/**
* Runs on deactivation.
*
* @return void
*/
public static function deactivate(): void {
// Placeholder for deactivation cleanup logic.
}
/**
* Load text domain for translations.
*
* @return void
*/
private function maybe_load_textdomain(): void {
load_plugin_textdomain( 'robotstxt-2fa', false, dirname( plugin_basename( ROBOTSTXT_2FA_FILE ) ) . '/languages/' );
}
}