PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / myyoast-client / application / exceptions / rate-limited-exception.php

rate-limited-exception.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at src/myyoast-client/application/exceptions/rate-limited-exception.php

79 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3
4 namespace Yoast\WP\SEO\MyYoast_Client\Application\Exceptions;
5
6 /**
7 * Exception thrown when the authorization server rate-limits a DCR or
8 * RFC 7592 request (HTTP 429).
9 *
10 * Extends Registration_Failed_Exception so existing catch sites keep working.
11 * Carries the parsed `Retry-After` value (in seconds) so the UI can show
12 * the user how long to wait.
13 */
14 class Rate_Limited_Exception extends Registration_Failed_Exception {
15
16 /**
17 * Number of seconds the caller should wait before retrying, or null
18 * when the server didn't send a usable `Retry-After` header.
19 *
20 * @var int|null
21 */
22 private $retry_after_seconds;
23
24 /**
25 * Rate_Limited_Exception constructor.
26 *
27 * @param string $message The exception message.
28 * @param int|null $retry_after_seconds Seconds until the caller may retry, or null.
29 */
30 public function __construct( string $message, ?int $retry_after_seconds = null ) {
31 parent::__construct( $message );
32 $this->retry_after_seconds = $retry_after_seconds;
33 }
34
35 /**
36 * Returns the parsed `Retry-After` value in seconds, or null when absent.
37 *
38 * @return int|null
39 */
40 public function get_retry_after_seconds(): ?int {
41 return $this->retry_after_seconds;
42 }
43
44 /**
45 * Parses an HTTP `Retry-After` value into seconds-until-retry.
46 *
47 * Accepts either the delta-seconds form (`"120"`) or the HTTP-date form
48 * (`"Wed, 27 May 2026 14:30:00 GMT"`) per RFC 9110 §10.2.3.
49 *
50 * phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint -- Raw header value is heterogeneous (string, numeric, array, or null).
51 *
52 * @param mixed $retry_after The raw header value (string, numeric, array, or null).
53 *
54 * @return int|null Seconds until retry (clamped to >= 0), or null when unparseable.
55 *
56 * phpcs:enable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint
57 */
58 public static function parse_retry_after( $retry_after ): ?int {
59 if ( \is_array( $retry_after ) ) {
60 $retry_after = \reset( $retry_after );
61 }
62
63 if ( $retry_after === null || $retry_after === '' ) {
64 return null;
65 }
66
67 if ( \is_numeric( $retry_after ) ) {
68 return \max( 0, (int) $retry_after );
69 }
70
71 $timestamp = \strtotime( (string) $retry_after );
72 if ( $timestamp === false ) {
73 return null;
74 }
75
76 return \max( 0, ( $timestamp - \time() ) );
77 }
78 }
79