<?php

use Dolphy\Plugin\Security\DPLCVerified;
use Dolphy\Plugin\Security\TwoFA;

defined('DOLPHY_PLUGIN') or die('Access Denied!');

add_filter('dolphy_login_actions', function ($actions, WP_User $user) {
  $twofa = TwoFA::get($user->ID);

  if ($twofa && !TwoFA::isRemembered($user->ID)) {
    $actions[] = '2fa';
  }

  return $actions;
}, 10, 2);


add_filter('dolphy_login_verify_actions', function ($verified, WP_User $user, $code) {
  $twofa = TwoFA::get($user->ID);

  if ($twofa && $verified === true) {
    $verified = TwoFA::isRemembered($user->ID) || DPLCVerified::check('2fa', $code);
  }

  return $verified;
}, 10, 3);


add_action('dolphy_login_complete_actions', function (WP_User $user, $code) {
  DPLCVerified::remove('2fa', $code);
}, 10, 3);
