Completed
Push — master ( e85ac8...7dbd8d )
by Der Mundschenk
03:18
created

Options::set()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 4
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
	public function __construct( $prefix ) {
49
		$this->prefix = $prefix;
50
	}
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
	public function get( $option, $default = null, $raw = false ) {
62
		$value = \get_option( $raw ? $option : $this->get_name( $option ), $default );
63
64
		if ( is_array( $default ) && '' === $value ) {
65
			$value = [];
66
		}
67
68
		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
	public function set( $option, $value, $autoload = true, $raw = false ) {
85
		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
	public function delete( $option, $raw = false ) {
97
		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
	public function get_name( $option ) {
108
		return "{$this->prefix}{$option}";
109
	}
110
}
111