Skip to content
Open
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
36 changes: 28 additions & 8 deletions pmpro-memberpress-migration-toolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,24 @@ function pmprompmt_page() {

/**
* Action Scheduler function to queue up all users for migration from MemberPress to PMPro.
*
* Users are queued in batches so that a single run of this task stays short. If there
* may be more users to queue, this task re-queues itself with an updated offset.
*/
function pmprompmt_queue_user_migrations( $migrate_stripe_gateway_id = false ) {
function pmprompmt_queue_user_migrations( $migrate_stripe_gateway_id = false, $offset = 0 ) {
global $wpdb;

// Get an array of all user IDs.
$user_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->users" );
$batch_size = 250;
$offset = intval( $offset );

if ( count( $user_ids ) > 250 ) {
PMPro_Action_Scheduler::instance()->halt();
}
// Get the next batch of user IDs.
$user_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->users ORDER BY ID ASC LIMIT %d OFFSET %d", $batch_size, $offset ) );
if ( empty( $user_ids ) ) {
return;
}

// Pause the Action Scheduler while we queue tasks.
PMPro_Action_Scheduler::instance()->halt();

foreach ( $user_ids as $user_id ) {
PMPro_Action_Scheduler::instance()->maybe_add_task(
Expand All @@ -112,10 +120,22 @@ function pmprompmt_queue_user_migrations( $migrate_stripe_gateway_id = false ) {
);
}

// If we paused the Action Scheduler, unpause it now.
// If this batch was full, there may be more users to queue.
if ( count( $user_ids ) === $batch_size ) {
PMPro_Action_Scheduler::instance()->maybe_add_task(
'pmprompmt_queue_user_migrations',
array(
'migrate_stripe_gateway_id' => $migrate_stripe_gateway_id,
'offset' => $offset + $batch_size,
),
'pmpro_async_tasks'
);
}

// Unpause the Action Scheduler now that we are done queuing tasks.
PMPro_Action_Scheduler::instance()->resume();
}
add_action( 'pmprompmt_queue_user_migrations', 'pmprompmt_queue_user_migrations', 10, 1 );
add_action( 'pmprompmt_queue_user_migrations', 'pmprompmt_queue_user_migrations', 10, 2 );

/**
* Action Scheduler function to migrate a single MemberPress member to PMPro.
Expand Down