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