| @@ -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<string,mixed> | |
| 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 |
| @@ -84,10 +76,18 @@ | ||
| 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. |
| @@ -99,31 +99,8 @@ | ||
| 99 | 99 | return apply_filters( $this->namespace . 'get_option', $value, $key, $default_value ); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
| 103 | - * Get an option using a dot notation key. | |
| 104 | - * | |
| 105 | - * @param string $key Option key in dot notation. | |
| 106 | - * @param bool $default_value Default value. | |
| 107 | - * | |
| 108 | - * @return mixed|void | |
| 109 | - */ | |
| 110 | - public function get_notation( $key = '', $default_value = false ) { | |
| 111 | - $keys = explode( '.', $key ); | |
| 112 | - $data = $this->get_all(); | |
| 113 | - | |
| 114 | - foreach ( $keys as $key ) { | |
| 115 | - if ( ! isset( $data[ $key ] ) ) { | |
| 116 | - return $default_value; | |
| 117 | - } | |
| 118 | - | |
| 119 | - $data = $data[ $key ]; | |
| 120 | - } | |
| 121 | - | |
| 122 | - return $data; | |
| 123 | - } | |
| 124 | - | |
| 125 | - /** | |
| 126 | 103 | * Update an option |
| 127 | 104 | * |
| 128 | 105 | * Updates an setting value in both the db and the global variable. |
| 129 | 106 | * Warning: Passing in an empty, false or null string value will remove |
| @@ -165,13 +142,8 @@ | ||
| 165 | 142 | // Next let's try to update the value. |
| 166 | 143 | $options[ $key ] = $value; |
| 167 | 144 | $did_update = \update_option( $this->prefix . 'settings', $options ); |
| 168 | 145 | |
| 169 | - // If it updated, let's update the global variable. | |
| 170 | - if ( $did_update ) { | |
| 171 | - $this->data[ $key ] = $value; | |
| 172 | - } | |
| 173 | - | |
| 174 | 146 | return $did_update; |
| 175 | 147 | } |
| 176 | 148 | |
| 177 | 149 | /** |
| @@ -210,13 +182,8 @@ | ||
| 210 | 182 | } |
| 211 | 183 | |
| 212 | 184 | $did_update = \update_option( $this->prefix . 'settings', $options ); |
| 213 | 185 | |
| 214 | - // If it updated, let's update the global variable. | |
| 215 | - if ( $did_update ) { | |
| 216 | - $this->data = $options; | |
| 217 | - } | |
| 218 | - | |
| 219 | 186 | return $did_update; |
| 220 | 187 | } |
| 221 | 188 | |
| 222 | 189 | /** |
| @@ -246,13 +213,8 @@ | ||
| 246 | 213 | } |
| 247 | 214 | |
| 248 | 215 | $did_update = \update_option( $this->prefix . 'settings', $options ); |
| 249 | 216 | |
| 250 | - // If it updated, let's update the global variable. | |
| 251 | - if ( $did_update ) { | |
| 252 | - $this->data = $options; | |
| 253 | - } | |
| 254 | - | |
| 255 | 217 | return $did_update; |
| 256 | 218 | } |
| 257 | 219 | |
| 258 | 220 | /** |
| @@ -277,13 +239,8 @@ | ||
| 277 | 239 | unset( $options[ $key ] ); |
| 278 | 240 | } |
| 279 | 241 | |
| 280 | 242 | $did_update = \update_option( $this->prefix . 'settings', $options ); |
| 281 | - | |
| 282 | - // If it updated, let's update the global variable. | |
| 283 | - if ( $did_update ) { | |
| 284 | - $this->data = $options; | |
| 285 | - } | |
| 286 | 243 | |
| 287 | 244 | return $did_update; |
| 288 | 245 | } |
| 289 | 246 | } |