Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/aimogen-pro/update-checker/Puc/v5p6/StateStore.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
namespace YahnisElsts\PluginUpdateChecker\v5p6;
3
+
4
+
if ( !class_exists(StateStore::class, false) ):
5
+
6
+
class StateStore {
7
+
/**
8
+
* @var int Last update check timestamp.
9
+
*/
10
+
protected $lastCheck = 0;
11
+
12
+
/**
13
+
* @var string Version number.
14
+
*/
15
+
protected $checkedVersion = '';
16
+
17
+
/**
18
+
* @var Update|null Cached update.
19
+
*/
20
+
protected $update = null;
21
+
22
+
/**
23
+
* @var string Site option name.
24
+
*/
25
+
private $optionName = '';
26
+
27
+
/**
28
+
* @var bool Whether we've already tried to load the state from the database.
29
+
*/
30
+
private $isLoaded = false;
31
+
32
+
public function __construct($optionName) {
33
+
$this->optionName = $optionName;
34
+
}
35
+
36
+
/**
37
+
* Get time elapsed since the last update check.
38
+
*
39
+
* If there are no recorded update checks, this method returns a large arbitrary number
40
+
* (i.e. time since the Unix epoch).
41
+
*
42
+
* @return int Elapsed time in seconds.
43
+
*/
44
+
public function timeSinceLastCheck() {
45
+
$this->lazyLoad();
46
+
return time() - $this->lastCheck;
47
+
}
48
+
49
+
/**
50
+
* @return int
51
+
*/
52
+
public function getLastCheck() {
53
+
$this->lazyLoad();
54
+
return $this->lastCheck;
55
+
}
56
+
57
+
/**
58
+
* Set the time of the last update check to the current timestamp.
59
+
*
60
+
* @return $this
61
+
*/
62
+
public function setLastCheckToNow() {
63
+
$this->lazyLoad();
64
+
$this->lastCheck = time();
65
+
return $this;
66
+
}
67
+
68
+
/**
69
+
* @return null|Update
70
+
*/
71
+
public function getUpdate() {
72
+
$this->lazyLoad();
73
+
return $this->update;
74
+
}
75
+
76
+
/**
77
+
* @param Update|null $update
78
+
* @return $this
79
+
*/
80
+
public function setUpdate($update = null) {
81
+
$this->lazyLoad();
82
+
$this->update = $update;
83
+
return $this;
84
+
}
85
+
86
+
/**
87
+
* @return string
88
+
*/
89
+
public function getCheckedVersion() {
90
+
$this->lazyLoad();
91
+
return $this->checkedVersion;
92
+
}
93
+
94
+
/**
95
+
* @param string $version
96
+
* @return $this
97
+
*/
98
+
public function setCheckedVersion($version) {
99
+
$this->lazyLoad();
100
+
$this->checkedVersion = strval($version);
101
+
return $this;
102
+
}
103
+
104
+
/**
105
+
* Get translation updates.
106
+
*
107
+
* @return array
108
+
*/
109
+
public function getTranslations() {
110
+
$this->lazyLoad();
111
+
if ( isset($this->update, $this->update->translations) ) {
112
+
return $this->update->translations;
113
+
}
114
+
return array();
115
+
}
116
+
117
+
/**
118
+
* Set translation updates.
119
+
*
120
+
* @param array $translationUpdates
121
+
*/
122
+
public function setTranslations($translationUpdates) {
123
+
$this->lazyLoad();
124
+
if ( isset($this->update) ) {
125
+
$this->update->translations = $translationUpdates;
126
+
$this->save();
127
+
}
128
+
}
129
+
130
+
public function save() {
131
+
$state = new \stdClass();
132
+
133
+
$state->lastCheck = $this->lastCheck;
134
+
$state->checkedVersion = $this->checkedVersion;
135
+
136
+
if ( isset($this->update)) {
137
+
$state->update = $this->update->toStdClass();
138
+
139
+
$updateClass = get_class($this->update);
140
+
$state->updateClass = $updateClass;
141
+
$prefix = $this->getLibPrefix();
142
+
if ( Utils::startsWith($updateClass, $prefix) ) {
143
+
$state->updateBaseClass = substr($updateClass, strlen($prefix));
144
+
}
145
+
}
146
+
147
+
update_site_option($this->optionName, $state);
148
+
$this->isLoaded = true;
149
+
}
150
+
151
+
/**
152
+
* @return $this
153
+
*/
154
+
public function lazyLoad() {
155
+
if ( !$this->isLoaded ) {
156
+
$this->load();
157
+
}
158
+
return $this;
159
+
}
160
+
161
+
protected function load() {
162
+
$this->isLoaded = true;
163
+
164
+
$state = get_site_option($this->optionName, null);
165
+
166
+
if (
167
+
!is_object($state)
168
+
//Sanity check: If the Utils class is missing, the plugin is probably in the process
169
+
//of being deleted (e.g. the old version gets deleted during an update).
170
+
|| !class_exists(Utils::class)
171
+
) {
172
+
$this->lastCheck = 0;
173
+
$this->checkedVersion = '';
174
+
$this->update = null;
175
+
return;
176
+
}
177
+
178
+
$this->lastCheck = intval(Utils::get($state, 'lastCheck', 0));
179
+
$this->checkedVersion = Utils::get($state, 'checkedVersion', '');
180
+
$this->update = null;
181
+
182
+
if ( isset($state->update) ) {
183
+
//This mess is due to the fact that the want the update class from this version
184
+
//of the library, not the version that saved the update.
185
+
186
+
$updateClass = null;
187
+
if ( isset($state->updateBaseClass) ) {
188
+
$updateClass = $this->getLibPrefix() . $state->updateBaseClass;
189
+
} else if ( isset($state->updateClass) ) {
190
+
$updateClass = $state->updateClass;
191
+
}
192
+
193
+
$factory = array($updateClass, 'fromObject');
194
+
if ( ($updateClass !== null) && is_callable($factory) ) {
195
+
$this->update = call_user_func($factory, $state->update);
196
+
}
197
+
}
198
+
}
199
+
200
+
public function delete() {
201
+
delete_site_option($this->optionName);
202
+
203
+
$this->lastCheck = 0;
204
+
$this->checkedVersion = '';
205
+
$this->update = null;
206
+
}
207
+
208
+
private function getLibPrefix() {
209
+
//This assumes that the current class is at the top of the versioned namespace.
210
+
return __NAMESPACE__ . '\\';
211
+
}
212
+
}
213
+
214
+
endif;
215
+