Skip to content

Commit f2c85f0

Browse files
committed
Allow freshness TTL to be disabled
Signed-off-by: Shyamsundar Gadde <[email protected]>
1 parent 26442b9 commit f2c85f0

File tree

3 files changed

+13
-52
lines changed

3 files changed

+13
-52
lines changed

plugins/optimization-detective/class-od-url-metric-group-collection.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ final class OD_URL_Metric_Group_Collection implements Countable, IteratorAggrega
7373
* A freshness age of zero means a URL Metric will always be considered stale.
7474
*
7575
* @since 0.1.0
76-
* @var int<0, max>
76+
* @var int<-1, max>
7777
*/
7878
private $freshness_ttl;
7979

@@ -104,7 +104,7 @@ final class OD_URL_Metric_Group_Collection implements Countable, IteratorAggrega
104104
*
105105
* @phpstan-param positive-int[] $breakpoints
106106
* @phpstan-param int<1, max> $sample_size
107-
* @phpstan-param int<0, max> $freshness_ttl
107+
* @phpstan-param int<-1, max> $freshness_ttl
108108
*
109109
* @param OD_URL_Metric[] $url_metrics URL Metrics.
110110
* @param non-empty-string $current_etag The current ETag.
@@ -168,17 +168,6 @@ public function __construct( array $url_metrics, string $current_etag, array $br
168168
$this->sample_size = $sample_size;
169169

170170
// Set freshness TTL.
171-
if ( $freshness_ttl < 0 ) {
172-
throw new InvalidArgumentException(
173-
esc_html(
174-
sprintf(
175-
/* translators: %d is the invalid sample size */
176-
__( 'Freshness TTL must be at least zero, but provided: %d', 'optimization-detective' ),
177-
$freshness_ttl
178-
)
179-
)
180-
);
181-
}
182171
$this->freshness_ttl = $freshness_ttl;
183172

184173
// Create groups and the URL Metrics to them.
@@ -226,7 +215,7 @@ public function get_sample_size(): int {
226215
*
227216
* @since 1.0.0
228217
*
229-
* @return int<0, max> Freshness age (TTL) for a given URL Metric.
218+
* @return int<-1, max> Freshness age (TTL) for a given URL Metric.
230219
*/
231220
public function get_freshness_ttl(): int {
232221
return $this->freshness_ttl;
@@ -702,7 +691,7 @@ public function count(): int {
702691
* @return array{
703692
* current_etag: non-empty-string,
704693
* breakpoints: positive-int[],
705-
* freshness_ttl: 0|positive-int,
694+
* freshness_ttl: int<-1, max>,
706695
* sample_size: positive-int,
707696
* all_element_max_intersection_ratios: array<string, float>,
708697
* common_lcp_element: ?OD_Element,

plugins/optimization-detective/class-od-url-metric-group.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ final class OD_URL_Metric_Group implements IteratorAggregate, Countable, JsonSer
6262
*
6363
* @since 0.1.0
6464
*
65-
* @var int<0, max>
65+
* @var int<-1, max>
6666
*/
6767
private $freshness_ttl;
6868

@@ -102,7 +102,7 @@ final class OD_URL_Metric_Group implements IteratorAggregate, Countable, JsonSer
102102
* @phpstan-param int<0, max> $minimum_viewport_width
103103
* @phpstan-param int<1, max>|null $maximum_viewport_width
104104
* @phpstan-param int<1, max> $sample_size
105-
* @phpstan-param int<0, max> $freshness_ttl
105+
* @phpstan-param int<-1, max> $freshness_ttl
106106
*
107107
* @param OD_URL_Metric[] $url_metrics URL Metrics to add to the group.
108108
* @param int $minimum_viewport_width Minimum possible viewport width (exclusive) for the group. Must be zero or greater.
@@ -145,17 +145,6 @@ public function __construct( array $url_metrics, int $minimum_viewport_width, ?i
145145
}
146146
$this->sample_size = $sample_size;
147147

148-
if ( $freshness_ttl < 0 ) {
149-
throw new InvalidArgumentException(
150-
esc_html(
151-
sprintf(
152-
/* translators: %d is the invalid sample size */
153-
__( 'Freshness TTL must be at least zero, but provided: %d', 'optimization-detective' ),
154-
$freshness_ttl
155-
)
156-
)
157-
);
158-
}
159148
$this->freshness_ttl = $freshness_ttl;
160149
$this->collection = $collection;
161150
$this->url_metrics = $url_metrics;
@@ -203,7 +192,7 @@ public function get_sample_size(): int {
203192
* @since 0.9.0
204193
*
205194
* @todo Eliminate in favor of readonly public property.
206-
* @return int<0, max> Freshness age.
195+
* @return int<-1, max> Freshness age.
207196
*/
208197
public function get_freshness_ttl(): int {
209198
return $this->freshness_ttl;
@@ -299,8 +288,8 @@ public function is_complete(): bool {
299288
}
300289
$current_time = microtime( true );
301290
foreach ( $this->url_metrics as $url_metric ) {
302-
// The URL Metric is too old to be fresh.
303-
if ( $current_time > $url_metric->get_timestamp() + $this->freshness_ttl ) {
291+
// The URL Metric is too old to be fresh (skip if freshness TTL is negative).
292+
if ( $this->freshness_ttl >= 0 && $current_time > $url_metric->get_timestamp() + $this->freshness_ttl ) {
304293
return false;
305294
}
306295

@@ -514,7 +503,7 @@ public function clear_cache(): void {
514503
* @since 0.3.1
515504
*
516505
* @return array{
517-
* freshness_ttl: 0|positive-int,
506+
* freshness_ttl: int<-1, max>,
518507
* sample_size: positive-int,
519508
* minimum_viewport_width: int<0, max>,
520509
* maximum_viewport_width: int<1, max>|null,

plugins/optimization-detective/storage/data.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @since 0.1.0
2121
* @access private
2222
*
23-
* @return int<0, max> Expiration TTL in seconds.
23+
* @return int<-1, max> Expiration TTL in seconds.
2424
*/
2525
function od_get_url_metric_freshness_ttl(): int {
2626
/**
@@ -31,26 +31,9 @@ function od_get_url_metric_freshness_ttl(): int {
3131
*
3232
* @since 0.1.0
3333
*
34-
* @param int $ttl Expiration TTL in seconds. Defaults to 1 week.
34+
* @param int<-1, max> $ttl Expiration TTL in seconds. Defaults to 1 week.
3535
*/
36-
$freshness_ttl = (int) apply_filters( 'od_url_metric_freshness_ttl', WEEK_IN_SECONDS );
37-
38-
if ( $freshness_ttl < 0 ) {
39-
_doing_it_wrong(
40-
esc_html( "Filter: 'od_url_metric_freshness_ttl'" ),
41-
esc_html(
42-
sprintf(
43-
/* translators: %s is the TTL freshness */
44-
__( 'Freshness TTL must be at least zero, but saw "%s".', 'optimization-detective' ),
45-
$freshness_ttl
46-
)
47-
),
48-
''
49-
);
50-
$freshness_ttl = 0;
51-
}
52-
53-
return $freshness_ttl;
36+
return (int) apply_filters( 'od_url_metric_freshness_ttl', WEEK_IN_SECONDS );
5437
}
5538

5639
/**

0 commit comments

Comments
 (0)