| 1 |
<?php |
| 2 |
|
| 3 |
namespace migration; |
| 4 |
|
| 5 |
abstract class WCPN_Upgrade_Migration |
| 6 |
{ |
| 7 |
/** |
| 8 |
* @var array |
| 9 |
*/ |
| 10 |
protected $optionSettingsMap = []; |
| 11 |
|
| 12 |
protected function __construct() |
| 13 |
{ |
| 14 |
$this->import(); |
| 15 |
$this->migrate(); |
| 16 |
$this->setOptionSettingsMap(); |
| 17 |
$this->save(); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Import any dependencies you might need using this function |
| 22 |
*/ |
| 23 |
protected function import(): void {} |
| 24 |
|
| 25 |
/** |
| 26 |
* Put the migration logic in this function. |
| 27 |
*/ |
| 28 |
protected function migrate(): void {} |
| 29 |
|
| 30 |
/** |
| 31 |
* |
| 32 |
*/ |
| 33 |
protected function setOptionSettingsMap(): void {} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param array $map |
| 37 |
* @param array|null $newSettings |
| 38 |
* @param array|null $oldSettings |
| 39 |
* |
| 40 |
* @return array|null |
| 41 |
*/ |
| 42 |
protected function migrateSettings(array $map, ?array $newSettings, array $oldSettings = null): ?array |
| 43 |
{ |
| 44 |
$oldSettings = $oldSettings ?? $newSettings; |
| 45 |
|
| 46 |
if (! $oldSettings) { |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
foreach ($map as $oldSetting => $newSetting) { |
| 51 |
if (array_key_exists($oldSetting, $oldSettings)) { |
| 52 |
$newSettings[$newSetting] = $oldSettings[$oldSetting]; |
| 53 |
} |
| 54 |
|
| 55 |
if (array_key_exists($oldSetting, $newSettings)) { |
| 56 |
unset($newSettings[$oldSetting]); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return $newSettings; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get settings array and replace the specified setting with a new value. |
| 65 |
* |
| 66 |
* @param array $map |
| 67 |
* @param string $settingName |
| 68 |
* @param mixed $newValue |
| 69 |
* |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
protected function replaceValue(array $map, string $settingName, $newValue): array |
| 73 |
{ |
| 74 |
foreach ($map as $name => $value) { |
| 75 |
if ($name === $settingName) { |
| 76 |
$map[$name] = $newValue; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return $map; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param array $map |
| 85 |
* @param array $newSettings |
| 86 |
* |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
protected function removeOldSettings(array $map, array $newSettings): array |
| 90 |
{ |
| 91 |
foreach ($map as $oldSetting => $newSetting) { |
| 92 |
if (array_key_exists($oldSetting, $newSettings)) { |
| 93 |
unset($newSettings[$oldSetting]); |
| 94 |
} |
| 95 |
} |
| 96 |
return $newSettings; |
| 97 |
} |
| 98 |
|
| 99 |
protected function save() |
| 100 |
{ |
| 101 |
foreach ($this->optionSettingsMap as $option => $settings) { |
| 102 |
if (get_option($option) === false) { |
| 103 |
add_option($option, $settings); |
| 104 |
} else { |
| 105 |
update_option($option, $settings); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get settings array. Falls back to empty array of get_option returns a falsy value. Not compatible with |
| 112 |
* non-array settings. |
| 113 |
* |
| 114 |
* @param string $settingName |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
protected function getSettings(string $settingName): array |
| 119 |
{ |
| 120 |
return get_option($settingName) ?: []; |
| 121 |
} |
| 122 |
} |
| 123 |
|