get_client_ip(); $lockout_transient = 'tt24_lockout_' . md5( $ip ); if ( get_transient( $lockout_transient ) ) { return new WP_Error( 'too_many_retries', sprintf( /* translators: %d: lockout duration in minutes */ __( 'ERROR: Too many failed login attempts. Perimeter locked. Please try again in %d minutes.', '24tt-login-security-and-brander' ), 15 ) ); } return $user; } /** * Increment the failed attempts counter for the IP. * * @param string $username The username that failed to authenticate. */ public function record_failed_attempt( $username ) { $ip = $this->get_client_ip(); $ip_hash = md5( $ip ); $failed_transient = 'tt24_failed_' . $ip_hash; $lockout_transient = 'tt24_lockout_' . $ip_hash; $attempts = (int) get_transient( $failed_transient ); $attempts++; if ( $attempts >= $this->max_attempts ) { // Strike 3: Set the 15-minute lockout transient set_transient( $lockout_transient, time(), $this->lockout_duration ); // Wipe the counter so they start fresh after the 15 minutes expire delete_transient( $failed_transient ); } else { // Strike 1 or 2: Update the counter, setting it to expire after 15 minutes of inactivity set_transient( $failed_transient, $attempts, $this->lockout_duration ); } } /** * Reset trackers when a user successfully logs in from an IP. * * @param string $user_login Username. * @param WP_User $user WP_User object. */ public function clear_failed_attempts( $user_login, $user ) { $ip = $this->get_client_ip(); $ip_hash = md5( $ip ); delete_transient( 'tt24_failed_' . $ip_hash ); delete_transient( 'tt24_lockout_' . $ip_hash ); } }