| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPStaging\Backup\Service; |
| 4 |
|
| 5 |
use WPStaging\Core\DTO\Settings; |
| 6 |
use WPStaging\Core\WPStaging; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class UpdateProtectionSettings |
| 15 |
{ |
| 16 |
|
| 17 |
const INTRO_SURFACES = ['modal', 'notice']; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public function isEnabled(): bool |
| 23 |
{ |
| 24 |
return WPStaging::make(Settings::class)->isBackupBeforeUpdateEnabled(); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function getMode(): string |
| 31 |
{ |
| 32 |
$mode = (string)get_option(Settings::OPTION_BACKUP_BEFORE_UPDATE_MODE, ''); |
| 33 |
|
| 34 |
return $mode === '' ? 'ask' : $mode; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function forgetMode() |
| 44 |
{ |
| 45 |
delete_option(Settings::OPTION_BACKUP_BEFORE_UPDATE_MODE); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function setEnabled(bool $isEnabled) |
| 56 |
{ |
| 57 |
$settings = get_option('wpstg_settings', []); |
| 58 |
if (is_object($settings)) { |
| 59 |
$settings = (array)$settings; |
| 60 |
} |
| 61 |
|
| 62 |
if (!is_array($settings)) { |
| 63 |
$settings = []; |
| 64 |
} |
| 65 |
|
| 66 |
$settings['enableBackupBeforeUpdate'] = $isEnabled ? '1' : '0'; |
| 67 |
update_option('wpstg_settings', $settings); |
| 68 |
|
| 69 |
if (!$isEnabled) { |
| 70 |
$this->forgetMode(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function getIntrosSeen(): array |
| 78 |
{ |
| 79 |
return array_values(array_filter(explode(',', (string)get_option(Settings::OPTION_BACKUP_BEFORE_UPDATE_INTRO_SEEN, '')))); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function hasSeenIntro(string $surface): bool |
| 87 |
{ |
| 88 |
return in_array($surface, $this->getIntrosSeen(), true); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function markIntroSeen(string $surface) |
| 96 |
{ |
| 97 |
$seen = $this->getIntrosSeen(); |
| 98 |
if (in_array($surface, $seen, true)) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
$seen[] = $surface; |
| 103 |
update_option(Settings::OPTION_BACKUP_BEFORE_UPDATE_INTRO_SEEN, implode(',', $seen)); |
| 104 |
} |
| 105 |
} |
| 106 |
|