forked from Objectivco/wordpress-simple-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordpress-simple-settings.php
More file actions
169 lines (150 loc) · 4.22 KB
/
wordpress-simple-settings.php
File metadata and controls
169 lines (150 loc) · 4.22 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* WordPress Simple Settings
*
* A simple framework for managing WordPress plugin settings.
*
* @author Clifton H. Griffin II
* @version 0.2.1
* @copyright Clif Griffin Development, Inc. 2013
* @license GNU GPL version 3 (or later) {@see license.txt}
**/
abstract class WordPress_SimpleSettings {
var $settings = array();
var $prefix;
/**
* Constructor
*
* @author Clifton H. Griffin II
* @since 0.1
*
* @return void
**/
public function __construct() {
// Set a default prefix
if( function_exists('get_called_class') && empty($this->prefix) ) $this->prefix = get_called_class();
$this->settings = $this->get_settings_obj( $this->prefix );
add_action('admin_init', array($this, 'save_settings') );
}
/**
* Add a new setting
*
* @author Clifton H. Griffin II
* @since 0.1
*
* @param string $setting The name of the new option
* @param string $value The value of the new option
* @return boolean True if successful, false otherwise
**/
public function add_setting ( $setting = false, $value ) {
if($setting === false ) return false;
if ( ! isset($this->settings[$setting]) ) {
return $this->update_setting($setting, $value);
} else return false;
}
/**
* Updates or adds a setting
*
* @author Clifton H. Griffin II
* @since 0.1
*
* @param string $setting The name of the option
* @param string $value The new value of the option
* @return boolean True if successful, false if not
**/
public function update_setting ( $setting = false, $value ) {
if( $setting === false ) return false;
$this->settings = $this->get_settings_obj($this->prefix);
$this->settings[$setting] = $value;
return $this->set_settings_obj($this->settings);
}
/**
* Retrieves a setting value
*
* @author Clifton H. Griffin II
* @since 0.1
*
* @param string $setting The name of the option
* @param string $type The return format preferred, string or array. Default: string
* @return mixed The value of the setting
**/
public function get_setting ( $setting = false, $type = 'string' ) {
if($setting === false || ! isset($this->settings[$setting]) ) return false;
$value = $this->settings[$setting];
if( strtolower($type) == 'array' && ! empty($value) ) {
$value = (array)explode(";", $value);
}
return apply_filters($this->prefix . '_get_setting', $value, $setting);
}
/**
* Generates HTML field name for a particular setting
*
* @author Clifton H. Griffin II
* @since 0.1
*
* @param string $setting The name of the setting
* @param string $type The return format of the field, string or array. Default: string
* @return string The name of the field
**/
public function get_field_name($setting, $type = 'string') {
return "{$this->prefix}_setting[$setting][$type]";
}
/**
* Prints nonce for admin form
*
* @author Clifton H. Griffin II
* @since 0.1
*
* @return void
**/
public function the_nonce() {
wp_nonce_field( "save_{$this->prefix}_settings", "{$this->prefix}_save" );
}
/**
* Saves settings
*
* @author Clifton H. Griffin II
* @since 0.1
*
* @return void
**/
public function save_settings() {
if( isset($_REQUEST["{$this->prefix}_setting"]) && check_admin_referer("save_{$this->prefix}_settings","{$this->prefix}_save") ) {
$new_settings = $_REQUEST["{$this->prefix}_setting"];
foreach( $new_settings as $setting_name => $setting_value ) {
foreach( $setting_value as $type => $value ) {
if( $type == "array" ) {
if ( ! is_array($value) && ! empty($value) ) $value = (array)explode(";", $value);
$this->update_setting($setting_name, $value);
} else {
$this->update_setting($setting_name, $value);
}
}
}
do_action("{$this->prefix}_settings_saved");
}
}
/**
* Gets settings object
*
* @author Clifton H. Griffin II
* @since 0.1
*
* @return void
**/
public function get_settings_obj () {
return get_option("{$this->prefix}_settings", false);
}
/**
* Sets settings object
*
* @author Clifton H. Griffin II
* @since 0.1
*
* @param array $newobj The new settings object
* @return boolean True if successful, false otherwise
**/
public function set_settings_obj ( $newobj ) {
return update_option("{$this->prefix}_settings", $newobj);
}
}