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. */