forked from thephuse/hackathon-wordpress-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.my-theme-options.php
More file actions
477 lines (373 loc) · 12.4 KB
/
Copy pathclass.my-theme-options.php
File metadata and controls
477 lines (373 loc) · 12.4 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* Master theme class
*
* @package Bolts
* @since 1.0
*/
class My_Theme_Options {
private $sections;
private $checkboxes;
private $settings;
/**
* Construct
*
* @since 1.0
*/
public function __construct() {
// This will keep track of the checkbox options for the validate_settings function.
$this->checkboxes = array();
$this->settings = array();
$this->get_settings();
$this->sections['footer'] = __( 'Footer Settings' );
add_action( 'admin_menu', array( &$this, 'add_pages' ) );
add_action( 'admin_init', array( &$this, 'register_settings' ) );
if ( ! get_option( 'mytheme_options' ) )
$this->initialize_settings();
}
/**
* Add options page
*
* @since 1.0
*/
public function add_pages() {
$admin_page = add_theme_page( __( 'Theme Options' ), __( 'Theme Options' ), 'manage_options', 'mytheme-options', array( &$this, 'display_page' ) );
add_action( 'admin_print_scripts-' . $admin_page, array( &$this, 'scripts' ) );
add_action( 'admin_print_styles-' . $admin_page, array( &$this, 'styles' ) );
}
/**
* Create settings field
*
* @since 1.0
*/
public function create_setting( $args = array() ) {
$defaults = array(
'id' => 'default_field',
'title' => __( 'Default Field' ),
'desc' => __( 'This is a default description.' ),
'std' => '',
'type' => 'text',
'section' => 'footer',
'choices' => array(),
'class' => ''
);
extract( wp_parse_args( $args, $defaults ) );
$field_args = array(
'type' => $type,
'id' => $id,
'desc' => $desc,
'std' => $std,
'choices' => $choices,
'label_for' => $id,
'class' => $class
);
if ( $type == 'checkbox' )
$this->checkboxes[] = $id;
add_settings_field( $id, $title, array( $this, 'display_setting' ), 'mytheme-options', $section, $field_args );
}
/**
* Display options page
*
* @since 1.0
*/
public function display_page() {
echo '<div class="wrap">
<div class="icon32" id="icon-options-general"></div>
<h2>' . __( 'Theme Options' ) . '</h2>';
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true )
echo '<div class="updated fade"><p>' . __( 'Theme options updated.' ) . '</p></div>';
echo '<form action="options.php" method="post">';
settings_fields( 'mytheme_options' );
echo '<div class="ui-tabs">
<ul class="ui-tabs-nav">';
foreach ( $this->sections as $section_slug => $section )
echo '<li><a href="#' . $section_slug . '">' . $section . '</a></li>';
echo '</ul>';
do_settings_sections( $_GET['page'] );
echo '</div>
<p class="submit"><input name="Submit" type="submit" class="button-primary" value="' . __( 'Save Changes' ) . '" /></p>
</form>';
echo '<script type="text/javascript">
jQuery(document).ready(function($) {
var sections = [];';
foreach ( $this->sections as $section_slug => $section )
echo "sections['$section'] = '$section_slug';";
echo 'var wrapped = $(".wrap h3").wrap("<div class=\"ui-tabs-panel\">");
wrapped.each(function() {
$(this).parent().append($(this).parent().nextUntil("div.ui-tabs-panel"));
});
$(".ui-tabs-panel").each(function(index) {
$(this).attr("id", sections[$(this).children("h3").text()]);
if (index > 0)
$(this).addClass("ui-tabs-hide");
});
$(".ui-tabs").tabs({
fx: { opacity: "toggle", duration: "fast" }
});
$("input[type=text], textarea").each(function() {
if ($(this).val() == $(this).attr("placeholder") || $(this).val() == "")
$(this).css("color", "#999");
});
$("input[type=text], textarea").focus(function() {
if ($(this).val() == $(this).attr("placeholder") || $(this).val() == "") {
$(this).val("");
$(this).css("color", "#000");
}
}).blur(function() {
if ($(this).val() == "" || $(this).val() == $(this).attr("placeholder")) {
$(this).val($(this).attr("placeholder"));
$(this).css("color", "#999");
}
});
$(".wrap h3, .wrap table").show();
// This will make the "warning" checkbox class really stand out when checked.
// I use it here for the Reset checkbox.
$(".warning").change(function() {
if ($(this).is(":checked"))
$(this).parent().css("background", "#c00").css("color", "#fff").css("fontWeight", "bold");
else
$(this).parent().css("background", "none").css("color", "inherit").css("fontWeight", "normal");
});
// Browser compatibility
if ($.browser.mozilla)
$("form").attr("autocomplete", "off");
});
</script>
</div>';
}
/**
* Description for section
*
* @since 1.0
*/
public function display_section() {
// code
}
/**
* Description for About section
*
* @since 1.0
*/
public function display_about_section() {
// This displays on the "About" tab. Echo regular HTML here, like so:
// echo '<p>Copyright 2011 me@example.com</p>';
}
/**
* HTML output for text field
*
* @since 1.0
*/
public function display_setting( $args = array() ) {
extract( $args );
$options = get_option( 'mytheme_options' );
if ( ! isset( $options[$id] ) && $type != 'checkbox' )
$options[$id] = $std;
elseif ( ! isset( $options[$id] ) )
$options[$id] = 0;
$field_class = '';
if ( $class != '' )
$field_class = ' ' . $class;
switch ( $type ) {
case 'heading':
echo '</td></tr><tr valign="top"><td colspan="2"><h4>' . $desc . '</h4>';
break;
case 'checkbox':
echo '<input class="checkbox' . $field_class . '" type="checkbox" id="' . $id . '" name="mytheme_options[' . $id . ']" value="1" ' . checked( $options[$id], 1, false ) . ' /> <label for="' . $id . '">' . $desc . '</label>';
break;
case 'select':
echo '<select class="select' . $field_class . '" name="mytheme_options[' . $id . ']">';
foreach ( $choices as $value => $label )
echo '<option value="' . esc_attr( $value ) . '"' . selected( $options[$id], $value, false ) . '>' . $label . '</option>';
echo '</select>';
if ( $desc != '' )
echo '<br /><span class="description">' . $desc . '</span>';
break;
case 'radio':
$i = 0;
foreach ( $choices as $value => $label ) {
echo '<input class="radio' . $field_class . '" type="radio" name="mytheme_options[' . $id . ']" id="' . $id . $i . '" value="' . esc_attr( $value ) . '" ' . checked( $options[$id], $value, false ) . '> <label for="' . $id . $i . '">' . $label . '</label>';
if ( $i < count( $options ) - 1 )
echo '<br />';
$i++;
}
if ( $desc != '' )
echo '<br /><span class="description">' . $desc . '</span>';
break;
case 'textarea':
echo '<textarea class="' . $field_class . '" id="' . $id . '" name="mytheme_options[' . $id . ']" placeholder="' . $std . '" rows="5" cols="30">' . wp_htmledit_pre( $options[$id] ) . '</textarea>';
if ( $desc != '' )
echo '<br /><span class="description">' . $desc . '</span>';
break;
case 'password':
echo '<input class="regular-text' . $field_class . '" type="password" id="' . $id . '" name="mytheme_options[' . $id . ']" value="' . esc_attr( $options[$id] ) . '" />';
if ( $desc != '' )
echo '<br /><span class="description">' . $desc . '</span>';
break;
case 'text':
default:
echo '<input class="regular-text' . $field_class . '" type="text" id="' . $id . '" name="mytheme_options[' . $id . ']" placeholder="' . $std . '" value="' . esc_attr( $options[$id] ) . '" />';
if ( $desc != '' )
echo '<br /><span class="description">' . $desc . '</span>';
break;
}
}
/**
* Settings and defaults
*
* @since 1.0
*/
public function get_settings() {
/* Footer Settings
===========================================*/
$this->settings['twitter'] = array(
'title' => __( 'Twitter URL' ),
'desc' => __( 'Enter the URL for your Twitter account (i.e. https://twitter.com/civichackingday) or leave it blank for none.' ),
'type' => 'text',
'section' => 'footer'
);
$this->settings['hashtag'] = array(
'title' => __( 'Twitter Hashtag' ),
'desc' => __( 'Enter the hashtag for your event (e.g. #hashtag) or leave it blank for none.' ),
'type' => 'text',
'section' => 'footer'
);
$this->settings['facebook'] = array(
'title' => __( 'Facebook URL' ),
'desc' => __( 'Enter the URL for your Facebook page (e.g. http://fb.me/hackforchange) or leave it blank for none.' ),
'type' => 'text',
'section' => 'footer'
);
$this->settings['flickr'] = array(
'title' => __( 'Flickr URL' ),
'desc' => __( 'Enter the URL for your Flickr group (e.g. https://www.flickr.com/groups/hackforchange/) or leave it blank for none.' ),
'type' => 'text',
'section' => 'footer'
);
$this->settings['contact-email'] = array(
'title' => __( 'Contact Email' ),
'desc' => __( 'Enter the contact email for the footer, or leave it blank for none.' ),
'type' => 'text',
'section' => 'footer'
);
/*
$this->settings['example_textarea'] = array(
'title' => __( 'Example Textarea Input' ),
'desc' => __( 'This is a description for the textarea input.' ),
'std' => 'Default value',
'type' => 'textarea',
'section' => 'footer'
);
$this->settings['example_checkbox'] = array(
'section' => 'general',
'title' => __( 'Example Checkbox' ),
'desc' => __( 'This is a description for the checkbox.' ),
'type' => 'checkbox',
'std' => 1 // Set to 1 to be checked by default, 0 to be unchecked by default.
);
$this->settings['example_heading'] = array(
'section' => 'general',
'title' => '', // Not used for headings.
'desc' => 'Example Heading',
'type' => 'heading'
);
$this->settings['example_radio'] = array(
'section' => 'general',
'title' => __( 'Example Radio' ),
'desc' => __( 'This is a description for the radio buttons.' ),
'type' => 'radio',
'std' => '',
'choices' => array(
'choice1' => 'Choice 1',
'choice2' => 'Choice 2',
'choice3' => 'Choice 3'
)
);
$this->settings['example_select'] = array(
'section' => 'general',
'title' => __( 'Example Select' ),
'desc' => __( 'This is a description for the drop-down.' ),
'type' => 'select',
'std' => '',
'choices' => array(
'choice1' => 'Other Choice 1',
'choice2' => 'Other Choice 2',
'choice3' => 'Other Choice 3'
)
);
*/
}
/**
* Initialize settings to their default values
*
* @since 1.0
*/
public function initialize_settings() {
$default_settings = array();
foreach ( $this->settings as $id => $setting ) {
if ( $setting['type'] != 'heading' )
$default_settings[$id] = $setting['std'];
}
update_option( 'mytheme_options', $default_settings );
}
/**
* Register settings
*
* @since 1.0
*/
public function register_settings() {
register_setting( 'mytheme_options', 'mytheme_options', array ( &$this, 'validate_settings' ) );
foreach ( $this->sections as $slug => $title ) {
if ( $slug == 'about' )
add_settings_section( $slug, $title, array( &$this, 'display_about_section' ), 'mytheme-options' );
else
add_settings_section( $slug, $title, array( &$this, 'display_section' ), 'mytheme-options' );
}
$this->get_settings();
foreach ( $this->settings as $id => $setting ) {
$setting['id'] = $id;
$this->create_setting( $setting );
}
}
/**
* jQuery Tabs
*
* @since 1.0
*/
public function scripts() {
wp_print_scripts( 'jquery-ui-tabs' );
}
/**
* Styling for the theme options page
*
* @since 1.0
*/
public function styles() {
wp_register_style( 'mytheme-admin', get_bloginfo( 'stylesheet_directory' ) . '/mytheme-options.css' );
wp_enqueue_style( 'mytheme-admin' );
}
/**
* Validate settings
*
* @since 1.0
*/
public function validate_settings( $input ) {
if ( ! isset( $input['reset_theme'] ) ) {
$options = get_option( 'mytheme_options' );
foreach ( $this->checkboxes as $id ) {
if ( isset( $options[$id] ) && ! isset( $input[$id] ) )
unset( $options[$id] );
}
return $input;
}
return false;
}
}
$theme_options = new My_Theme_Options();
function mytheme_option( $option ) {
$options = get_option( 'mytheme_options' );
if ( isset( $options[$option] ) )
return $options[$option];
else
return false;
}
?>