This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-settings-framework.php
349 lines (325 loc) · 13.9 KB
/
wp-settings-framework.php
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* WordPress Settings Framework
*
* @author Gilbert Pellegrom
* @link https://github.com/gilbitron/WordPress-Settings-Framework
* @version 1.4
* @license MIT
*/
if( !class_exists('WordPressSettingsFramework') ){
/**
* WordPressSettingsFramework class
*/
class WordPressSettingsFramework {
/**
* @access private
* @var string
*/
private $option_group;
/**
* Constructor
*
* @param string path to settings file
* @param string optional "option_group" override
*/
function __construct( $settings_file, $option_group = '' )
{
if( !is_file( $settings_file ) ) return;
require_once( $settings_file );
$this->option_group = preg_replace("/[^a-z0-9]+/i", "", basename( $settings_file, '.php' ));
if( $option_group ) $this->option_group = $option_group;
add_action('admin_init', array(&$this, 'admin_init'));
add_action('admin_notices', array(&$this, 'admin_notices'));
add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts'));
}
/**
* Get the option group for this instance
*
* @return string the "option_group"
*/
function get_option_group()
{
return $this->option_group;
}
/**
* Registers the internal WordPress settings
*/
function admin_init()
{
register_setting( $this->option_group, $this->option_group .'_settings', array(&$this, 'settings_validate') );
$this->process_settings();
}
/**
* Displays any errors from the WordPress settings API
*/
function admin_notices()
{
settings_errors();
}
/**
* Enqueue scripts and styles
*/
function admin_enqueue_scripts()
{
wp_enqueue_style('farbtastic');
wp_enqueue_style('thickbox');
wp_enqueue_script('jquery');
wp_enqueue_script('farbtastic');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
}
/**
* Adds a filter for settings validation
*
* @param array the un-validated settings
* @return array the validated settings
*/
function settings_validate( $input )
{
return apply_filters( $this->option_group .'_settings_validate', $input );
}
/**
* Displays the "section_description" if speicified in $wpsf_settings
*
* @param array callback args from add_settings_section()
*/
function section_intro( $args )
{
global $wpsf_settings;
if(!empty($wpsf_settings)){
foreach($wpsf_settings as $section){
if($section['section_id'] == $args['id']){
if(isset($section['section_description']) && $section['section_description']) echo '<p>'. $section['section_description'] .'</p>';
break;
}
}
}
}
/**
* Processes $wpsf_settings and adds the sections and fields via the WordPress settings API
*/
function process_settings()
{
global $wpsf_settings;
if(!empty($wpsf_settings)){
usort($wpsf_settings, array(&$this, 'sort_array'));
foreach($wpsf_settings as $section){
if(isset($section['section_id']) && $section['section_id'] && isset($section['section_title'])){
add_settings_section( $section['section_id'], $section['section_title'], array(&$this, 'section_intro'), $this->option_group );
if(isset($section['fields']) && is_array($section['fields']) && !empty($section['fields'])){
foreach($section['fields'] as $field){
if(isset($field['id']) && $field['id'] && isset($field['title'])){
add_settings_field( $field['id'], $field['title'], array(&$this, 'generate_setting'), $this->option_group, $section['section_id'], array('section' => $section, 'field' => $field) );
}
}
}
}
}
}
}
/**
* Usort callback. Sorts $wpsf_settings by "section_order"
*
* @param mixed section order a
* @param mixed section order b
* @return int order
*/
function sort_array( $a, $b )
{
return $a['section_order'] > $b['section_order'];
}
/**
* Generates the HTML output of the settings fields
*
* @param array callback args from add_settings_field()
*/
function generate_setting( $args )
{
$section = $args['section'];
$defaults = array(
'id' => 'default_field',
'title' => 'Default Field',
'desc' => '',
'std' => '',
'type' => 'text',
'choices' => array(),
'class' => ''
);
$defaults = apply_filters( 'wpsf_defaults', $defaults );
extract( wp_parse_args( $args['field'], $defaults ) );
$options = get_option( $this->option_group .'_settings' );
$el_id = $this->option_group .'_'. $section['section_id'] .'_'. $id;
$val = (isset($options[$el_id])) ? $options[$el_id] : $std;
do_action('wpsf_before_field');
do_action('wpsf_before_field_'. $el_id);
switch( $type ){
case 'text':
$val = esc_attr(stripslashes($val));
echo '<input type="text" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="'. $val .'" class="regular-text '. $class .'" />';
if($desc) echo '<p class="description">'. $desc .'</p>';
break;
case 'textarea':
$val = esc_html(stripslashes($val));
echo '<textarea name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" rows="5" cols="60" class="'. $class .'">'. $val .'</textarea>';
if($desc) echo '<p class="description">'. $desc .'</p>';
break;
case 'select':
$val = esc_html(esc_attr($val));
echo '<select name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" class="'. $class .'">';
foreach($choices as $ckey=>$cval){
echo '<option value="'. $ckey .'"'. (($ckey == $val) ? ' selected="selected"' : '') .'>'. $cval .'</option>';
}
echo '</select>';
if($desc) echo '<p class="description">'. $desc .'</p>';
break;
case 'radio':
$val = esc_html(esc_attr($val));
foreach($choices as $ckey=>$cval){
echo '<label><input type="radio" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'_'. $ckey .'" value="'. $ckey .'" class="'. $class .'"'. (($ckey == $val) ? ' checked="checked"' : '') .' /> '. $cval .'</label><br />';
}
if($desc) echo '<p class="description">'. $desc .'</p>';
break;
case 'checkbox':
$val = esc_attr(stripslashes($val));
echo '<input type="hidden" name="'. $this->option_group .'_settings['. $el_id .']" value="0" />';
echo '<label><input type="checkbox" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="1" class="'. $class .'"'. (($val) ? ' checked="checked"' : '') .' /> '. $desc .'</label>';
break;
case 'checkboxes':
foreach($choices as $ckey=>$cval){
$val = '';
if(isset($options[$el_id .'_'. $ckey])) $val = $options[$el_id .'_'. $ckey];
elseif(is_array($std) && in_array($ckey, $std)) $val = $ckey;
$val = esc_html(esc_attr($val));
echo '<input type="hidden" name="'. $this->option_group .'_settings['. $el_id .'_'. $ckey .']" value="0" />';
echo '<label><input type="checkbox" name="'. $this->option_group .'_settings['. $el_id .'_'. $ckey .']" id="'. $el_id .'_'. $ckey .'" value="'. $ckey .'" class="'. $class .'"'. (($ckey == $val) ? ' checked="checked"' : '') .' /> '. $cval .'</label><br />';
}
if($desc) echo '<p class="description">'. $desc .'</p>';
break;
case 'color':
$val = esc_attr(stripslashes($val));
echo '<div style="position:relative;">';
echo '<input type="text" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="'. $val .'" class="'. $class .'" />';
echo '<div id="'. $el_id .'_cp" style="position:absolute;top:0;left:190px;background:#fff;z-index:9999;"></div>';
if($desc) echo '<p class="description">'. $desc .'</p>';
echo '<script type="text/javascript">
jQuery(document).ready(function($){
var colorPicker = $("#'. $el_id .'_cp");
colorPicker.farbtastic("#'. $el_id .'");
colorPicker.hide();
$("#'. $el_id .'").live("focus", function(){
colorPicker.show();
});
$("#'. $el_id .'").live("blur", function(){
colorPicker.hide();
if($(this).val() == "") $(this).val("#");
});
});
</script></div>';
break;
case 'file':
$val = esc_attr($val);
echo '<input type="text" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="'. $val .'" class="regular-text '. $class .'" /> ';
echo '<input type="button" class="button wpsf-browse" id="'. $el_id .'_button" value="Browse" />';
echo '<script type="text/javascript">
jQuery(document).ready(function($){
$("#'. $el_id .'_button").click(function() {
tb_show("", "media-upload.php?post_id=0&type=image&TB_iframe=true");
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html) {
var imgurl = $("img",html).attr("src");
$("#'. $el_id .'").val(imgurl);
tb_remove();
window.send_to_editor = window.original_send_to_editor;
};
return false;
});
});
</script>';
break;
case 'editor':
wp_editor( $val, $el_id, array( 'textarea_name' => $this->option_group .'_settings['. $el_id .']' ) );
if($desc) echo '<p class="description">'. $desc .'</p>';
break;
case 'custom':
echo $std;
break;
default:
break;
}
do_action('wpsf_after_field');
do_action('wpsf_after_field_'. $el_id);
}
/**
* Output the settings form
*/
function settings()
{
do_action('wpsf_before_settings');
?>
<form action="options.php" method="post">
<?php do_action('wpsf_before_settings_fields'); ?>
<?php settings_fields( $this->option_group ); ?>
<?php do_settings_sections( $this->option_group ); ?>
<p class="submit"><input type="submit" class="button-primary" value="<?php _e( 'Save All Changes' ); ?>" /></p>
</form>
<?php
do_action('wpsf_after_settings');
}
}
}
if( !function_exists('wpsf_get_option_group') ){
/**
* Converts the settings file name to option group id
*
* @param string settings file
* @return string option group id
*/
function wpsf_get_option_group( $settings_file ){
$option_group = preg_replace("/[^a-z0-9]+/i", "", basename( $settings_file, '.php' ));
return $option_group;
}
}
if( !function_exists('wpsf_get_settings') ){
/**
* Get the settings from a settings file/option group
*
* @param string path to settings file
* @param string optional "option_group" override
* @return array settings
*/
function wpsf_get_settings( $settings_file, $option_group = '' ){
$opt_group = preg_replace("/[^a-z0-9]+/i", "", basename( $settings_file, '.php' ));
if( $option_group ) $opt_group = $option_group;
return get_option( $opt_group .'_settings' );
}
}
if( !function_exists('wpsf_get_setting') ){
/**
* Get a setting from an option group
*
* @param string option group id
* @param string section id
* @param string field id
* @return mixed setting or false if no setting exists
*/
function wpsf_get_setting( $option_group, $section_id, $field_id ){
$options = get_option( $option_group .'_settings' );
if(isset($options[$option_group .'_'. $section_id .'_'. $field_id])) return $options[$option_group .'_'. $section_id .'_'. $field_id];
return false;
}
}
if( !function_exists('wpsf_delete_settings') ){
/**
* Delete all the saved settings from a settings file/option group
*
* @param string path to settings file
* @param string optional "option_group" override
*/
function wpsf_delete_settings( $settings_file, $option_group = '' ){
$opt_group = preg_replace("/[^a-z0-9]+/i", "", basename( $settings_file, '.php' ));
if( $option_group ) $opt_group = $option_group;
delete_option( $opt_group .'_settings' );
}
}
?>