| 1 |
<?php |
| 2 |
/* |
| 3 |
* SOFTWARE LICENSE INFORMATION |
| 4 |
* |
| 5 |
* Copyright (c) 2017 Buttonizer, all rights reserved. |
| 6 |
* |
| 7 |
* This file is part of Buttonizer |
| 8 |
* |
| 9 |
* For detailed information regarding to the licensing of |
| 10 |
* this software, please review the license.txt or visit: |
| 11 |
* https://buttonizer.pro/license/ |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Buttonizer\Migration; |
| 15 |
|
| 16 |
# No script kiddies |
| 17 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 18 |
|
| 19 |
/** |
| 20 |
* Per-module migration bookkeeping. |
| 21 |
* |
| 22 |
* One option holds the state of every module: |
| 23 |
* |
| 24 |
* buttonizer_migration_state = [ |
| 25 |
* 'chat-button' => [ |
| 26 |
* 'requested' => true, // written by the source plugin's own notice |
| 27 |
* 'status' => 'migrated', |
| 28 |
* 'detected_state' => 'cloud_connected', |
| 29 |
* 'result' => 'connection_adopted', |
| 30 |
* 'migrated_at' => '2026-08-14T10:00:00+00:00', |
| 31 |
* 'notice_dismissed' => false, |
| 32 |
* ], |
| 33 |
* ] |
| 34 |
* |
| 35 |
* Backups are stored separately, one option per module, so they can be |
| 36 |
* restored or deleted independently of this bookkeeping. |
| 37 |
*/ |
| 38 |
class MigrationState |
| 39 |
{ |
| 40 |
const OPTION = 'buttonizer_migration_state'; |
| 41 |
|
| 42 |
const STATUS_PENDING = 'pending'; |
| 43 |
const STATUS_MIGRATED = 'migrated'; |
| 44 |
const STATUS_SKIPPED = 'skipped'; |
| 45 |
const STATUS_FAILED = 'failed'; |
| 46 |
|
| 47 |
/** |
| 48 |
* Full state map. |
| 49 |
*/ |
| 50 |
public static function all(): array |
| 51 |
{ |
| 52 |
$state = get_option(self::OPTION, []); |
| 53 |
|
| 54 |
return is_array($state) ? $state : []; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* State of a single module. |
| 59 |
*/ |
| 60 |
public static function get(string $moduleId): array |
| 61 |
{ |
| 62 |
$state = self::all(); |
| 63 |
|
| 64 |
return isset($state[$moduleId]) && is_array($state[$moduleId]) ? $state[$moduleId] : []; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Merge values into a module's state. |
| 69 |
* |
| 70 |
* @param string $moduleId Module identifier. |
| 71 |
* @param array $values Values to merge. |
| 72 |
*/ |
| 73 |
public static function set(string $moduleId, array $values): void |
| 74 |
{ |
| 75 |
$state = self::all(); |
| 76 |
|
| 77 |
$state[$moduleId] = array_merge(self::get($moduleId), $values); |
| 78 |
|
| 79 |
update_option(self::OPTION, $state); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Has this module already been migrated? |
| 84 |
*/ |
| 85 |
public static function isMigrated(string $moduleId): bool |
| 86 |
{ |
| 87 |
$module = self::get($moduleId); |
| 88 |
|
| 89 |
return isset($module['status']) && $module['status'] === self::STATUS_MIGRATED; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Mark a module as migrated. |
| 94 |
* |
| 95 |
* @param string $moduleId Module identifier. |
| 96 |
* @param string $result How the migration resolved (for support/debugging). |
| 97 |
*/ |
| 98 |
public static function markMigrated(string $moduleId, string $result): void |
| 99 |
{ |
| 100 |
self::set($moduleId, [ |
| 101 |
'status' => self::STATUS_MIGRATED, |
| 102 |
'result' => $result, |
| 103 |
'migrated_at' => (new \DateTime('now'))->format(\DateTime::ATOM), |
| 104 |
'notice_dismissed' => false, |
| 105 |
]); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Reset a module back to its pre-migration bookkeeping. |
| 110 |
* |
| 111 |
* Used by the rollback path; the actual data is restored by Backup. |
| 112 |
*/ |
| 113 |
public static function reset(string $moduleId): void |
| 114 |
{ |
| 115 |
$state = self::all(); |
| 116 |
|
| 117 |
unset($state[$moduleId]); |
| 118 |
|
| 119 |
update_option(self::OPTION, $state); |
| 120 |
} |
| 121 |
} |
| 122 |
|