abstracts
3 years ago
admin
3 years ago
blocks
5 years ago
cli
4 years ago
customizer
4 years ago
data-stores
4 years ago
emails
4 years ago
export
4 years ago
gateways
3 years ago
import
4 years ago
integrations
4 years ago
interfaces
4 years ago
legacy
4 years ago
libraries
6 years ago
log-handlers
4 years ago
payment-tokens
5 years ago
queue
4 years ago
react-admin
3 years ago
rest-api
4 years ago
shipping
4 years ago
shortcodes
4 years ago
theme-support
4 years ago
tracks
4 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
4 years ago
class-wc-ajax.php
4 years ago
class-wc-api.php
4 years ago
class-wc-auth.php
4 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
4 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
4 years ago
class-wc-cart-totals.php
4 years ago
class-wc-cart.php
4 years ago
class-wc-checkout.php
4 years ago
class-wc-cli.php
4 years ago
class-wc-comments.php
4 years ago
class-wc-countries.php
4 years ago
class-wc-coupon.php
4 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
4 years ago
class-wc-customer.php
4 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
4 years ago
class-wc-discounts.php
4 years ago
class-wc-download-handler.php
4 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
4 years ago
class-wc-frontend-scripts.php
4 years ago
class-wc-geo-ip.php
4 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
4 years ago
class-wc-https.php
4 years ago
class-wc-install.php
4 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
4 years ago
class-wc-meta-data.php
4 years ago
class-wc-order-factory.php
5 years ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
4 years ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
4 years ago
class-wc-order-item-shipping.php
4 years ago
class-wc-order-item-tax.php
4 years ago
class-wc-order-item.php
4 years ago
class-wc-order-query.php
4 years ago
class-wc-order-refund.php
4 years ago
class-wc-order.php
4 years ago
class-wc-payment-gateways.php
4 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
4 years ago
class-wc-post-types.php
4 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
4 years ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
4 years ago
class-wc-product-attribute.php
4 years ago
class-wc-product-download.php
4 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 years ago
class-wc-product-variable.php
4 years ago
class-wc-product-variation.php
4 years ago
class-wc-query.php
4 years ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
4 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
4 years ago
class-wc-shipping-rate.php
4 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
4 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
4 years ago
class-wc-template-loader.php
4 years ago
class-wc-tracker.php
4 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
4 years ago
wc-account-functions.php
5 years ago
wc-attribute-functions.php
4 years ago
wc-cart-functions.php
4 years ago
wc-conditional-functions.php
4 years ago
wc-core-functions.php
4 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
4 years ago
wc-formatting-functions.php
4 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
4 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
4 years ago
wc-rest-functions.php
4 years ago
wc-stock-functions.php
4 years ago
wc-template-functions.php
4 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
4 years ago
wc-update-functions.php
4 years ago
wc-user-functions.php
4 years ago
wc-webhook-functions.php
4 years ago
wc-widget-functions.php
5 years ago
class-wc-rate-limiter.php
174 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Provide basic rate limiting functionality via WP Options API. |
| 4 | * |
| 5 | * Currently only provides a simple limit by delaying action by X seconds. |
| 6 | * |
| 7 | * Example usage: |
| 8 | * |
| 9 | * When an action runs, call set_rate_limit, e.g.: |
| 10 | * |
| 11 | * WC_Rate_Limiter::set_rate_limit( "{$my_action_name}_{$user_id}", $delay ); |
| 12 | * |
| 13 | * This sets a timestamp for future timestamp after which action can run again. |
| 14 | * |
| 15 | * |
| 16 | * Then before running the action again, check if the action is allowed to run, e.g.: |
| 17 | * |
| 18 | * if ( WC_Rate_Limiter::retried_too_soon( "{$my_action_name}_{$user_id}" ) ) { |
| 19 | * add_notice( 'Sorry, too soon!' ); |
| 20 | * } |
| 21 | * |
| 22 | * @package WooCommerce\Classes |
| 23 | * @version 3.9.0 |
| 24 | * @since 3.9.0 |
| 25 | */ |
| 26 | |
| 27 | defined( 'ABSPATH' ) || exit; |
| 28 | |
| 29 | /** |
| 30 | * Rate limit class. |
| 31 | */ |
| 32 | class WC_Rate_Limiter { |
| 33 | |
| 34 | /** |
| 35 | * Cache group. |
| 36 | */ |
| 37 | const CACHE_GROUP = 'wc_rate_limit'; |
| 38 | |
| 39 | /** |
| 40 | * Hook in methods. |
| 41 | */ |
| 42 | public static function init() { |
| 43 | add_action( 'woocommerce_cleanup_rate_limits', array( __CLASS__, 'cleanup' ) ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Constructs key name from action identifier. |
| 48 | * Left in for backwards compatibility. |
| 49 | * |
| 50 | * @param string $action_id Identifier of the action. |
| 51 | * @return string |
| 52 | */ |
| 53 | public static function storage_id( $action_id ) { |
| 54 | return $action_id; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Gets a cache prefix. |
| 59 | * |
| 60 | * @param string $action_id Identifier of the action. |
| 61 | * @return string |
| 62 | */ |
| 63 | protected static function get_cache_key( $action_id ) { |
| 64 | return WC_Cache_Helper::get_cache_prefix( 'rate_limit' . $action_id ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Retrieve a cached rate limit. |
| 69 | * |
| 70 | * @param string $action_id Identifier of the action. |
| 71 | * @return bool|int |
| 72 | */ |
| 73 | protected static function get_cached( $action_id ) { |
| 74 | return wp_cache_get( self::get_cache_key( $action_id ), self::CACHE_GROUP ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Cache a rate limit. |
| 79 | * |
| 80 | * @param string $action_id Identifier of the action. |
| 81 | * @param int $expiry Timestamp when the limit expires. |
| 82 | * @return bool |
| 83 | */ |
| 84 | protected static function set_cache( $action_id, $expiry ) { |
| 85 | return wp_cache_set( self::get_cache_key( $action_id ), $expiry, self::CACHE_GROUP ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns true if the action is not allowed to be run by the rate limiter yet, false otherwise. |
| 90 | * |
| 91 | * @param string $action_id Identifier of the action. |
| 92 | * @return bool |
| 93 | */ |
| 94 | public static function retried_too_soon( $action_id ) { |
| 95 | global $wpdb; |
| 96 | |
| 97 | $next_try_allowed_at = self::get_cached( $action_id ); |
| 98 | |
| 99 | if ( false === $next_try_allowed_at ) { |
| 100 | $next_try_allowed_at = $wpdb->get_var( |
| 101 | $wpdb->prepare( |
| 102 | " |
| 103 | SELECT rate_limit_expiry |
| 104 | FROM {$wpdb->prefix}wc_rate_limits |
| 105 | WHERE rate_limit_key = %s |
| 106 | ", |
| 107 | $action_id |
| 108 | ) |
| 109 | ); |
| 110 | |
| 111 | self::set_cache( $action_id, $next_try_allowed_at ); |
| 112 | } |
| 113 | |
| 114 | // No record of action running, so action is allowed to run. |
| 115 | if ( null === $next_try_allowed_at ) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // Before the next run is allowed, retry forbidden. |
| 120 | if ( time() <= $next_try_allowed_at ) { |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | // After the next run is allowed, retry allowed. |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Sets the rate limit delay in seconds for action with identifier $id. |
| 130 | * |
| 131 | * @param string $action_id Identifier of the action. |
| 132 | * @param int $delay Delay in seconds. |
| 133 | * @return bool True if the option setting was successful, false otherwise. |
| 134 | */ |
| 135 | public static function set_rate_limit( $action_id, $delay ) { |
| 136 | global $wpdb; |
| 137 | |
| 138 | $next_try_allowed_at = time() + $delay; |
| 139 | |
| 140 | $result = $wpdb->replace( |
| 141 | $wpdb->prefix . 'wc_rate_limits', |
| 142 | array( |
| 143 | 'rate_limit_key' => $action_id, |
| 144 | 'rate_limit_expiry' => $next_try_allowed_at, |
| 145 | ), |
| 146 | array( '%s', '%d' ) |
| 147 | ); |
| 148 | |
| 149 | self::set_cache( $action_id, $next_try_allowed_at ); |
| 150 | |
| 151 | return false !== $result; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Cleanup expired rate limits from the database and clear caches. |
| 156 | */ |
| 157 | public static function cleanup() { |
| 158 | global $wpdb; |
| 159 | |
| 160 | $wpdb->query( |
| 161 | $wpdb->prepare( |
| 162 | "DELETE FROM {$wpdb->prefix}wc_rate_limits WHERE rate_limit_expiry < %d", |
| 163 | time() |
| 164 | ) |
| 165 | ); |
| 166 | |
| 167 | if ( class_exists( 'WC_Cache_Helper' ) ) { |
| 168 | WC_Cache_Helper::invalidate_cache_group( self::CACHE_GROUP ); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | WC_Rate_Limiter::init(); |
| 174 |