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