PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.2
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.2
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.2, at classes/Plugin/Options.php

289 lines 6.1 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
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 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 *
96 * @return mixed
97 */
98 return apply_filters( $this->namespace . 'get_option', $value, $key, $default_value );
99 }
100
101 /**
102 * Get an option using a dot notation key.
103 *
104 * @param string $key Option key in dot notation.
105 * @param bool $default_value Default value.
106 *
107 * @return mixed|void
108 */
109 public function get_notation( $key = '', $default_value = false ) {
110 $keys = explode( '.', $key );
111 $data = $this->get_all();
112
113 foreach ( $keys as $key ) {
114 if ( ! isset( $data[ $key ] ) ) {
115 return $default_value;
116 }
117
118 $data = $data[ $key ];
119 }
120
121 return $data;
122 }
123
124 /**
125 * Update an option
126 *
127 * Updates an setting value in both the db and the global variable.
128 * Warning: Passing in an empty, false or null string value will remove
129 * the key from the _options array.
130 *
131 * @since 1.0.0
132 *
133 * @param string $key The Key to update.
134 * @param string|bool|int $value The value to set the key to.
135 *
136 * @return boolean True if updated, false if not.
137 */
138 public function update( $key = '', $value = false ) {
139
140 // If no key, exit.
141 if ( empty( $key ) ) {
142 return false;
143 }
144
145 if ( empty( $value ) ) {
146 $remove_option = $this->delete( $key );
147
148 return $remove_option;
149 }
150
151 // First let's grab the current settings.
152 $options = $this->get_all();
153
154 /**
155 * Filter the new option value.
156 *
157 * @param mixed $value Option value.
158 * @param string $key Option key.
159 *
160 * @return mixed
161 */
162 $value = apply_filters( $this->namespace . 'update_option', $value, $key );
163
164 // Next let's try to update the value.
165 $options[ $key ] = $value;
166 $did_update = \update_option( $this->prefix . 'settings', $options );
167
168 // If it updated, let's update the global variable.
169 if ( $did_update ) {
170 $this->data[ $key ] = $value;
171 }
172
173 return $did_update;
174 }
175
176 /**
177 * Update many values at once.
178 *
179 * @param array $new_options Array of new replacement options.
180 *
181 * @return bool
182 */
183 public function update_many( $new_options = [] ) {
184 $options = $this->get_all();
185
186 // Lets merge options that may exist previously that are not existing now.
187 foreach ( $new_options as $key => $value ) {
188 // If no key, exit.
189 if ( empty( $key ) ) {
190 continue;
191 }
192
193 if ( empty( $value ) && isset( $options[ $key ] ) ) {
194 unset( $options[ $key ] );
195 }
196
197 /**
198 * Filter the new option value.
199 *
200 * @param mixed $value Option value.
201 * @param string $key Option key.
202 *
203 * @return mixed
204 */
205 $value = apply_filters( $this->namespace . 'update_option', $value, $key );
206
207 // Next let's try to update the value.
208 $options[ $key ] = $value;
209 }
210
211 $did_update = \update_option( $this->prefix . 'settings', $options );
212
213 // If it updated, let's update the global variable.
214 if ( $did_update ) {
215 $this->data = $options;
216 }
217
218 return $did_update;
219 }
220
221 /**
222 * Remove an option
223 *
224 * @param string|string[] $keys Can be a single string or array of option keys.
225 *
226 * @return boolean True if updated, false if not.
227 */
228 public function delete( $keys ) {
229
230 // If no key, exit.
231 if ( empty( $keys ) ) {
232 return false;
233 } elseif ( is_string( $keys ) ) {
234 $keys = [ $keys ];
235 }
236
237 // First let's grab the current settings.
238 $options = $this->get_all();
239
240 // Remove each key/value pair.
241 foreach ( $keys as $key ) {
242 if ( isset( $options[ $key ] ) ) {
243 unset( $options[ $key ] );
244 }
245 }
246
247 $did_update = \update_option( $this->prefix . 'settings', $options );
248
249 // If it updated, let's update the global variable.
250 if ( $did_update ) {
251 $this->data = $options;
252 }
253
254 return $did_update;
255 }
256
257 /**
258 * Remaps option keys.
259 *
260 * @param array $remap_array an array of $old_key => $new_key values.
261 *
262 * @return bool
263 */
264 public function remap_keys( $remap_array = [] ) {
265 $options = $this->get_all();
266
267 /**
268 * Remap array keys by first getting current value,
269 * moving it to new key, finally deleting old key.
270 */
271 foreach ( $remap_array as $key => $new_key ) {
272 $value = $this->get( $key, false );
273 if ( ! empty( $value ) ) {
274 $options[ $new_key ] = $value;
275 }
276 unset( $options[ $key ] );
277 }
278
279 $did_update = \update_option( $this->prefix . 'settings', $options );
280
281 // If it updated, let's update the global variable.
282 if ( $did_update ) {
283 $this->data = $options;
284 }
285
286 return $did_update;
287 }
288 }
289