Skip to content

Commit 80f8762

Browse files
authored
Merge pull request #1950 from hbhalodia/fix/issue-1948
Change default garbage collection TTL of `od_url_metrics` posts from 1 month to 3 months and add the filter to customize expiration
2 parents 48db4ba + 14bddaf commit 80f8762

File tree

4 files changed

+106
-4
lines changed

4 files changed

+106
-4
lines changed

plugins/optimization-detective/docs/hooks.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,19 @@ The ETag is a unique identifier that changes whenever the underlying data used t
367367
6. The list of active plugins.
368368

369369
A change in ETag means that any previously-collected URL Metrics will be immediately considered stale. When the ETag for URL Metrics in a complete viewport group no longer matches the current environment's ETag, new URL Metrics will then begin to be collected until there are no more stored URL Metrics with the old ETag.
370+
371+
### Filter: `od_url_metric_garbage_collection_ttl` (default: 3 months in seconds)
372+
373+
Filters the expiration age (TTL) after which an `od_url_metrics` post will be garbage collected if it has not been modified since that time.
374+
375+
```php
376+
add_filter( 'od_url_metric_garbage_collection_ttl', function (): int {
377+
return 6 * MONTH_IN_SECONDS;
378+
} );
379+
```
380+
381+
To prevent garbage collection of `od_url_metrics` posts, add a filter that returns zero:
382+
383+
```php
384+
add_filter( 'od_url_metric_garbage_collection_ttl', '__return_zero' );
385+
```

plugins/optimization-detective/docs/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ add_filter( 'query_vars', function ( $query_vars ) {
186186

187187
Then in your template code you may continue to use `isset( $_GET['thank_you'] )` but you might as well use `get_query_var( 'thank_you' )` instead.
188188

189-
When an `od_url_metrics` post has not been updated in a month then it is garbage-collected, since it is likely the original URL has gone away.
189+
When an `od_url_metrics` post has not been modified for three (3) months, it will then be garbage collected since it is likely the original URL for the metrics has gone away. If you have a site that gets very little traffic and you want to increase the TTL to prevent premature garbage collection, you can use the [`od_url_metric_garbage_collection_ttl`](https://github.com/WordPress/performance/blob/trunk/plugins/optimization-detective/docs/hooks.md#:~:text=Filter%3A%20od_url_metric_garbage_collection_ttl) filter to increase this.
190190

191191
Extensions to Optimization Detective rarely need to directly interface with the custom post type, so far. See the experimental [Optimization Detective Content Visibility](https://github.com/westonruter/od-content-visibility/) plugin which interfaces with the `od_url_metrics` post at submission time to add post meta via the `od_url_metric_stored` action, and then retrieves post meta in the tag visitor via the context object’s `url_metrics_id` property.
192192

plugins/optimization-detective/storage/class-od-url-metrics-post-type.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,29 @@ public static function schedule_garbage_collection(): void {
298298
* @since 0.1.0
299299
*/
300300
public static function delete_stale_posts(): void {
301-
$one_month_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-1 month' ) );
301+
/**
302+
* Filters the expiration time (TTL) after which a since-unmodified od_url_metrics post will be garbage collected.
303+
*
304+
* @since n.e.x.t
305+
* @link https://github.com/WordPress/performance/blob/trunk/plugins/optimization-detective/docs/hooks.md#:~:text=Filter%3A%20od_url_metric_garbage_collection_ttl
306+
*
307+
* @return int TTL for garbage collection in seconds. Defaults to 3 months.
308+
*/
309+
$ttl = (int) apply_filters( 'od_url_metric_garbage_collection_ttl', 3 * MONTH_IN_SECONDS );
310+
311+
if ( $ttl <= 0 ) {
312+
return;
313+
}
314+
315+
$before_time = gmdate( 'Y-m-d H:i:s', time() - $ttl );
302316

303317
$query = new WP_Query(
304318
array(
305319
'post_type' => self::SLUG,
306320
'posts_per_page' => 100,
307321
'date_query' => array(
308322
'column' => 'post_modified_gmt',
309-
'before' => $one_month_ago,
323+
'before' => $before_time,
310324
),
311325
)
312326
);

plugins/optimization-detective/tests/storage/test-class-od-url-metrics-post-type.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public function test_schedule_garbage_collection_reschedule(): void {
288288
public function test_delete_stale_posts(): void {
289289
global $wpdb;
290290

291-
$stale_timestamp_gmt = gmdate( 'Y-m-d H:i:s', strtotime( '-1 month' ) - HOUR_IN_SECONDS );
291+
$stale_timestamp_gmt = gmdate( 'Y-m-d H:i:s', time() - ( 3 * MONTH_IN_SECONDS + 1 ) );
292292

293293
$new_generic_post = self::factory()->post->create();
294294
$old_generic_post = self::factory()->post->create();
@@ -323,6 +323,78 @@ public function test_delete_stale_posts(): void {
323323
$this->assertInstanceOf( WP_Post::class, get_post( $old_generic_post ), 'Expected old generic post to not have been deleted.' );
324324
$this->assertInstanceOf( WP_Post::class, get_post( $new_url_metrics_post ), 'Expected new URL Metrics post to not have been deleted.' );
325325
$this->assertNull( get_post( $old_url_metrics_post ), 'Expected old URL Metrics post to have been deleted.' );
326+
327+
// Remove the URL Metrics post older than 6 months.
328+
add_filter(
329+
'od_url_metric_garbage_collection_ttl',
330+
static function (): int {
331+
return 6 * MONTH_IN_SECONDS;
332+
}
333+
);
334+
335+
// Update timestamp to 6 months older.
336+
$new_stale_timestamp_gmt = gmdate( 'Y-m-d H:i:s', time() - ( 6 * MONTH_IN_SECONDS + 1 ) );
337+
338+
$older_generic_post = self::factory()->post->create();
339+
$wpdb->update(
340+
$wpdb->posts,
341+
array(
342+
'post_modified' => get_date_from_gmt( $new_stale_timestamp_gmt ),
343+
'post_modified_gmt' => $new_stale_timestamp_gmt,
344+
),
345+
array( 'ID' => $older_generic_post )
346+
);
347+
clean_post_cache( $older_generic_post );
348+
349+
$older_url_metrics_slug = od_get_url_metrics_slug( array( 'p' => $older_generic_post ) );
350+
$older_url_metrics_post = $this->store_url_metric( $older_url_metrics_slug, $this->get_sample_url_metric( array( 'url' => get_permalink( $older_generic_post ) ) ) );
351+
$wpdb->update(
352+
$wpdb->posts,
353+
array(
354+
'post_modified' => get_date_from_gmt( $new_stale_timestamp_gmt ),
355+
'post_modified_gmt' => $new_stale_timestamp_gmt,
356+
),
357+
array( 'ID' => $older_url_metrics_post )
358+
);
359+
clean_post_cache( $older_url_metrics_post );
360+
361+
// Now we delete the stale URL Metrics.
362+
OD_URL_Metrics_Post_Type::delete_stale_posts();
363+
364+
$this->assertInstanceOf( WP_Post::class, get_post( $older_generic_post ), 'Expected old generic post to not have been deleted.' );
365+
$this->assertNull( get_post( $older_url_metrics_post ), 'Expected old URL Metrics post to not have been deleted.' );
366+
367+
// Prevent garbage collection to happen.
368+
add_filter( 'od_url_metric_garbage_collection_ttl', '__return_zero' );
369+
370+
$generic_post = self::factory()->post->create();
371+
$wpdb->update(
372+
$wpdb->posts,
373+
array(
374+
'post_modified' => get_date_from_gmt( $new_stale_timestamp_gmt ),
375+
'post_modified_gmt' => $new_stale_timestamp_gmt,
376+
),
377+
array( 'ID' => $generic_post )
378+
);
379+
clean_post_cache( $generic_post );
380+
381+
$url_metrics_slug = od_get_url_metrics_slug( array( 'p' => $generic_post ) );
382+
$url_metrics_post = $this->store_url_metric( $url_metrics_slug, $this->get_sample_url_metric( array( 'url' => get_permalink( $generic_post ) ) ) );
383+
$wpdb->update(
384+
$wpdb->posts,
385+
array(
386+
'post_modified' => get_date_from_gmt( $new_stale_timestamp_gmt ),
387+
'post_modified_gmt' => $new_stale_timestamp_gmt,
388+
),
389+
array( 'ID' => $url_metrics_post )
390+
);
391+
clean_post_cache( $url_metrics_post );
392+
393+
// Now we delete the stale URL Metrics.
394+
OD_URL_Metrics_Post_Type::delete_stale_posts();
395+
396+
$this->assertInstanceOf( WP_Post::class, get_post( $generic_post ), 'Expected old generic post to not have been deleted.' );
397+
$this->assertNotNull( get_post( $url_metrics_post ), 'Expected old URL Metrics post to not have been deleted.' );
326398
}
327399

328400
/**

0 commit comments

Comments
 (0)