Options.php
5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* Plugin options manager..
*
* @copyright (c) 2021, Code Atlantic LLC.
*
* @package ContentControl\Plugin
*/
namespace ContentControl\Plugin;
defined( 'ABSPATH' ) || exit;
/**
* Class Options
*/
class Options {
/**
* Unique Prefix per plugin.
*
* @var string
*/
public $prefix;
/**
* Action namespace.
*
* @var string
*/
public $namespace;
/**
* Initialize Options on run.
*
* @param string $prefix Settings key prefix.
*/
public function __construct( $prefix = 'content_control' ) {
// Set the prefix on init.
$this->prefix = ! empty( $prefix ) ? trim( $prefix, '_' ) . '_' : '';
$this->namespace = ! empty( $prefix ) ? trim( $prefix, '_/' ) . '/' : '';
}
/**
* Get Settings
*
* Retrieves all plugin settings
*
* @return array<string,mixed> settings
*/
public function get_all() {
$settings = \get_option( $this->prefix . 'settings' );
if ( ! is_array( $settings ) ) {
$settings = \ContentControl\get_default_settings();
\update_option( $this->prefix . 'settings', $settings );
}
/**
* Filter the settings.
*
* @param array $settings Settings.
*
* @return array
*/
return apply_filters( $this->namespace . 'get_options', $settings );
}
/**
* Get an option
*
* Looks to see if the specified setting exists, returns default if not
*
* @param string $key Option key.
* @param bool $default_value Default value.
*
* @return mixed|void
*/
public function get( $key = '', $default_value = false ) {
$data = $this->get_all();
// Fetch key from array, converting to camelcase (how data is stored). Supports dot.notation.
$value = \ContentControl\fetch_key_from_array( $key, $data, 'camelCase' );
// If no value, return default.
if ( null === $value ) {
$value = $default_value;
}
/**
* Filter the option.
*
* @param mixed $value Option value.
* @param string $key Option key.
* @param mixed $default_value Default value.
*
* @return mixed
*/
return apply_filters( $this->namespace . 'get_option', $value, $key, $default_value );
}
/**
* Update an option
*
* Updates an setting value in both the db and the global variable.
* Warning: Passing in an empty, false or null string value will remove
* the key from the _options array.
*
* @since 1.0.0
*
* @param string $key The Key to update.
* @param string|bool|int $value The value to set the key to.
*
* @return boolean True if updated, false if not.
*/
public function update( $key = '', $value = false ) {
// If no key, exit.
if ( empty( $key ) ) {
return false;
}
if ( empty( $value ) ) {
$remove_option = $this->delete( $key );
return $remove_option;
}
// First let's grab the current settings.
$options = $this->get_all();
/**
* Filter the new option value.
*
* @param mixed $value Option value.
* @param string $key Option key.
*
* @return mixed
*/
$value = apply_filters( $this->namespace . 'update_option', $value, $key );
// Next let's try to update the value.
$options[ $key ] = $value;
$did_update = \update_option( $this->prefix . 'settings', $options );
return $did_update;
}
/**
* Update many values at once.
*
* @param array<string,mixed> $new_options Array of new replacement options.
*
* @return bool
*/
public function update_many( $new_options = [] ) {
$options = $this->get_all();
// Lets merge options that may exist previously that are not existing now.
foreach ( $new_options as $key => $value ) {
// If no key, exit.
if ( empty( $key ) ) {
continue;
}
if ( empty( $value ) && isset( $options[ $key ] ) ) {
unset( $options[ $key ] );
}
/**
* Filter the new option value.
*
* @param mixed $value Option value.
* @param string $key Option key.
*
* @return mixed
*/
$value = apply_filters( $this->namespace . 'update_option', $value, $key );
// Next let's try to update the value.
$options[ $key ] = $value;
}
$did_update = \update_option( $this->prefix . 'settings', $options );
return $did_update;
}
/**
* Remove an option
*
* @param string|string[] $keys Can be a single string or array of option keys.
*
* @return boolean True if updated, false if not.
*/
public function delete( $keys ) {
// If no key, exit.
if ( empty( $keys ) ) {
return false;
} elseif ( is_string( $keys ) ) {
$keys = [ $keys ];
}
// First let's grab the current settings.
$options = $this->get_all();
// Remove each key/value pair.
foreach ( $keys as $key ) {
if ( isset( $options[ $key ] ) ) {
unset( $options[ $key ] );
}
}
$did_update = \update_option( $this->prefix . 'settings', $options );
return $did_update;
}
/**
* Remaps option keys.
*
* @param array<string,string> $remap_array an array of $old_key => $new_key values.
*
* @return bool
*/
public function remap_keys( $remap_array = [] ) {
$options = $this->get_all();
/**
* Remap array keys by first getting current value,
* moving it to new key, finally deleting old key.
*/
foreach ( $remap_array as $key => $new_key ) {
$value = $this->get( $key, false );
if ( ! empty( $value ) ) {
$options[ $new_key ] = $value;
}
unset( $options[ $key ] );
}
$did_update = \update_option( $this->prefix . 'settings', $options );
return $did_update;
}
}