forked from devinsays/options-framework-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options-framework.php
573 lines (472 loc) · 17.8 KB
/
options-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
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
<?php
/*
Plugin Name: Options Framework
Plugin URI: http://www.wptheming.com
Description: A framework for building theme options.
Version: 1.6
Author: Devin Price
Author URI: http://www.wptheming.com
License: GPLv2
*/
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* Basic plugin definitions */
define( 'OPTIONS_FRAMEWORK_VERSION', '1.6' );
define( 'OPTIONS_FRAMEWORK_URL', plugin_dir_url( __FILE__ ) );
load_plugin_textdomain( 'optionsframework', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
/* Make sure we don't expose any info if called directly */
if ( !function_exists( 'add_action' ) ) {
echo "Hi there! I'm just a little plugin, don't mind me.";
exit;
}
/* If the user can't edit theme options, no use running this plugin */
add_action( 'init', 'optionsframework_rolescheck', 20 );
function optionsframework_rolescheck() {
if ( current_user_can( 'edit_theme_options' ) ) {
$options =& _optionsframework_options();
if ( $options ) {
// If the user can edit theme options, let the fun begin!
add_action( 'admin_menu', 'optionsframework_add_page' );
add_action( 'admin_init', 'optionsframework_init' );
add_action( 'wp_before_admin_bar_render', 'optionsframework_adminbar' );
}
else {
// Display a notice if options aren't present in the theme
add_action( 'admin_notices', 'optionsframework_admin_notice' );
add_action( 'admin_init', 'optionsframework_nag_ignore' );
}
}
}
function optionsframework_admin_notice() {
global $pagenow;
if ( !is_multisite() && ( $pagenow == 'plugins.php' || $pagenow == 'themes.php' ) ) {
global $current_user ;
$user_id = $current_user->ID;
if ( ! get_user_meta($user_id, 'optionsframework_ignore_notice') ) {
echo '<div class="updated optionsframework_setup_nag"><p>';
printf( __('Your current theme does not have support for the Options Framework plugin. <a href="%1$s" target="_blank">Learn More</a> | <a href="%2$s">Hide Notice</a>', 'optionsframework'), 'http://wptheming.com/options-framework-plugin', '?optionsframework_nag_ignore=0');
echo "</p></div>";
}
}
}
function optionsframework_nag_ignore() {
global $current_user;
$user_id = $current_user->ID;
if ( isset( $_GET['optionsframework_nag_ignore'] ) && '0' == $_GET['optionsframework_nag_ignore'] ) {
add_user_meta( $user_id, 'optionsframework_ignore_notice', 'true', true );
}
}
/* Register plugin activation hooks */
register_activation_hook( __FILE__,'optionsframework_activation_hook' );
function optionsframework_activation_hook() {
register_uninstall_hook( __FILE__, 'optionsframework_delete_options' );
}
/* When uninstalled, deletes options */
register_uninstall_hook( __FILE__, 'optionsframework_delete_options' );
function optionsframework_delete_options() {
$optionsframework_settings = get_option( 'optionsframework' );
// Each theme saves its data in a seperate option, which all gets deleted
$knownoptions = $optionsframework_settings['knownoptions'];
if ( $knownoptions ) {
foreach ( $knownoptions as $key ) {
delete_option( $key );
}
}
delete_option( 'optionsframework' );
delete_user_meta( $user_id, 'optionsframework_ignore_notice', 'true' );
}
/* Loads the file for option sanitization */
add_action( 'init', 'optionsframework_load_sanitization' );
function optionsframework_load_sanitization() {
require_once dirname( __FILE__ ) . '/options-sanitize.php';
}
/*
* The optionsframework_init loads all the required files and registers the settings.
*
* Read more about the Settings API in the WordPress codex:
* http://codex.wordpress.org/Settings_API
*
* The theme options are saved using a unique option id in the database. Developers
* traditionally set the option id via in theme using the function
* optionsframework_option_name, but it can also be set using a hook of the same name.
*
* If a theme developer doesn't explictly set the unique option id using one of those
* functions it will be set by default to: optionsframework_[the theme name]
*
*/
function optionsframework_init() {
// Include the required files
require_once dirname( __FILE__ ) . '/options-interface.php';
require_once dirname( __FILE__ ) . '/options-media-uploader.php';
// Optionally Loads the options file from the theme
$location = apply_filters( 'options_framework_location', array( 'options.php' ) );
$optionsfile = locate_template( $location );
// Load settings
$optionsframework_settings = get_option( 'optionsframework' );
// Updates the unique option id in the database if it has changed
if ( function_exists( 'optionsframework_option_name' ) ) {
optionsframework_option_name();
}
elseif ( has_action( 'optionsframework_option_name' ) ) {
do_action( 'optionsframework_option_name' );
}
// If the developer hasn't explicitly set an option id, we'll use a default
else {
$default_themename = get_option( 'stylesheet' );
$default_themename = preg_replace("/\W/", "_", strtolower($default_themename) );
$default_themename = 'optionsframework_' . $default_themename;
if ( isset( $optionsframework_settings['id'] ) ) {
if ( $optionsframework_settings['id'] == $default_themename ) {
// All good, using default theme id
} else {
$optionsframework_settings['id'] = $default_themename;
update_option( 'optionsframework', $optionsframework_settings );
}
}
else {
$optionsframework_settings['id'] = $default_themename;
update_option( 'optionsframework', $optionsframework_settings );
}
}
// If the option has no saved data, load the defaults
if ( ! get_option( $optionsframework_settings['id'] ) ) {
optionsframework_setdefaults();
}
// Registers the settings fields and callback
register_setting( 'optionsframework', $optionsframework_settings['id'], 'optionsframework_validate' );
// Change the capability required to save the 'optionsframework' options group.
add_filter( 'option_page_capability_optionsframework', 'optionsframework_page_capability' );
}
/**
* Ensures that a user with the 'edit_theme_options' capability can actually set the options
* See: http://core.trac.wordpress.org/ticket/14365
*
* @param string $capability The capability used for the page, which is manage_options by default.
* @return string The capability to actually use.
*/
function optionsframework_page_capability( $capability ) {
return 'edit_theme_options';
}
/*
* Adds default options to the database if they aren't already present.
* May update this later to load only on plugin activation, or theme
* activation since most people won't be editing the options.php
* on a regular basis.
*
* http://codex.wordpress.org/Function_Reference/add_option
*
*/
function optionsframework_setdefaults() {
$optionsframework_settings = get_option( 'optionsframework' );
// Gets the unique option id
$option_name = $optionsframework_settings['id'];
/*
* Each theme will hopefully have a unique id, and all of its options saved
* as a separate option set. We need to track all of these option sets so
* it can be easily deleted if someone wishes to remove the plugin and
* its associated data. No need to clutter the database.
*
*/
if ( isset( $optionsframework_settings['knownoptions'] ) ) {
$knownoptions = $optionsframework_settings['knownoptions'];
if ( !in_array( $option_name, $knownoptions ) ) {
array_push( $knownoptions, $option_name );
$optionsframework_settings['knownoptions'] = $knownoptions;
update_option( 'optionsframework', $optionsframework_settings);
}
} else {
$newoptionname = array($option_name);
$optionsframework_settings['knownoptions'] = $newoptionname;
update_option('optionsframework', $optionsframework_settings);
}
// Gets the default options data from the array in options.php
$options =& _optionsframework_options();
// If the options haven't been added to the database yet, they are added now
$values = of_get_default_values();
if ( isset($values) ) {
add_option( $option_name, $values ); // Add option with default settings
}
}
/* Define menu options (still limited to appearance section)
*
* Examples usage:
*
* add_filter( 'optionsframework_menu', function($menu) {
* $menu['page_title'] = 'Hello Options';
* $menu['menu_title'] = 'Hello Options';
* return $menu;
* });
*/
function optionsframework_menu_settings() {
$menu = array(
'page_title' => __( 'Theme Options', 'optionsframework'),
'menu_title' => __('Theme Options', 'optionsframework'),
'capability' => 'edit_theme_options',
'menu_slug' => 'options-framework',
'callback' => 'optionsframework_page'
);
return apply_filters( 'optionsframework_menu', $menu );
}
/* Add a subpage called "Theme Options" to the appearance menu. */
function optionsframework_add_page() {
$menu = optionsframework_menu_settings();
$of_page = add_theme_page( $menu['page_title'], $menu['menu_title'], $menu['capability'], $menu['menu_slug'], $menu['callback'] );
// Load the required CSS and javscript
add_action( 'admin_enqueue_scripts', 'optionsframework_load_scripts' );
add_action( 'admin_print_styles-' . $of_page, 'optionsframework_load_styles' );
}
/* Loads the CSS */
function optionsframework_load_styles() {
wp_enqueue_style( 'optionsframework', OPTIONS_FRAMEWORK_URL.'css/optionsframework.css' );
if ( !wp_style_is( 'wp-color-picker','registered' ) ) {
wp_register_style('wp-color-picker', OPTIONS_FRAMEWORK_URL.'css/color-picker.min.css');
}
wp_enqueue_style( 'wp-color-picker' );
}
/* Loads the javascript */
function optionsframework_load_scripts( $hook ) {
$menu = optionsframework_menu_settings();
if ( 'appearance_page_' . $menu['menu_slug'] != $hook )
return;
// Enqueue colorpicker scripts for versions below 3.5 for compatibility
if ( !wp_script_is( 'wp-color-picker', 'registered' ) ) {
wp_register_script( 'iris', OPTIONS_FRAMEWORK_URL . 'js/iris.min.js', array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), false, 1 );
wp_register_script( 'wp-color-picker', OPTIONS_FRAMEWORK_URL . 'js/color-picker.min.js', array( 'jquery', 'iris' ) );
$colorpicker_l10n = array(
'clear' => __( 'Clear' ),
'defaultString' => __( 'Default' ),
'pick' => __( 'Select Color' )
);
wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', $colorpicker_l10n );
}
// Enqueue custom option panel JS
wp_enqueue_script( 'options-custom', OPTIONS_FRAMEWORK_URL . 'js/options-custom.js', array( 'jquery','wp-color-picker' ) );
// Inline scripts from options-interface.php
add_action( 'admin_head', 'of_admin_head' );
}
function of_admin_head() {
// Hook to add custom scripts
do_action( 'optionsframework_custom_scripts' );
}
/*
* Builds out the options panel.
*
* If we were using the Settings API as it was likely intended we would use
* do_settings_sections here. But as we don't want the settings wrapped in a table,
* we'll call our own custom optionsframework_fields. See options-interface.php
* for specifics on how each individual field is generated.
*
* Nonces are provided using the settings_fields()
*
*/
if ( !function_exists( 'optionsframework_page' ) ) :
function optionsframework_page() { ?>
<div id="optionsframework-wrap" class="wrap">
<?php screen_icon( 'themes' ); ?>
<h2 class="nav-tab-wrapper">
<?php echo optionsframework_tabs(); ?>
</h2>
<?php settings_errors( 'options-framework' ); ?>
<div id="optionsframework-metabox" class="metabox-holder">
<div id="optionsframework" class="postbox">
<form action="options.php" method="post">
<?php settings_fields( 'optionsframework' ); ?>
<?php optionsframework_fields(); /* Settings */ ?>
<div id="optionsframework-submit">
<input type="submit" class="button-primary" name="update" value="<?php esc_attr_e( 'Save Options', 'optionsframework' ); ?>" />
<input type="submit" class="reset-button button-secondary" name="reset" value="<?php esc_attr_e( 'Restore Defaults', 'optionsframework' ); ?>" onclick="return confirm( '<?php print esc_js( __( 'Click OK to reset. Any theme settings will be lost!', 'optionsframework' ) ); ?>' );" />
<div class="clear"></div>
</div>
</form>
</div> <!-- / #container -->
</div>
<?php do_action( 'optionsframework_after' ); ?>
</div> <!-- / .wrap -->
<?php
}
endif;
/**
* Validate Options.
*
* This runs after the submit/reset button has been clicked and
* validates the inputs.
*
* @uses $_POST['reset'] to restore default options
*/
function optionsframework_validate( $input ) {
/*
* Restore Defaults.
*
* In the event that the user clicked the "Restore Defaults"
* button, the options defined in the theme's options.php
* file will be added to the option for the active theme.
*/
if ( isset( $_POST['reset'] ) ) {
add_settings_error( 'options-framework', 'restore_defaults', __( 'Default options restored.', 'optionsframework' ), 'updated fade' );
return of_get_default_values();
}
/*
* Update Settings
*
* This used to check for $_POST['update'], but has been updated
* to be compatible with the theme customizer introduced in WordPress 3.4
*/
$clean = array();
$options =& _optionsframework_options();
foreach ( $options as $option ) {
if ( ! isset( $option['id'] ) ) {
continue;
}
if ( ! isset( $option['type'] ) ) {
continue;
}
$id = preg_replace( '/[^a-zA-Z0-9._\-]/', '', strtolower( $option['id'] ) );
// Set checkbox to false if it wasn't sent in the $_POST
if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) {
$input[$id] = false;
}
// Set each item in the multicheck to false if it wasn't sent in the $_POST
if ( 'multicheck' == $option['type'] && ! isset( $input[$id] ) ) {
foreach ( $option['options'] as $key => $value ) {
$input[$id][$key] = false;
}
}
// For a value to be submitted to database it must pass through a sanitization filter
if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
$clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option );
}
}
// Hook to run after validation
do_action( 'optionsframework_after_validate', $clean );
return $clean;
}
/**
* Display message when options have been saved
*/
function optionsframework_save_options_notice() {
add_settings_error( 'options-framework', 'save_options', __( 'Options saved.', 'optionsframework' ), 'updated fade' );
}
add_action( 'optionsframework_after_validate', 'optionsframework_save_options_notice' );
/**
* Format Configuration Array.
*
* Get an array of all default values as set in
* options.php. The 'id','std' and 'type' keys need
* to be defined in the configuration array. In the
* event that these keys are not present the option
* will not be included in this function's output.
*
* @return array Rey-keyed options configuration array.
*
* @access private
*/
function of_get_default_values() {
$output = array();
$config =& _optionsframework_options();
foreach ( (array) $config as $option ) {
if ( ! isset( $option['id'] ) ) {
continue;
}
if ( ! isset( $option['std'] ) ) {
continue;
}
if ( ! isset( $option['type'] ) ) {
continue;
}
if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
$output[$option['id']] = apply_filters( 'of_sanitize_' . $option['type'], $option['std'], $option );
}
}
return $output;
}
/**
* Add Theme Options menu item to Admin Bar.
*/
function optionsframework_adminbar() {
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'parent' => 'appearance',
'id' => 'of_theme_options',
'title' => __( 'Theme Options', 'optionsframework' ),
'href' => admin_url( 'themes.php?page=options-framework' )
));
}
/**
* Get Option.
*
* Helper function to return the theme option value.
* If no value has been saved, it returns $default.
* Needed because options are saved as serialized strings.
*/
if ( ! function_exists( 'of_get_option' ) ) :
function of_get_option( $name, $default = false ) {
$config = get_option( 'optionsframework' );
if ( ! isset( $config['id'] ) ) {
return $default;
}
$options = get_option( $config['id'] );
if ( isset( $options[$name] ) ) {
return $options[$name];
}
return $default;
}
endif;
/**
* Wrapper for optionsframework_options()
*
* Allows for manipulating or setting options via 'of_options' filter
* For example:
*
* <code>
* add_filter('of_options', function($options) {
* $options[] = array(
* 'name' => 'Input Text Mini',
* 'desc' => 'A mini text input field.',
* 'id' => 'example_text_mini',
* 'std' => 'Default',
* 'class' => 'mini',
* 'type' => 'text'
* );
*
* return $options;
* });
* </code>
*
* Also allows for setting options via a return statement in the
* options.php file. For example (in options.php):
*
* <code>
* return array(...);
* </code>
*
* @return array (by reference)
*/
function &_optionsframework_options() {
static $options = null;
if ( !$options ) {
// Load options from options.php file (if it exists)
$location = apply_filters( 'options_framework_location', array('options.php') );
if ( $optionsfile = locate_template( $location ) ) {
$maybe_options = require_once $optionsfile;
if (is_array($maybe_options)) {
$options = $maybe_options;
} else if ( function_exists( 'optionsframework_options' ) ) {
$options = optionsframework_options();
}
}
// Allow setting/manipulating options via filters
$options = apply_filters('of_options', $options);
}
return $options;
}