isValidContext() || QueueFactory::isMigrationCompleted()) {
return;
}
$status = Option::getOptionGroup('queue_background_process', 'status', null);
if ($status !== 'done') {
return;
}
$message = sprintf(
'
%1$s
—
%2$s
',
esc_html__('Update complete', 'wp-statistics'),
esc_html__('thanks for staying up to date!', 'wp-statistics')
);
Notice::addFlashNotice($message, 'success', false);
Option::saveOptionGroup('status', null, 'queue_background_process');
}
/**
* Displays an admin notice with a start button for queue migration.
*
* This method checks if queue migration is needed and displays an admin notice
* with a start button to allow users to manually trigger the migration process.
* The notice includes information about the number of steps to be processed.
*
* @return void
*/
public function handleNotice()
{
if (
!$this->isValidContext() ||
QueueFactory::isMigrationCompleted() ||
!QueueFactory::needsMigration()
) {
return;
}
$isMigrated = QueueFactory::isDatabaseMigrated();
if (!$isMigrated) {
return;
}
$migrationUrl = add_query_arg(
[
'action' => self::MIGRATION_ACTION,
'nonce' => wp_create_nonce(self::MIGRATION_NONCE),
'current_page' => DatabaseHelper::getCurrentAdminUrl()
],
admin_url('admin-post.php')
);
$message = sprintf(
'',
esc_html__('WP Statistics needs a quick update', 'wp-statistics'),
__('Run this brief update to keep your stats accurate.', 'wp-statistics'),
esc_url($migrationUrl),
esc_html__('Update Now', 'wp-statistics')
);
Notice::addNotice($message, 'start_queue_background_process', 'warning', false);
}
/**
* Handles the request to start the queue migration process.
*
* This method processes the admin POST request to start queue migration.
* It validates the request action, verifies the nonce for security,
* executes all pending migrations, marks the process as completed,
* and handles the redirect back to the original page.
*
* @return bool|void False if the request is invalid, void otherwise
*/
public function handleQueueMigration()
{
check_admin_referer(self::MIGRATION_NONCE, 'nonce');
if (!Request::compare('action', self::MIGRATION_ACTION)) {
return false;
}
$this->verifyMigrationPermission();
if (!QueueFactory::needsMigration()) {
return false;
}
$this->executeAllMigrations();
Option::saveOptionGroup('status', 'done', 'queue_background_process');
$this->handleRedirect();
}
/**
* Executes all pending queue-based migration steps.
*
* This method retrieves all pending migration steps from the QueueFactory
* and executes them sequentially. Each step is processed individually
* to ensure proper completion and error handling.
*
* @return void
*/
private function executeAllMigrations()
{
$pendingSteps = QueueFactory::getPendingMigrationSteps();
foreach ($pendingSteps as $step) {
QueueFactory::executeMigrationStep($step);
}
}
/**
* Handles the redirect after processing queue migrations.
*
* This method redirects the user back to the original page after
* the migration process is completed. It attempts to use the current_page
* parameter from the request, falling back to the home URL if not available.
*
* @return void This method always exits after redirect
*/
private function handleRedirect()
{
$redirectUrl = $_POST['current_page'] ?? $_GET['current_page'] ?? '';
if (empty($redirectUrl)) {
$redirectUrl = home_url();
}
wp_redirect(esc_url_raw($redirectUrl));
exit;
}
}