get_boolean( 'common.force_master' ) ? 'm' : 'c'; $blog_home_url = preg_replace( '/[^a-zA-Z0-9\+\.%~!:()\/\-\_]/', '', $blog_home_url ); $blog_ids[ $blog_home_url ] = $w3tc_data . $GLOBALS['current_blog']->blog_id; $w3tc_data = json_encode( $blog_ids ); try { Util_File::file_put_contents_atomic( $filename, $w3tc_data ); } catch ( \Exception $ex ) { return false; } /** * Increment the per-IP counter only on a successful write. * Dedup short-circuits and write failures don't count against * the rate limit — legitimate writes are what we throttle. */ self::_rate_limit_record_write(); unset( self::$content_by_filename[ $filename ] ); unset( $GLOBALS['w3tc_blogmap_register_new_item'] ); return true; } /** * Compute the per-IP rate-limit transient key. * * The raw IP is salted with `wp_salt('nonce')` and hashed so the * persisted transient does not contain the client IP literal * (defense against transient-table dumps revealing the visitor * IP set). Returns empty string when no IP is detected (e.g. * CLI / WP-Cron without REMOTE_ADDR) — the caller treats empty * as "no rate limit applies" so legitimate non-HTTP contexts * are never throttled. * * @since 2.10.0 * * @return string Transient key, or '' when no IP available. */ private static function _ip_throttle_key() { $ip = Util_Environment::get_client_ip(); if ( '' === $ip ) { return ''; } $salt = \function_exists( 'wp_salt' ) ? \wp_salt( 'nonce' ) : ''; return self::RATE_LIMIT_TRANSIENT_PREFIX . \substr( \hash( 'sha256', $ip . '|' . $salt ), 0, 16 ); } /** * True when the current request's IP has not yet exhausted its * registration quota within the active window. Permits writes * when no IP can be derived (CLI, edge cases) so legitimate * non-HTTP contexts are never blocked. * * @since 2.10.0 * * @return bool */ private static function _rate_limit_allows_write() { $w3tc_key = self::_ip_throttle_key(); if ( '' === $w3tc_key ) { return true; } $w3tc_count = \get_transient( $w3tc_key ); if ( false === $w3tc_count || ! \is_numeric( $w3tc_count ) ) { $w3tc_count = 0; } return (int) $w3tc_count < self::RATE_LIMIT_MAX_PER_WINDOW; } /** * Increment the per-IP counter after a successful write. No-op * when no IP can be derived (matching the permissive policy in * {@see self::_rate_limit_allows_write()}). * * @since 2.10.0 * * @return void */ private static function _rate_limit_record_write() { $w3tc_key = self::_ip_throttle_key(); if ( '' === $w3tc_key ) { return; } $w3tc_count = \get_transient( $w3tc_key ); if ( false === $w3tc_count || ! \is_numeric( $w3tc_count ) ) { $w3tc_count = 0; } \set_transient( $w3tc_key, ( (int) $w3tc_count ) + 1, self::RATE_LIMIT_WINDOW_SECONDS ); } }