| 1 |
<?php |
| 2 |
|
| 3 |
namespace JP\CC; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Options |
| 11 |
* @package JP\CC |
| 12 |
*/ |
| 13 |
class Options { |
| 14 |
|
| 15 |
/** |
| 16 |
* Unique Prefix per plugin. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
public static $_prefix; |
| 21 |
|
| 22 |
/** |
| 23 |
* Keeps static copy of the options during runtime. |
| 24 |
* |
| 25 |
* @var null|array |
| 26 |
*/ |
| 27 |
private static $_data; |
| 28 |
|
| 29 |
/** |
| 30 |
* Initialize Options on run. |
| 31 |
* |
| 32 |
* @param string $prefix |
| 33 |
*/ |
| 34 |
public static function init( $prefix = 'jp_cc' ) { |
| 35 |
// Set the prefix on init. |
| 36 |
static::$_prefix = ! empty( $prefix ) ? trim( $prefix, '_' ) . '_' : ''; |
| 37 |
static::$_data = static::get_all(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get Settings |
| 42 |
* |
| 43 |
* Retrieves all plugin settings |
| 44 |
* |
| 45 |
* @return array settings |
| 46 |
*/ |
| 47 |
public static function get_all() { |
| 48 |
$settings = get_option( static:: $_prefix . 'settings' ); |
| 49 |
if ( ! is_array( $settings ) ) { |
| 50 |
$settings = array(); |
| 51 |
} |
| 52 |
|
| 53 |
return apply_filters( static:: $_prefix . 'get_options', $settings ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get an option |
| 58 |
* |
| 59 |
* Looks to see if the specified setting exists, returns default if not |
| 60 |
* |
| 61 |
* @param string $key |
| 62 |
* @param bool $default |
| 63 |
* |
| 64 |
* @return mixed|void |
| 65 |
*/ |
| 66 |
public static function get( $key = '', $default = false ) { |
| 67 |
$value = isset( static::$_data[ $key ] ) ? static::$_data[ $key ] : $default; |
| 68 |
|
| 69 |
return apply_filters( static::$_prefix . 'get_option', $value, $key, $default ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Update an option |
| 74 |
* |
| 75 |
* Updates an setting value in both the db and the global variable. |
| 76 |
* Warning: Passing in an empty, false or null string value will remove |
| 77 |
* the key from the _options array. |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
* |
| 81 |
* @param string $key The Key to update |
| 82 |
* @param string|bool|int $value The value to set the key to |
| 83 |
* |
| 84 |
* @return boolean True if updated, false if not. |
| 85 |
*/ |
| 86 |
public static function update( $key = '', $value = false ) { |
| 87 |
|
| 88 |
// If no key, exit |
| 89 |
if ( empty( $key ) ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
if ( empty( $value ) ) { |
| 94 |
$remove_option = static::delete( $key ); |
| 95 |
|
| 96 |
return $remove_option; |
| 97 |
} |
| 98 |
|
| 99 |
// First let's grab the current settings |
| 100 |
$options = (array) get_option( static:: $_prefix . 'settings', array() ); |
| 101 |
|
| 102 |
// Let's let devs alter that value coming in |
| 103 |
$value = apply_filters( static::$_prefix . 'update_option', $value, $key ); |
| 104 |
|
| 105 |
// Next let's try to update the value |
| 106 |
$options[ $key ] = $value; |
| 107 |
$did_update = update_option( static:: $_prefix . 'settings', $options ); |
| 108 |
|
| 109 |
// If it updated, let's update the global variable |
| 110 |
if ( $did_update ) { |
| 111 |
static::$_data[ $key ] = $value; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
return $did_update; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Remove an option |
| 120 |
* |
| 121 |
* Removes a setting value in both the db and the global variable. |
| 122 |
* |
| 123 |
* @since 1.0.0 |
| 124 |
* |
| 125 |
* @param string $key The Key to delete |
| 126 |
* |
| 127 |
* @return boolean True if updated, false if not. |
| 128 |
*/ |
| 129 |
public static function delete( $key = '' ) { |
| 130 |
|
| 131 |
// If no key, exit |
| 132 |
if ( empty( $key ) ) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
// First let's grab the current settings |
| 137 |
$options = get_option( static:: $_prefix . 'settings' ); |
| 138 |
|
| 139 |
// Next let's try to update the value |
| 140 |
if ( isset( $options[ $key ] ) ) { |
| 141 |
unset( $options[ $key ] ); |
| 142 |
} |
| 143 |
|
| 144 |
$did_update = update_option( static:: $_prefix . 'settings', $options ); |
| 145 |
|
| 146 |
// If it updated, let's update the global variable |
| 147 |
if ( $did_update ) { |
| 148 |
static::$_data = $options; |
| 149 |
} |
| 150 |
|
| 151 |
return $did_update; |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|