@@ -24,11 +24,12 @@ class Migration {
24
24
* Initialize the class, registering WordPress hooks.
25
25
*/
26
26
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
-
31
27
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 ' ) );
32
33
}
33
34
34
35
/**
@@ -122,13 +123,12 @@ public static function maybe_migrate() {
122
123
$ version_from_db = ACTIVITYPUB_PLUGIN_VERSION ;
123
124
}
124
125
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
- }
129
126
if ( \version_compare ( $ version_from_db , '0.17.0 ' , '< ' ) ) {
130
127
self ::migrate_from_0_16 ();
131
128
}
129
+ if ( \version_compare ( $ version_from_db , '1.0.0 ' , '< ' ) ) {
130
+ \wp_schedule_single_event ( \time (), 'activitypub_migrate_from_0_17 ' );
131
+ }
132
132
if ( \version_compare ( $ version_from_db , '1.3.0 ' , '< ' ) ) {
133
133
self ::migrate_from_1_2_0 ();
134
134
}
@@ -160,8 +160,9 @@ public static function maybe_migrate() {
160
160
add_action ( 'init ' , 'flush_rewrite_rules ' , 20 );
161
161
}
162
162
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 ' );
165
166
add_action ( 'init ' , 'flush_rewrite_rules ' , 20 );
166
167
}
167
168
if ( \version_compare ( $ version_from_db , '5.4.0 ' , '< ' ) ) {
@@ -231,49 +232,6 @@ public static function maybe_migrate() {
231
232
self ::unlock ();
232
233
}
233
234
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
-
277
235
/**
278
236
* Updates the custom template to use shortcodes instead of the deprecated templates.
279
237
*/
@@ -515,25 +473,12 @@ public static function migrate_to_4_7_2() {
515
473
* @see Comment::pre_wp_update_comment_count_now()
516
474
* @param int $batch_size Optional. Number of posts to process per batch. Default 100.
517
475
* @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.
518
478
*/
519
479
public static function update_comment_counts ( $ batch_size = 100 , $ offset = 0 ) {
520
480
global $ wpdb ;
521
481
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
-
537
482
Comment::register_comment_types ();
538
483
$ comment_types = Comment::get_comment_type_slugs ();
539
484
$ type_inclusion = "AND comment_type IN (' " . implode ( "',' " , $ comment_types ) . "') " ;
@@ -554,17 +499,8 @@ public static function update_comment_counts( $batch_size = 100, $offset = 0 ) {
554
499
555
500
if ( count ( $ post_ids ) === $ batch_size ) {
556
501
// 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 );
565
503
}
566
-
567
- self ::unlock ();
568
504
}
569
505
570
506
/**
0 commit comments