1212use Activitypub \Collection \Actors ;
1313use Activitypub \Collection \Inbox ;
1414use Activitypub \Collection \Outbox ;
15+ use Activitypub \Collection \Posts ;
1516use Activitypub \Collection \Remote_Actors ;
1617use Activitypub \Scheduler \Actor ;
1718use Activitypub \Scheduler \Collection_Sync ;
@@ -61,14 +62,16 @@ public static function init() {
6162 \add_action ( 'activitypub_reprocess_outbox ' , array ( self ::class, 'reprocess_outbox ' ) );
6263 \add_action ( 'activitypub_outbox_purge ' , array ( self ::class, 'purge_outbox ' ) );
6364 \add_action ( 'activitypub_inbox_purge ' , array ( self ::class, 'purge_inbox ' ) );
65+ \add_action ( 'activitypub_ap_post_purge ' , array ( self ::class, 'purge_ap_posts ' ) );
6466 \add_action ( 'activitypub_inbox_create_item ' , array ( self ::class, 'process_inbox_activity ' ) );
6567 \add_action ( 'activitypub_sync_blocklist_subscriptions ' , array ( Blocklist_Subscriptions::class, 'sync_all ' ) );
6668
6769 \add_action ( 'post_activitypub_add_to_outbox ' , array ( self ::class, 'schedule_outbox_activity_for_federation ' ) );
6870 \add_action ( 'post_activitypub_add_to_outbox ' , array ( self ::class, 'schedule_announce_activity ' ), 10 , 4 );
6971
70- \add_action ( 'update_option_activitypub_outbox_purge_days ' , array ( self ::class, 'handle_outbox_purge_days_update ' ), 10 , 2 );
71- \add_action ( 'update_option_activitypub_inbox_purge_days ' , array ( self ::class, 'handle_inbox_purge_days_update ' ), 10 , 2 );
72+ \add_action ( 'update_option_activitypub_outbox_purge_days ' , array ( self ::class, 'update_outbox_purge_schedule ' ), 10 , 2 );
73+ \add_action ( 'update_option_activitypub_inbox_purge_days ' , array ( self ::class, 'update_inbox_purge_schedule ' ), 10 , 2 );
74+ \add_action ( 'update_option_activitypub_ap_post_purge_days ' , array ( self ::class, 'update_ap_post_purge_schedule ' ), 10 , 2 );
7275 }
7376
7477 /**
@@ -134,6 +137,10 @@ public static function register_schedules() {
134137 \wp_schedule_event ( time (), 'daily ' , 'activitypub_inbox_purge ' );
135138 }
136139
140+ if ( ! \wp_next_scheduled ( 'activitypub_ap_post_purge ' ) ) {
141+ \wp_schedule_event ( time (), 'daily ' , 'activitypub_ap_post_purge ' );
142+ }
143+
137144 if ( ! \wp_next_scheduled ( 'activitypub_sync_blocklist_subscriptions ' ) ) {
138145 \wp_schedule_event ( time (), 'weekly ' , 'activitypub_sync_blocklist_subscriptions ' );
139146 }
@@ -150,6 +157,7 @@ public static function deregister_schedules() {
150157 \wp_unschedule_hook ( 'activitypub_reprocess_outbox ' );
151158 \wp_unschedule_hook ( 'activitypub_outbox_purge ' );
152159 \wp_unschedule_hook ( 'activitypub_inbox_purge ' );
160+ \wp_unschedule_hook ( 'activitypub_ap_post_purge ' );
153161 \wp_unschedule_hook ( 'activitypub_sync_blocklist_subscriptions ' );
154162 }
155163
@@ -315,66 +323,24 @@ public static function reprocess_outbox() {
315323 * Purge outbox items based on a schedule.
316324 */
317325 public static function purge_outbox () {
318- $ total_posts = (int ) wp_count_posts ( Outbox::POST_TYPE )->publish ;
319- if ( $ total_posts <= 20 ) {
320- return ;
321- }
322-
323- $ days = (int ) get_option ( 'activitypub_outbox_purge_days ' , 180 );
324- $ post_ids = \get_posts (
325- array (
326- 'post_type ' => Outbox::POST_TYPE ,
327- 'post_status ' => 'any ' ,
328- 'fields ' => 'ids ' ,
329- 'numberposts ' => -1 ,
330- 'date_query ' => array (
331- array (
332- 'before ' => gmdate ( 'Y-m-d ' , time () - ( $ days * DAY_IN_SECONDS ) ),
333- ),
334- ),
335- // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
336- 'meta_query ' => array (
337- array (
338- 'key ' => '_activitypub_activity_type ' ,
339- 'value ' => 'Follow ' ,
340- 'compare ' => '!= ' ,
341- ),
342- ),
343- )
344- );
345-
346- foreach ( $ post_ids as $ post_id ) {
347- \wp_delete_post ( $ post_id , true );
348- }
326+ $ days = (int ) \get_option ( 'activitypub_outbox_purge_days ' , 180 );
327+ Outbox::purge ( $ days );
349328 }
350329
351330 /**
352331 * Purge inbox items based on a schedule.
353332 */
354333 public static function purge_inbox () {
355- $ total_posts = (int ) wp_count_posts ( Inbox::POST_TYPE )->publish ;
356- if ( $ total_posts <= 200 ) {
357- return ;
358- }
359-
360- $ days = (int ) get_option ( 'activitypub_inbox_purge_days ' , 180 );
361- $ post_ids = \get_posts (
362- array (
363- 'post_type ' => Inbox::POST_TYPE ,
364- 'post_status ' => 'any ' ,
365- 'fields ' => 'ids ' ,
366- 'numberposts ' => -1 ,
367- 'date_query ' => array (
368- array (
369- 'before ' => gmdate ( 'Y-m-d ' , time () - ( $ days * DAY_IN_SECONDS ) ),
370- ),
371- ),
372- )
373- );
334+ $ days = (int ) \get_option ( 'activitypub_inbox_purge_days ' , 180 );
335+ Inbox::purge ( $ days );
336+ }
374337
375- foreach ( $ post_ids as $ post_id ) {
376- \wp_delete_post ( $ post_id , true );
377- }
338+ /**
339+ * Purge remote posts based on a schedule.
340+ */
341+ public static function purge_ap_posts () {
342+ $ days = (int ) \get_option ( 'activitypub_ap_post_purge_days ' , 30 );
343+ Posts::purge ( $ days );
378344 }
379345
380346 /**
@@ -430,7 +396,7 @@ public static function process_inbox_activity( $activity_id ) {
430396 * @param int $old_value The old value.
431397 * @param int $value The new value.
432398 */
433- public static function handle_outbox_purge_days_update ( $ old_value , $ value ) {
399+ public static function update_outbox_purge_schedule ( $ old_value , $ value ) {
434400 if ( 0 === (int ) $ value ) {
435401 \wp_clear_scheduled_hook ( 'activitypub_outbox_purge ' );
436402 } elseif ( ! \wp_next_scheduled ( 'activitypub_outbox_purge ' ) ) {
@@ -444,14 +410,28 @@ public static function handle_outbox_purge_days_update( $old_value, $value ) {
444410 * @param int $old_value The old value.
445411 * @param int $value The new value.
446412 */
447- public static function handle_inbox_purge_days_update ( $ old_value , $ value ) {
413+ public static function update_inbox_purge_schedule ( $ old_value , $ value ) {
448414 if ( 0 === (int ) $ value ) {
449415 \wp_clear_scheduled_hook ( 'activitypub_inbox_purge ' );
450416 } elseif ( ! \wp_next_scheduled ( 'activitypub_inbox_purge ' ) ) {
451417 \wp_schedule_event ( \time (), 'daily ' , 'activitypub_inbox_purge ' );
452418 }
453419 }
454420
421+ /**
422+ * Update schedules when remote posts purge days settings change.
423+ *
424+ * @param int $old_value The old value.
425+ * @param int $value The new value.
426+ */
427+ public static function update_ap_post_purge_schedule ( $ old_value , $ value ) {
428+ if ( 0 === (int ) $ value ) {
429+ \wp_clear_scheduled_hook ( 'activitypub_ap_post_purge ' );
430+ } elseif ( ! \wp_next_scheduled ( 'activitypub_ap_post_purge ' ) ) {
431+ \wp_schedule_event ( \time (), 'daily ' , 'activitypub_ap_post_purge ' );
432+ }
433+ }
434+
455435 /**
456436 * Asynchronously runs batch processing routines.
457437 *
0 commit comments