= 1 ) ? (int) $limit : self::DEFAULT_LIMIT; } /** * Count one request against the coarse limiter. * * @param string $client_key Client key. * @return bool True while the client is within the limit, false once it is over. */ public static function allow( $client_key ) { return self::bump( 'c', (string) $client_key ) <= self::limit(); } /** * Count one submission against the per-form limiter. * * @param string $form_id Effective formId. * @param string $client_key Client key. * @param int $limit Submissions per client and minute (spam.rate_limit_per_minute). 0 switches the limiter off. * @return bool True while the client is within the limit. */ public static function allow_form( $form_id, $client_key, $limit ) { $limit = (int) $limit; if ( $limit <= 0 ) { return true; } return self::bump( 'f', md5( (string) $form_id ) . '_' . $client_key ) <= $limit; } /** * Claim a payload for the duplicate guard. * * @param string $form_id Effective formId. * @param string $client_key Client key. * @param string $payload A hash of the submitted values. * @return bool True when the payload is new (now claimed), false when the same client sent the same payload in the last 10 seconds. */ public static function claim( $form_id, $client_key, $payload ) { $name = self::duplicate_name( $form_id, $client_key, $payload ); $seen = self::read( $name ); if ( $seen > 0 && Clock::now() - $seen < self::DUPLICATE_SECONDS ) { return false; } self::write( $name, Clock::now(), self::DUPLICATE_SECONDS * 2 ); return true; } /** * Forget a claim (the submission turned out invalid, so the visitor may send it again). * * @param string $form_id Effective formId. * @param string $client_key Client key. * @param string $payload A hash of the submitted values. * @return void */ public static function release( $form_id, $client_key, $payload ) { $name = self::duplicate_name( $form_id, $client_key, $payload ); if ( wp_using_ext_object_cache() ) { wp_cache_delete( $name, self::CACHE_GROUP ); } else { delete_transient( $name ); } } /** * Add one to the counter of the current window. * * @param string $kind Counter kind (c or f). * @param string $key Counter key. * @return int The new count. */ private static function bump( $kind, $key ) { $name = 'formwright_' . $kind . '_' . (int) floor( Clock::now() / self::WINDOW ) . '_' . $key; if ( wp_using_ext_object_cache() ) { wp_cache_add( $name, 0, self::CACHE_GROUP, self::WINDOW * 2 ); $count = wp_cache_incr( $name, 1, self::CACHE_GROUP ); return false === $count ? 1 : (int) $count; } $count = self::read( $name ) + 1; self::write( $name, $count, self::WINDOW * 2 ); return $count; } /** * Name of a duplicate-guard entry. * * @param string $form_id Effective formId. * @param string $client_key Client key. * @param string $payload Payload hash. * @return string */ private static function duplicate_name( $form_id, $client_key, $payload ) { return 'formwright_d_' . md5( $form_id . '|' . $client_key . '|' . $payload ); } /** * Read a number. * * @param string $name Name. * @return int */ private static function read( $name ) { $value = wp_using_ext_object_cache() ? wp_cache_get( $name, self::CACHE_GROUP ) : get_transient( $name ); return is_numeric( $value ) ? (int) $value : 0; } /** * Write a number with an expiry. * * @param string $name Name. * @param int $value Value. * @param int $seconds Seconds until it expires. * @return void */ private static function write( $name, $value, $seconds ) { if ( wp_using_ext_object_cache() ) { wp_cache_set( $name, $value, self::CACHE_GROUP, $seconds ); } else { set_transient( $name, $value, $seconds ); } } }