PluginProbe
The GDPR Framework By Data443 / trunk
The GDPR Framework By Data443 vtrunk
2.5.0 2.4.0 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.3 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 All 41 releases
gdpr-framework / src / Options / OptionsBase.php

OptionsBase.php in The GDPR Framework By Data443 trunk, at src/Options/OptionsBase.php

76 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Fix: this compared strpos() against "" instead of 0. strpos() returns
28 // int 0 when $name starts with the prefix and false when it does not --
29 // it never returns "" -- so under strict comparison the guard was
30 // unreachable and already-prefixed names were silently read from and
31 // written to 'gdpr_gdpr_*'.
32 if (0 === strpos($name, $this->prefix)) {
33 trigger_error("You appear to have a duplicate prefix for option {$name}", E_USER_NOTICE);
34 return $name;
35 }
36
37 return $this->prefix . $name;
38 }
39
40 /**
41 * Checks if the option with the given name exists or not.
42 *
43 * @param string $name
44 *
45 * @return bool
46 */
47 public function has($name)
48 {
49 return null !== $this->get($name);
50 }
51
52 /**
53 * Gets the option for the given name. Returns the default value if the value does not exist.
54 *
55 * @param string $name
56 * @param mixed $default
57 *
58 * @return mixed
59 */
60 abstract public function get($name, $default = null);
61
62 /**
63 * Removes the option with the given name.
64 *
65 * @param string $name
66 */
67 abstract public function delete($name);
68
69 /**
70 * Sets an option. Overwrites the existing option if the name is already in use.
71 *
72 * @param string $name
73 * @param mixed $value
74 */
75 abstract public function set($name, $value);
76 }