Array_To_CSV.php
3 years ago
Currency.php
3 years ago
Device.php
2 years ago
Number_Formatter.php
3 years ago
Option.php
2 years ago
Request.php
2 years ago
Salt.php
3 years ago
Security.php
3 years ago
Singleton.php
2 years ago
String_Util.php
3 years ago
URL.php
3 years ago
WP_Async_Request.php
3 years ago
WordPress_Site_Date_Format_Pattern.php
3 years ago
Option.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP\Utils; |
| 4 | |
| 5 | class Option |
| 6 | { |
| 7 | public const STRING = 'string'; |
| 8 | public const BOOLEAN = 'boolean'; |
| 9 | public const BOOL = 'boolean'; |
| 10 | public const INT = 'integer'; |
| 11 | public const INTEGER = 'integer'; |
| 12 | public const FLOAT = 'float'; |
| 13 | public const DOUBLE = 'float'; |
| 14 | public const ARRAY = 'array'; |
| 15 | /** |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $option_name; |
| 19 | private static $supported_types = ['string', 'boolean', 'integer', 'float', 'array']; |
| 20 | /** |
| 21 | * @var array |
| 22 | */ |
| 23 | private static $registered_options = []; |
| 24 | /** |
| 25 | * @var string |
| 26 | */ |
| 27 | private $type; |
| 28 | /** |
| 29 | * @var mixed|null |
| 30 | */ |
| 31 | private $default_value; |
| 32 | /** |
| 33 | * @var boolean |
| 34 | */ |
| 35 | private $allow_empty_string = \false; |
| 36 | public function __construct(string $option_name, string $type, $default_value = null, array $options = []) |
| 37 | { |
| 38 | $this->option_name = $option_name; |
| 39 | $this->type = $type; |
| 40 | $this->default_value = $default_value; |
| 41 | if (\array_key_exists('allow_empty_string', $options)) { |
| 42 | $this->allow_empty_string = \filter_var($options['allow_empty_string'], \FILTER_VALIDATE_BOOLEAN); |
| 43 | } |
| 44 | // Throw an error if the type is unsupported |
| 45 | if (!\in_array($type, self::$supported_types)) { |
| 46 | throw new \Exception("{$type} is not a supported type"); |
| 47 | } |
| 48 | // Throw an error if the default_value is set, but does not match the options types |
| 49 | if (!\is_null($default_value) && $type !== $this->get_type($default_value)) { |
| 50 | throw new \Exception("Default value for {$option_name} is not of type {$type}"); |
| 51 | } |
| 52 | // Throw an error if option name is already registered |
| 53 | foreach (self::$registered_options as $option) { |
| 54 | if ($option->option_name === $option_name) { |
| 55 | throw new \Exception("Option {$option_name} is already registered"); |
| 56 | } |
| 57 | } |
| 58 | self::$registered_options[] = $this; |
| 59 | } |
| 60 | public function set($new_value = null) |
| 61 | { |
| 62 | if (\is_null($new_value)) { |
| 63 | $this->delete(); |
| 64 | return; |
| 65 | } |
| 66 | $new_value_type = $this->get_type($new_value); |
| 67 | if ($this->type !== $new_value_type) { |
| 68 | throw new \Exception("{$this->option_name} expects type {$this->type} but {$new_value_type} was provided"); |
| 69 | } |
| 70 | if ($new_value === '' && !$this->allow_empty_string) { |
| 71 | $this->delete(); |
| 72 | return; |
| 73 | } |
| 74 | \update_option($this->option_name, $this->convert_to_wordpress($new_value)); |
| 75 | } |
| 76 | public function get() |
| 77 | { |
| 78 | $value = \get_option($this->option_name, new \Exception()); |
| 79 | if ($value instanceof \Exception) { |
| 80 | return $this->default_value; |
| 81 | } |
| 82 | return $this->convert_from_wordpress($value); |
| 83 | } |
| 84 | public function delete() |
| 85 | { |
| 86 | \delete_option($this->option_name); |
| 87 | } |
| 88 | /** |
| 89 | * @param $value |
| 90 | * |
| 91 | * @return string |
| 92 | */ |
| 93 | private function get_type($value) : string |
| 94 | { |
| 95 | $type = \gettype($value); |
| 96 | if ($type === 'double') { |
| 97 | return 'float'; |
| 98 | } |
| 99 | return $type; |
| 100 | } |
| 101 | /** |
| 102 | * @param mixed $value |
| 103 | * |
| 104 | * @return mixed |
| 105 | */ |
| 106 | private function convert_to_wordpress($value) |
| 107 | { |
| 108 | switch ($this->type) { |
| 109 | case 'boolean': |
| 110 | return $value === \true ? 1 : 0; |
| 111 | } |
| 112 | return $value; |
| 113 | } |
| 114 | /** |
| 115 | * @param mixed $value |
| 116 | * |
| 117 | * @return mixed |
| 118 | */ |
| 119 | private function convert_from_wordpress($value) |
| 120 | { |
| 121 | switch ($this->type) { |
| 122 | case 'integer': |
| 123 | /** |
| 124 | * If the stored value is not parsable as an integer, use the default value instead |
| 125 | */ |
| 126 | $filtered_value = \filter_var($value, \FILTER_VALIDATE_INT); |
| 127 | return $filtered_value !== \false ? $filtered_value : $this->default_value; |
| 128 | case 'float': |
| 129 | /** |
| 130 | * If the stored value is not parsable as an float, use the default value instead |
| 131 | */ |
| 132 | $filtered_value = \filter_var($value, \FILTER_VALIDATE_FLOAT); |
| 133 | return $filtered_value !== \false ? $filtered_value : $this->default_value; |
| 134 | case 'boolean': |
| 135 | return \filter_var($value, \FILTER_VALIDATE_BOOLEAN); |
| 136 | case 'array': |
| 137 | /** |
| 138 | * If an array was saved with update_option, then get_option would return a parsed |
| 139 | * array. If get_option returns a string, that means the value wasn't a parsable |
| 140 | * array and the default value should be used. |
| 141 | */ |
| 142 | return $this->get_type($value) === 'string' ? $this->default_value : $value; |
| 143 | case 'string': |
| 144 | if ($value === '' && !$this->allow_empty_string) { |
| 145 | return $this->default_value; |
| 146 | } |
| 147 | return $value; |
| 148 | } |
| 149 | return $value; |
| 150 | } |
| 151 | public static function find($option_name) |
| 152 | { |
| 153 | foreach (Option::$registered_options as $option) { |
| 154 | if ($option->option_name === $option_name) { |
| 155 | return $option; |
| 156 | } |
| 157 | } |
| 158 | throw new \Exception("Option {$option_name} is not registered"); |
| 159 | } |
| 160 | public static function set_option($option_name, $value = null) |
| 161 | { |
| 162 | $option = self::find($option_name); |
| 163 | $option->set($value); |
| 164 | } |
| 165 | public static function get_option($option_name) |
| 166 | { |
| 167 | $option = self::find($option_name); |
| 168 | return $option->get(); |
| 169 | } |
| 170 | public static function delete_option($option_name) |
| 171 | { |
| 172 | $option = self::find($option_name); |
| 173 | return $option->delete(); |
| 174 | } |
| 175 | } |
| 176 |