0, 'until' => $now + MINUTE_IN_SECONDS, ); } if ( $window['count'] >= $limit ) { return true; } ++$window['count']; set_transient( $key, $window, $window['until'] - $now ); return false; } /** * A one-way fingerprint of the visitor's address, so the rate limit never stores the address itself. * * @return string */ public static function visitor_hash() { return hash_hmac( 'sha256', self::ip(), Utill::secret() ); } /** * The address the request came from. Forwarding headers are ignored: they are set by the client * and would let anyone dodge the rate limit. * * @return string */ public static function ip() { $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; return filter_var( $ip, FILTER_VALIDATE_IP ) ? $ip : ''; } /** * @param string $verdict ok, spam or reject. * @param string $message Message for the visitor (reject only). * @return array */ private static function verdict( $verdict, $message = '' ) { return array( 'verdict' => $verdict, 'message' => $message, ); } }