| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\Options; |
| 4 |
|
| 5 |
/** |
| 6 |
* Main class for handling options on a non-multisite install. |
| 7 |
* Adapted from https://carlalexander.ca/designing-classes-wordpress-options-api/ |
| 8 |
* |
| 9 |
* Class Options |
| 10 |
* |
| 11 |
* @package Codelight\GDPR\Options |
| 12 |
*/ |
| 13 |
class Options extends OptionsBase |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Gets the option for the given name. Returns the default value if the value does not exist. |
| 17 |
* |
| 18 |
* @param string $name |
| 19 |
* @param mixed $default |
| 20 |
* |
| 21 |
* @return mixed |
| 22 |
*/ |
| 23 |
public function get($name, $default = null, $enableFilter = true) |
| 24 |
{ |
| 25 |
$value = get_option($this->prefix($name), $default); |
| 26 |
|
| 27 |
if ($enableFilter) { |
| 28 |
$value = apply_filters("gdpr/options/get/{$name}", $value); |
| 29 |
} |
| 30 |
|
| 31 |
if (is_array($default) && !is_array($value)) { |
| 32 |
$value = (array) $value; |
| 33 |
} |
| 34 |
|
| 35 |
return $value; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Sets an option. Overwrites the existing option if the name is already in use. |
| 40 |
* |
| 41 |
* @param string $name |
| 42 |
* @param mixed $value |
| 43 |
*/ |
| 44 |
public function set($name, $value, $enableFilter = true) |
| 45 |
{ |
| 46 |
if ($enableFilter) { |
| 47 |
$value = apply_filters("gdpr/options/set/{$name}", $value, get_option($this->prefix($name))); |
| 48 |
} |
| 49 |
|
| 50 |
update_option( |
| 51 |
$this->prefix($name), |
| 52 |
$value, |
| 53 |
false |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Removes the option with the given name. |
| 59 |
* |
| 60 |
* @param string $name |
| 61 |
*/ |
| 62 |
public function delete($name) |
| 63 |
{ |
| 64 |
delete_option($this->prefix($name)); |
| 65 |
} |
| 66 |
} |