|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of mundschenk-at/wp-data-storage. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright 2017-2018 Peter Putzer. |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or |
|
8
|
|
|
* modify it under the terms of the GNU General Public License |
|
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
10
|
|
|
* of the License, or ( at your option ) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program; if not, write to the Free Software |
|
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
20
|
|
|
* |
|
21
|
|
|
* @package mundschenk-at/wp-data-storage/tests |
|
22
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Mundschenk\Data_Storage; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Implements an interface to the WordPress Options API. |
|
29
|
|
|
* |
|
30
|
|
|
* @since 1.0.0 |
|
31
|
|
|
* |
|
32
|
|
|
* @author Peter Putzer <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
class Options { |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The prefix added to all keys. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $prefix; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create new Options instance. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $prefix The prefix automatically added to option names. |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function __construct( $prefix ) { |
|
49
|
1 |
|
$this->prefix = $prefix; |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Retrieves an option value. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $option The option name (without the plugin-specific prefix). |
|
56
|
|
|
* @param mixed $default Optional. Default value to return if the option does not exist. Default null. |
|
57
|
|
|
* @param bool $raw Optional. Use the raw option name (i.e. don't call get_name). Default false. |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed Value set for the option. |
|
60
|
|
|
*/ |
|
61
|
3 |
|
public function get( $option, $default = null, $raw = false ) { |
|
62
|
3 |
|
$value = \get_option( $raw ? $option : $this->get_name( $option ), $default ); |
|
63
|
|
|
|
|
64
|
3 |
|
if ( is_array( $default ) && '' === $value ) { |
|
65
|
1 |
|
$value = []; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
return $value; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Sets or updates an option. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $option The option name (without the plugin-specific prefix). |
|
75
|
|
|
* @param mixed $value The value to store. |
|
76
|
|
|
* @param bool $autoload Optional. Whether to load the option when WordPress |
|
77
|
|
|
* starts up. For existing options, $autoload can only |
|
78
|
|
|
* be updated using update_option() if $value is also |
|
79
|
|
|
* changed. Default true. |
|
80
|
|
|
* @param bool $raw Optional. Use the raw option name (i.e. don't call get_name). Default false. |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool False if value was not updated and true if value was updated. |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function set( $option, $value, $autoload = true, $raw = false ) { |
|
85
|
2 |
|
return \update_option( $raw ? $option : $this->get_name( $option ), $value, $autoload ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Deletes an option. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $option The option name (without the plugin-specific prefix). |
|
92
|
|
|
* @param bool $raw Optional. Use the raw option name (i.e. don't call get_name). Default false. |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool True, if option is successfully deleted. False on failure. |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function delete( $option, $raw = false ) { |
|
97
|
2 |
|
return \delete_option( $raw ? $option : $this->get_name( $option ) ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Retrieves the complete option name to use. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $option The option name (without the plugin-specific prefix). |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function get_name( $option ) { |
|
108
|
2 |
|
return "{$this->prefix}{$option}"; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|