robotstxt-mlp2-mlp5/includes/class-robotstxt-mlp2-mlp5-wizard-state.php
2026-03-02 11:22:59 +00:00

151 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Migration wizard state manager.
*
* Persists the wizard's current step, per-step data, and completion flags
* in a single network option. All public methods handle serialization
* and defaults internally.
*
* @package Robotstxt_Mlp2_Mlp5
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Robotstxt_Mlp2_Mlp5_Wizard_State
*/
class Robotstxt_Mlp2_Mlp5_Wizard_State {
/**
* Sitemeta key used to store the wizard state.
*
* @var string
*/
const OPTION_KEY = 'mlp_migrator_wizard_state';
/**
* Total number of wizard steps.
*
* @var int
*/
const TOTAL_STEPS = 6;
/**
* Cached state array.
*
* @var array|null
*/
private static ?array $cache = null;
/**
* Return the current wizard step (1–6). Defaults to 1 if no state exists.
*
* @return int
*/
public static function get_current_step(): int {
$state = self::load();
$step = isset( $state['step'] ) ? (int) $state['step'] : 1;
return max( 1, min( self::TOTAL_STEPS, $step ) );
}
/**
* Set the current wizard step.
*
* @param int $step Step number (1–6).
*/
public static function set_step( int $step ): void {
$state = self::load();
$state['step'] = max( 1, min( self::TOTAL_STEPS, $step ) );
self::save( $state );
}
/**
* Return stored data for a specific step.
*
* @param int $step Step number (1–6).
* @return array
*/
public static function get_step_data( int $step ): array {
$state = self::load();
$key = 'step_' . $step . '_data';
return isset( $state[ $key ] ) && is_array( $state[ $key ] ) ? $state[ $key ] : array();
}
/**
* Store data for a specific step.
*
* @param int $step Step number (1–6).
* @param array $data Arbitrary data to store.
*/
public static function set_step_data( int $step, array $data ): void {
$state = self::load();
$state[ 'step_' . $step . '_data' ] = $data;
self::save( $state );
}
/**
* Merge data into an existing step's stored data.
*
* @param int $step Step number (1–6).
* @param array $data Data to merge (existing keys are overwritten).
*/
public static function merge_step_data( int $step, array $data ): void {
$existing = self::get_step_data( $step );
self::set_step_data( $step, array_merge( $existing, $data ) );
}
/**
* Mark a step as complete.
*
* @param int $step Step number (1–6).
*/
public static function mark_step_complete( int $step ): void {
$state = self::load();
$state[ 'step_' . $step . '_complete' ] = true;
self::save( $state );
}
/**
* Return whether a step has been marked complete.
*
* @param int $step Step number (1–6).
* @return bool
*/
public static function is_step_complete( int $step ): bool {
$state = self::load();
return ! empty( $state[ 'step_' . $step . '_complete' ] );
}
/**
* Reset the wizard by deleting the network option entirely.
*/
public static function reset(): void {
delete_site_option( self::OPTION_KEY );
self::$cache = null;
}
/**
* Load (and cache) the state array from the network option.
*
* @return array
*/
private static function load(): array {
if ( null === self::$cache ) {
$raw = get_site_option( self::OPTION_KEY, array() );
self::$cache = is_array( $raw ) ? $raw : array();
}
return self::$cache;
}
/**
* Persist the state array to the network option.
*
* @param array $state State to persist.
*/
private static function save( array $state ): void {
self::$cache = $state;
update_site_option( self::OPTION_KEY, $state );
}
}