Array of provider instances keyed by class name. */ 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 ( 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 instances keyed by class name. */ 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 ); if ( ! $primary ) { return null; } $class = get_class( $primary ); return false !== $class ? $class : 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 ); } }