config-manager.php
2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* @author W-Shadow
* @copyright 2009
*/
if ( ! class_exists( 'blcConfigurationManager' ) ) {
class blcConfigurationManager {
var $option_name;
var $options;
var $defaults;
var $loaded_values;
/**
* @var bool Whether options have been successfully loaded from the database.
*/
public $db_option_loaded = false;
function __construct( $option_name = '', $default_settings = null ) {
$this->option_name = $option_name;
if ( is_array( $default_settings ) ) {
$this->defaults = $default_settings;
} else {
$this->defaults = array();
}
$this->loaded_values = array();
$this->options = $this->defaults;
if ( ! empty( $this->option_name ) ) {
$this->load_options();
}
}
function set_defaults( $default_settings = null ) {
if ( is_array( $default_settings ) ) {
$this->defaults = array();
} else {
$this->defaults = $default_settings;
}
$this->options = array_merge( $this->defaults, $this->loaded_values );
}
/**
* blcOptionManager::load_options()
* Load plugin options from the database. The current $options values are not affected
* if this function fails.
*
* @param string $option_name
* @return bool True if options were loaded, false otherwise.
*/
function load_options( $option_name = '' ) {
$this->db_option_loaded = false;
if ( ! empty( $option_name ) ) {
$this->option_name = $option_name;
}
if ( empty( $this->option_name ) ) {
return false;
}
$new_options = get_option( $this->option_name );
//Decode JSON (if applicable).
if ( is_string( $new_options ) && ! empty( $new_options ) ) {
$new_options = json_decode( $new_options, true );
}
if ( ! is_array( $new_options ) ) {
return false;
} else {
$this->loaded_values = $new_options;
$this->options = array_merge( $this->defaults, $this->loaded_values );
$this->db_option_loaded = true;
return true;
}
}
/**
* blcOptionManager::save_options()
* Save plugin options to the database.
*
* @param string $option_name (Optional) Save the options under this name
* @return bool True if settings were saved, false if settings haven't been changed or if there was an error.
*/
function save_options( $option_name = '' ) {
if ( ! empty( $option_name ) ) {
$this->option_name = $option_name;
}
if ( empty( $this->option_name ) ) {
return false;
}
return update_option( $this->option_name, json_encode( $this->options ) );
}
/**
* Retrieve a specific setting.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
function get( $key, $default = null ) {
if ( array_key_exists( $key, $this->options ) ) {
return $this->options[ $key ];
} else {
return $default;
}
}
/**
* Update or add a setting.
*
* @param string $key
* @param mixed $value
* @return void
*/
function set( $key, $value ) {
$this->options[ $key ] = $value;
}
}
}