32 hex chars; cryptographically strong. $token = bin2hex( random_bytes( 16 ) ); } catch ( \Exception $e ) { /** * Random_bytes() only throws if the OS RNG is unavailable. * Fall back to wp_generate_password but normalise so the * output still matches the strict /^[a-f0-9]{32}$/ shape * the consume side requires. */ $raw = \wp_generate_password( 16, false, false ); $token = strtolower( bin2hex( substr( $raw, 0, 16 ) ) ); } \set_site_transient( $prefix . $token, '1', self::TTL ); return $token; } /** * Validate the inbound probe token and consume it on success so it * cannot be replayed. * * @since 2.10.0 * * @param string $prefix Transient-key prefix used at issue * time, e.g. `w3tc_pgcache_probe_`. * @param string $header_name HTTP header carrying the token, * e.g. `X-W3TC-PgCache-Probe`. Case- * insensitive; PHP normalises to the * `HTTP_X_W3TC_PGCACHE_PROBE` env var. * * @return bool True if the request presents a matching probe token. */ public static function consume( $prefix, $header_name ) { $w3tc_key = 'HTTP_' . strtoupper( str_replace( '-', '_', $header_name ) ); if ( empty( $_SERVER[ $w3tc_key ] ) ) { return false; } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- value is normalised by the strict /^[a-f0-9]{32}$/ regex on the next non-comment line. $presented = trim( (string) \wp_unslash( $_SERVER[ $w3tc_key ] ) ); if ( ! preg_match( '/^[a-f0-9]{32}$/', $presented ) ) { return false; } /** * Token IS the transient key. Lookup is the validation: a * missing transient (false) means the token is invalid, has * expired, or has already been consumed. The hex-format check * above bounds the lookup keyspace; the unguessable random * token bounds the probability that a guess collides with a * live issued token. */ $tkey = $prefix . $presented; $valid = \get_site_transient( $tkey ); if ( false === $valid ) { return false; } \delete_site_transient( $tkey ); return true; } /** * Emit a 404-style response and exit. Standard rejection used by * probe endpoints when {@see consume()} fails. * * @since 2.10.0 * * @return void */ public static function reject() { if ( ! \headers_sent() ) { \status_header( 404 ); \nocache_headers(); } exit(); } }