Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Chip Bennett
file 379 lines (363 sloc) 13.221 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
<?php
/**
* Oenology Theme Options Settings API
*
* This file implements the WordPress Settings API for the
* Options for the Oenology Theme.
*
* @package Oenology
* @copyright Copyright (c) 2011, Chip Bennett
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, v2 (or newer)
*
* @since Oenology 1.0
*/

/**
* Register Theme Settings
*
* Register theme_oenology_options array to hold
* all Theme options.
*
* @link http://codex.wordpress.org/Function_Reference/register_setting Codex Reference: register_setting()
*
* @param string $option_group Unique Settings API identifier; passed to settings_fields() call
* @param string $option_name Name of the wp_options database table entry
* @param callback $sanitize_callback Name of the callback function in which user input data are sanitized
*/
register_setting(
// $option_group
'theme_oenology_options',
// $option_name
'theme_oenology_options',
// $sanitize_callback
'oenology_options_validate'
);


/**
* Theme register_setting() sanitize callback
*
* Validate and whitelist user-input data before updating Theme
* Options in the database. Only whitelisted options are passed
* back to the database, and user-input data for all whitelisted
* options are sanitized.
*
* @link http://codex.wordpress.org/Data_Validation Codex Reference: Data Validation
*
* @param array $input Raw user-input data submitted via the Theme Settings page
* @return array $input Sanitized user-input data passed to the database
*/
function oenology_options_validate( $input ) {
// This is the "whitelist": current settings
$valid_input = oenology_get_options();
// Get the array of Theme settings, by Settings Page tab
$settingsbytab = oenology_get_settings_by_tab();
// Get the array of option parameters
$option_parameters = oenology_get_option_parameters();
// Get the array of option defaults
$option_defaults = oenology_get_option_defaults();
// Get list of tabs
$tabs = oenology_get_settings_page_tabs();

// Determine what type of submit was input
$submittype = 'submit';
foreach ( $tabs as $tab ) {
$resetname = 'reset-' . $tab['name'];
if ( ! empty( $input[$resetname] ) ) {
$submittype = 'reset';
}
}

// Determine what tab was input
$submittab = 'varietals';
foreach ( $tabs as $tab ) {
$submitname = 'submit-' . $tab['name'];
$resetname = 'reset-' . $tab['name'];
if ( ! empty( $input[$submitname] ) || ! empty($input[$resetname] ) ) {
$submittab = $tab['name'];
}
}
global $wp_customize;
// Get settings by tab
$tabsettings = ( isset ( $wp_customize ) ? $settingsbytab['all'] : $settingsbytab[$submittab] );
// Loop through each tab setting
foreach ( $tabsettings as $setting ) {
// If no option is selected, set the default
$valid_input[$setting] = ( ! isset( $input[$setting] ) ? $option_defaults[$setting] : $input[$setting] );

// If submit, validate/sanitize $input
if ( 'submit' == $submittype ) {

// Get the setting details from the defaults array
$optiondetails = $option_parameters[$setting];
// Get the array of valid options, if applicable
$valid_options = ( isset( $optiondetails['valid_options'] ) ? $optiondetails['valid_options'] : false );

// Validate checkbox fields
if ( 'checkbox' == $optiondetails['type'] ) {
// If input value is set and is true, return true; otherwise return false
$valid_input[$setting] = ( ( isset( $input[$setting] ) && true == $input[$setting] ) ? true : false );
}
// Validate radio button fields
else if ( 'radio' == $optiondetails['type'] ) {
// Only update setting if input value is in the list of valid options
$valid_input[$setting] = ( array_key_exists( $input[$setting], $valid_options ) ? $input[$setting] : $valid_input[$setting] );
}
// Validate select fields
else if ( 'select' == $optiondetails['type'] ) {
// Only update setting if input value is in the list of valid options
$valid_input[$setting] = ( array_key_exists( $input[$setting], $valid_options ) ? $input[$setting] : $valid_input[$setting] );
}
// Validate text input and textarea fields
else if ( ( 'text' == $optiondetails['type'] || 'textarea' == $optiondetails['type'] ) ) {
// Validate no-HTML content
if ( 'nohtml' == $optiondetails['sanitize'] ) {
// Pass input data through the wp_filter_nohtml_kses filter
$valid_input[$setting] = wp_filter_nohtml_kses( $input[$setting] );
}
// Validate HTML content
if ( 'html' == $optiondetails['sanitize'] ) {
// Pass input data through the wp_filter_kses filter
$valid_input[$setting] = wp_filter_kses( $input[$setting] );
}
}
// Validate custom fields
else if ( 'custom' == $optiondetails['type'] ) {
// Validate the Varietal setting
if ( 'varietal' == $setting ) {
// Only update setting if input value is in the list of valid options
$valid_input[$setting] = ( array_key_exists( $input[$setting], $valid_options ) ? $input[$setting] : $valid_input[$setting] );
}
}
}
// If reset, reset defaults
elseif ( 'reset' == $submittype ) {
// Set $setting to the default value
$valid_input[$setting] = $option_defaults[$setting];
}
}
return $valid_input;

}

/**
* Globalize the variable that holds
* the Settings Page tab definitions
*
* @global array Settings Page Tab definitions
*/
global $oenology_tabs;
$oenology_tabs = oenology_get_settings_page_tabs();
/**
* Call add_settings_section() for each Settings
*
* Loop through each Theme Settings page tab, and add
* a new section to the Theme Settings page for each
* section specified for each tab.
*
* @link http://codex.wordpress.org/Function_Reference/add_settings_section Codex Reference: add_settings_section()
*
* @param string $sectionid Unique Settings API identifier; passed to add_settings_field() call
* @param string $title Title of the Settings page section
* @param callback $callback Name of the callback function in which section text is output
* @param string $pageid Name of the Settings page to which to add the section; passed to do_settings_sections()
*/
foreach ( $oenology_tabs as $tab ) {
$tabname = $tab['name'];
$tabsections = $tab['sections'];
foreach ( $tabsections as $section ) {
$sectionname = $section['name'];
$sectiontitle = $section['title'];
// Add settings section
add_settings_section(
// $sectionid
'oenology_' . $sectionname . '_section',
// $title
$sectiontitle,
// $callback
'oenology_sections_callback',
// $pageid
'oenology_' . $tabname . '_tab'
);
}
}

/**
* Callback for add_settings_section()
*
* Generic callback to output the section text
* for each Plugin settings section.
*
* @uses oenology_get_settings_page_tabs() Defined in /functions/options.php
*
* @param array $section_passed Array passed from add_settings_section()
*/
function oenology_sections_callback( $section_passed ) {
global $oenology_tabs;
$oenology_tabs = oenology_get_settings_page_tabs();
foreach ( $oenology_tabs as $tabname => $tab ) {
$tabsections = $tab['sections'];
foreach ( $tabsections as $sectionname => $section ) {
if ( 'oenology_' . $sectionname . '_section' == $section_passed['id'] ) {
?>
<p><?php echo $section['description']; ?></p>
<?php
}
}
}
}

/**
* Globalize the variable that holds
* all the Theme option parameters
*
* @global array Theme options parameters
*/
global $option_parameters;
$option_parameters = oenology_get_option_parameters();
/**
* Call add_settings_field() for each Setting Field
*
* Loop through each Theme option, and add a new
* setting field to the Theme Settings page for each
* setting.
*
* @link http://codex.wordpress.org/Function_Reference/add_settings_field Codex Reference: add_settings_field()
*
* @param string $settingid Unique Settings API identifier; passed to the callback function
* @param string $title Title of the setting field
* @param callback $callback Name of the callback function in which setting field markup is output
* @param string $pageid Name of the Settings page to which to add the setting field; passed from add_settings_section()
* @param string $sectionid ID of the Settings page section to which to add the setting field; passed from add_settings_section()
* @param array $args Array of arguments to pass to the callback function
*/
foreach ( $option_parameters as $option ) {
$optionname = $option['name'];
$optiontitle = $option['title'];
$optiontab = $option['tab'];
$optionsection = $option['section'];
$optiontype = $option['type'];
if ( 'custom' != $optiontype ) {
add_settings_field(
// $settingid
'oenology_setting_' . $optionname,
// $title
$optiontitle,
// $callback
'oenology_setting_callback',
// $pageid
'oenology_' . $optiontab . '_tab',
// $sectionid
'oenology_' . $optionsection . '_section',
// $args
$option
);
} if ( 'custom' == $optiontype ) {
add_settings_field(
// $settingid
'oenology_setting_' . $optionname,
// $title
$optiontitle,
//$callback
'oenology_setting_' . $optionname,
// $pageid
'oenology_' . $optiontab . '_tab',
// $sectionid
'oenology_' . $optionsection . '_section'
);
}
}

/**
* Callback for get_settings_field()
*/
function oenology_setting_callback( $option ) {
$oenology_options = oenology_get_options();
$option_parameters = oenology_get_option_parameters();
$optionname = $option['name'];
$optiontitle = $option['title'];
$optiondescription = $option['description'];
$fieldtype = $option['type'];
$fieldname = 'theme_oenology_options[' . $optionname . ']';

// Output checkbox form field markup
if ( 'checkbox' == $fieldtype ) {
?>
<input type="checkbox" name="<?php echo $fieldname; ?>" <?php checked( $oenology_options[$optionname] ); ?> />
<?php
}
// Output radio button form field markup
else if ( 'radio' == $fieldtype ) {
$valid_options = array();
$valid_options = $option['valid_options'];
foreach ( $valid_options as $valid_option ) {
?>
<input type="radio" name="<?php echo $fieldname; ?>" <?php checked( $valid_option['name'] == $oenology_options[$optionname] ); ?> value="<?php echo $valid_option['name']; ?>" />
<span>
<?php echo $valid_option['title']; ?>
<?php if ( $valid_option['description'] ) { ?>
<span style="padding-left:5px;"><em><?php echo $valid_option['description']; ?></em></span>
<?php } ?>
</span>
<br />
<?php
}
}
// Output select form field markup
else if ( 'select' == $fieldtype ) {
$valid_options = array();
$valid_options = $option['valid_options'];
?>
<select name="<?php echo $fieldname; ?>">
<?php
foreach ( $valid_options as $valid_option ) {
?>
<option <?php selected( $valid_option['name'] == $oenology_options[$optionname] ); ?> value="<?php echo $valid_option['name']; ?>"><?php echo $valid_option['title']; ?></option>
<?php
}
?>
</select>
<?php
}
// Output text input form field markup
else if ( 'text' == $fieldtype ) {
?>
<input type="text" name="<?php echo $fieldname; ?>" value="<?php echo wp_filter_nohtml_kses( $oenology_options[$optionname] ); ?>" />
<?php
}
// Output the setting description
?>
<span class="description"><?php echo $optiondescription; ?></span>
<?php
}

/**
* Callback for Varietal Setting Custom Form Field Markup
*/
function oenology_setting_varietal() {
$option_parameters = oenology_get_option_parameters();
$oenology_varietals = $option_parameters['varietal']['valid_options'];

function oenology_output_varietal( $varietal ) {
$oenology_options = oenology_get_options();
$varietal_thumbnail = oenology_locate_template_uri( array( '/varietals/' . $varietal['name'] . '.png' ), false, false );
$currentvarietal = ( $varietal['name'] == $oenology_options['varietal'] ? true : false );
$dlclass = ( $currentvarietal ? 'oenology-varietal oenology-varietal-current' : 'oenology-varietal' );
?>
<dl class="<?php echo $dlclass; ?>">
<dt><strong><?php echo $varietal['title']; ?></strong></dt>
<dd><img src="<?php echo $varietal_thumbnail; ?>" width="150px" height="110px" alt="<?php echo $varietal['title']; ?>" title="<?php echo $varietal['description']; ?>" /></dd>
<dd><input type="radio" name="theme_oenology_options[varietal]" <?php checked( $currentvarietal ); ?> value="<?php echo $varietal['name']; ?>" /></dd>
</dl>
<?php
}
?>
<h4 style="display:block;clear:both;"><?php _e( 'White (Light)', 'oenology' ); ?></h4>
<?php
foreach ( $oenology_varietals as $varietal ) {
if ( 'light' == $varietal['scheme'] ) {
oenology_output_varietal( $varietal );
}
}
?>
<h4 style="display:block;clear:both;"><?php _e( 'Red (Dark)', 'oenology' ); ?></h4>
<?php
foreach ( $oenology_varietals as $varietal ) {
if ( 'dark' == $varietal['scheme'] ) {
oenology_output_varietal( $varietal );
}
}
?>
<h4 style="display:block;clear:both;"><?php _e( 'Cuvee (unstyled)', 'oenology' ); ?></h4>
<?php
foreach ( $oenology_varietals as $varietal ) {
if ( 'cuvee' == $varietal['scheme'] ) {
oenology_output_varietal( $varietal );
}
}
}
?>
Something went wrong with that request. Please try again.