|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\json; |
|
4
|
|
|
|
|
5
|
|
|
use EventEspresso\core\services\database\WordPressOption; |
|
6
|
|
|
use stdClass; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class JsonDataWordpressOption |
|
10
|
|
|
* A version of WordPressOption that allows more complex data to be saved and accessed via individual properties |
|
11
|
|
|
* |
|
12
|
|
|
* @author Brent Christensen |
|
13
|
|
|
* @package EventEspresso\core\services\json |
|
14
|
|
|
* @since $VID:$ |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class JsonDataWordpressOption extends WordPressOption |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $options; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var JsonDataHandler |
|
26
|
|
|
*/ |
|
27
|
|
|
private $json_data_handler; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* JsonDataWordpressOption constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param JsonDataHandler $json_data_handler |
|
34
|
|
|
* @param string $option_name |
|
35
|
|
|
* @param $default_value |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(JsonDataHandler $json_data_handler, string $option_name, $default_value) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->json_data_handler = $json_data_handler; |
|
40
|
|
|
$this->json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
41
|
|
|
parent::__construct($option_name, $default_value); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param $options |
|
47
|
|
|
*/ |
|
48
|
|
|
private function update($options) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($this->updateOption($this->json_data_handler->encodeData($options))) { |
|
51
|
|
|
$this->options = $options; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $property |
|
58
|
|
|
* @param mixed $value |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
|
public function addProperty(string $property, $value) |
|
62
|
|
|
{ |
|
63
|
|
|
$options = $this->getAll(); |
|
64
|
|
|
$options->{$property} = $value; |
|
65
|
|
|
$this->update($options); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $property |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getProperty(string $property) |
|
74
|
|
|
{ |
|
75
|
|
|
$options = $this->getAll(); |
|
76
|
|
|
return property_exists($options, $property) ? $options->{$property} : null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return array|mixed|stdClass |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getAll() |
|
84
|
|
|
{ |
|
85
|
|
|
if (empty($this->options)) { |
|
86
|
|
|
$this->options = $this->json_data_handler->decodeJson($this->loadOption()); |
|
87
|
|
|
} |
|
88
|
|
|
return $this->options; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $property |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public function removeProperty(string $property) |
|
97
|
|
|
{ |
|
98
|
|
|
$options = $this->getAll(); |
|
99
|
|
|
unset($options->{$property}); |
|
100
|
|
|
$this->update($options); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|