PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.30.0
Independent Analytics – WordPress Analytics Plugin v1.30.0
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Utils / Option.php
independent-analytics / IAWP / Utils Last commit date
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