class-capabilities.php
5 months ago
class-plugin-settings.php
5 months ago
class-settings.php
5 months ago
class-plugin-settings.php
109 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Plugin; |
| 4 | |
| 5 | abstract class Plugin_Settings { |
| 6 | /** @var static|null */ |
| 7 | protected static $instance = null; |
| 8 | |
| 9 | /** |
| 10 | * Settings |
| 11 | * |
| 12 | * @var array<string, mixed> |
| 13 | */ |
| 14 | protected array $settings = []; |
| 15 | |
| 16 | // Don't allow this to be created directly |
| 17 | /** |
| 18 | * @return void |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | $this->settings = $this->load(); |
| 22 | |
| 23 | $defaults = $this->get_defaults(); |
| 24 | |
| 25 | foreach ( $defaults as $key => $value ) { |
| 26 | $this->settings[ $key ] ??= $value; |
| 27 | } |
| 28 | |
| 29 | // Remove any expired settings |
| 30 | foreach ( array_keys( $this->settings ) as $key ) { |
| 31 | if ( ! isset( $defaults[ $key ] ) ) { |
| 32 | unset( $this->settings[ $key ] ); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return string |
| 39 | */ |
| 40 | abstract protected function get_setting_name(); |
| 41 | |
| 42 | /** |
| 43 | * Get default Search Regex options |
| 44 | * |
| 45 | * @return array<string, mixed> |
| 46 | */ |
| 47 | protected function get_defaults() { |
| 48 | return []; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Return Search Regex options |
| 53 | * |
| 54 | * @param string $name Name of setting. |
| 55 | * @param mixed $default_value Default value. |
| 56 | * @return mixed Data to return |
| 57 | */ |
| 58 | protected function get( $name, $default_value = false ) { |
| 59 | return $this->settings[ $name ] ?? $default_value; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Set Search Regex options. Can be passed as many options as necessary and the rest will be unchanged |
| 64 | * |
| 65 | * @param array<string, mixed> $settings Array of name => value. |
| 66 | * @return void |
| 67 | */ |
| 68 | protected function set( array $settings ) { |
| 69 | // @phpstan-ignore-next-line |
| 70 | $this->settings = array_merge( $this->settings, $settings ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @param array<string, mixed> $settings Settings array. |
| 75 | * @return array<string, mixed> |
| 76 | */ |
| 77 | protected function get_save_data( array $settings ) { |
| 78 | return $settings; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return void |
| 83 | */ |
| 84 | public function save() { |
| 85 | \update_option( $this->get_setting_name(), $this->get_save_data( $this->settings ) ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return array<string, mixed> |
| 90 | */ |
| 91 | protected function load() { |
| 92 | return \get_option( $this->get_setting_name(), [] ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @return bool |
| 97 | */ |
| 98 | public function delete() { |
| 99 | return \delete_option( $this->get_setting_name() ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return array<string, mixed> |
| 104 | */ |
| 105 | public function get_as_json() { |
| 106 | return $this->settings; |
| 107 | } |
| 108 | } |
| 109 |