| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\Options; |
| 4 |
|
| 5 |
/** |
| 6 |
* Parent class for options. |
| 7 |
* Adapted from https://carlalexander.ca/designing-classes-wordpress-options-api/ |
| 8 |
* |
| 9 |
* Class OptionsBase |
| 10 |
* |
| 11 |
* @package Codelight\GDPR\Options |
| 12 |
*/ |
| 13 |
abstract class OptionsBase |
| 14 |
{ |
| 15 |
/* @var string */ |
| 16 |
protected $prefix = 'gdpr_'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Auto-prefix all options |
| 20 |
* |
| 21 |
* @param $name |
| 22 |
* @return string |
| 23 |
*/ |
| 24 |
public function prefix($name) |
| 25 |
{ |
| 26 |
// Check for accidental duplicate prefix |
| 27 |
if ("" === strpos($name, $this->prefix)) { |
| 28 |
trigger_error("You appear to have a duplicate prefix for option {$name}", E_USER_NOTICE); |
| 29 |
return $name; |
| 30 |
} |
| 31 |
|
| 32 |
return $this->prefix . $name; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Checks if the option with the given name exists or not. |
| 37 |
* |
| 38 |
* @param string $name |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function has($name) |
| 43 |
{ |
| 44 |
return null !== $this->get($name); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Gets the option for the given name. Returns the default value if the value does not exist. |
| 49 |
* |
| 50 |
* @param string $name |
| 51 |
* @param mixed $default |
| 52 |
* |
| 53 |
* @return mixed |
| 54 |
*/ |
| 55 |
abstract public function get($name, $default = null); |
| 56 |
|
| 57 |
/** |
| 58 |
* Removes the option with the given name. |
| 59 |
* |
| 60 |
* @param string $name |
| 61 |
*/ |
| 62 |
abstract public function delete($name); |
| 63 |
|
| 64 |
/** |
| 65 |
* Sets an option. Overwrites the existing option if the name is already in use. |
| 66 |
* |
| 67 |
* @param string $name |
| 68 |
* @param mixed $value |
| 69 |
*/ |
| 70 |
abstract public function set($name, $value); |
| 71 |
} |