@@ -33,6 +33,7 @@ public static function init() {
3333
3434 \add_action ( 'activitypub_async_batch ' , array ( self ::class, 'async_batch ' ), 10 , 99 );
3535 \add_action ( 'activitypub_reprocess_outbox ' , array ( self ::class, 'reprocess_outbox ' ) );
36+ \add_action ( 'activitypub_outbox_purge ' , array ( self ::class, 'purge_outbox ' ) );
3637
3738 \add_action ( 'post_activitypub_add_to_outbox ' , array ( self ::class, 'schedule_outbox_activity_for_federation ' ) );
3839 \add_action ( 'post_activitypub_add_to_outbox ' , array ( self ::class, 'schedule_announce_activity ' ), 10 , 4 );
@@ -69,6 +70,10 @@ public static function register_schedules() {
6970 if ( ! \wp_next_scheduled ( 'activitypub_reprocess_outbox ' ) ) {
7071 \wp_schedule_event ( time (), 'hourly ' , 'activitypub_reprocess_outbox ' );
7172 }
73+
74+ if ( ! wp_next_scheduled ( 'activitypub_outbox_purge ' ) ) {
75+ wp_schedule_event ( time (), 'daily ' , 'activitypub_outbox_purge ' );
76+ }
7277 }
7378
7479 /**
@@ -80,6 +85,7 @@ public static function deregister_schedules() {
8085 wp_unschedule_hook ( 'activitypub_update_followers ' );
8186 wp_unschedule_hook ( 'activitypub_cleanup_followers ' );
8287 wp_unschedule_hook ( 'activitypub_reprocess_outbox ' );
88+ wp_unschedule_hook ( 'activitypub_outbox_purge ' );
8389 }
8490
8591 /**
@@ -199,6 +205,39 @@ public static function reprocess_outbox() {
199205 }
200206 }
201207
208+ /**
209+ * Purge outbox items based on a schedule.
210+ */
211+ public static function purge_outbox () {
212+ $ total_posts = (int ) wp_count_posts ( Outbox::POST_TYPE )->publish ;
213+ if ( $ total_posts <= 20 ) {
214+ return ;
215+ }
216+
217+ $ days = 180 ; // TODO: Replace with a setting.
218+ $ timezone = new \DateTimeZone ( 'UTC ' );
219+ $ date = new \DateTime ( 'now ' , $ timezone );
220+
221+ $ date ->sub ( \DateInterval::createFromDateString ( "$ days days " ) );
222+
223+ $ post_ids = get_posts (
224+ array (
225+ 'post_type ' => Outbox::POST_TYPE ,
226+ 'post_status ' => 'any ' ,
227+ 'fields ' => 'ids ' ,
228+ 'date_query ' => array (
229+ array (
230+ 'before ' => $ date ->format ( 'Y-m-d ' ),
231+ ),
232+ ),
233+ )
234+ );
235+
236+ foreach ( $ post_ids as $ post_id ) {
237+ \wp_delete_post ( $ post_id , true );
238+ }
239+ }
240+
202241 /**
203242 * Asynchronously runs batch processing routines.
204243 *
0 commit comments