/
class.evalsplot-theme-options.php
613 lines (462 loc) · 17.5 KB
/
class.evalsplot-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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<?php
// manages all of the theme options
// heavy lifting via http://alisothegeek.com/2011/01/wordpress-settings-api-tutorial-1/
// Revision Oct 27, 2017 as jQuery update killed TAB UI
class evalsplot_Theme_Options {
/* Array of sections for the theme options page */
private $sections;
private $checkboxes;
private $settings;
/* Initialize */
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['general'] = __( 'All Settings' );
// create a colllection of callbacks for each section heading
foreach ( $this->sections as $slug => $title ) {
$this->section_callbacks[$slug] = 'display_' . $slug;
}
// enqueue scripts for media uploader
add_action( 'admin_enqueue_scripts', 'evalsplot_enqueue_options_scripts' );
add_action( 'admin_menu', array( &$this, 'add_pages' ) );
add_action( 'admin_init', array( &$this, 'register_settings' ) );
if ( ! get_option( 'evalsplot_options' ) )
$this->initialize_settings();
}
/* Add page(s) to the admin menu */
public function add_pages() {
$admin_page = add_theme_page( 'evalsplot Options', 'evalsplot Options', 'manage_options', 'evalsplot-options', array( &$this, 'display_page' ) );
// documents page, but don't add to menu
$docs_page = add_theme_page( 'evalsplot Documentation', '', 'manage_options', 'evalsplot-docs', array( &$this, 'display_docs' ) );
}
/* HTML to display the theme options page */
public function display_page() {
echo '<div class="wrap">
<h2>evalsplot 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" enctype="multipart/form-data">';
settings_fields( 'evalsplot_options' );
echo '<h2 class="nav-tab-wrapper"><a class="nav-tab nav-tab-active" href="?page=evalsplot-options">Settings</a>
<a class="nav-tab" href="?page=evalsplot-docs">Documentation</a></h2>';
do_settings_sections( $_GET['page'] );
echo '<p class="submit"><input name="Submit" type="submit" class="button-primary" value="' . __( 'Save Changes' ) . '" /></p>
</form>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$("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");
}
});
// 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");
});
});
</script>';
}
/* display documentation in a tab */
public function display_docs() {
// This displays on the "Documentation" tab.
echo '<div class="wrap">
<h1>evalsplot Documentation</h1>
<h2 class="nav-tab-wrapper">
<a class="nav-tab" href="?page=evalsplot-options">Settings</a>
<a class="nav-tab nav-tab-active" href="?page=evalsplot-docs">Documentation</a></h2>';
include( get_stylesheet_directory() . '/includes/evalsplot-theme-options-docs.php');
echo '</div>';
}
/* Define all settings and their defaults */
public function get_settings() {
/* General Settings
===========================================*/
// ------- access options
$this->settings['access_heading'] = array(
'section' => 'general',
'title' => '', // Not used for headings.
'desc' => 'Access and Publishing Controls',
'std' => '',
'type' => 'heading'
);
$this->settings['accesscode'] = array(
'title' => __( 'Access Code' ),
'desc' => __( 'Set code to access the sharing tool; leave blank to allow open access (we recommend setting the <strong>Status for New Items</strong> below to <code>draft</code>' ),
'std' => '',
'type' => 'text',
'section' => 'general'
);
$this->settings['accesshint'] = array(
'title' => __( 'Access Hint' ),
'desc' => __( 'Suggestion if someone cannot guess the code. Not super secure, but hey.' ),
'std' => 'Name of this site (lower the case, Ace!)',
'type' => 'text',
'section' => 'general'
);
$this->settings['new_item_status'] = array(
'section' => 'general',
'title' => __( 'Status For New Items' ),
'desc' => __( 'Set to draft to moderate submissions.' ),
'type' => 'radio',
'std' => 'publish',
'choices' => array(
'publish' => 'Publish immediately',
'draft' => 'Set to draft',
)
);
// ------- sort options
$this->settings['sort_heading'] = array(
'section' => 'general',
'title' => '', // Not used for headings.
'desc' => 'Item Sorting',
'std' => '',
'type' => 'heading'
);
$this->settings['sort_by'] = array(
'section' => 'general',
'title' => __( 'Sort Items by'),
'desc' => '',
'type' => 'radio',
'std' => 'date',
'choices' => array (
'date' => 'Date Published',
'title' => 'Title',
)
);
$this->settings['sort_direction'] = array(
'section' => 'general',
'title' => __( 'Sort Order'),
'desc' => '',
'type' => 'radio',
'std' => 'DESC',
'choices' => array (
'DESC' => 'Descending',
'ASC' => 'Ascending',
)
);
// ------- single item display
$this->settings['single_heading'] = array(
'section' => 'general',
'title' => '', // Not used for headings.
'desc' => 'Single Item Display',
'std' => '',
'type' => 'heading'
);
// Build array to hold options for select, an array of post categories
// Walk those cats, store as array index=ID
$all_cats = get_categories('hide_empty=0');
foreach ( $all_cats as $item ) {
$cat_options[$item->term_id] = $item->name;
}
$this->settings['def_cat'] = array(
'section' => 'general',
'title' => __( 'Default Category for New Item'),
'desc' => '',
'type' => 'select',
'std' => get_option('default_category'),
'choices' => $cat_options
);
$this->settings['allow_comments'] = array(
'section' => 'general',
'title' => __( 'Allow Comments?' ),
'desc' => __( 'Enable comments on items.' ),
'type' => 'checkbox',
'std' => 0 // Set to 1 to be checked by default, 0 to be unchecked by default.
);
$this->settings['use_upload_media'] = array(
'section' => 'general',
'title' => __( 'Allow media uploads (if "no" then media can only be added by external URL)'),
'desc' => '',
'type' => 'radio',
'std' => '1',
'choices' => array (
'0' => 'No',
'1' => 'Yes'
)
);
$this->settings['use_caption'] = array(
'section' => 'general',
'title' => __( 'Use rich text description field on submission form and item display?'),
'desc' => '',
'type' => 'radio',
'std' => '1',
'choices' => array (
'0' => 'No',
'1' => 'Yes, but make it optional',
'2' => 'Yes, and make it required'
)
);
$this->settings['caption_field'] = array(
'section' => 'general',
'title' => __( 'Caption Editing Field'),
'desc' => __( 'Use a plain text entry field or rich text editor.'),
'type' => 'radio',
'std' => 's',
'choices' => array (
's' => 'Simple plain text input field',
'r' => 'Rich text editor'
)
);
$this->settings['use_source'] = array(
'section' => 'general',
'title' => __( 'Use source field (e.g. to provide credit for media) on submission form and item display?'),
'desc' => '',
'type' => 'radio',
'std' => '1',
'choices' => array (
'0' => 'No',
'1' => 'Yes, but make it optional',
'2' => 'Yes, and make it required'
)
);
$this->settings['notify'] = array(
'title' => __( 'Notification Emails' ),
'desc' => __( 'Send notifications to these addresses (separate multiple wth commas). They must have an Editor Role on this site to be able to moderate' ),
'std' => '',
'type' => 'text',
'section' => 'general'
);
// ------- sort options
$this->settings['License and Attribution'] = array(
'section' => 'general',
'title' => '', // Not used for headings.
'desc' => 'License and Attribution',
'std' => '',
'type' => 'heading'
);
$this->settings['use_license'] = array(
'section' => 'general',
'title' => __( 'Use rights license on submission form and item display?'),
'desc' => '',
'type' => 'radio',
'std' => '0',
'choices' => array (
'0' => 'No',
'1' => 'Yes, but make it optional',
'2' => 'Yes, and make it required'
)
);
$this->settings['show_attribution'] = array(
'section' => 'general',
'title' => __( 'Cut and Paste Attribution' ),
'desc' => __( 'If license options used, show cut and paste attribution on single item displays?' ),
'type' => 'radio',
'std' => '0',
'choices' => array(
'0' => 'No',
'1' => 'Yes',
)
);
// ------- single item display
$this->settings['single_heading'] = array(
'section' => 'general',
'title' => '', // Not used for headings.
'desc' => 'Author Account',
'std' => '',
'type' => 'heading'
);
$this->settings['authorcheck'] = array(
'section' => 'general',
'title' => '' ,// Not used for headings.
'desc' => 'Author Account',
'std' => evalsplot_author_user_check( 'sharer' ),
'type' => 'heading'
);
/* Reset
===========================================*/
$this->settings['reset_heading'] = array(
'section' => 'general',
'title' => '', // Not used for headings.
'desc' => 'With Great Power Comes...',
'std' => '',
'type' => 'heading'
);
$this->settings['reset_theme'] = array(
'section' => 'general',
'title' => __( 'Reset All Options' ),
'type' => 'checkbox',
'std' => 0,
'class' => 'warning', // Custom class for CSS
'desc' => __( 'Check this box and click "Save Changes" below to reset theme options to their defaults.' )
);
}
public function display_general() {
// section heading for general setttings
echo '<p>These settings manaage the behavior and appearance of your evalsplot site. There are quite a few of them!</p>';
}
public function display_reset() {
// section heading for reset section setttings
}
/* HTML output for individual settings */
public function display_setting( $args = array() ) {
extract( $args );
$options = get_option( 'evalsplot_options' );
if ( ! isset( $options[$id] ) && $type != 'checkbox' )
$options[$id] = $std;
elseif ( ! isset( $options[$id] ) )
$options[$id] = 0;
$options['new_types'] = 'New Type Name'; // always reset
$field_class = '';
if ( $class != '' )
$field_class = ' ' . $class;
switch ( $type ) {
case 'heading':
echo '<tr><td colspan="2" class="alternate"><h3>' . $desc . '</h3><p>' . $std . '</p></td></tr>';
break;
case 'checkbox':
echo '<input class="checkbox' . $field_class . '" type="checkbox" id="' . $id . '" name="evalsplot_options[' . $id . ']" value="1" ' . checked( $options[$id], 1, false ) . ' /> <label for="' . $id . '">' . $desc . '</label>';
break;
case 'select':
echo '<select class="select' . $field_class . '" name="evalsplot_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="evalsplot_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="evalsplot_options[' . $id . ']" placeholder="' . $std . '" rows="5" cols="60">' . format_for_editor( $options[$id] ) . '</textarea>';
if ( $desc != '' )
echo '<br /><span class="description">' . $desc . '</span>';
break;
case 'medialoader':
echo '<div id="uploader_' . $id . '">';
if ( $options[$id] ) {
$front_img = wp_get_attachment_image_src( $options[$id], 'radcliffe' );
echo '<img id="previewimage_' . $id . '" src="' . $front_img[0] . '" width="640" height="300" alt="default thumbnail" />';
} else {
echo '<img id="previewimage_' . $id . '" src="https://placehold.it/640x300" alt="default header image" />';
}
echo '<input type="hidden" name="evalsplot_options[' . $id . ']" id="' . $id . '" value="' . $options[$id] . '" />
<br /><input type="button" class="upload_image_button button-primary" name="_evalsplot_button' . $id .'" id="_evalsplot_button' . $id .'" data-options_id="' . $id . '" data-uploader_title="Set Default Header Image" data-uploader_button_text="Select Image" value="Set/Change Image" />
</div><!-- uploader -->';
if ( $desc != '' )
echo '<br /><span class="description">' . $desc . '</span>';
break;
case 'password':
echo '<input class="regular-text' . $field_class . '" type="text" id="' . $id . '" name="evalsplot_options[' . $id . ']" value="' . esc_attr( $options[$id] ) . '" /> <input type="button" id="showHide" value="Show" /> ';
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="evalsplot_options[' . $id . ']" placeholder="' . $std . '" value="' . esc_attr( $options[$id] ) . '" />';
if ( $desc != '' ) {
if ($id == 'def_thumb') $desc .= '<br /><a href="' . $options[$id] . '" target="_blank"><img src="' . $options[$id] . '" style="overflow: hidden;" width="' . $options["index_thumb_w"] . '"></a>';
echo '<br /><span class="description">' . $desc . '</span>';
}
break;
}
}
/**
* Description for Docs section
*
* @since 1.0
*/
public function display_docs_section() {
// This displays on the "Documentation" tab.
include( get_stylesheet_directory() . '/includes/evalsplot-theme-options-docs.php');
}
/* Initialize settings to their default values */
public function initialize_settings() {
$default_settings = array();
foreach ( $this->settings as $id => $setting ) {
if ( $setting['type'] != 'heading' )
$default_settings[$id] = $setting['std'];
}
update_option( 'evalsplot_options', $default_settings );
}
/* Register settings via the WP Settings API */
public function register_settings() {
register_setting( 'evalsplot_options', 'evalsplot_options', array ( &$this, 'validate_settings' ) );
foreach ( $this->sections as $slug => $title ) {
add_settings_section( $slug, $title, array( &$this, $this->section_callbacks[$slug] ), 'evalsplot-options' );
}
$this->get_settings();
foreach ( $this->settings as $id => $setting ) {
$setting['id'] = $id;
$this->create_setting( $setting );
}
}
/* tool to create settings fields */
public function create_setting( $args = array() ) {
$defaults = array(
'id' => 'default_field',
'title' => 'Default Field',
'desc' => 'This is a default description.',
'std' => '',
'type' => 'text',
'section' => 'general',
'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' ), 'evalsplot-options', $section, $field_args );
}
/* jQuery Tabs */
public function scripts() {
wp_print_scripts( 'jquery-ui-tabs' );
}
public function validate_settings( $input ) {
if ( ! isset( $input['reset_theme'] ) ) {
$options = get_option( 'evalsplot_options' );
if ( $input['notify'] != $options['notify'] ) {
$input['notify'] = str_replace(' ', '', $input['notify']);
}
foreach ( $this->checkboxes as $id ) {
if ( isset( $options[$id] ) && ! isset( $input[$id] ) )
unset( $options[$id] );
}
return $input;
}
return false;
}
}
$theme_options = new evalsplot_Theme_Options();
function evalsplot_option( $option ) {
$options = get_option( 'evalsplot_options' );
if ( isset( $options[$option] ) )
return $options[$option];
else
return false;
}
?>