PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.12
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.12
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Plugin / Options.php

Options.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.12, at classes/Plugin/Options.php

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