/
theme-options.php
executable file
·392 lines (344 loc) · 13.6 KB
/
theme-options.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
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
<?php
/**
* Credits:
* http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
*/
add_action( 'admin_init', 'blaskan_options_init' );
add_action( 'admin_menu', 'blaskan_options_add_page' );
/**
* Init plugin options to white list our options
*/
function blaskan_options_init() {
register_setting( 'theme_options', 'blaskan_options', 'blaskan_options_validate' );
register_taxonomy_for_object_type( 'post_tag', 'page' );
register_taxonomy_for_object_type( 'category', 'page' );
}
/**
* Properly enqueue styles and scripts for our theme options page
*/
function blaskan_admin_enqueue_scripts( $hook_suffix ) {
wp_enqueue_style( 'blaskan-theme-options', get_template_directory_uri() . '/theme-options.css' );
wp_enqueue_script( 'blaskan-theme-options', get_template_directory_uri() . '/theme-options.js', array( 'farbtastic' ) );
wp_enqueue_style( 'farbtastic' );
}
add_action( 'admin_print_styles-appearance_page_theme_options', 'blaskan_admin_enqueue_scripts' );
/**
* Load up the menu page
*/
function blaskan_options_add_page() {
add_theme_page( __( 'Theme Options', 'blaskan' ), __( 'Theme Options', 'blaskan' ), 'edit_theme_options', 'theme_options', 'blaskan_options_do_page' );
}
/**
* Create arrays for the sidebars on pages options
*/
$sidebars_options = array(
'2' => array(
'value' => 'two_sidebars'
),
'1' => array(
'value' => 'one_sidebar'
)
);
/**
* Create arrays for typeface in titles options
*/
$typeface_title_options = array(
'1' => array(
'value' => 'default',
'label' => __( 'League Gothic (default)', 'blaskan' )
),
'2' => array(
'value' => 'sans_serif',
'label' => __( 'Helvetica Neue, sans-serif', 'blaskan' )
)
);
/**
* Create the options page
*/
function blaskan_options_do_page() {
global $sidebars_options;
global $typeface_title_options;
if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false;
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'blaskan' ) . "</h2>"; ?>
<?php if ( false !== $_REQUEST['updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e( 'Options saved', 'blaskan' ); ?></strong></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'theme_options' ); ?>
<?php $options = get_option( 'blaskan_options' ); ?>
<table class="form-table">
<tr><th colspan="2"><strong><?php _e( 'Design', 'blaskan' ); ?></strong></th></tr>
<?php
/**
* Typeface in titles
*/
?>
<tr valign="top"><th scope="row"><?php _e( 'Typeface in titles', 'blaskan' ); ?></th>
<td>
<p>
<?php _e( "Choose which typeface to use in titles. The default typeface, League Gothic, doesn't play nicely in all languages and/or perhaps you'd prefer Helvetica instead.", 'blaskan' ); ?>
</p>
<?php
if ( empty( $options['typeface_titles'] ) ) {
$options['typeface_titles'] = '';
}
$selected = $options['typeface_titles'];
$typeface_options = array();
foreach ( $typeface_title_options as $option ) {
if ( $selected == $option['value'] ) {
$typeface_options[] = '<input checked="checked" type="radio" name="blaskan_options[typeface_titles]" value="' . esc_attr( $option['value'] ) . '">';
} else {
$typeface_options[] = '<input type="radio" name="blaskan_options[typeface_titles]" value="' . esc_attr( $option['value'] ) . '">';
}
}
?>
<div class="blaskan-option">
<label class="description">
<?php echo $typeface_options[0]; ?>
<br>
<span class="blaskan-typeface-option blaskan-typeface-option-league-gothic">
League Gothic
</span>
</label>
</div>
<div class="blaskan-option">
<label class="description">
<?php echo $typeface_options[1]; ?>
<br>
<span class="blaskan-typeface-option blaskan-typeface-option-helvetica">
Helvetica Neue / Sans Serif
</span>
</label>
</div>
<div style="clear: both;"></div>
</td>
</tr>
<?php
/**
* Link color
*/
if ( empty( $options['link_color'] ) ) {
$link_color = '#2e6eb0';
} else {
$link_color = stripslashes( $options['link_color'] );
}
?>
<tr valign="top"><th scope="row"><?php _e( 'Link color', 'blaskan' ); ?></th>
<td style="position:relative">
<input type="text" name="blaskan_options[link_color]" id="link-color" value="<?php echo esc_attr( $link_color ); ?>" />
<a href="#" class="pickcolor hide-if-no-js" id="link-color-example"></a>
<input type="button" class="pickcolor button hide-if-no-js" value="<?php esc_attr_e( 'Select a color', 'blaskan' ); ?>" />
<div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
<br />
<span><?php printf( __( 'Default color: %s', 'blaskan' ), '<span id="default-color">#2e6eb0</span>' ); ?></span>
</td>
</tr>
<tr><th colspan="2"><strong><?php _e( 'Layout', 'blaskan' ); ?></strong></th></tr>
<?php
/**
* Content layout
*/
?>
<tr valign="top"><th scope="row"><?php _e( 'Default layout', 'blaskan' ); ?></th>
<td>
<?php
$selected = $options['sidebars'];
$layout_options = array();
foreach ( $sidebars_options as $option ) {
$label = __($option['label'], 'blaskan');
if ( $selected == $option['value'] ) {
$layout_options[] = '<input checked="checked" type="radio" name="blaskan_options[sidebars]" value="' . esc_attr( $option['value'] ) . '">';
} else {
$layout_options[] = '<input type="radio" name="blaskan_options[sidebars]" value="' . esc_attr( $option['value'] ) . '">';
}
}
?>
<div class="blaskan-option">
<label class="description">
<?php echo $layout_options[0]; ?>
<br>
<img src="<?php echo get_template_directory_uri(); ?>/img/content-two-sidebars.png">
<br>
<?php _e( "Narrow content column", 'blaskan' ); ?><br>
<?php _e( "Max two sidebars", 'blaskan' ); ?>
</label>
</div>
<div class="blaskan-option">
<label class="description">
<?php echo $layout_options[1]; ?>
<br>
<img src="<?php echo get_template_directory_uri(); ?>/img/content-wide-one-sidebar.png">
<br>
<?php _e( "Wide content column", 'blaskan' ); ?><br>
<?php _e( "Max one sidebar", 'blaskan' ); ?>
</label>
</div>
<div style="clear: both;"></div>
</td>
</tr>
<?php
/**
* Custom sidebars in pages?
*/
?>
<tr valign="top"><th scope="row"><?php _e( 'Separate sidebar widget areas', 'blaskan' ); ?></th>
<td>
<input id="blaskan_options[custom_sidebars_in_pages]" name="blaskan_options[custom_sidebars_in_pages]" type="checkbox" value="1" <?php checked( '1', $options['custom_sidebars_in_pages'] ); ?> />
<label class="description" for="blaskan_options[custom_sidebars_in_pages]"><?php _e( 'Use separate sidebar widget areas in pages and posts.', 'blaskan' ); ?></label>
</td>
</tr>
<tr><th colspan="2"><strong><?php _e( 'Header', 'blaskan' ); ?></strong></th></tr>
<?php
/**
* Header message
*/
if ( empty( $options['header_message'] ) ) {
$header_message = '';
} else {
$header_message = stripslashes( $options['header_message'] );
}
?>
<tr valign="top"><th scope="row"><?php _e( 'Header message', 'blaskan' ); ?></th>
<td>
<textarea id="blaskan_options[header_message]" class="large-text" cols="50" rows="10" name="blaskan_options[header_message]"><?php echo $header_message; ?></textarea>
<label class="description" for="blaskan_options[header_message]"><?php _e( 'A message that is displayed in the header. Falls back to the blog description if empty.', 'blaskan' ); ?></label>
</td>
</tr>
<?php
/**
* Header image height
*/
if ( empty ( $options['header_image_height'] ) || !is_numeric ( $options['header_image_height'] ) ) {
$header_image_height = '160';
} else {
$header_image_height = $options['header_image_height'];
}
?>
<tr valign="top"><th scope="row"><?php _e( 'Header image height', 'blaskan' ); ?></th>
<td>
<input type="text" id="blaskan_options[header_image_height]" name="blaskan_options[header_image_height]" value="<?php echo $header_image_height; ?>" /> px<br/>
<label class="description" for="blaskan_options[header_image_height]"><?php _e( 'The height of the image in the header', 'blaskan' ); ?></label>
</td>
</tr>
<?php
/**
* Hide site title and header message?
*/
?>
<tr valign="top"><th scope="row"><?php _e( 'Hide site title and header message?', 'blaskan' ); ?></th>
<td>
<input id="blaskan_options[hide_site_title_header_message]" name="blaskan_options[hide_site_title_header_message]" type="checkbox" value="1" <?php checked( '1', $options['hide_site_title_header_message'] ); ?> />
<label class="description" for="blaskan_options[hide_site_title_header_message]"><?php _e( "Hide", 'blaskan' ); ?></label><br/>
<small style="color: #666"><?php _e( "Might be useful if you're using a custom header.", 'blaskan' ); ?></small>
</td>
</tr>
<tr><th colspan="2"><strong><?php _e( 'Footer', 'blaskan' ); ?></strong></th></tr>
<?php
/**
* Footer message
*/
if ( empty( $options['footer_message'] ) ) {
$footer_message = '';
} else {
$footer_message = stripslashes( $options['footer_message'] );
}
?>
<tr valign="top"><th scope="row"><?php _e( 'Footer message', 'blaskan' ); ?></th>
<td>
<textarea id="blaskan_options[footer_message]" class="large-text" cols="50" rows="10" name="blaskan_options[footer_message]"><?php echo $footer_message; ?></textarea>
<label class="description" for="blaskan_options[footer_message]"><?php _e( 'A message that is displayed in the footer.', 'blaskan' ); ?></label>
</td>
</tr>
<tr><th colspan="2"><strong><?php _e( 'Support Blaskan', 'blaskan' ); ?></strong></th></tr>
<?php
/**
* Show credits?
*/
?>
<tr valign="top"><th scope="row"><?php _e( 'Show credits', 'blaskan' ); ?></th>
<td>
<input id="blaskan_options[show_credits]" name="blaskan_options[show_credits]" type="checkbox" value="1" <?php checked( '1', $options['show_credits'] ); ?> />
<label class="description" for="blaskan_options[show_credits]"><?php _e( 'Display links to the Blaskan theme and WordPress.org in the footer.', 'blaskan' ); ?></label>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'blaskan' ); ?>" />
</p>
</form>
<p style="color: #777; margin-top: 30px;">
<?php printf( __( '<a href="%s">%s</a> is designed and developed by <a href="%s">%s</a>. Please report bugs and provide feedback on <a href="%s">GitHub</a>.', 'blaskan' ), 'http://www.blaskan.net', 'Blaskan', 'http://www.helloper.com', 'Per Sandström', 'http://github.com/persand/blaskan' ); ?>
</p>
</div>
<?php
}
/**
* Sanitize and validate input. Accepts an array, return a sanitized array.
*/
function blaskan_options_validate( $input ) {
// Validate typeface in titles options
if ( $input['typeface_titles'] !== 'default' ) {
$input['typeface_titles'] = 'sans_serif';
} else {
$input['typeface_titles'] = 'default';
}
// Link color must be 3 or 6 hexadecimal characters
if ( isset( $input['link_color'] ) && preg_match( '/^#?([a-f0-9]{3}){1,2}$/i', $input['link_color'] ) )
$input['link_color'] = '#' . strtolower( ltrim( $input['link_color'], '#' ) );
// Validate layout options
if ( $input['sidebars'] !== 'one_sidebar' ) {
$input['sidebars'] = 'two_sidebars';
} else {
$input['sidebars'] = 'one_sidebar';
}
// Our custom sidebars value is either 0 or 1
if ( ! isset( $input['custom_sidebars_in_pages'] ) )
$input['custom_sidebars_in_pages'] = null;
$input['custom_sidebars_in_pages'] = ( $input['custom_sidebars_in_pages'] == 1 ? 1 : 0 );
// Header message may contain allowed HTML tags
$input['header_message'] = wp_filter_post_kses( $input['header_message'] );
// Hide site title and header message?
if ( ! isset( $input['hide_site_title_header_message'] ) )
$input['hide_site_title_header_message'] = null;
$input['hide_site_title_header_message'] = ( $input['hide_site_title_header_message'] == 1 ? 1 : 0 );
// Header image height
$input['header_image_height'] = esc_attr( $input['header_image_height'] );
// Footer message may contain allowed HTML tags
$input['footer_message'] = wp_filter_post_kses( $input['footer_message'] );
// Our show_credits value is either 0 or 1
if ( ! isset( $input['show_credits'] ) )
$input['show_credits'] = null;
$input['show_credits'] = ( $input['show_credits'] == 1 ? 1 : 0 );
return $input;
}
/**
* Add a style block to the theme for the current link color.
*
* This function is attached to the wp_head action hook.
*
* Lifted from Twenty Ten theme
*/
function blaskan_print_link_color_style() {
$options = get_option( 'blaskan_options' );
$default_link_color = '#2e6eb0';
if ( ! empty( $options['link_color'] ) ) {
$link_color = $options['link_color'];
} else {
$link_color = $default_link_color;
}
// Don't do anything if the current link color is the default.
if ( $default_link_color == $link_color || empty( $link_color ) )
return;
?>
<style>
/* Link color */
a {
color: <?php echo $link_color; ?>;
}
</style>
<?php
}
add_action( 'wp_head', 'blaskan_print_link_color_style', 100 );