@@ -24,11 +24,12 @@ class Migration {
2424 * Initialize the class, registering WordPress hooks.
2525 */
2626 public static function init () {
27- \add_action ( 'activitypub_migrate ' , array ( self ::class, 'async_migration ' ) );
28- \add_action ( 'activitypub_upgrade ' , array ( self ::class, 'async_upgrade ' ), 10 , 99 );
29- \add_action ( 'activitypub_update_comment_counts ' , array ( self ::class, 'update_comment_counts ' ), 10 , 2 );
30-
3127 self ::maybe_migrate ();
28+
29+ Scheduler::register_async_batch_callback ( 'activitypub_migrate_from_0_17 ' , array ( self ::class, 'migrate_from_0_17 ' ) );
30+ Scheduler::register_async_batch_callback ( 'activitypub_update_comment_counts ' , array ( self ::class, 'update_comment_counts ' ) );
31+ Scheduler::register_async_batch_callback ( 'activitypub_create_post_outbox_items ' , array ( self ::class, 'create_post_outbox_items ' ) );
32+ Scheduler::register_async_batch_callback ( 'activitypub_create_comment_outbox_items ' , array ( self ::class, 'create_comment_outbox_items ' ) );
3233 }
3334
3435 /**
@@ -122,13 +123,12 @@ public static function maybe_migrate() {
122123 $ version_from_db = ACTIVITYPUB_PLUGIN_VERSION ;
123124 }
124125
125- // Schedule the async migration.
126- if ( ! \wp_next_scheduled ( 'activitypub_migrate ' , $ version_from_db ) ) {
127- \wp_schedule_single_event ( \time (), 'activitypub_migrate ' , array ( $ version_from_db ) );
128- }
129126 if ( \version_compare ( $ version_from_db , '0.17.0 ' , '< ' ) ) {
130127 self ::migrate_from_0_16 ();
131128 }
129+ if ( \version_compare ( $ version_from_db , '1.0.0 ' , '< ' ) ) {
130+ \wp_schedule_single_event ( \time (), 'activitypub_migrate_from_0_17 ' );
131+ }
132132 if ( \version_compare ( $ version_from_db , '1.3.0 ' , '< ' ) ) {
133133 self ::migrate_from_1_2_0 ();
134134 }
@@ -160,8 +160,9 @@ public static function maybe_migrate() {
160160 add_action ( 'init ' , 'flush_rewrite_rules ' , 20 );
161161 }
162162 if ( \version_compare ( $ version_from_db , '5.0.0 ' , '< ' ) ) {
163- \wp_schedule_single_event ( \time (), 'activitypub_upgrade ' , array ( 'create_post_outbox_items ' ) );
164- \wp_schedule_single_event ( \time () + 15 , 'activitypub_upgrade ' , array ( 'create_comment_outbox_items ' ) );
163+ Scheduler::register_schedules ();
164+ \wp_schedule_single_event ( \time (), 'activitypub_create_post_outbox_items ' );
165+ \wp_schedule_single_event ( \time () + 15 , 'activitypub_create_comment_outbox_items ' );
165166 add_action ( 'init ' , 'flush_rewrite_rules ' , 20 );
166167 }
167168 if ( \version_compare ( $ version_from_db , '5.4.0 ' , '< ' ) ) {
@@ -231,49 +232,6 @@ public static function maybe_migrate() {
231232 self ::unlock ();
232233 }
233234
234- /**
235- * Asynchronously migrates the database structure.
236- *
237- * @param string $version_from_db The version from which to migrate.
238- */
239- public static function async_migration ( $ version_from_db ) {
240- if ( \version_compare ( $ version_from_db , '1.0.0 ' , '< ' ) ) {
241- self ::migrate_from_0_17 ();
242- }
243- }
244-
245- /**
246- * Asynchronously runs upgrade routines.
247- *
248- * @param callable $callback Callable upgrade routine. Must be a method of this class.
249- * @params mixed ...$args Optional. Parameters that get passed to the callback.
250- */
251- public static function async_upgrade ( $ callback ) {
252- $ args = \func_get_args ();
253-
254- // Bail if the existing lock is still valid.
255- if ( self ::is_locked () ) {
256- \wp_schedule_single_event ( time () + MINUTE_IN_SECONDS , 'activitypub_upgrade ' , $ args );
257- return ;
258- }
259-
260- self ::lock ();
261-
262- $ callback = array_shift ( $ args ); // Remove $callback from arguments.
263- $ next = \call_user_func_array ( array ( self ::class, $ callback ), $ args );
264-
265- self ::unlock ();
266-
267- if ( ! empty ( $ next ) ) {
268- // Schedule the next run, adding the result to the arguments.
269- \wp_schedule_single_event (
270- \time () + 30 ,
271- 'activitypub_upgrade ' ,
272- \array_merge ( array ( $ callback ), \array_values ( $ next ) )
273- );
274- }
275- }
276-
277235 /**
278236 * Updates the custom template to use shortcodes instead of the deprecated templates.
279237 */
@@ -515,25 +473,12 @@ public static function migrate_to_4_7_2() {
515473 * @see Comment::pre_wp_update_comment_count_now()
516474 * @param int $batch_size Optional. Number of posts to process per batch. Default 100.
517475 * @param int $offset Optional. Number of posts to skip. Default 0.
476+ *
477+ * @return int[]|void Array with batch size and offset if there are more posts to process.
518478 */
519479 public static function update_comment_counts ( $ batch_size = 100 , $ offset = 0 ) {
520480 global $ wpdb ;
521481
522- // Bail if the existing lock is still valid.
523- if ( self ::is_locked () ) {
524- \wp_schedule_single_event (
525- time () + ( 5 * MINUTE_IN_SECONDS ),
526- 'activitypub_update_comment_counts ' ,
527- array (
528- 'batch_size ' => $ batch_size ,
529- 'offset ' => $ offset ,
530- )
531- );
532- return ;
533- }
534-
535- self ::lock ();
536-
537482 Comment::register_comment_types ();
538483 $ comment_types = Comment::get_comment_type_slugs ();
539484 $ type_inclusion = "AND comment_type IN (' " . implode ( "',' " , $ comment_types ) . "') " ;
@@ -554,17 +499,8 @@ public static function update_comment_counts( $batch_size = 100, $offset = 0 ) {
554499
555500 if ( count ( $ post_ids ) === $ batch_size ) {
556501 // Schedule next batch.
557- \wp_schedule_single_event (
558- time () + MINUTE_IN_SECONDS ,
559- 'activitypub_update_comment_counts ' ,
560- array (
561- 'batch_size ' => $ batch_size ,
562- 'offset ' => $ offset + $ batch_size ,
563- )
564- );
502+ return array ( $ batch_size , $ offset + $ batch_size );
565503 }
566-
567- self ::unlock ();
568504 }
569505
570506 /**
0 commit comments