Options Framework - Version 0.7

Version Description

  • Added filtering for recognized arrays (like Font Face)
  • Using isset rather than !empty to return of_get_option
  • Significant updates for setting and restoring defaults
  • Background option outputs no-repeat rather than none
Download this release

Release Info

Developer downstairsdev
Plugin Icon wp plugin Options Framework
Version 0.7
Comparing to
See all releases

Code changes from version 0.6 to 0.7

css/admin-style.css CHANGED
@@ -9,7 +9,7 @@
9
  margin: 10px 0;
10
  width: 785px;
11
  position:relative;
12
- z-index: 0
13
  }
14
  #of_container #header {
15
  height: 60px;
9
  margin: 10px 0;
10
  width: 785px;
11
  position:relative;
12
+ z-index: 0;
13
  }
14
  #of_container #header {
15
  height: 60px;
options-framework.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Options Framework
4
  Plugin URI: http://www.wptheming.com
5
  Description: A framework for building theme options.
6
- Version: 0.6
7
  Author: Devin Price
8
  Author URI: http://www.wptheming.com
9
  License: GPLv2
@@ -27,7 +27,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27
 
28
  /* Basic plugin definitions */
29
 
30
- define('OPTIONS_FRAMEWORK_VERSION', '0.6');
31
  define('OPTIONS_FRAMEWORK_URL', plugin_dir_url( __FILE__ ));
32
 
33
  /* Make sure we don't expose any info if called directly */
@@ -42,10 +42,10 @@ if ( !function_exists( 'add_action' ) ) {
42
  add_action('init', 'optionsframework_rolescheck' );
43
 
44
  function optionsframework_rolescheck () {
45
- if ( current_user_can('edit_theme_options') ) {
46
  // If the user can edit theme options, let the fun begin!
47
- add_action('admin_menu', 'optionsframework_add_page');
48
- add_action('admin_init', 'optionsframework_init' );
49
  add_action( 'admin_init', 'optionsframework_mlu_init' );
50
  }
51
  }
@@ -79,8 +79,7 @@ function optionsframework_delete_options() {
79
  /*
80
  * Creates the settings in the database by looping through the array
81
  * we supplied in options.php. This is a neat way to do it since
82
- * we won't have to save settings for headers, descriptions, or arguments-
83
- * and it makes it a little easier to change and set up in my opinion.
84
  *
85
  * Read more about the Settings API in the WordPress codex:
86
  * http://codex.wordpress.org/Settings_API
@@ -102,19 +101,26 @@ function optionsframework_init() {
102
  require_once dirname( __FILE__ ) . '/options.php';
103
  }
104
 
105
- $optionsframework_settings = get_option('optionsframework');
106
 
107
  // Updates the unique option id in the database if it has changed
108
  optionsframework_option_name();
109
 
110
  // Gets the unique id, returning a default if it isn't defined
111
- $option_name = $optionsframework_settings['id'];
 
 
 
 
 
112
 
113
- // Set the option defaults in case they have changed
114
- optionsframework_setdefaults();
 
 
115
 
116
  // Registers the settings fields and callback
117
- register_setting('optionsframework', $option_name, 'optionsframework_validate' );
118
  }
119
 
120
  /*
@@ -128,7 +134,7 @@ function optionsframework_init() {
128
  */
129
 
130
  function optionsframework_setdefaults() {
131
-
132
  $optionsframework_settings = get_option('optionsframework');
133
 
134
  // Gets the unique option id
@@ -141,7 +147,7 @@ function optionsframework_setdefaults() {
141
  * its associated data. No need to clutter the database.
142
  *
143
  */
144
-
145
  if ( isset($optionsframework_settings['knownoptions']) ) {
146
  $knownoptions = $optionsframework_settings['knownoptions'];
147
  if ( !in_array($option_name, $knownoptions) ) {
@@ -157,32 +163,12 @@ function optionsframework_setdefaults() {
157
 
158
  // Gets the default options data from the array in options.php
159
  $options = optionsframework_options();
160
-
161
- // If the options haven't been added to the database yet, they are added now
162
- foreach ($options as $option) {
163
 
164
- if ( ($option['type'] != 'heading') && ($option['type'] != 'info') ) {
165
- $option_id = preg_replace('/\W/', '', strtolower($option['id']) );
166
-
167
- // wp_filter_post_kses for strings
168
- if (isset($option['std' ]) ) {
169
- if ( !is_array($option['std' ]) ) {
170
- $values[$option_id] = wp_filter_post_kses($option['std']);
171
- } else {
172
- foreach ($option['std' ] as $key => $value) {
173
- $optionarray[$key] = wp_filter_post_kses($value);
174
- }
175
- $values[$option_id] = $optionarray;
176
- unset($optionarray);
177
- }
178
- } else {
179
- $value = '';
180
- }
181
- }
182
- }
183
 
184
  if ( isset($values) ) {
185
- add_option($option_name, $values);
186
  }
187
  }
188
 
@@ -287,105 +273,110 @@ function optionsframework_page() {
287
  }
288
  }
289
 
290
- /*
291
- * Data sanitization!
292
  *
293
  * This runs after the submit/reset button has been clicked and
294
  * validates the inputs.
295
  *
 
 
296
  */
 
297
 
298
- function optionsframework_validate($input) {
299
-
300
- $optionsframework_settings = get_option('optionsframework');
301
-
302
- // Gets the unique option id
303
- $option_name = $optionsframework_settings['id'];
304
-
305
- // If the reset button was clicked
306
- if (!empty($_POST['reset'])) {
307
- // If options are deleted sucessfully update the error message
308
- if (delete_option($option_name) ) {
309
- add_settings_error('options-framework', 'restore_defaults', __('Default options restored.'), 'updated fade');
310
- }
311
  }
312
-
313
- else
314
-
315
- {
316
-
317
- if (!empty($_POST['update'])) {
318
-
319
- $clean = array();
320
 
321
- // Get the options array we have defined in options.php
 
 
 
 
 
322
  $options = optionsframework_options();
323
-
324
- foreach ($options as $option) {
325
-
326
- // Verify that the option has an id
327
- if ( isset ($option['id']) ) {
328
-
329
- // Keep all ids lowercase with no spaces
330
- $id = preg_replace( '/\W/', '', strtolower( $option['id'] ) );
331
-
332
- // Set checkbox to false if it wasn't sent in the $_POST
333
- if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) {
334
- $input[$id] = "0";
335
- }
336
-
337
- // Set each item in the multicheck to false if it wasn't sent in the $_POST
338
- if ( 'multicheck' == $option['type'] && ! isset( $input[$id] ) ) {
339
- foreach ( $option['options'] as $key => $value ) {
340
- $input[$id][$key] = "0";
341
- }
342
- }
343
-
344
- // For a value to be submitted to database it must pass through a sanitization filter
345
- if ( isset ( $input[$id] ) && has_filter('of_sanitize_' . $option['type']) ) {
346
- $clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option );
347
  }
348
-
349
- } // end isset $input
350
-
351
- } // end isset $id
352
-
353
- } // end foreach
354
-
355
- if ( isset($clean) ) {
356
- add_settings_error('options-framework', 'save_options', __('Options saved.'), 'updated fade');
357
- return $clean; // Return validated input
358
  }
 
 
 
 
359
 
360
- } // end $_POST['update']
361
-
362
  }
363
 
364
-
365
- /*
366
- * Helper function to return the theme option value. If no value has been saved, it returns $default.
367
- * Needed because options are saved as serialized strings.
368
  *
 
 
 
 
 
 
 
 
 
369
  */
370
-
371
- if ( !function_exists( 'of_get_option' ) ) {
372
- function of_get_option($name, $default = false) {
373
-
374
- $optionsframework_settings = get_option('optionsframework');
375
-
376
- // Gets the unique option id
377
- $option_name = $optionsframework_settings['id'];
378
-
379
- if ( get_option($option_name) ) {
380
- $options = get_option($option_name);
381
- }
382
-
383
- if ( !empty($options[$name]) ) {
384
- return $options[$name];
385
- } else {
386
- return $default;
387
  }
388
- }
389
  }
390
 
391
  /**
@@ -404,4 +395,31 @@ function optionsframework_adminbar() {
404
  'title' => __( 'Theme Options' ),
405
  'href' => admin_url( 'themes.php?page=options-framework' )
406
  ));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407
  }
3
  Plugin Name: Options Framework
4
  Plugin URI: http://www.wptheming.com
5
  Description: A framework for building theme options.
6
+ Version: 0.7
7
  Author: Devin Price
8
  Author URI: http://www.wptheming.com
9
  License: GPLv2
27
 
28
  /* Basic plugin definitions */
29
 
30
+ define('OPTIONS_FRAMEWORK_VERSION', '0.7');
31
  define('OPTIONS_FRAMEWORK_URL', plugin_dir_url( __FILE__ ));
32
 
33
  /* Make sure we don't expose any info if called directly */
42
  add_action('init', 'optionsframework_rolescheck' );
43
 
44
  function optionsframework_rolescheck () {
45
+ if ( current_user_can( 'edit_theme_options' ) ) {
46
  // If the user can edit theme options, let the fun begin!
47
+ add_action( 'admin_menu', 'optionsframework_add_page');
48
+ add_action( 'admin_init', 'optionsframework_init' );
49
  add_action( 'admin_init', 'optionsframework_mlu_init' );
50
  }
51
  }
79
  /*
80
  * Creates the settings in the database by looping through the array
81
  * we supplied in options.php. This is a neat way to do it since
82
+ * we won't have to save settings for headers, descriptions, or arguments.
 
83
  *
84
  * Read more about the Settings API in the WordPress codex:
85
  * http://codex.wordpress.org/Settings_API
101
  require_once dirname( __FILE__ ) . '/options.php';
102
  }
103
 
104
+ $optionsframework_settings = get_option('optionsframework' );
105
 
106
  // Updates the unique option id in the database if it has changed
107
  optionsframework_option_name();
108
 
109
  // Gets the unique id, returning a default if it isn't defined
110
+ if ( isset($optionsframework_settings['id']) ) {
111
+ $option_name = $optionsframework_settings['id'];
112
+ }
113
+ else {
114
+ $option_name = 'optionsframework';
115
+ }
116
 
117
+ // If the option has no saved data, load the defaults
118
+ if ( ! get_option($option_name) ) {
119
+ optionsframework_setdefaults();
120
+ }
121
 
122
  // Registers the settings fields and callback
123
+ register_setting( 'optionsframework', $option_name, 'optionsframework_validate' );
124
  }
125
 
126
  /*
134
  */
135
 
136
  function optionsframework_setdefaults() {
137
+
138
  $optionsframework_settings = get_option('optionsframework');
139
 
140
  // Gets the unique option id
147
  * its associated data. No need to clutter the database.
148
  *
149
  */
150
+
151
  if ( isset($optionsframework_settings['knownoptions']) ) {
152
  $knownoptions = $optionsframework_settings['knownoptions'];
153
  if ( !in_array($option_name, $knownoptions) ) {
163
 
164
  // Gets the default options data from the array in options.php
165
  $options = optionsframework_options();
 
 
 
166
 
167
+ // If the options haven't been added to the database yet, they are added now
168
+ $values = of_get_default_values();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
  if ( isset($values) ) {
171
+ add_option( $option_name, $values ); // Add option with default settings
172
  }
173
  }
174
 
273
  }
274
  }
275
 
276
+ /**
277
+ * Validate Options.
278
  *
279
  * This runs after the submit/reset button has been clicked and
280
  * validates the inputs.
281
  *
282
+ * @uses $_POST['reset']
283
+ * @uses $_POST['update']
284
  */
285
+ function optionsframework_validate( $input ) {
286
 
287
+ /*
288
+ * Restore Defaults.
289
+ *
290
+ * In the event that the user clicked the "Restore Defaults"
291
+ * button, the options defined in the theme's options.php
292
+ * file will be added to the option for the active theme.
293
+ */
294
+
295
+ if ( isset( $_POST['reset'] ) ) {
296
+ add_settings_error( 'options-framework', 'restore_defaults', __( 'Default options restored.', 'optionsframework' ), 'updated fade' );
297
+ return of_get_default_values();
 
 
298
  }
 
 
 
 
 
 
 
 
299
 
300
+ /*
301
+ * Udpdate Settings.
302
+ */
303
+
304
+ if ( isset( $_POST['update'] ) ) {
305
+ $clean = array();
306
  $options = optionsframework_options();
307
+ foreach ( $options as $option ) {
308
+
309
+ if ( ! isset( $option['id'] ) ) {
310
+ continue;
311
+ }
312
+
313
+ if ( ! isset( $option['type'] ) ) {
314
+ continue;
315
+ }
316
+
317
+ $id = preg_replace( '/\W/', '', strtolower( $option['id'] ) );
318
+
319
+ // Set checkbox to false if it wasn't sent in the $_POST
320
+ if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) {
321
+ $input[$id] = '0';
322
+ }
323
+
324
+ // Set each item in the multicheck to false if it wasn't sent in the $_POST
325
+ if ( 'multicheck' == $option['type'] && ! isset( $input[$id] ) ) {
326
+ foreach ( $option['options'] as $key => $value ) {
327
+ $input[$id][$key] = '0';
 
 
 
328
  }
329
+ }
330
+
331
+ // For a value to be submitted to database it must pass through a sanitization filter
332
+ if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
333
+ $clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option );
334
+ }
335
+ }
336
+
337
+ add_settings_error( 'options-framework', 'save_options', __( 'Options saved.', 'optionsframework' ), 'updated fade' );
338
+ return $clean;
339
  }
340
+
341
+ /*
342
+ * Request Not Recognized.
343
+ */
344
 
345
+ return of_get_default_values();
 
346
  }
347
 
348
+ /**
349
+ * Format Configuration Array.
 
 
350
  *
351
+ * Get an array of all default values as set in
352
+ * options.php. The 'id','std' and 'type' keys need
353
+ * to be defined in the configuration array. In the
354
+ * event that these keys are not present the option
355
+ * will not be included in this function's output.
356
+ *
357
+ * @return array Rey-keyed options configuration array.
358
+ *
359
+ * @access private
360
  */
361
+
362
+ function of_get_default_values() {
363
+ $output = array();
364
+ $config = optionsframework_options();
365
+ foreach ( (array) $config as $option ) {
366
+ if ( ! isset( $option['id'] ) ) {
367
+ continue;
368
+ }
369
+ if ( ! isset( $option['std'] ) ) {
370
+ continue;
371
+ }
372
+ if ( ! isset( $option['type'] ) ) {
373
+ continue;
374
+ }
375
+ if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
376
+ $output[$option['id']] = apply_filters( 'of_sanitize_' . $option['type'], $option['std'], $option );
377
+ }
378
  }
379
+ return $output;
380
  }
381
 
382
  /**
395
  'title' => __( 'Theme Options' ),
396
  'href' => admin_url( 'themes.php?page=options-framework' )
397
  ));
398
+ }
399
+
400
+ if ( ! function_exists( 'of_get_option' ) ) {
401
+
402
+ /**
403
+ * Get Option.
404
+ *
405
+ * Helper function to return the theme option value.
406
+ * If no value has been saved, it returns $default.
407
+ * Needed because options are saved as serialized strings.
408
+ */
409
+
410
+ function of_get_option( $name, $default = false ) {
411
+ $config = get_option( 'optionsframework' );
412
+
413
+ if ( ! isset( $config['id'] ) ) {
414
+ return $default;
415
+ }
416
+
417
+ $options = get_option( $config['id'] );
418
+
419
+ if ( isset( $options[$name] ) ) {
420
+ return $options[$name];
421
+ }
422
+
423
+ return $default;
424
+ }
425
  }
options-interface.php CHANGED
@@ -194,12 +194,9 @@ function optionsframework_fields() {
194
  // Font Weight
195
  $output .= '<select class="of-typography of-typography-style" name="'.$option_name.'['.$value['id'].'][style]" id="'. $value['id'].'_style">';
196
 
197
- $styles = array('normal'=>'Normal',
198
- 'italic'=>'Italic',
199
- 'bold'=>'Bold',
200
- 'bold italic'=>'Bold Italic');
201
-
202
- foreach ($styles as $key => $style) {
203
  $output .= '<option value="' . esc_attr( $key ) . '" ' . selected( $typography_stored['style'], $key, false ) . '>'. $style .'</option>';
204
  }
205
  $output .= '</select>';
@@ -276,7 +273,7 @@ function optionsframework_fields() {
276
  $output .= '<h3 class="heading">' . esc_html( $value['name'] ) . '</h3>' . "\n";
277
  }
278
  if ( $value['desc'] ) {
279
- $output .= '<p>'. wp_kses( $value['desc'], $allowedtags) . '</p>' . "\n";
280
  }
281
  $output .= '<div class="clear"></div></div>' . "\n";
282
  break;
194
  // Font Weight
195
  $output .= '<select class="of-typography of-typography-style" name="'.$option_name.'['.$value['id'].'][style]" id="'. $value['id'].'_style">';
196
 
197
+ /* Font Style */
198
+ $styles = of_recognized_font_styles();
199
+ foreach ( $styles as $key => $style ) {
 
 
 
200
  $output .= '<option value="' . esc_attr( $key ) . '" ' . selected( $typography_stored['style'], $key, false ) . '>'. $style .'</option>';
201
  }
202
  $output .= '</select>';
273
  $output .= '<h3 class="heading">' . esc_html( $value['name'] ) . '</h3>' . "\n";
274
  }
275
  if ( $value['desc'] ) {
276
+ $output .= wpautop( wp_kses( $value['desc'], $allowedtags) ) . "\n";
277
  }
278
  $output .= '<div class="clear"></div></div>' . "\n";
279
  break;
options-sanitize.php CHANGED
@@ -153,6 +153,7 @@ add_filter( 'of_sanitize_typography', 'of_sanitize_typography' );
153
 
154
  function of_sanitize_font_size( $value ) {
155
  $recognized = of_recognized_font_sizes();
 
156
  if ( in_array( (int) $value, $recognized ) ) {
157
  return (int) $value;
158
  }
@@ -187,12 +188,13 @@ add_filter( 'of_font_face', 'of_sanitize_font_face' );
187
  *
188
  */
189
  function of_recognized_background_repeat() {
190
- return array(
191
- 'none' => 'No Repeat',
192
- 'repeat-x' => 'Repeat Horizontally',
193
- 'repeat-y' => 'Repeat Vertically',
194
- 'repeat' => 'Repeat All',
195
  );
 
196
  }
197
 
198
  /**
@@ -202,17 +204,18 @@ function of_recognized_background_repeat() {
202
  *
203
  */
204
  function of_recognized_background_position() {
205
- return array(
206
- 'top left' => 'Top Left',
207
- 'top center' => 'Top Center',
208
- 'top right' => 'Top Right',
209
- 'center left' => 'Middle Left',
210
  'center center' => 'Middle Center',
211
- 'center right' => 'Middle Right',
212
- 'bottom left' => 'Bottom Left',
213
  'bottom center' => 'Bottom Center',
214
- 'bottom right' => 'Bottom Right'
215
  );
 
216
  }
217
 
218
  /**
@@ -222,10 +225,11 @@ function of_recognized_background_position() {
222
  *
223
  */
224
  function of_recognized_background_attachment() {
225
- return array(
226
  'scroll' => 'Scroll Normally',
227
- 'fixed' => 'Fixed in Place'
228
  );
 
229
  }
230
 
231
  /**
@@ -272,7 +276,7 @@ function of_recognized_font_sizes() {
272
  *
273
  */
274
  function of_recognized_font_faces() {
275
- return array(
276
  'arial' => 'Arial',
277
  'verdana' => 'Verdana, Geneva',
278
  'trebuchet' => 'Trebuchet',
@@ -282,6 +286,7 @@ function of_recognized_font_faces() {
282
  'palatino' => 'Palatino',
283
  'helvetica' => 'Helvetica*'
284
  );
 
285
  }
286
 
287
  /**
@@ -295,12 +300,13 @@ function of_recognized_font_faces() {
295
  *
296
  */
297
  function of_recognized_font_styles() {
298
- return array(
299
  'normal' => 'Normal',
300
  'italic' => 'Italic',
301
  'bold' => 'Bold',
302
  'bold italic' => 'Bold Italic'
303
  );
 
304
  }
305
 
306
  /**
153
 
154
  function of_sanitize_font_size( $value ) {
155
  $recognized = of_recognized_font_sizes();
156
+ $value = preg_replace('/px/','', $value);
157
  if ( in_array( (int) $value, $recognized ) ) {
158
  return (int) $value;
159
  }
188
  *
189
  */
190
  function of_recognized_background_repeat() {
191
+ $default = array(
192
+ 'no-repeat' => 'No Repeat',
193
+ 'repeat-x' => 'Repeat Horizontally',
194
+ 'repeat-y' => 'Repeat Vertically',
195
+ 'repeat' => 'Repeat All',
196
  );
197
+ return apply_filters( 'of_recognized_background_repeat', $default );
198
  }
199
 
200
  /**
204
  *
205
  */
206
  function of_recognized_background_position() {
207
+ $default = array(
208
+ 'top left' => 'Top Left',
209
+ 'top center' => 'Top Center',
210
+ 'top right' => 'Top Right',
211
+ 'center left' => 'Middle Left',
212
  'center center' => 'Middle Center',
213
+ 'center right' => 'Middle Right',
214
+ 'bottom left' => 'Bottom Left',
215
  'bottom center' => 'Bottom Center',
216
+ 'bottom right' => 'Bottom Right'
217
  );
218
+ return apply_filters( 'of_recognized_background_position', $default );
219
  }
220
 
221
  /**
225
  *
226
  */
227
  function of_recognized_background_attachment() {
228
+ $default = array(
229
  'scroll' => 'Scroll Normally',
230
+ 'fixed' => 'Fixed in Place'
231
  );
232
+ return apply_filters( 'of_recognized_background_attachment', $default );
233
  }
234
 
235
  /**
276
  *
277
  */
278
  function of_recognized_font_faces() {
279
+ $default = array(
280
  'arial' => 'Arial',
281
  'verdana' => 'Verdana, Geneva',
282
  'trebuchet' => 'Trebuchet',
286
  'palatino' => 'Palatino',
287
  'helvetica' => 'Helvetica*'
288
  );
289
+ return apply_filters( 'of_recognized_font_faces', $default );
290
  }
291
 
292
  /**
300
  *
301
  */
302
  function of_recognized_font_styles() {
303
+ $default = array(
304
  'normal' => 'Normal',
305
  'italic' => 'Italic',
306
  'bold' => 'Bold',
307
  'bold italic' => 'Bold Italic'
308
  );
309
+ return apply_filters( 'of_recognized_font_styles', $default );
310
  }
311
 
312
  /**
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: options, theme options
4
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=X238BDP4QGTV2
5
  Requires at least: 3.0
6
  Tested up to: 3.1
7
- Stable tag: 0.6
8
  License: GPLv2
9
 
10
  == Description ==
@@ -51,6 +51,13 @@ You can also watch the video screencast I have at [http://wptheming.com/options-
51
 
52
  == Changelog ==
53
 
 
 
 
 
 
 
 
54
  = 0.6 =
55
 
56
  * Introduces validation filters
4
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=X238BDP4QGTV2
5
  Requires at least: 3.0
6
  Tested up to: 3.1
7
+ Stable tag: 0.7
8
  License: GPLv2
9
 
10
  == Description ==
51
 
52
  == Changelog ==
53
 
54
+ = 0.7 =
55
+
56
+ * Added filtering for recognized arrays (like Font Face)
57
+ * Using isset rather than !empty to return of_get_option
58
+ * Significant updates for setting and restoring defaults
59
+ * Background option outputs no-repeat rather than none
60
+
61
  = 0.6 =
62
 
63
  * Introduces validation filters