forked from UpThemes/UpThemes-Framework
/
options.php
569 lines (484 loc) · 16.8 KB
/
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
<?php
/**
* UpThemes Framework Theme Options
*
* This file defines the Options for the UpThemes Framework.
*
* Theme Options Functions
*
* - Define Default Theme Options
* - Register/Initialize Theme Options
* - Define Admin Settings Page
* - Register Contextual Help
*
* @package UpThemes Framework
* @copyright Copyright (c) 2013, UpThemes
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, v2 (or newer)
*
* @since upfw 1.0
*/
/**
* UpThemes Framework Version
*/
define('UPTHEMES_VER', '2.4.2');
/**
* Globalize the variable that holds the Theme Options
*
* @global array $up_theme_options holds Theme options
*/
global $up_theme_options;
$up_theme_options = array();
add_action( 'init', 'upfw_rolescheck', 20 );
function upfw_rolescheck() {
if ( current_user_can( 'edit_theme_options' ) ) {
// Load the functionality for theme options page
add_action( 'admin_menu', 'upfw_add_theme_page' );
add_action( 'admin_print_scripts-appearance_page_upfw-settings', 'upfw_enqueue_scripts_styles', 40 );
add_action( 'admin_init', 'upfw_init' );
//add_action( 'wp_before_admin_bar_render', 'optionsframework_adminbar' );
require_once( dirname( __FILE__ ) . '/library/theme-customizer.php' );
}
}
function upfw_init() {
require_once( dirname( __FILE__ ) . '/library/options-sanitize.php' );
require_once( dirname( __FILE__ ) . '/library/options-register.php' );
require_once( dirname( __FILE__ ) . '/library/custom.php' );
register_setting(
// $option_group
"theme_" . upfw_get_current_theme_id() . "_options",
// $option_name
"theme_" . upfw_get_current_theme_id() . "_options",
// $sanitize_callback
'upfw_options_validate'
);
}
function register_theme_options( $options ) {
global $up_theme_options;
$up_theme_options = array_merge( $up_theme_options, $options );
}
function register_theme_option_tab( $args ) {
global $up_tabs;
extract( $args );
if($name):
$up_tabs[] = $args;
return true;
endif;
}
/**
* Define Theme Title Constant
*
* Set up the constant named THEME_TITLE
*
* @link http://codex.wordpress.org/Function_Reference/register_setting Codex Reference: register_setting()
*
* @uses wp_get_theme() http://codex.wordpress.org/Function_Reference/wp_get_theme Codex Reference: wp_get_theme()
* @param array $theme Holds the theme object
* @param string $theme_title Name of the current theme
*/
function upfw_define_theme_title() {
$theme = wp_get_theme();
if( !defined('THEME_TITLE') )
define('THEME_TITLE',$theme->title);
}
add_action('after_setup_theme','upfw_define_theme_title',5);
/**
* Return current theme ID
*
* Checks theme data for theme ID and returns it
*
* @uses wp_get_theme() http://codex.wordpress.org/Function_Reference/wp_get_theme Codex Reference: wp_get_theme()
* @param array $theme Holds the theme object
* @param string $theme_title Name of the current theme
*
* @return string $theme_shortname Name of current theme
*/
function upfw_get_current_theme_id() {
$theme = wp_get_theme();
$theme_shortname = strtolower(preg_replace('/ /', '_', $theme->title));
return $theme_shortname;
}
function upfw_get_theme_options_directory() {
return dirname(__FILE__);
}
function upfw_get_theme_dir(){
$theme = wp_get_theme();
$theme_template = $theme->get('Template');
if( ! empty( $theme_template ) ){
return get_stylesheet_directory_uri();
}
return get_template_directory_uri();
}
function upfw_get_theme_options_directory_uri() {
$theme_dir = upfw_get_theme_dir();
return trailingslashit( trailingslashit( $theme_dir ) . basename( dirname(__FILE__) ) );
}
/**
* Add CSS and Javascript includes
*/
function upfw_enqueue_scripts_styles() {
wp_enqueue_style('up_framework',upfw_get_theme_options_directory_uri() . "css/up_framework.css");
wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script('up_framework', upfw_get_theme_options_directory_uri() . "js/up_framework.js", array('jquery'));
// This function loads in the required media files for the media manager.
wp_enqueue_media();
// Register, localize and enqueue our custom JS.
wp_register_script( 'upfw-nmp-media', upfw_get_theme_options_directory_uri() . 'js/media.js', array( 'jquery' ), '1.0.0', true );
wp_localize_script( 'upfw-nmp-media', 'upfw_nmp_media',
array(
'title' => __( 'Upload or Choose Your Custom Image File', 'upfw' ), // This will be used as the default title
'button' => __( 'Insert Image into Input Field', 'upfw' ) // This will be used as the default button text
)
);
wp_enqueue_script( 'upfw-nmp-media' );
}
/**
* Setup the Theme Admin Settings Page
*
* Add "Theme Options" link to the "Appearance" menu
*
* @uses upfw_get_settings_page_cap() defined in \functions\wordpress-hooks.php
*/
function upfw_add_theme_page() {
// Globalize Theme options page
global $upfw_settings_page;
// Add Theme options page
if( !defined('UPFW_NO_THEME_OPTIONS_PAGE') ){
$upfw_settings_page = add_theme_page(
// $page_title
// Name displayed in HTML title tag
__( 'Theme Options', 'upfw' ),
// $menu_title
// Name displayed in the Admin Menu
__( 'Theme Options', 'upfw' ),
// $capability
// User capability required to access page
upfw_get_settings_page_cap(),
// $menu_slug
// String to append to URL after "themes.php"
'upfw-settings',
// $callback
// Function to define settings page markup
'upfw_admin_options_page'
);
}
}
/**
* upfw Theme Settings Page Markup
*
* @uses upfw_get_current_tab() defined in \library\custom.php
* @uses upfw_get_page_tab_markup() defined in \library\custom.php
*/
function upfw_admin_options_page() {
global $up_tabs;
// Determine the current page tab
$currenttab = upfw_get_current_tab();
// Define the page section accordingly
$settings_section = 'upfw_' . $currenttab . '_tab';
?>
<div class="wrap">
<?php upfw_get_page_tab_markup(); ?>
<?php if ( isset( $_GET['settings-updated'] ) ) {
echo '<div class="updated"><p>';
echo __( 'Theme settings updated successfully.', 'upfw' );
echo '</p></div>';
} ?>
<form action="options.php" method="post">
<?php
// Implement settings field security, nonces, etc.
settings_fields("theme_" . ( upfw_get_current_theme_id() ) . "_options");
// Output each settings section, and each
// Settings field in each section
do_settings_sections( $settings_section );
?>
<?php submit_button( __( 'Save Settings', 'upfw' ), 'primary', "theme_" . ( upfw_get_current_theme_id() ) . "_options[submit-{$currenttab}]", false ); ?>
<?php submit_button( __( 'Reset Defaults', 'upfw' ), 'secondary', "theme_" . ( upfw_get_current_theme_id() ) . "_options[reset-{$currenttab}]", false ); ?>
</form>
</div>
<?php
}
/**
* upfw Theme Option Defaults
*
* Returns an associative array that holds
* all of the default values for all Theme
* options.
*
* @uses upfw_get_option_parameters() defined in \functions\options.php
*
* @return array $defaults associative array of option defaults
*/
function upfw_get_option_defaults() {
// Get the array that holds all
// Theme option parameters
$option_parameters = upfw_get_option_parameters();
// Initialize the array to hold
// the default values for all
// Theme options
$option_defaults = array();
// Loop through the option
// parameters array
foreach ( $option_parameters as $option_parameter ) {
$name = $option_parameter['name'];
// Add an associative array key
// to the defaults array for each
// option in the parameters array
if( isset($option_parameter['default']) )
$option_defaults[$name] = $option_parameter['default'];
}
// Return the defaults array
return $option_defaults;
}
/**
* upfw Theme Option Default
*
* Returns an associative array that holds
* all of the default values for all Theme
* options.
*
* @uses upfw_get_option_parameters() defined in \functions\options.php
*
* @return string $default single default value
*/
function upfw_get_option_default($name) {
// Get the array that holds all
// Theme option parameters
$option_parameters = upfw_get_option_parameters();
// Initialize the array to hold
// the default values for all
// Theme options
$option_parameter = $option_parameters[$name];
if( isset($option_parameter['default']) )
$default = $option_parameter['default'];
return $default;
}
/**
* upfw Theme Option Parameters
*
* Array that holds parameters for all options for
* upfw. The 'type' key is used to generate
* the proper form field markup and to sanitize
* the user-input data properly. The 'tab' key
* determines the Settings Page on which the
* option appears, and the 'section' tab determines
* the section of the Settings Page tab in which
* the option appears.
*
* @return array $options array of arrays of option parameters
*/
function upfw_get_option_parameters() {
global $up_theme_options;
return $up_theme_options;
}
/**
* Get upfw Theme Options
*
* Array that holds all of the defined values
* for upfw Theme options. If the user
* has not specified a value for a given Theme
* option, then the option's default value is
* used instead.
*
* @uses upfw_get_option_defaults() defined in \functions\options.php
*
* @uses get_option()
* @uses wp_parse_args()
*
* @return array $upfw_options current values for all Theme options
*/
function upfw_get_options() {
// Get the option defaults
$option_defaults = upfw_get_option_defaults();
// Globalize the variable that holds the Theme options
global $up_options;
// Parse the stored options with the defaults
$up_options = (object) wp_parse_args( get_option( "theme_" . upfw_get_current_theme_id() . "_options", array() ), $option_defaults );
// Return the parsed array
return $up_options;
}
/**
* Separate settings by tab
*
* Returns an array of tabs, each of
* which is an indexed array of settings
* included with the specified tab.
*
* @uses upfw_get_option_parameters() defined in \functions\options.php
*
* @return array $settingsbytab array of arrays of settings by tab
*/
function upfw_get_settings_by_tab() {
global $up_tabs;
// Initialize an array to hold
// an indexed array of tabnames
$settingsbytab = array();
// Loop through the array of tabs
foreach ( $up_tabs as $tab ) {
$tabname = $tab['name'];
// Add an indexed array key
// to the settings-by-tab
// array for each tab name
$tabs[] = $tabname;
}
// Get the array of option parameters
$option_parameters = upfw_get_option_parameters();
// Loop through the option parameters
// array
foreach ( $option_parameters as $option_parameter ) {
$optiontab = $option_parameter['tab'];
$optionname = $option_parameter['name'];
// Add an indexed array key to the
// settings-by-tab array for each
// setting associated with each tab
$settingsbytab[$optiontab][] = $optionname;
$settingsbytab['all'][] = $optionname;
}
// Return the settings-by-tab
// array
return $settingsbytab;
}
function upfw_get_settings_page_cap() {
return 'edit_theme_options';
}
// Hook into option_page_capability_{option_page}
add_action( 'option_page_capability_upfw-settings', 'upfw_get_settings_page_cap' );
/**
* Hook for adding custom fields
*/
global $upfw_custom_callbacks;
function upfw_add_custom_field($type = null, $callback = null) {
// don't do anything if they don't input the correct args
if (is_null($type) or is_null($callback)) {
return false;
}
// check to see if $callback is an actual function
// we only want to add the callback if the function exists
if (function_exists($callback)) {
global $upfw_custom_callbacks;
// for right now we will override any previous callbacks added
$upfw_custom_callbacks[$type] = $callback;
}
}
function upfw_text($value,$attr) { ?>
<input type="text" name="theme_<?php echo esc_attr( upfw_get_current_theme_id() ); ?>_options[<?php echo esc_attr( $attr['name'] ); ?>]" value="<?php echo esc_attr( $value ); ?>">
<?php
if( $attr['description'] ){
echo '<div><em>' . $attr['description'] . '</em></div>';
}
}
function upfw_textarea($value,$attr) { ?>
<textarea name="theme_<?php echo esc_attr( upfw_get_current_theme_id() ); ?>_options[<?php echo esc_attr( $attr['name'] ); ?>]" cols="48" rows="8"><?php echo esc_attr( $value ); ?></textarea>
<?php
if( $attr['description'] ){
echo '<div><em>' . $attr['description'] . '</em></div>';
}
}
function upfw_editor($value, $attr) {
// setup some basic variables to help
$theme_id = upfw_get_current_theme_id();
$name = $attr['name'];
// remap some of the $attr keys to wp_editor keys
// more settings can be remapped once they are needed
$editor_settings = array(
'textarea_name' => "theme_{$theme_id}_options[{$name}]"
);
// WordPress Editor generator
wp_editor($value, $attr['name'], $editor_settings);
if( $attr['description'] ){
echo '<div><em>' . $attr['description'] . '</em></div>';
}
}
function upfw_select($value,$attr) { ?>
<select name="theme_<?php echo esc_attr( upfw_get_current_theme_id() ); ?>_options[<?php echo esc_attr( $attr['name'] ); ?>]">
<?php
if ( isset( $attr['valid_options'] ) ) :
$options = $attr['valid_options'];
foreach( $options as $option ) :
?>
<option value="<?php echo esc_attr( $option['name'] ); ?>" <?php selected($option['name'],$value); ?>><?php echo esc_html( $option['title'] ); ?></option>
<?php
endforeach;
else:
_e("This option has no valid options. Please create valid options as an array inside the UpThemes Framework.","upfw");
endif;
?>
</select>
<?php
if( $attr['description'] ){
echo '<div><em>' . $attr['description'] . '</em></div>';
}
}
function upfw_radio_image($value,$attr) {
if ( isset( $attr['valid_options'] ) ) :
$options = $attr['valid_options'];
foreach( $options as $option ) :
?>
<label class="radio_image">
<input type="radio" name="theme_<?php echo esc_attr( upfw_get_current_theme_id() ); ?>_options[<?php echo esc_attr( $attr['name'] ); ?>]" value="<?php echo esc_attr( $option['name'] ); ?>" <?php checked( $option['name'], $value ); ?>>
<?php if( $option['image'] ) echo '<img src="' . esc_url( $option['image'] ) . '">'; ?>
</label>
<?php
endforeach;
if( $attr['description'] ):
echo '<div><em>' . $attr['description'] . '</em></div>';
endif;
else:
_e("This option has no valid options. Please create valid options as an array inside the UpThemes Framework.","upfw");
endif;
}
function upfw_radio($value,$attr) {
if ( isset( $attr['valid_options'] ) ) :
$options = $attr['valid_options'];
foreach( $options as $option ) :
?>
<label class="radio">
<input type="radio" name="theme_<?php echo esc_attr( upfw_get_current_theme_id() ); ?>_options[<?php echo esc_attr( $attr['name'] ); ?>]" value="<?php echo esc_attr( $option['name'] ); ?>" <?php checked( esc_attr( $option['name'] ), $value ); ?>> <?php echo esc_attr( $option['title'] ); ?>
</label>
<?php
endforeach;
if( $attr['description'] ):
echo '<div><em>' . $attr['description'] . '</em></div>';
endif;
else:
_e("This option has no valid options. Please create valid options as an array inside the UpThemes Framework.","upfw");
endif;
}
function upfw_multicheck($value,$attr) {
if ( isset( $attr['valid_options'] ) ) :
$options = $attr['valid_options'];
foreach( $options as $option_key => $option_value ) :
$checked;
if( isset( $value[$option_value['name']] ) ){
$checked = checked( $value[$option_value['name']], true, false );
} else {
$checked = checked( false, true, false );
}
?>
<input type="checkbox" <?php echo $checked; ?> name="theme_<?php echo esc_attr( upfw_get_current_theme_id() ); ?>_options[<?php echo esc_attr( $attr['name'] ); ?>][<?php echo esc_attr( $option_value['name'] ) ?>]">
<label for="<?php echo esc_html( $option_value['name'] ) ?>"><?php echo esc_html( $option_value['title'] ); ?></label><br>
<?php endforeach;
endif;
if( $attr['description'] ):
echo '<div><em>' . $attr['description'] . '</em></div>';
endif;
}
function upfw_color($value,$attr) { ?>
<span class="colorPickerWrapper">
<input type="text" class="popup-colorpicker" id="<?php echo esc_attr( $attr['name'] ); ?>" name="theme_<?php echo esc_attr( upfw_get_current_theme_id() ); ?>_options[<?php echo esc_attr( $attr['name'] ); ?>]" value="<?php echo esc_attr( $value ); ?>" />
</span>
<?php
if( $attr['description'] ):
echo '<div><em>' . $attr['description'] . '</em></div>';
endif;
}
function upfw_upload($value,$attr) { ?>
<div id="<?php echo esc_html( $attr['name'] ); ?>_container" class="imageWrapper">
<input type="text" class="upfw-open-media" id="<?php echo esc_attr( $attr['name'] ); ?>" name="theme_<?php echo esc_attr( upfw_get_current_theme_id() ); ?>_options[<?php echo esc_attr( $attr['name'] ); ?>]" value="<?php echo esc_attr( $value ); ?>">
<input class="upfw-open-media button button-primary" type="submit" value="<?php esc_attr_e('Upload or Select a File','upfw'); ?>" />
<div class="image_preview"></div>
</div>
<?php
if( $attr['description'] ):
echo '<div><em>' . $attr['description'] . '</em></div>';
endif;
}