| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Settings implements HC3_ISettings |
| 3 |
{ |
| 4 |
protected $prefix = ''; |
| 5 |
|
| 6 |
protected $defaults = array(); |
| 7 |
protected $loaded = array(); |
| 8 |
|
| 9 |
public function __construct( $prefix ) |
| 10 |
{ |
| 11 |
$this->prefix = $prefix; |
| 12 |
} |
| 13 |
|
| 14 |
public function init( $name, $value ) |
| 15 |
{ |
| 16 |
$this->defaults[$name] = $value; |
| 17 |
return $this; |
| 18 |
} |
| 19 |
|
| 20 |
public function set( $name, $value ) |
| 21 |
{ |
| 22 |
update_option( $this->prefix . $name, $value ); |
| 23 |
|
| 24 |
$this->loaded[ $name ] = $value; |
| 25 |
return $this; |
| 26 |
} |
| 27 |
|
| 28 |
public function resetAll() |
| 29 |
{ |
| 30 |
$allOptions = wp_load_alloptions(); |
| 31 |
foreach( $allOptions as $option => $value ){ |
| 32 |
if( strpos($option, $this->prefix) === 0 ){ |
| 33 |
delete_option( $option ); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public function reset( $name ) |
| 39 |
{ |
| 40 |
delete_option( $this->prefix . $name ); |
| 41 |
unset( $this->loaded[$name] ); |
| 42 |
return $this; |
| 43 |
} |
| 44 |
|
| 45 |
public function get( $name, $wantArray = false ) |
| 46 |
{ |
| 47 |
static $repo = null; |
| 48 |
if( null === $repo ){ |
| 49 |
$repo = wp_load_alloptions(); |
| 50 |
} |
| 51 |
|
| 52 |
$ret = null; |
| 53 |
|
| 54 |
if( array_key_exists($name, $this->loaded) ){ |
| 55 |
$ret = $this->loaded[ $name ]; |
| 56 |
} |
| 57 |
else { |
| 58 |
$default = array_key_exists( $name, $this->defaults ) ? $this->defaults[$name] : null; |
| 59 |
|
| 60 |
$prefixedName = $this->prefix . $name; |
| 61 |
$ret = array_key_exists( $prefixedName, $repo ) ? $repo[ $prefixedName ] : $default; |
| 62 |
$ret = maybe_unserialize( $ret ); |
| 63 |
|
| 64 |
if( null !== $ret ){ |
| 65 |
$this->loaded[ $name ] = $ret; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if( $wantArray && (! is_array($ret)) ){ |
| 70 |
$ret = ( null === $ret ) ? array() : array($ret); |
| 71 |
} |
| 72 |
|
| 73 |
if( (! $wantArray) && is_array($ret) ){ |
| 74 |
$ret = array_shift( $ret ); |
| 75 |
} |
| 76 |
|
| 77 |
return $ret; |
| 78 |
} |
| 79 |
|
| 80 |
public function get_( $name, $wantArray = false ) |
| 81 |
{ |
| 82 |
$ret = null; |
| 83 |
|
| 84 |
if( array_key_exists($name, $this->loaded) ){ |
| 85 |
$ret = $this->loaded[$name]; |
| 86 |
} |
| 87 |
else { |
| 88 |
$default = array_key_exists($name, $this->defaults) ? $this->defaults[$name] : NULL; |
| 89 |
$ret = get_option( $this->prefix . $name, $default ); |
| 90 |
|
| 91 |
if( NULL !== $ret ){ |
| 92 |
$this->loaded[$name] = $ret; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if( $wantArray && (! is_array($ret)) ){ |
| 97 |
$ret = ( null === $ret ) ? array() : array($ret); |
| 98 |
} |
| 99 |
|
| 100 |
if( (! $wantArray) && is_array($ret) ){ |
| 101 |
$ret = array_shift( $ret ); |
| 102 |
} |
| 103 |
|
| 104 |
return $ret; |
| 105 |
} |
| 106 |
} |