0 ) { return new \WP_Error( 'cooldown', sprintf( /* translators: %d: seconds */ __( 'Please wait %d seconds and try again.', 'adev-otp-login' ), $left ), array( 'cooldown' => $left ) ); } delete_transient( $cd_key ); } $daily_phone = (int) Plugin::get_setting( 'daily_limit_phone', 10 ); $phone_count = (int) get_transient( 'adev_otp_dp_' . md5( $phone ) ); if ( $phone_count >= $daily_phone ) { return new \WP_Error( 'daily_phone', __( 'Daily send limit for this number has been reached.', 'adev-otp-login' ) ); } $daily_ip = (int) Plugin::get_setting( 'daily_limit_ip', 20 ); $ip_count = (int) get_transient( 'adev_otp_di_' . md5( $ip ) ); if ( $ip_count >= $daily_ip ) { return new \WP_Error( 'daily_ip', __( 'Daily send limit for this IP has been reached.', 'adev-otp-login' ) ); } return true; } /** * Record a successful send for limits. * * @param string $phone Normalized phone. */ public static function record_send( string $phone ): void { $ip = self::client_ip(); $cooldown = (int) Plugin::get_setting( 'resend_cooldown', 60 ); set_transient( 'adev_otp_cd_' . md5( $phone ), time() + $cooldown, $cooldown ); self::bump_counter( 'adev_otp_dp_' . md5( $phone ), DAY_IN_SECONDS ); self::bump_counter( 'adev_otp_di_' . md5( $ip ), DAY_IN_SECONDS ); } /** * Increment a numeric transient under an atomic option lock. * * @param string $key Transient key. * @param int $ttl Expiration in seconds. */ private static function bump_counter( string $key, int $ttl ): void { self::with_lock( $key, static function () use ( $key, $ttl ) { $count = (int) get_transient( $key ); set_transient( $key, $count + 1, $ttl ); } ); } /** * Run a callback while holding a short-lived exclusive lock. * * `add_option()` is atomic in MySQL, so only one request creates the lock row. * * @param string $name Lock name (transient key or similar). * @param callable $cb Callback. * @return mixed Callback return value. */ public static function with_lock( string $name, callable $cb ) { $lock = '_transient_timeout_adev_otp_lk_' . md5( $name ); $acquired = false; for ( $i = 0; $i < 10; $i++ ) { // Autoload "no"; name reuses timeout prefix so clear_all() removes stale locks. if ( add_option( $lock, (string) ( time() + 10 ), '', 'no' ) ) { $acquired = true; break; } usleep( 30000 ); } try { return $cb(); } finally { if ( $acquired ) { delete_option( $lock ); } } } /** * Clear cooldown + daily counters for one phone. * * @param string $phone Raw or normalized phone. */ public static function clear_phone( string $phone ): void { $phone = Phone::normalize( $phone ); if ( ! $phone ) { return; } $hash = md5( $phone ); delete_transient( 'adev_otp_cd_' . $hash ); delete_transient( 'adev_otp_dp_' . $hash ); delete_transient( 'adev_otp_' . $hash ); } /** * Clear all OTP-related rate limits and pending codes. */ public static function clear_all(): void { global $wpdb; $patterns = array( $wpdb->esc_like( '_transient_adev_otp_' ) . '%', $wpdb->esc_like( '_transient_timeout_adev_otp_' ) . '%', $wpdb->esc_like( '_site_transient_adev_otp_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_adev_otp_' ) . '%', ); foreach ( $patterns as $like ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $like ) ); } } /** * Client IP. */ public static function client_ip(): string { $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '0.0.0.0'; /** * Filter client IP used for rate limiting. * * @param string $ip IP address. */ return (string) apply_filters( 'adev_otp_client_ip', $ip ); } }