| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles all the WPCode settings. |
| 4 |
* |
| 5 |
* @package WPCode |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Wsrw_Settings. |
| 10 |
*/ |
| 11 |
class WSRW_Settings { |
| 12 |
|
| 13 |
/** |
| 14 |
* The key used for storing settings in the db. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $settings_key = 'WSRW_Settings'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Options as they are loaded from the db. |
| 22 |
* |
| 23 |
* @var array |
| 24 |
* @see WSRW_Settings::get_options |
| 25 |
*/ |
| 26 |
private $options; |
| 27 |
|
| 28 |
/** |
| 29 |
* Get an option by name with an optional default value. |
| 30 |
* |
| 31 |
* @param string $option_name The option name. |
| 32 |
* @param mixed $default The default value (optional). |
| 33 |
* |
| 34 |
* @return mixed |
| 35 |
* @see get_option |
| 36 |
*/ |
| 37 |
public function get_option( $option_name, $default = false ) { |
| 38 |
$options = $this->get_options(); |
| 39 |
$value = $default; |
| 40 |
if ( isset( $options[ $option_name ] ) ) { |
| 41 |
$value = $options[ $option_name ]; |
| 42 |
} |
| 43 |
|
| 44 |
return apply_filters( "wsrw_get_option_{$option_name}", $value ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get all the options as they are stored in the db. |
| 49 |
* |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
public function get_options() { |
| 53 |
if ( ! isset( $this->options ) ) { |
| 54 |
$this->options = get_option( $this->settings_key, array() ); |
| 55 |
} |
| 56 |
|
| 57 |
return $this->options; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Update an option in the settings object. |
| 62 |
* |
| 63 |
* @param string $option The option name. |
| 64 |
* @param mixed $value The new value. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function update_option( $option, $value ) { |
| 69 |
if ( empty( $value ) ) { |
| 70 |
$this->delete_option( $option ); |
| 71 |
|
| 72 |
return; |
| 73 |
} |
| 74 |
if ( isset( $this->options[ $option ] ) && $this->options[ $option ] === $value ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
$this->options[ $option ] = $value; |
| 78 |
|
| 79 |
$this->save_options(); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Delete an option by its name. |
| 84 |
* |
| 85 |
* @param string $option The option name. |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function delete_option( $option ) { |
| 90 |
// If there's nothing to delete, do nothing. |
| 91 |
if ( isset( $this->options[ $option ] ) ) { |
| 92 |
unset( $this->options[ $option ] ); |
| 93 |
$this->save_options(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Save the current options object to the db. |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
private function save_options() { |
| 103 |
update_option( $this->settings_key, (array) $this->options ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Use an array to update multiple settings at once. |
| 108 |
* |
| 109 |
* @param array $options The new options array. |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function bulk_update_options( $options ) { |
| 114 |
$this->options = array_merge( $this->get_options(), $options ); |
| 115 |
|
| 116 |
$this->save_options(); |
| 117 |
} |
| 118 |
} |
| 119 |
|