From 95373cffca58db1cbb1a4c01deaac62442b0fec9 Mon Sep 17 00:00:00 2001 From: Faisal Ahammad Date: Mon, 10 Aug 2026 20:04:12 +0600 Subject: [PATCH] fix(migration): defer maybe_migrate until post types are registered Migration::init runs at init priority 1, before ActivityPub's custom post types are registered at init priority 11. On fresh installs this caused maybe_migrate to insert extra-field posts before the ap_extrafield CPTs existed, triggering a map_meta_cap doing_it_wrong notice. Defer maybe_migrate to init priority 20 (after CPT registration at 11). Scheduler callback registration stays synchronous at priority 1 because register_async_batch_callback warns if called after init. Fixes #3332 --- .github/changelog/fix-3332-migration-cpt-order | 4 ++++ includes/class-migration.php | 2 +- .../phpunit/tests/includes/class-test-migration.php | 13 +++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 .github/changelog/fix-3332-migration-cpt-order diff --git a/.github/changelog/fix-3332-migration-cpt-order b/.github/changelog/fix-3332-migration-cpt-order new file mode 100644 index 0000000000..571a5b5af3 --- /dev/null +++ b/.github/changelog/fix-3332-migration-cpt-order @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Fixed a PHP notice that could appear when ActivityPub migration ran before custom post types were registered. diff --git a/includes/class-migration.php b/includes/class-migration.php index 39f1d5fef9..b4c4887719 100644 --- a/includes/class-migration.php +++ b/includes/class-migration.php @@ -26,7 +26,7 @@ class Migration { * Initialize the class, registering WordPress hooks. */ public static function init() { - self::maybe_migrate(); + \add_action( 'init', array( self::class, 'maybe_migrate' ), 20 ); Scheduler::register_async_batch_callback( 'activitypub_migrate_from_0_17', array( self::class, 'migrate_from_0_17' ) ); Scheduler::register_async_batch_callback( 'activitypub_update_comment_counts', array( self::class, 'update_comment_counts' ) ); diff --git a/tests/phpunit/tests/includes/class-test-migration.php b/tests/phpunit/tests/includes/class-test-migration.php index 5bfaf2962d..7f8bc886db 100644 --- a/tests/phpunit/tests/includes/class-test-migration.php +++ b/tests/phpunit/tests/includes/class-test-migration.php @@ -633,6 +633,19 @@ public function mock_webfinger() { ); } + /** + * Test migration is deferred until post types are registered. + */ + public function test_init_defers_migration_until_after_post_types() { + \remove_action( 'init', array( Migration::class, 'maybe_migrate' ), 20 ); + $this->setExpectedIncorrectUsage( 'Activitypub\\Scheduler::register_async_batch_callback' ); + + Migration::init(); + + $this->assertSame( 20, \has_action( 'init', array( Migration::class, 'maybe_migrate' ) ) ); + \remove_action( 'init', array( Migration::class, 'maybe_migrate' ), 20 ); + } + /** * Test add_default_extra_field. */