|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Wordlift_Configuration_Service class. |
|
4
|
|
|
* |
|
5
|
|
|
* The {@link Wordlift_Configuration_Service} class provides helper functions to get configuration parameter values. |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://wordlift.io |
|
8
|
|
|
* |
|
9
|
|
|
* @package Wordlift |
|
10
|
|
|
* @since 3.6.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get WordLift's configuration settings stored in WordPress database. |
|
19
|
|
|
* |
|
20
|
|
|
* @since 3.6.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Wordlift_Configuration_Service { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The entity base path option name. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 3.6.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The skip wizard (admin installation wizard) option name. |
|
33
|
|
|
* |
|
34
|
|
|
* @since 3.9.0 |
|
35
|
|
|
*/ |
|
36
|
|
|
const SKIP_WIZARD = 'wl_skip_wizard'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* WordLift's key option name. |
|
40
|
|
|
* |
|
41
|
|
|
* @since 3.9.0 |
|
42
|
|
|
*/ |
|
43
|
|
|
const KEY = 'key'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* WordLift's configured language option name. |
|
47
|
|
|
* |
|
48
|
|
|
* @since 3.9.0 |
|
49
|
|
|
*/ |
|
50
|
|
|
const LANGUAGE = 'site_language'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The publisher entity post ID option name. |
|
54
|
|
|
* |
|
55
|
|
|
* @since 3.9.0 |
|
56
|
|
|
*/ |
|
57
|
|
|
const PUBLISHER_ID = 'publisher_id'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The dataset URI option name |
|
61
|
|
|
* |
|
62
|
|
|
* @since 3.10.0 |
|
63
|
|
|
*/ |
|
64
|
|
|
const DATASET_URI = 'redlink_dataset_uri'; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The Wordlift_Configuration_Service's singleton instance. |
|
68
|
|
|
* |
|
69
|
|
|
* @since 3.6.0 |
|
70
|
|
|
* |
|
71
|
|
|
* @access private |
|
72
|
|
|
* @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
|
73
|
|
|
*/ |
|
74
|
|
|
private static $instance; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create a Wordlift_Configuration_Service's instance. |
|
78
|
|
|
* |
|
79
|
|
|
* @since 3.6.0 |
|
80
|
|
|
*/ |
|
81
|
|
|
public function __construct() { |
|
82
|
|
|
|
|
83
|
|
|
self::$instance = $this; |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get the singleton instance. |
|
89
|
|
|
* |
|
90
|
|
|
* @since 3.6.0 |
|
91
|
|
|
* |
|
92
|
|
|
* @return \Wordlift_Configuration_Service |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function get_instance() { |
|
95
|
|
|
|
|
96
|
|
|
return self::$instance; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get a configuration given the option name and a key. The option value is |
|
101
|
|
|
* expected to be an array. |
|
102
|
|
|
* |
|
103
|
|
|
* @since 3.6.0 |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $option The option name. |
|
106
|
|
|
* @param string $key A key in the option value array. |
|
107
|
|
|
* @param string $default The default value in case the key is not found (by default an empty string). |
|
108
|
|
|
* |
|
109
|
|
|
* @return mixed The configuration value or the default value if not found. |
|
110
|
|
|
*/ |
|
111
|
|
|
private function get( $option, $key, $default = '' ) { |
|
112
|
|
|
|
|
113
|
|
|
$options = get_option( $option, array() ); |
|
114
|
|
|
|
|
115
|
|
|
return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Set a configuration parameter. |
|
120
|
|
|
* |
|
121
|
|
|
* @since 3.9.0 |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
|
124
|
|
|
* @param string $key The value key. |
|
125
|
|
|
* @param mixed $value The value. |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
private function set( $option, $key, $value ) { |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
$values = get_option( $option ); |
|
130
|
|
|
$values = isset( $values ) ? $values : array(); |
|
131
|
|
|
$values[ $key ] = $value; |
|
132
|
|
|
update_option( $option, $values ); |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the entity base path, by default 'entity'. |
|
138
|
|
|
* |
|
139
|
|
|
* @since 3.6.0 |
|
140
|
|
|
* |
|
141
|
|
|
* @return string The entity base path. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function get_entity_base_path() { |
|
144
|
|
|
|
|
145
|
|
|
return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get the entity base path. |
|
150
|
|
|
* |
|
151
|
|
|
* @since 3.9.0 |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $value The entity base path. |
|
154
|
|
|
*/ |
|
155
|
|
|
public function set_entity_base_path( $value ) { |
|
156
|
|
|
|
|
157
|
|
|
$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Whether the installation skip wizard should be skipped. |
|
162
|
|
|
* |
|
163
|
|
|
* @since 3.9.0 |
|
164
|
|
|
* |
|
165
|
|
|
* @return bool True if it should be skipped otherwise false. |
|
166
|
|
|
*/ |
|
167
|
|
|
public function is_skip_wizard() { |
|
168
|
|
|
|
|
169
|
|
|
return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Set the skip wizard parameter. |
|
174
|
|
|
* |
|
175
|
|
|
* @since 3.9.0 |
|
176
|
|
|
* |
|
177
|
|
|
* @param bool $value True to skip the wizard. We expect a boolean value. |
|
178
|
|
|
*/ |
|
179
|
|
|
public function set_skip_wizard( $value ) { |
|
180
|
|
|
|
|
181
|
|
|
$this->set( 'wl_general_settings', self::SKIP_WIZARD, $value === true ); |
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get WordLift's key. |
|
187
|
|
|
* |
|
188
|
|
|
* @since 3.9.0 |
|
189
|
|
|
* |
|
190
|
|
|
* @return WordLift's key or an empty string if not set. |
|
|
|
|
|
|
191
|
|
|
*/ |
|
192
|
|
|
public function get_key() { |
|
193
|
|
|
|
|
194
|
|
|
return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Set WordLift's key. |
|
199
|
|
|
* |
|
200
|
|
|
* @since 3.9.0 |
|
201
|
|
|
* |
|
202
|
|
|
* @param string $value WordLift's key. |
|
203
|
|
|
*/ |
|
204
|
|
|
public function set_key( $value ) { |
|
205
|
|
|
|
|
206
|
|
|
$this->set( 'wl_general_settings', self::KEY, $value ); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Get WordLift's configured language, by default 'en'. |
|
211
|
|
|
* |
|
212
|
|
|
* Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
|
213
|
|
|
* |
|
214
|
|
|
* @since 3.9.0 |
|
215
|
|
|
* |
|
216
|
|
|
* @return string WordLift's configured language code ('en' by default). |
|
217
|
|
|
*/ |
|
218
|
|
|
public function get_language_code() { |
|
219
|
|
|
|
|
220
|
|
|
return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' ); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Set WordLift's language code, used when storing strings to the Linked Data dataset. |
|
225
|
|
|
* |
|
226
|
|
|
* @since 3.9.0 |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $value WordLift's language code. |
|
229
|
|
|
*/ |
|
230
|
|
|
public function set_language_code( $value ) { |
|
231
|
|
|
|
|
232
|
|
|
$this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
233
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get the publisher entity post id. |
|
238
|
|
|
* |
|
239
|
|
|
* The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
|
240
|
|
|
* Article markup. |
|
241
|
|
|
* |
|
242
|
|
|
* @since 3.9.0 |
|
243
|
|
|
* |
|
244
|
|
|
* @return int|NULL The publisher entity post id or NULL if not set. |
|
245
|
|
|
*/ |
|
246
|
|
|
public function get_publisher_id() { |
|
247
|
|
|
|
|
248
|
|
|
return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Set the publisher entity post id. |
|
253
|
|
|
* |
|
254
|
|
|
* @since 3.9.0 |
|
255
|
|
|
* |
|
256
|
|
|
* @param int $value The publisher entity post id. |
|
257
|
|
|
*/ |
|
258
|
|
|
public function set_publisher_id( $value ) { |
|
259
|
|
|
|
|
260
|
|
|
$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
261
|
|
|
|
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Get the dataset URI. |
|
266
|
|
|
* |
|
267
|
|
|
* @since 3.10.0 |
|
268
|
|
|
* |
|
269
|
|
|
* @return string The dataset URI or an empty string if not set. |
|
270
|
|
|
*/ |
|
271
|
|
|
public function get_dataset_uri() { |
|
272
|
|
|
|
|
273
|
|
|
return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Set the dataset URI. |
|
278
|
|
|
* |
|
279
|
|
|
* @since 3.10.0 |
|
280
|
|
|
* |
|
281
|
|
|
* @param string $value The dataset URI. |
|
282
|
|
|
*/ |
|
283
|
|
|
public function set_dataset_uri( $value ) { |
|
284
|
|
|
|
|
285
|
|
|
$this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Intercept the change of the WordLift key in order to set the dataset URI. |
|
290
|
|
|
* |
|
291
|
|
|
* @since 3.11.0 |
|
292
|
|
|
* |
|
293
|
|
|
* @param array $old_value The old settings. |
|
294
|
|
|
* @param array $new_value The new settings. |
|
295
|
|
|
*/ |
|
296
|
|
|
public function update_key( $old_value, $new_value ) { |
|
297
|
|
|
|
|
298
|
|
|
// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed. |
|
299
|
|
|
$old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
300
|
|
|
$new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
301
|
|
|
|
|
302
|
|
|
// If the key hasn't changed, don't do anything. |
|
303
|
|
|
// WARN The 'update_option' hook is fired only if the new and old value are not equal |
|
304
|
|
|
if ( $old_key === $new_key ) { |
|
305
|
|
|
return; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
// If the key is empty, empty the dataset URI. |
|
309
|
|
|
if ( '' === $new_key ) { |
|
310
|
|
|
$this->set_dataset_uri( '' ); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
// Request the dataset URI. |
|
314
|
|
|
$response = wp_remote_get( $this->get_accounts_by_key_dataset_uri( $new_key ), unserialize( WL_REDLINK_API_HTTP_OPTIONS ) ); |
|
315
|
|
|
|
|
316
|
|
|
// If the response is valid, then set the value. |
|
317
|
|
|
if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) { |
|
318
|
|
|
|
|
319
|
|
|
$this->set_dataset_uri( $response['body'] ); |
|
320
|
|
|
|
|
321
|
|
|
} else { |
|
|
|
|
|
|
322
|
|
|
// TO DO User notification is needed here. |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Get the API URI to retrieve the dataset URI using the WordLift Key. |
|
329
|
|
|
* |
|
330
|
|
|
* @since 3.11.0 |
|
331
|
|
|
* |
|
332
|
|
|
* @param string $key The WordLift key to use. |
|
333
|
|
|
* |
|
334
|
|
|
* @return string The API URI. |
|
335
|
|
|
*/ |
|
336
|
|
|
public function get_accounts_by_key_dataset_uri( $key ) { |
|
337
|
|
|
|
|
338
|
|
|
return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
} |
|
342
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.