1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of mundschenk-at/wp-data-storage. |
4
|
|
|
* |
5
|
|
|
* Copyright 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 Network Options API. |
29
|
|
|
* |
30
|
|
|
* @since 1.0.0 |
31
|
|
|
* |
32
|
|
|
* @author Peter Putzer <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class Network_Options extends Options { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The network ID. |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $network_id; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create new Network Options instance. |
45
|
|
|
* |
46
|
|
|
* @param string $prefix The prefix automatically added to option names. |
47
|
|
|
* @param int|null $network_id Optional. The network ID or null for the current network. Default null. |
48
|
|
|
*/ |
49
|
|
|
public function __construct( $prefix, $network_id = null ) { |
50
|
|
|
$this->network_id = ! empty( $network_id ) ? $network_id : \get_current_network_id(); |
51
|
|
|
|
52
|
|
|
parent::__construct( $prefix ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Retrieves an option value. |
57
|
|
|
* |
58
|
|
|
* @param string $option The option name (without the plugin-specific prefix). |
59
|
|
|
* @param mixed $default Optional. Default value to return if the option does not exist. Default null. |
60
|
|
|
* @param bool $raw Optional. Use the raw option name (i.e. don't call get_name). Default false. |
61
|
|
|
* |
62
|
|
|
* @return mixed Value set for the option. |
63
|
|
|
*/ |
64
|
|
|
public function get( $option, $default = null, $raw = false ) { |
65
|
|
|
$value = \get_network_option( $this->network_id, $raw ? $option : $this->get_name( $option ), $default ); |
66
|
|
|
|
67
|
|
|
if ( is_array( $default ) && '' === $value ) { |
68
|
|
|
$value = []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Sets or updates an option. |
76
|
|
|
* |
77
|
|
|
* @param string $option The option name (without the plugin-specific prefix). |
78
|
|
|
* @param mixed $value The value to store. |
79
|
|
|
* @param bool $autoload Optional. This value is ignored for network options, |
80
|
|
|
* which are always autoloaded. Default true. |
81
|
|
|
* @param bool $raw Optional. Use the raw option name (i.e. don't call get_name). Default false. |
82
|
|
|
* |
83
|
|
|
* @return bool False if value was not updated and true if value was updated. |
84
|
|
|
*/ |
85
|
|
|
public function set( $option, $value, $autoload = true, $raw = false ) { |
86
|
|
|
return \update_network_option( $this->network_id, $raw ? $option : $this->get_name( $option ), $value ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Deletes an option. |
91
|
|
|
* |
92
|
|
|
* @param string $option The option name (without the plugin-specific prefix). |
93
|
|
|
* @param bool $raw Optional. Use the raw option name (i.e. don't call get_name). Default false. |
94
|
|
|
* |
95
|
|
|
* @return bool True, if option is successfully deleted. False on failure. |
96
|
|
|
*/ |
97
|
|
|
public function delete( $option, $raw = false ) { |
98
|
|
|
return \delete_network_option( $this->network_id, $raw ? $option : $this->get_name( $option ) ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|