$type, 'value' => $recipient, ) ); if ( is_wp_error( $access ) ) { return $access; } $ip = Request::ip(); $allowed = Rate_Limiter::check( Rate_Limiter::ACTION_SEND, $recipient, $ip ); if ( is_wp_error( $allowed ) ) { return $allowed; } $issued = Otp_Manager::issue( $recipient, $purpose, '' ); if ( is_wp_error( $issued ) ) { return $issued; } $sent = Dispatcher::send_code( $recipient, $type, $issued['code'], $purpose ); Rate_Limiter::record( Rate_Limiter::ACTION_SEND, $recipient, $ip, $sent['success'] ); if ( ! $sent['success'] ) { Otp_Manager::discard( $recipient, $purpose ); return new WP_Error( 'aslansheed_delivery_failed', __( 'We could not send the code. Please try again in a moment or use another method.', 'aslansheed-login' ) ); } Audit_Log::log( 'otp_sent', array( 'identifier' => 'phone' === $type ? Phone_Normalizer::mask( $recipient ) : $recipient, 'context' => array( 'channel' => $sent['channel'], 'purpose' => $purpose, ), ) ); return array( 'sent' => true, 'resend_in' => (int) Options::get( 'otp.resend_delay' ), 'length' => (int) Options::get( 'otp.length' ), ); } /** * Checks a code. * * @param string $recipient E.164 number or email address. * @param string $purpose Purpose slug. * @param string $code Code typed by the visitor. * @return true|WP_Error */ public static function check( $recipient, $purpose, $code ) { $ip = Request::ip(); $allowed = Rate_Limiter::check( Rate_Limiter::ACTION_VERIFY, $recipient, $ip ); if ( is_wp_error( $allowed ) ) { return $allowed; } $result = Otp_Manager::verify( $recipient, $purpose, preg_replace( '/\D+/', '', Phone_Normalizer::ascii_digits( (string) $code ) ) ); if ( is_wp_error( $result ) ) { Rate_Limiter::record( Rate_Limiter::ACTION_VERIFY, $recipient, $ip, false ); return $result; } Rate_Limiter::record( Rate_Limiter::ACTION_VERIFY, $recipient, $ip, true ); Rate_Limiter::reset_failures( $recipient ); return true; } }