This commit is contained in:
Javier Casares 2026-02-17 14:21:39 +00:00
commit 212d64abfa
27 changed files with 8654 additions and 2 deletions

View file

@ -0,0 +1,184 @@
<?php
/**
* 2FA provider detector class
*
* @package TwoFactorExtended
* @since 0.1.0
*/
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Two_Factor_Extended_Provider_Detector
*
* Detects and manages Two Factor authentication providers.
*
* @since 0.1.0
*/
class Two_Factor_Extended_Provider_Detector {
/**
* Get all available Two Factor providers.
*
* @since 0.1.0
*
* @return array Array of provider class names.
*/
public static function get_all_providers(): array {
if ( ! class_exists( 'Two_Factor_Core' ) ) {
return array();
}
return Two_Factor_Core::get_providers();
}
/**
* Get provider display names.
*
* @since 0.1.0
*
* @return array Array of provider class => display name.
*/
public static function get_provider_names(): array {
$providers = self::get_all_providers();
$names = array();
foreach ( $providers as $class_name => $provider ) {
if ( is_object( $provider ) && method_exists( $provider, 'get_label' ) ) {
$names[ $class_name ] = $provider->get_label();
} else {
$names[ $class_name ] = $class_name;
}
}
return $names;
}
/**
* Get user's enabled providers.
*
* @since 0.1.0
*
* @param int $user_id User ID.
*
* @return array Array of enabled provider class names.
*/
public static function get_user_enabled_providers( int $user_id ): array {
if ( ! class_exists( 'Two_Factor_Core' ) ) {
return array();
}
return Two_Factor_Core::get_enabled_providers_for_user( $user_id );
}
/**
* Get user's primary provider.
*
* @since 0.1.0
*
* @param int $user_id User ID.
*
* @return string|null Primary provider class name or null.
*/
public static function get_user_primary_provider( int $user_id ): ?string {
if ( ! class_exists( 'Two_Factor_Core' ) ) {
return null;
}
$primary = Two_Factor_Core::get_primary_provider_for_user( $user_id );
return $primary ? get_class( $primary ) : null;
}
/**
* Check if user has specific provider enabled.
*
* @since 0.1.0
*
* @param int $user_id User ID.
* @param string $provider_class Provider class name.
*
* @return bool True if provider is enabled, false otherwise.
*/
public static function user_has_provider_enabled( int $user_id, string $provider_class ): bool {
$enabled_providers = self::get_user_enabled_providers( $user_id );
return isset( $enabled_providers[ $provider_class ] );
}
/**
* Check if Two Factor plugin is available.
*
* @since 0.1.0
*
* @return bool True if Two Factor is available, false otherwise.
*/
public static function is_two_factor_available(): bool {
return class_exists( 'Two_Factor_Core' );
}
/**
* Get provider by class name.
*
* @since 0.1.0
*
* @param string $class_name Provider class name.
*
* @return object|null Provider instance or null if not found.
*/
public static function get_provider_instance( string $class_name ): ?object {
$providers = self::get_all_providers();
return $providers[ $class_name ] ?? null;
}
/**
* Get email provider class name.
*
* @since 0.1.0
*
* @return string|null Email provider class name or null if not available.
*/
public static function get_email_provider_class(): ?string {
$providers = self::get_all_providers();
// Common email provider class names.
$email_classes = array(
'Two_Factor_Email',
'Two_Factor_Email_Provider',
);
foreach ( $email_classes as $class ) {
if ( isset( $providers[ $class ] ) ) {
return $class;
}
}
// Fallback: check if any provider has 'email' in the name.
foreach ( array_keys( $providers ) as $class ) {
if ( stripos( $class, 'email' ) !== false ) {
return $class;
}
}
return null;
}
/**
* Check if user has 2FA enabled.
*
* @since 0.1.0
*
* @param int $user_id User ID.
*
* @return bool True if user has at least one provider enabled.
*/
public static function user_has_2fa_enabled( int $user_id ): bool {
$enabled_providers = self::get_user_enabled_providers( $user_id );
return ! empty( $enabled_providers );
}
}