EditButton.php
2 days ago
EditorFavorites.php
2 days ago
EditorMenuStatus.php
2 days ago
IntegrationStatus.php
2 days ago
ListColumnOrder.php
2 days ago
ListScreenOrder.php
2 days ago
OriginalColumnsRepository.php
2 days ago
TableListOrder.php
2 days ago
UserColumnOrder.php
2 days ago
EditorMenuStatus.php
64 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Storage\Repository; |
| 6 | |
| 7 | use AC\Storage\UserOption; |
| 8 | |
| 9 | class EditorMenuStatus |
| 10 | { |
| 11 | private UserOption $storage; |
| 12 | |
| 13 | public function __construct() |
| 14 | { |
| 15 | $this->storage = new UserOption('ac_preferences_menu_status'); |
| 16 | } |
| 17 | |
| 18 | public function get_groups(): array |
| 19 | { |
| 20 | return $this->storage->get() ?: []; |
| 21 | } |
| 22 | |
| 23 | public function save_status(string $group, bool $active): void |
| 24 | { |
| 25 | $active |
| 26 | ? $this->add_group($group) |
| 27 | : $this->remove($group); |
| 28 | } |
| 29 | |
| 30 | private function add_group(string $group): void |
| 31 | { |
| 32 | $data = $this->get_groups(); |
| 33 | |
| 34 | if (! in_array($group, $data, true)) { |
| 35 | $data[] = $group; |
| 36 | } |
| 37 | |
| 38 | $this->save($data); |
| 39 | } |
| 40 | |
| 41 | private function save(array $data): void |
| 42 | { |
| 43 | $data |
| 44 | ? $this->storage->save($data) |
| 45 | : $this->storage->delete(); |
| 46 | } |
| 47 | |
| 48 | private function remove(string $group): void |
| 49 | { |
| 50 | $data = $this->get_groups(); |
| 51 | |
| 52 | $key = array_search($group, $data); |
| 53 | |
| 54 | if (false !== $key) { |
| 55 | unset($data[$key]); |
| 56 | } |
| 57 | |
| 58 | $this->save( |
| 59 | array_values($data) |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | } |
| 64 |