Completed
Push — master ( 48986a...95c69f )
by David
06:07
created

Wordlift_Configuration_Service::get_key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 14.

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.

Loading history...
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 ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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.
0 ignored issues
show
Documentation introduced by
The doc-type WordLift's could not be parsed: Unknown type name "WordLift's" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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 {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
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