Skip to content

Commit 5fa9356

Browse files
committed
Add WP-Cron cleanup for wp_sync_updates table
Schedule a daily cron job to delete sync update rows older than 1 day, preventing unbounded table growth from abandoned collaborative editing sessions. The expiration is filterable via the wp_sync_updates_expiration hook.
1 parent cdd8335 commit 5fa9356

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/wp-admin/admin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@
113113
wp_schedule_event( time(), 'daily', 'delete_expired_transients' );
114114
}
115115

116+
// Schedule sync updates cleanup.
117+
if ( ! wp_next_scheduled( 'wp_delete_old_sync_updates' ) && ! wp_installing() ) {
118+
wp_schedule_event( time(), 'daily', 'wp_delete_old_sync_updates' );
119+
}
120+
116121
set_screen_options();
117122

118123
$date_format = __( 'F j, Y' );

src/wp-includes/collaboration.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,34 @@ function wp_collaboration_inject_setting() {
2222
);
2323
}
2424
}
25+
26+
/**
27+
* Deletes sync updates older than 1 day from the wp_sync_updates table.
28+
*
29+
* Rows left behind by abandoned collaborative editing sessions are cleaned up
30+
* to prevent unbounded table growth.
31+
*
32+
* @since 7.0.0
33+
*/
34+
function wp_delete_old_sync_updates() {
35+
global $wpdb;
36+
37+
/**
38+
* Filters the lifetime, in seconds, of a sync update row.
39+
*
40+
* By default, the lifetime is 1 day. Once a row reaches that age, it will
41+
* automatically be deleted by a cron job.
42+
*
43+
* @since 7.0.0
44+
*
45+
* @param int $expiration The expiration age of a sync update row, in seconds.
46+
*/
47+
$expiration = apply_filters( 'wp_sync_updates_expiration', DAY_IN_SECONDS );
48+
49+
$wpdb->query(
50+
$wpdb->prepare(
51+
"DELETE FROM {$wpdb->sync_updates} WHERE created_at < %s",
52+
gmdate( 'Y-m-d H:i:s', time() - $expiration )
53+
)
54+
);
55+
}

src/wp-includes/default-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@
454454
add_action( 'importer_scheduled_cleanup', 'wp_delete_attachment' );
455455
add_action( 'upgrader_scheduled_cleanup', 'wp_delete_attachment' );
456456
add_action( 'delete_expired_transients', 'delete_expired_transients' );
457+
add_action( 'wp_delete_old_sync_updates', 'wp_delete_old_sync_updates' );
457458

458459
// Navigation menu actions.
459460
add_action( 'delete_post', '_wp_delete_post_menu_item' );

0 commit comments

Comments
 (0)