/**
  * Create an instance of the class with the Application ID and secret obtained from Eleven Paths
  * @param $appId
  * @param $secretKey
  * @deprecated
  */
 function __construct($appId, $secretKey)
 {
     parent::__construct($appId, $secretKey);
 }
 static function filter_authenticate($user, $username = '', $password = '')
 {
     if (!is_a($user, 'WP_User')) {
         return $user;
     } else {
         $appId = get_option('latch_appId');
         $appSecret = get_option('latch_appSecret');
         $host = get_option('latch_host');
         if (!empty($host)) {
             LatchApp::setHost($host);
         }
         if (!empty($appId) && !empty($appSecret)) {
             remove_action('authenticate', 'wp_authenticate_username_password', 20);
             $user = wp_authenticate_username_password(null, $username, $password);
             if (isset($_POST["latch_two_factor"])) {
                 $expectedToken = get_user_option('latch_two_factor', $user->ID);
                 update_user_option($user->ID, 'latch_two_factor', null, true);
                 if (!empty($expectedToken) && $_POST["latch_two_factor"] === $expectedToken) {
                     return $user;
                 } else {
                     return new WP_Error('latch_invalid_token', __('<strong>Error</strong>: Invalid token', 'latch'));
                 }
             }
             $latch_accountId = get_user_option('latch_id', $user->ID);
             if (!empty($latch_accountId)) {
                 $api = new LatchApp($appId, $appSecret);
                 $statusResponse = $api->status($latch_accountId);
                 $responseData = $statusResponse->getData();
                 $responseError = $statusResponse->getError();
                 //Error_log(print_r($responseData, true));
                 //Error_log(print_r($responseError, true));
                 // If something goes wrong, disable Latch temporary or permanently to prevent blocking the user
                 if (empty($statusResponse) || empty($responseData) && empty($responseError)) {
                     return $user;
                 } else {
                     if (!empty($responseError)) {
                         if ($responseError->getCode() == 201) {
                             // If the account is externally unpaired, apply the changes in WP database
                             update_user_option($user->ID, 'latch_id', null, true);
                             update_user_option($user->ID, 'latch_two_factor', null, true);
                         }
                         return $user;
                     }
                 }
                 if (!empty($responseData) && $responseData->{"operations"}->{$appId}->{"status"} === "on") {
                     $two_factor_token = "";
                     if ($responseData->{"operations"}->{$appId}->{"two_factor"}) {
                         $two_factor_token = $responseData->{"operations"}->{$appId}->{"two_factor"}->{"token"};
                     }
                     if (!empty($two_factor_token)) {
                         update_user_option($user->ID, 'latch_two_factor', $two_factor_token, true);
                         include 'two_factor.php';
                         die;
                     }
                     update_user_option($user->ID, 'latch_two_factor', null, true);
                     return $user;
                 } else {
                     //return new WP_Error('latch_account_blocked', __('<strong>Error</strong>: The account is blocked by Latch', 'latch'));
                     return new WP_Error('authentication_failed', __('<strong>Error</strong>: Invalid username or incorrect password.', 'latch'));
                 }
             } else {
                 return $user;
             }
         } else {
             return $user;
         }
     }
 }