| @@ -30,15 +30,8 @@ | ||
| 30 | 30 | */ |
| 31 | 31 | public $namespace; |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | - * Keeps static copy of the options during runtime. | |
| 35 | - * | |
| 36 | - * @var null|array | |
| 37 | - */ | |
| 38 | - private $data; | |
| 39 | - | |
| 40 | - /** | |
| 41 | 34 | * Initialize Options on run. |
| 42 | 35 | * |
| 43 | 36 | * @param string $prefix Settings key prefix. |
| 44 | 37 | */ |
| @@ -45,9 +38,8 @@ | ||
| 45 | 38 | public function __construct( $prefix = 'content_control' ) { |
| 46 | 39 | // Set the prefix on init. |
| 47 | 40 | $this->prefix = ! empty( $prefix ) ? trim( $prefix, '_' ) . '_' : ''; |
| 48 | 41 | $this->namespace = ! empty( $prefix ) ? trim( $prefix, '_/' ) . '/' : ''; |
| 49 | - $this->data = $this->get_all(); | |
| 50 | 42 | } |
| 51 | 43 | |
| 52 | 44 | /** |
| 53 | 45 | * Get Settings |
| @@ -53,9 +45,9 @@ | ||
| 53 | 45 | * Get Settings |
| 54 | 46 | * |
| 55 | 47 | * Retrieves all plugin settings |
| 56 | 48 | * |
| 57 | - * @return array settings | |
| 49 | + * @return array<string,mixed> settings | |
| 58 | 50 | */ |
| 59 | 51 | public function get_all() { |
| 60 | 52 | $settings = \get_option( $this->prefix . 'settings' ); |
| 61 | 53 | |
| @@ -84,15 +76,24 @@ | ||
| 84 | 76 | * |
| 85 | 77 | * @return mixed|void |
| 86 | 78 | */ |
| 87 | 79 | public function get( $key = '', $default_value = false ) { |
| 88 | - $value = isset( $this->data[ $key ] ) ? $this->data[ $key ] : $default_value; | |
| 80 | + $data = $this->get_all(); | |
| 89 | 81 | |
| 82 | + // Fetch key from array, converting to camelcase (how data is stored). Supports dot.notation. | |
| 83 | + $value = \ContentControl\fetch_key_from_array( $key, $data, 'camelCase' ); | |
| 84 | + | |
| 85 | + // If no value, return default. | |
| 86 | + if ( null === $value ) { | |
| 87 | + $value = $default_value; | |
| 88 | + } | |
| 89 | + | |
| 90 | 90 | /** |
| 91 | 91 | * Filter the option. |
| 92 | 92 | * |
| 93 | 93 | * @param mixed $value Option value. |
| 94 | 94 | * @param string $key Option key. |
| 95 | + * @param mixed $default_value Default value. | |
| 95 | 96 | * |
| 96 | 97 | * @return mixed |
| 97 | 98 | */ |
| 98 | 99 | return apply_filters( $this->namespace . 'get_option', $value, $key, $default_value ); |
| @@ -98,31 +99,8 @@ | ||
| 98 | 99 | return apply_filters( $this->namespace . 'get_option', $value, $key, $default_value ); |
| 99 | 100 | } |
| 100 | 101 | |
| 101 | 102 | /** |
| 102 | - * Get an option using a dot notation key. | |
| 103 | - * | |
| 104 | - * @param string $key Option key in dot notation. | |
| 105 | - * @param bool $default_value Default value. | |
| 106 | - * | |
| 107 | - * @return mixed|void | |
| 108 | - */ | |
| 109 | - public function get_notation( $key = '', $default_value = false ) { | |
| 110 | - $keys = explode( '.', $key ); | |
| 111 | - $data = $this->get_all(); | |
| 112 | - | |
| 113 | - foreach ( $keys as $key ) { | |
| 114 | - if ( ! isset( $data[ $key ] ) ) { | |
| 115 | - return $default_value; | |
| 116 | - } | |
| 117 | - | |
| 118 | - $data = $data[ $key ]; | |
| 119 | - } | |
| 120 | - | |
| 121 | - return $data; | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | 103 | * Update an option |
| 126 | 104 | * |
| 127 | 105 | * Updates an setting value in both the db and the global variable. |
| 128 | 106 | * Warning: Passing in an empty, false or null string value will remove |
| @@ -164,13 +142,8 @@ | ||
| 164 | 142 | // Next let's try to update the value. |
| 165 | 143 | $options[ $key ] = $value; |
| 166 | 144 | $did_update = \update_option( $this->prefix . 'settings', $options ); |
| 167 | 145 | |
| 168 | - // If it updated, let's update the global variable. | |
| 169 | - if ( $did_update ) { | |
| 170 | - $this->data[ $key ] = $value; | |
| 171 | - } | |
| 172 | - | |
| 173 | 146 | return $did_update; |
| 174 | 147 | } |
| 175 | 148 | |
| 176 | 149 | /** |
| @@ -175,9 +148,9 @@ | ||
| 175 | 148 | |
| 176 | 149 | /** |
| 177 | 150 | * Update many values at once. |
| 178 | 151 | * |
| 179 | - * @param array $new_options Array of new replacement options. | |
| 152 | + * @param array<string,mixed> $new_options Array of new replacement options. | |
| 180 | 153 | * |
| 181 | 154 | * @return bool |
| 182 | 155 | */ |
| 183 | 156 | public function update_many( $new_options = [] ) { |
| @@ -209,13 +182,8 @@ | ||
| 209 | 182 | } |
| 210 | 183 | |
| 211 | 184 | $did_update = \update_option( $this->prefix . 'settings', $options ); |
| 212 | 185 | |
| 213 | - // If it updated, let's update the global variable. | |
| 214 | - if ( $did_update ) { | |
| 215 | - $this->data = $options; | |
| 216 | - } | |
| 217 | - | |
| 218 | 186 | return $did_update; |
| 219 | 187 | } |
| 220 | 188 | |
| 221 | 189 | /** |
| @@ -245,13 +213,8 @@ | ||
| 245 | 213 | } |
| 246 | 214 | |
| 247 | 215 | $did_update = \update_option( $this->prefix . 'settings', $options ); |
| 248 | 216 | |
| 249 | - // If it updated, let's update the global variable. | |
| 250 | - if ( $did_update ) { | |
| 251 | - $this->data = $options; | |
| 252 | - } | |
| 253 | - | |
| 254 | 217 | return $did_update; |
| 255 | 218 | } |
| 256 | 219 | |
| 257 | 220 | /** |
| @@ -256,9 +219,9 @@ | ||
| 256 | 219 | |
| 257 | 220 | /** |
| 258 | 221 | * Remaps option keys. |
| 259 | 222 | * |
| 260 | - * @param array $remap_array an array of $old_key => $new_key values. | |
| 223 | + * @param array<string,string> $remap_array an array of $old_key => $new_key values. | |
| 261 | 224 | * |
| 262 | 225 | * @return bool |
| 263 | 226 | */ |
| 264 | 227 | public function remap_keys( $remap_array = [] ) { |
| @@ -276,13 +239,8 @@ | ||
| 276 | 239 | unset( $options[ $key ] ); |
| 277 | 240 | } |
| 278 | 241 | |
| 279 | 242 | $did_update = \update_option( $this->prefix . 'settings', $options ); |
| 280 | - | |
| 281 | - // If it updated, let's update the global variable. | |
| 282 | - if ( $did_update ) { | |
| 283 | - $this->data = $options; | |
| 284 | - } | |
| 285 | 243 | |
| 286 | 244 | return $did_update; |
| 287 | 245 | } |
| 288 | 246 | } |