Skip to content

Commit 69bcfb7

Browse files
committed
Replace hardcoded async batch allowlist with extensible registration system
* Add Scheduler::register_async_batch_callback() method for dynamic callback registration * Remove hardcoded Migration callbacks from Scheduler class * Update Migration class to register its callbacks through new system * Add safety check to ensure callbacks are registered at proper time * Maintain backward compatibility - async_batch works exactly the same way * Enable any class to add async batch jobs without modifying core files
1 parent f98c706 commit 69bcfb7

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

includes/class-migration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Activitypub;
99

10-
use Activitypub\Activity\Actor;
1110
use Activitypub\Collection\Actors;
1211
use Activitypub\Collection\Extra_Fields;
1312
use Activitypub\Collection\Followers;
@@ -25,6 +24,11 @@ class Migration {
2524
*/
2625
public static function init() {
2726
self::maybe_migrate();
27+
28+
Scheduler::register_async_batch_callback( 'activitypub_migrate_from_0_17', array( self::class, 'migrate_from_0_17' ) );
29+
Scheduler::register_async_batch_callback( 'activitypub_update_comment_counts', array( self::class, 'update_comment_counts' ) );
30+
Scheduler::register_async_batch_callback( 'activitypub_create_post_outbox_items', array( self::class, 'create_post_outbox_items' ) );
31+
Scheduler::register_async_batch_callback( 'activitypub_create_comment_outbox_items', array( self::class, 'create_comment_outbox_items' ) );
2832
}
2933

3034
/**

includes/class-scheduler.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,17 @@ class Scheduler {
2727
*
2828
* @var array
2929
*/
30-
private static $batch_callbacks = array();
30+
private static $batch_callbacks = array(
31+
'activitypub_send_activity' => array( Dispatcher::class, 'send_to_followers' ),
32+
'activitypub_retry_activity' => array( Dispatcher::class, 'retry_send_to_followers' ),
33+
);
3134

3235
/**
3336
* Initialize the class, registering WordPress hooks.
3437
*/
3538
public static function init() {
3639
self::register_schedulers();
3740

38-
self::$batch_callbacks = array(
39-
'activitypub_send_activity' => array( Dispatcher::class, 'send_to_followers' ),
40-
'activitypub_retry_activity' => array( Dispatcher::class, 'retry_send_to_followers' ),
41-
'activitypub_migrate_from_0_17' => array( Migration::class, 'migrate_from_0_17' ),
42-
'activitypub_update_comment_counts' => array( Migration::class, 'update_comment_counts' ),
43-
'activitypub_create_post_outbox_items' => array( Migration::class, 'create_post_outbox_items' ),
44-
'activitypub_create_comment_outbox_items' => array( Migration::class, 'create_comment_outbox_items' ),
45-
);
46-
4741
// Follower Cleanups.
4842
\add_action( 'activitypub_update_remote_actors', array( self::class, 'update_remote_actors' ) );
4943
\add_action( 'activitypub_cleanup_remote_actors', array( self::class, 'cleanup_remote_actors' ) );
@@ -52,10 +46,6 @@ public static function init() {
5246
\add_action( 'activitypub_async_batch', array( self::class, 'async_batch' ), 10, 99 );
5347
\add_action( 'activitypub_send_activity', array( self::class, 'async_batch' ), 10, 3 );
5448
\add_action( 'activitypub_retry_activity', array( self::class, 'async_batch' ), 10, 3 );
55-
\add_action( 'activitypub_migrate_from_0_17', array( self::class, 'async_batch' ) );
56-
\add_action( 'activitypub_update_comment_counts', array( self::class, 'async_batch' ), 10, 2 );
57-
\add_action( 'activitypub_create_post_outbox_items', array( self::class, 'async_batch' ), 10, 2 );
58-
\add_action( 'activitypub_create_comment_outbox_items', array( self::class, 'async_batch' ), 10, 2 );
5949
\add_action( 'activitypub_reprocess_outbox', array( self::class, 'reprocess_outbox' ) );
6050
\add_action( 'activitypub_outbox_purge', array( self::class, 'purge_outbox' ) );
6151

@@ -81,6 +71,28 @@ public static function register_schedulers() {
8171
do_action( 'activitypub_register_schedulers' );
8272
}
8373

74+
/**
75+
* Register a batch callback for async processing.
76+
*
77+
* @param string $hook The cron event hook name.
78+
* @param callable $callback The callback to execute.
79+
*/
80+
public static function register_async_batch_callback( $hook, $callback ) {
81+
if ( \did_action( 'init' ) && ! \doing_action( 'init' ) ) {
82+
\_doing_it_wrong( __METHOD__, 'Async batch callbacks should be registered before or during the init action.', '5.2.0' );
83+
return;
84+
}
85+
86+
if ( ! \is_callable( $callback ) ) {
87+
return;
88+
}
89+
90+
self::$batch_callbacks[ $hook ] = $callback;
91+
92+
// Register the WordPress action hook to trigger async_batch.
93+
\add_action( $hook, array( self::class, 'async_batch' ), 10, 99 );
94+
}
95+
8496
/**
8597
* Schedule all ActivityPub schedules.
8698
*/

tests/includes/class-test-migration.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Activitypub\Tests;
99

10+
use Activitypub\Activity\Actor;
11+
use Activitypub\Collection\Actors;
1012
use Activitypub\Collection\Extra_Fields;
1113
use Activitypub\Collection\Followers;
1214
use Activitypub\Collection\Outbox;
@@ -475,7 +477,8 @@ public function test_create_outbox_items_batching() {
475477
/**
476478
* Test async upgrade with multiple arguments.
477479
*
478-
* @covers ::async_upgrade
480+
* @covers ::update_comment_counts
481+
* @covers \Activitypub\Scheduler::async_batch
479482
*/
480483
public function test_async_upgrade_multiple_args() {
481484
// Test that multiple arguments are passed correctly.

0 commit comments

Comments
 (0)