Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 382 lines (369 sloc) 14.265 kb
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
1 <?php
2 /**
3 * Oenology Theme Options Settings API
4 *
5 * This file implements the WordPress Settings API for the
6 * Options for the Oenology Theme.
7 *
8 * @package Oenology
9 * @copyright Copyright (c) 2011, Chip Bennett
10 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, v2 (or newer)
11 *
12 * @since Oenology 1.0
13 */
2fd30490 »
2011-01-31 Theme settings: fixed PHP warning, added Reset Defaults button
14
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
15 /**
16 * Register Theme Settings
17 *
18 * Register theme_oenology_options array to hold
19 * all Theme options.
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
20 *
21 * @link http://codex.wordpress.org/Function_Reference/register_setting Codex Reference: register_setting()
22 *
23 * @param string $option_group Unique Settings API identifier; passed to settings_fields() call
24 * @param string $option_name Name of the wp_options database table entry
25 * @param callback $sanitize_callback Name of the callback function in which user input data are sanitized
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
26 */
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
27 register_setting(
28 // $option_group
29 'theme_oenology_options',
30 // $option_name
31 'theme_oenology_options',
32 // $sanitize_callback
33 'oenology_options_validate'
34 );
2fd30490 »
2011-01-31 Theme settings: fixed PHP warning, added Reset Defaults button
35
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
36 /**
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
37 * Theme register_settin() sanitize callback
38 *
39 * Validate and whitelist user-input data before updating Theme
40 * Options in the database. Only whitelisted options are passed
41 * back to the database, and user-input data for all whitelisted
42 * options are sanitized.
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
43 *
44 * @link http://codex.wordpress.org/Data_Validation Codex Reference: Data Validation
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
45 *
46 * @param array $input Raw user-input data submitted via the Theme Settings page
47 * @return array $input Sanitized user-input data passed to the database
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
48 */
49 function oenology_options_validate( $input ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
50 // This is the "whitelist": current settings
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
51 $valid_input = get_option( 'theme_oenology_options' );
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
52 // Get the array of Theme settings, by Settings Page tab
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
53 $settingsbytab = oenology_get_settings_by_tab();
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
54 // Get the array of default options
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
55 $default_options = oenology_get_default_options();
56
57 // Determine what type of submit was input
58 $submittype = (
59 ( ! empty( $input['submit-general'] )
60 || ! empty( $input['submit-varietals'] )
61 ) ? 'submit' : 'reset' );
62 // Determine what tab was input
63 $submittab = 'general';
64 if ( ! empty( $input['submit-varietals'] ) || ! empty($input['reset-varietals'] ) ) {
65 $submittab = 'varietals';
66 }
67 // Get settings by tab
68 $tabsettings = $settingsbytab[$submittab];
69 // Loop through each tab setting
70 foreach ( $tabsettings as $setting ) {
71 // If submit, validate/sanitize $input
72 if ( 'submit' == $submittype ) {
73 $optiondetails = false;
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
74 // Loop through the array of default options
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
75 foreach ( $default_options as $default_option ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
76 // Find the current setting in the array of default options
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
77 if ( $default_option['name'] == $setting ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
78 // Pull out the array for the current setting
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
79 $optiondetails = $default_option;
80 }
81 }
82 // Validate checkbox fields
83 if ( 'checkbox' == $optiondetails['type'] ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
84 // If input value is set and is true, return true; otherwise return false
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
85 $valid_input[$setting] = ( ( isset( $input[$setting] ) && true == $input[$setting] ) ? true : false );
86 }
87 // Validate radio button fields
88 else if ( 'radio' == $optiondetails['type'] ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
89 // Get the list of valid options
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
90 $valid_options = $optiondetails['valid_options'];
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
91 // Only update setting if input value is in the list of valid options
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
92 $valid_input[$setting] = ( array_key_exists( $input[$setting], $valid_options ) ? $input[$setting] : $valid_input[$setting] );
93 }
94 // Validate select fields
95 else if ( 'select' == $optiondetails['type'] ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
96 // Get the list of valid options
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
97 $valid_options = $optiondetails['valid_options'];
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
98 // Only update setting if input value is in the list of valid options
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
99 $valid_input[$setting] = ( array_key_exists( $input[$setting], $valid_options ) ? $input[$setting] : $valid_input[$setting] );
100 }
101 // Validate text input and textarea fields
102 else if ( ( 'text' == $optiondetails['type'] || 'textarea' == $optiondetails['type'] ) ) {
103 // Validate no-HTML content
104 if ( 'nohtml' == $optiondetails['sanitize'] ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
105 // Pass input data through the wp_filter_nohtml_kses filter
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
106 $valid_input[$setting] = wp_filter_nohtml_kses( $input[$setting] );
107 }
108 // Validate HTML content
109 if ( 'html' == $optiondetails['sanitize'] ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
110 // Pass input data through the wp_filter_kses filter
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
111 $valid_input[$setting] = wp_filter_kses( $input[$setting] );
112 }
113 }
114 // Validate custom fields
115 else if ( 'custom' == $optiondetails['type'] ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
116 // Validate the Varietal setting
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
117 if ( 'varietal' == $setting ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
118 // Get the list of valid options
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
119 $valid_options = $optiondetails['valid_options'];
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
120 // Only update setting if input value is in the list of valid options
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
121 $valid_input[$setting] = ( array_key_exists( $input[$setting], $valid_options ) ? $input[$setting] : $valid_input[$setting] );
122 }
123 }
124 }
125 // If reset, reset defaults
126 elseif ( 'reset' == $submittype ) {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
127 // Get the array for $setting
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
128 $option = $default_options[$setting];
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
129 // Set $setting to the default value
130 $valid_input[$setting] = $option['default'];
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
131 }
132 }
133 return $valid_input;
2fd30490 »
2011-01-31 Theme settings: fixed PHP warning, added Reset Defaults button
134
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
135 }
2fd30490 »
2011-01-31 Theme settings: fixed PHP warning, added Reset Defaults button
136
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
137 /**
138 * Globalize the variable that holds
139 * the Settings Page tab definitions
140 *
141 * @global array Settings Page Tab definitions
142 */
143 global $oenology_tabs;
144 $oenology_tabs = oenology_get_settings_page_tabs();
145 /**
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
146 * Call add_settings_section() for each Settings
147 *
148 * Loop through each Theme Settings page tab, and add
149 * a new section to the Theme Settings page for each
150 * section specified for each tab.
151 *
152 * @link http://codex.wordpress.org/Function_Reference/add_settings_section Codex Reference: add_settings_section()
153 *
154 * @param string $sectionid Unique Settings API identifier; passed to add_settings_field() call
155 * @param string $title Title of the Settings page section
156 * @param callback $callback Name of the callback function in which section text is output
157 * @param string $pageid Name of the Settings page to which to add the section; passed to do_settings_sections()
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
158 */
159 foreach ( $oenology_tabs as $tab ) {
160 $tabname = $tab['name'];
161 $tabsections = $tab['sections'];
162 foreach ( $tabsections as $section ) {
163 $sectionname = $section['name'];
164 $sectiontitle = $section['title'];
165 // Add settings section
166 add_settings_section(
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
167 // $sectionid
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
168 'oenology_' . $sectionname . '_section',
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
169 // $title
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
170 $sectiontitle,
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
171 // $callback
172 'oenology_sections_callback',
173 // $pageid
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
174 'oenology_' . $tabname . '_tab'
175 );
176 }
177 }
2fd30490 »
2011-01-31 Theme settings: fixed PHP warning, added Reset Defaults button
178
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
179 /**
180 * Callback for add_settings_section()
181 *
182 * Generic callback to output the section text
183 * for each Plugin settings section.
184 *
185 * @param array $section_passed Array passed from add_settings_section()
186 */
187 function oenology_sections_callback( $section_passed ) {
188 global $oenology_tabs;
189 $oenology_hooks_tabs = oenology_get_settings_page_tabs();
190 foreach ( $oenology_tabs as $tab ) {
191 $tabname = $tab['name'];
192 $tabsections = $tab['sections'];
193 foreach ( $tabsections as $section ) {
194 $sectionname = $section['name'];
195 $sectiondescription = $section['description'];
196 $section_callback_id = 'oenology_' . $sectionname . '_section';
197 if ( $section_callback_id == $section_passed['id'] ) {
198 ?>
199 <p><?php echo $sectiondescription; ?></p>
200 <?php
201 }
202 }
203 }
204 }
2e646d1e »
2011-02-07 Changed function file names
205
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
206 /**
207 * Add Section Text for the Varietal Settings Section
208 */
209 function oenology_get_varietal_text() {
2fd30490 »
2011-01-31 Theme settings: fixed PHP warning, added Reset Defaults button
210
56ceea09 »
2011-01-31 Tabbed Settings Submit Buttons now work properly
211 $oenology_options = get_option( 'theme_oenology_options' );
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
212 $default_options = oenology_get_default_options();
213 $oenology_varietals = $default_options['varietal']['valid_options'];
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
214 $imgstyle = 'float:left;margin-right:20px;margin-bottom:20px;border: 1px solid #bbb;-moz-box-shadow: 2px 2px 2px #777;-webkit-box-shadow: 2px 2px 2px #777;box-shadow: 2px 2px 2px #777;';
215 foreach ( $oenology_varietals as $varietal ) {
216 if ( $varietal['name'] == $oenology_options['varietal'] ) {
217 $oenology_current_varietal = $varietal;
997b47fc »
2011-04-25 Version 1.2
218 }
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
219 }
220 $text = '';
221 $text .= '<p>"Varietal" refers to wine made from exclusively or predominantly one variety of grape. Each varietal has unique flavor and aromatic characteristics. Refer to the contextual help screen for descriptions and help regarding each theme option.</p>';
222 $text .= '<img style="' . $imgstyle . '" src="' . get_template_directory_uri() . '/varietals/' . $oenology_options['varietal'] . '.png' . '" width="150px" height="110px" alt="' . $oenology_options['varietal'] . '" />';
223 $text .= '<h4>Current Varietal</h4>';
224 $text .= '<dl><dt><strong>' . $oenology_current_varietal['title'] . '</strong></dt><dd>' . $oenology_current_varietal['description'] . '</dd></dl>';
225 return $text;
226 }
227
228 /**
229 * Globalize the variable that holds
230 * the Theme default settings definitions
231 *
232 * @global array Theme settings definitions
233 */
234 global $default_options;
235 $default_options = oenology_get_default_options();
236 /**
237 * Call add_settings_field() for each Setting Field
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
238 *
239 * Loop through each Theme option, and add a new
240 * setting field to the Theme Settings page for each
241 * setting.
242 *
243 * @link http://codex.wordpress.org/Function_Reference/add_settings_field Codex Reference: add_settings_field()
244 *
245 * @param string $settingid Unique Settings API identifier; passed to the callback function
246 * @param string $title Title of the setting field
247 * @param callback $callback Name of the callback function in which setting field markup is output
248 * @param string $pageid Name of the Settings page to which to add the setting field; passed from add_settings_section()
249 * @param string $sectionid ID of the Settings page section to which to add the setting field; passed from add_settings_section()
250 * @param array $args Array of arguments to pass to the callback function
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
251 */
252 foreach ( $default_options as $option ) {
253 $optionname = $option['name'];
254 $optiontitle = $option['title'];
255 $optiontab = $option['tab'];
256 $optionsection = $option['section'];
257 $optiontype = $option['type'];
258 if ( 'internal' != $optiontype && 'custom' != $optiontype ) {
259 add_settings_field(
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
260 // $settingid
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
261 'oenology_setting_' . $optionname,
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
262 // $title
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
263 $optiontitle,
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
264 // $callback
265 'oenology_setting_callback',
266 // $pageid
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
267 'oenology_' . $optiontab . '_tab',
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
268 // $sectionid
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
269 'oenology_' . $optionsection . '_section',
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
270 // $args
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
271 $option
272 );
273 } if ( 'custom' == $optiontype ) {
274 add_settings_field(
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
275 // $settingid
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
276 'oenology_setting_' . $optionname,
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
277 // $title
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
278 $optiontitle,
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
279 //$callback
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
280 'oenology_setting_' . $optionname,
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
281 // $pageid
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
282 'oenology_' . $optiontab . '_tab',
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
283 // $sectionid
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
284 'oenology_' . $optionsection . '_section'
285 );
286 }
287 }
288
289 /**
290 * Callback for get_settings_field()
291 */
292 function oenology_setting_callback( $option ) {
293 $oenology_options = get_option( 'theme_oenology_options' );
294 $default_options = oenology_get_default_options();
295 $optionname = $option['name'];
296 $optiontitle = $option['title'];
297 $optiondescription = $option['description'];
298 $fieldtype = $option['type'];
299 $fieldname = 'theme_oenology_options[' . $optionname . ']';
300 // Output checkbox form field markup
301 if ( 'checkbox' == $fieldtype ) {
302 ?>
303 <input type="checkbox" name="<?php echo $fieldname; ?>" <?php checked( $oenology_options[$optionname] ); ?> />
304 <?php
305 }
306 // Output radio button form field markup
307 else if ( 'radio' == $fieldtype ) {
308 $valid_options = array();
309 $valid_options = $option['valid_options'];
310 foreach ( $valid_options as $valid_option ) {
311 ?>
312 <input type="radio" name="<?php echo $fieldname; ?>" <?php checked( $valid_option['name'] == $oenology_options[$optionname] ); ?> value="<?php echo $valid_option['name']; ?>" />
313 <?php
997b47fc »
2011-04-25 Version 1.2
314 }
2fd30490 »
2011-01-31 Theme settings: fixed PHP warning, added Reset Defaults button
315 }
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
316 // Output select form field markup
317 else if ( 'select' == $fieldtype ) {
318 $valid_options = array();
319 $valid_options = $option['valid_options'];
320 ?>
321 <select name="<?php echo $fieldname; ?>">
322 <?php
323 foreach ( $valid_options as $valid_option ) {
324 ?>
325 <option <?php selected( $valid_option['name'] == $oenology_options[$optionname] ); ?> value="<?php echo $valid_option['name']; ?>"><?php echo $valid_option['title']; ?></option>
326 <?php
327 }
328 ?>
329 </select>
330 <?php
331 }
332 // Output text input form field markup
333 else if ( 'text' == $fieldtype ) {
334 ?>
335 <input type="text" name="<?php echo $fieldname; ?>" value="<?php echo wp_filter_nohtml_kses( $oenology_options[$optionname] ); ?>" />
336 <?php
337 }
338 // Output the setting description
339 ?>
340 <span class="description"><?php echo $optiondescription; ?></span>
341 <?php
342 }
343
344 /**
345 * Callback for Varietal Setting Custom Form Field Markup
346 */
347 function oenology_setting_varietal() {
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
348 $default_options = oenology_get_default_options();
349 $oenology_varietals = $default_options['varietal']['valid_options'];
56ceea09 »
2011-01-31 Tabbed Settings Submit Buttons now work properly
350
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
351 function oenology_output_varietal( $varietal ) {
352 $oenology_options = get_option( 'theme_oenology_options' );
353 $dlstylebase = 'border: 1px solid transparent;float:left;padding:5px;margin-top:5px;margin-bottom:5px;text-align:center;max-width:160px;';
354 $dlstylecurrent = 'background-color:#eee;border: 1px solid #999;-moz-box-shadow: 2px 2px 2px #777;-webkit-box-shadow: 2px 2px 2px #777;box-shadow: 2px 2px 2px #777;';
355 $currentvarietal = ( $varietal['name'] == $oenology_options['varietal'] ? true : false );
356 $dlstyle = ( $currentvarietal ? $dlstylebase . $dlstylecurrent : $dlstylebase ); ?>
357 <dl style="<?php echo $dlstyle; ?>">
358 <dt><strong><?php echo $varietal['title']; ?></strong></dt>
359 <dd><img style="border: 1px solid #bbb;" src="<?php echo get_template_directory_uri() . '/varietals/' . $varietal['name'] . '.png'; ?>" width="150px" height="110px" alt="<?php echo $varietal['title']; ?>" title="<?php echo $varietal['description']; ?>" /></dd>
360 <dd><input type="radio" name="theme_oenology_options[varietal]" <?php checked( $currentvarietal ); ?> value="<?php echo $varietal['name']; ?>" /></dd>
361 </dl>
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
362 <?php
363 }
364 ?>
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
365 <h4 style="display:block;clear:both;">White (Light)</h4>
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
366 <?php
367 foreach ( $oenology_varietals as $varietal ) {
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
368 if ( 'light' == $varietal['scheme'] ) {
369 oenology_output_varietal( $varietal );
370 }
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
371 }
372 ?>
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
373 <h4 style="display:block;clear:both;">Red (Dark)</h4>
a1b5607d »
2011-06-06 A few more settings code improvements and more docs
374 <?php
375 foreach ( $oenology_varietals as $varietal ) {
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
376 if ( 'dark' == $varietal['scheme'] ) {
377 oenology_output_varietal( $varietal );
378 }
379 }
2fd30490 »
2011-01-31 Theme settings: fixed PHP warning, added Reset Defaults button
380 }
c21c3244 »
2011-06-06 Settings API rewritten and major documentation overhaul
381
e0d794c9 »
2011-01-22 Working copy 20110122
382 ?>
Something went wrong with that request. Please try again.