Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/changelog/fix-3332-migration-cpt-order
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fixed a warning that could appear when the plugin was activated or updated.
18 changes: 13 additions & 5 deletions includes/class-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ class Migration {
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
self::maybe_migrate();
/*
* Priority 15: after `register_extra_fields_post_types` at 11 (so the
* `ap_extrafield` / `ap_extrafield_blog` post types exist when the
* initial migration inserts the default "Powered by" field) and before
* the rewrite flush below at priority 20. If you renumber one, renumber
* the other, a callback added to the same priority bucket during its
* own dispatch does not run for that dispatch.
*/
\add_action( 'init', array( self::class, 'maybe_migrate' ), 15 );
Comment thread
faisalahammad marked this conversation as resolved.

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' ) );
Expand Down Expand Up @@ -226,10 +234,10 @@ public static function maybe_migrate() {
}

/*
* Defer the flush to late in the `init` cycle (priority 20). Migration::init
* runs at priority 1, which is earlier than most plugins register their
* rewrite rules. Flushing synchronously here would persist a truncated
* ruleset that omits third-party rules added on `init` at priority 10.
* Defer the flush to late in the `init` cycle (priority 20), after the
* priority-10 crowd that registers most plugin rewrite rules. Flushing
* synchronously here would persist a truncated ruleset that omits
* third-party rules added on `init` at priority 10.
*/
\add_action( 'init', array( Activitypub::class, 'flush_rewrite_rules' ), 20 );

Expand Down
28 changes: 28 additions & 0 deletions tests/phpunit/tests/includes/class-test-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Activitypub\Collection\Remote_Actors;
use Activitypub\Comment;
use Activitypub\Migration;
use Activitypub\Post_Types;
use Activitypub\Scheduler;
use Activitypub\Tombstone;

Expand Down Expand Up @@ -633,6 +634,33 @@ public function mock_webfinger() {
);
}

/**
* Regression guard: Migration::init() must register maybe_migrate() on
* `init` at a priority strictly greater than the priority used by
* Post_Types::register_extra_fields_post_types() (11), so the
* `ap_extrafield` / `ap_extrafield_blog` post types exist when the
* fresh-install migration inserts the default "Powered by" field (see
* #3332). This pins the hook-ordering contract, it does not by itself
* prove the `map_meta_cap` `_doing_it_wrong` notice is gone.
*
* @covers ::init
*/
public function test_init_defers_migration_past_post_type_registration() {
Comment thread
faisalahammad marked this conversation as resolved.
$migration_priority = \has_action( 'init', array( Migration::class, 'maybe_migrate' ) );
$cpt_registration_callback = array( Post_Types::class, 'register_extra_fields_post_types' );
$cpt_registration_priority = \has_action( 'init', $cpt_registration_callback );

$this->assertNotFalse( $migration_priority, 'Migration::init() must register maybe_migrate() on init.' );
$this->assertNotFalse( $cpt_registration_priority, 'Extra-fields CPT registration must be hooked on init.' );

$this->assertIsInt( $migration_priority, 'maybe_migrate() must be registered with a numeric priority.' );
$this->assertGreaterThan(
(int) $cpt_registration_priority,
(int) $migration_priority,
'Migration must run after the extra-fields post types are registered.'
);
}

/**
* Test add_default_extra_field.
*/
Expand Down
Loading