WordPressSettingsFramework

WordPressSettingsFramework class.

Defined (2)

The class is defined in the following location(s).

/wp-settings-framework.php  
  1. class WordPressSettingsFramework { 
  2.  
  3. /** 
  4. * @access private 
  5. * @var string  
  6. */ 
  7. private $option_group; 
  8.  
  9. /** 
  10. * Constructor 
  11. *  
  12. * @param string path to settings file 
  13. * @param string optional "option_group" override 
  14. */ 
  15. function __construct( $settings_file, $option_group = '' ) 
  16. if( !is_file( $settings_file ) ) return; 
  17. require_once( $settings_file ); 
  18.  
  19. $this->option_group = preg_replace("/[^a-z0-9]+/i", "", basename( $settings_file, '.php' )); 
  20. if( $option_group ) $this->option_group = $option_group; 
  21.  
  22. add_action('admin_init', array(&$this, 'admin_init')); 
  23. add_action('admin_notices', array(&$this, 'admin_notices')); 
  24. add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts')); 
  25.  
  26. /** 
  27. * Get the option group for this instance 
  28. *  
  29. * @return string the "option_group" 
  30. */ 
  31. function get_option_group() 
  32. return $this->option_group; 
  33.  
  34. /** 
  35. * Registers the internal WordPress settings 
  36. */ 
  37. function admin_init() 
  38. register_setting( $this->option_group, $this->option_group .'_settings', array(&$this, 'settings_validate') ); 
  39. $this->process_settings(); 
  40.  
  41. /** 
  42. * Displays any errors from the WordPress settings API 
  43. */ 
  44. function admin_notices() 
  45. settings_errors(); 
  46.  
  47. /** 
  48. * Enqueue scripts and styles 
  49. */ 
  50. function admin_enqueue_scripts() 
  51. wp_enqueue_style('farbtastic'); 
  52. wp_enqueue_style('thickbox'); 
  53.  
  54. wp_enqueue_script('jquery'); 
  55. wp_enqueue_script('farbtastic'); 
  56. wp_enqueue_script('media-upload'); 
  57. wp_enqueue_script('thickbox'); 
  58.  
  59. /** 
  60. * Adds a filter for settings validation 
  61. *  
  62. * @param array the un-validated settings 
  63. * @return array the validated settings 
  64. */ 
  65. function settings_validate( $input ) 
  66. return apply_filters( $this->option_group .'_settings_validate', $input ); 
  67.  
  68. /** 
  69. * Displays the "section_description" if speicified in $wpsf_settings 
  70. * @param array callback args from add_settings_section() 
  71. */ 
  72. function section_intro( $args ) 
  73. global $wpsf_settings; 
  74. if(!empty($wpsf_settings)) { 
  75. foreach($wpsf_settings as $section) { 
  76. if($section['section_id'] == $args['id']) { 
  77. if(isset($section['section_description']) && $section['section_description']) echo '<p>'. $section['section_description'] .'</p>'; 
  78. break; 
  79.  
  80. /** 
  81. * Processes $wpsf_settings and adds the sections and fields via the WordPress settings API 
  82. */ 
  83. function process_settings() 
  84. global $wpsf_settings; 
  85. if(!empty($wpsf_settings)) { 
  86. usort($wpsf_settings, array(&$this, 'sort_array')); 
  87. foreach($wpsf_settings as $section) { 
  88. if(isset($section['section_id']) && $section['section_id'] && isset($section['section_title'])) { 
  89. add_settings_section( $section['section_id'], $section['section_title'], array(&$this, 'section_intro'), $this->option_group ); 
  90. if(isset($section['fields']) && is_array($section['fields']) && !empty($section['fields'])) { 
  91. foreach($section['fields'] as $field) { 
  92. if(isset($field['id']) && $field['id'] && isset($field['title'])) { 
  93. add_settings_field( $field['id'], $field['title'], array(&$this, 'generate_setting'), $this->option_group, $section['section_id'], array('section' => $section, 'field' => $field) ); 
  94.  
  95. /** 
  96. * Usort callback. Sorts $wpsf_settings by "section_order" 
  97. *  
  98. * @param mixed section order a 
  99. * @param mixed section order b 
  100. * @return int order 
  101. */ 
  102. function sort_array( $a, $b ) 
  103. return $a['section_order'] > $b['section_order']; 
  104.  
  105. /** 
  106. * Generates the HTML output of the settings fields 
  107. * @param array callback args from add_settings_field() 
  108. */ 
  109. function generate_setting( $args ) 
  110. $section = $args['section']; 
  111. $defaults = array( 
  112. 'id' => 'default_field',  
  113. 'title' => 'Default Field',  
  114. 'desc' => '',  
  115. 'std' => '',  
  116. 'type' => 'text',  
  117. 'choices' => array(),  
  118. 'class' => '' 
  119. ); 
  120. $defaults = apply_filters( 'wpsf_defaults', $defaults ); 
  121. extract( wp_parse_args( $args['field'], $defaults ) ); 
  122.  
  123. $options = get_option( $this->option_group .'_settings' ); 
  124. $el_id = $this->option_group .'_'. $section['section_id'] .'_'. $id; 
  125. $val = (isset($options[$el_id])) ? $options[$el_id] : $std; 
  126.  
  127. do_action('wpsf_before_field'); 
  128. do_action('wpsf_before_field_'. $el_id); 
  129. switch( $type ) { 
  130. case 'text': 
  131. $val = esc_attr(stripslashes($val)); 
  132. echo '<input type="text" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="'. $val .'" class="regular-text '. $class .'" />'; 
  133. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  134. break; 
  135. case 'textarea': 
  136. $val = esc_html(stripslashes($val)); 
  137. echo '<textarea name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" rows="5" cols="60" class="'. $class .'">'. $val .'</textarea>'; 
  138. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  139. break; 
  140. case 'select': 
  141. $val = esc_html(esc_attr($val)); 
  142. echo '<select name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" class="'. $class .'">'; 
  143. foreach($choices as $ckey=>$cval) { 
  144. echo '<option value="'. $ckey .'"'. (($ckey == $val) ? ' selected="selected"' : '') .'>'. $cval .'</option>'; 
  145. echo '</select>'; 
  146. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  147. break; 
  148. case 'radio': 
  149. $val = esc_html(esc_attr($val)); 
  150. foreach($choices as $ckey=>$cval) { 
  151. echo '<label><input type="radio" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'_'. $ckey .'" value="'. $ckey .'" class="'. $class .'"'. (($ckey == $val) ? ' checked="checked"' : '') .' /> '. $cval .'</label><br />'; 
  152. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  153. break; 
  154. case 'checkbox': 
  155. $val = esc_attr(stripslashes($val)); 
  156. echo '<input type="hidden" name="'. $this->option_group .'_settings['. $el_id .']" value="0" />'; 
  157. echo '<label><input type="checkbox" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="1" class="'. $class .'"'. (($val) ? ' checked="checked"' : '') .' /> '. $desc .'</label>'; 
  158. break; 
  159. case 'checkboxes': 
  160. foreach($choices as $ckey=>$cval) { 
  161. $val = ''; 
  162. if(isset($options[$el_id .'_'. $ckey])) $val = $options[$el_id .'_'. $ckey]; 
  163. elseif(is_array($std) && in_array($ckey, $std)) $val = $ckey; 
  164. $val = esc_html(esc_attr($val)); 
  165. echo '<input type="hidden" name="'. $this->option_group .'_settings['. $el_id .'_'. $ckey .']" value="0" />'; 
  166. echo '<label><input type="checkbox" name="'. $this->option_group .'_settings['. $el_id .'_'. $ckey .']" id="'. $el_id .'_'. $ckey .'" value="'. $ckey .'" class="'. $class .'"'. (($ckey == $val) ? ' checked="checked"' : '') .' /> '. $cval .'</label><br />'; 
  167. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  168. break; 
  169. case 'color': 
  170. $val = esc_attr(stripslashes($val)); 
  171. echo '<div style="position:relative;">'; 
  172. echo '<input type="text" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="'. $val .'" class="'. $class .'" />'; 
  173. echo '<div id="'. $el_id .'_cp" style="position:absolute;top:0;left:190px;background:#fff;"></div>'; 
  174. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  175. echo '<script type="text/javascript"> 
  176. jQuery(document).ready(function($) {  
  177. var colorPicker = $("#'. $el_id .'_cp"); 
  178. colorPicker.farbtastic("#'. $el_id .'"); 
  179. colorPicker.hide(); 
  180. $("#'. $el_id .'").live("focus", function() { 
  181. colorPicker.show(); 
  182. }); 
  183. $("#'. $el_id .'").live("blur", function() { 
  184. colorPicker.hide(); 
  185. if($(this).val() == "") $(this).val("#"); 
  186. }); 
  187. }); 
  188. </script></div>'; 
  189. break; 
  190. case 'file': 
  191. $val = esc_attr($val); 
  192. echo '<input type="text" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="'. $val .'" class="regular-text '. $class .'" /> '; 
  193. echo '<input type="button" class="button wpsf-browse" id="'. $el_id .'_button" value="Browse" />'; 
  194. echo '<script type="text/javascript"> 
  195. jQuery(document).ready(function($) { 
  196. $("#'. $el_id .'_button").click(function() { 
  197. tb_show("", "media-upload.php?post_id=0&type=image&TB_iframe=true"); 
  198. window.original_send_to_editor = window.send_to_editor; 
  199. window.send_to_editor = function(html) { 
  200. var imgurl = $("img", html).attr("src"); 
  201. $("#'. $el_id .'").val(imgurl); 
  202. tb_remove(); 
  203. window.send_to_editor = window.original_send_to_editor; 
  204. }; 
  205. return false; 
  206. }); 
  207. }); 
  208. </script>'; 
  209. break; 
  210. default: 
  211. break; 
  212. do_action('wpsf_after_field'); 
  213. do_action('wpsf_after_field_'. $el_id); 
  214.  
  215. /** 
  216. * Output the settings form 
  217. */ 
  218. function settings() 
  219. do_action('wpsf_before_settings'); 
  220. ?> 
  221. <form action="options.php" method="post"> 
  222. <?php do_action('wpsf_before_settings_fields'); ?> 
  223. <?php settings_fields( $this->option_group ); ?> 
  224. <?php do_settings_sections( $this->option_group ); ?> 
  225. <p class="submit"><input type="submit" class="button-primary" value="<?php _e( 'Save Changes' ); ?>" /></p> 
  226. </form> 
  227. <?php 
  228. do_action('wpsf_after_settings'); 
  229.  
  230. }  
  1. class WordPressSettingsFramework { 
  2.  
  3. /** 
  4. * @access private 
  5. * @var string  
  6. */ 
  7. private $option_group; 
  8.  
  9. /** 
  10. * Constructor 
  11. *  
  12. * @param string path to settings file 
  13. * @param string optional "option_group" override 
  14. */ 
  15. function __construct( $settings_file, $option_group = '' ) 
  16. if( !is_file( $settings_file ) ) return; 
  17. require_once( $settings_file ); 
  18.  
  19. $this->option_group = preg_replace("/[^a-z0-9]+/i", "", basename( $settings_file, '.php' )); 
  20. if( $option_group ) $this->option_group = $option_group; 
  21.  
  22. add_action('admin_init', array(&$this, 'admin_init')); 
  23. add_action('admin_notices', array(&$this, 'admin_notices')); 
  24. add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts')); 
  25.  
  26. /** 
  27. * Get the option group for this instance 
  28. *  
  29. * @return string the "option_group" 
  30. */ 
  31. function get_option_group() 
  32. return $this->option_group; 
  33.  
  34. /** 
  35. * Registers the internal WordPress settings 
  36. */ 
  37. function admin_init() 
  38. register_setting( $this->option_group, $this->option_group .'_settings', array(&$this, 'settings_validate') ); 
  39. $this->process_settings(); 
  40.  
  41. /** 
  42. * Displays any errors from the WordPress settings API 
  43. */ 
  44. function admin_notices() 
  45. settings_errors(); 
  46.  
  47. /** 
  48. * Enqueue scripts and styles 
  49. */ 
  50. function admin_enqueue_scripts() 
  51. wp_enqueue_style('farbtastic'); 
  52. wp_enqueue_style('thickbox'); 
  53.  
  54. wp_enqueue_script('jquery'); 
  55. wp_enqueue_script('farbtastic'); 
  56. wp_enqueue_script('media-upload'); 
  57. wp_enqueue_script('thickbox'); 
  58.  
  59. /** 
  60. * Adds a filter for settings validation 
  61. *  
  62. * @param array the un-validated settings 
  63. * @return array the validated settings 
  64. */ 
  65. function settings_validate( $input ) 
  66. return apply_filters( $this->option_group .'_settings_validate', $input ); 
  67.  
  68. /** 
  69. * Displays the "section_description" if speicified in $wpsf_settings 
  70. * @param array callback args from add_settings_section() 
  71. */ 
  72. function section_intro( $args ) 
  73. global $wpsf_settings; 
  74. if(!empty($wpsf_settings)) { 
  75. foreach($wpsf_settings as $section) { 
  76. if($section['section_id'] == $args['id']) { 
  77. if(isset($section['section_description']) && $section['section_description']) echo '<p>'. $section['section_description'] .'</p>'; 
  78. break; 
  79.  
  80. /** 
  81. * Processes $wpsf_settings and adds the sections and fields via the WordPress settings API 
  82. */ 
  83. function process_settings() 
  84. global $wpsf_settings; 
  85. if(!empty($wpsf_settings)) { 
  86. usort($wpsf_settings, array(&$this, 'sort_array')); 
  87. foreach($wpsf_settings as $section) { 
  88. if(isset($section['section_id']) && $section['section_id'] && isset($section['section_title'])) { 
  89. add_settings_section( $section['section_id'], $section['section_title'], array(&$this, 'section_intro'), $this->option_group ); 
  90. if(isset($section['fields']) && is_array($section['fields']) && !empty($section['fields'])) { 
  91. foreach($section['fields'] as $field) { 
  92. if(isset($field['id']) && $field['id'] && isset($field['title'])) { 
  93. add_settings_field( $field['id'], $field['title'], array(&$this, 'generate_setting'), $this->option_group, $section['section_id'], array('section' => $section, 'field' => $field) ); 
  94.  
  95. /** 
  96. * Usort callback. Sorts $wpsf_settings by "section_order" 
  97. *  
  98. * @param mixed section order a 
  99. * @param mixed section order b 
  100. * @return int order 
  101. */ 
  102. function sort_array( $a, $b ) 
  103. return $a['section_order'] > $b['section_order']; 
  104.  
  105. /** 
  106. * Generates the HTML output of the settings fields 
  107. * @param array callback args from add_settings_field() 
  108. */ 
  109. function generate_setting( $args ) 
  110. $section = $args['section']; 
  111. $defaults = array( 
  112. 'id' => 'default_field',  
  113. 'title' => 'Default Field',  
  114. 'desc' => '',  
  115. 'std' => '',  
  116. 'type' => 'text',  
  117. 'choices' => array(),  
  118. 'class' => '' 
  119. ); 
  120. $defaults = apply_filters( 'wpsf_defaults', $defaults ); 
  121. extract( wp_parse_args( $args['field'], $defaults ) ); 
  122.  
  123. $options = get_option( $this->option_group .'_settings' ); 
  124. $el_id = $this->option_group .'_'. $section['section_id'] .'_'. $id; 
  125. $val = (isset($options[$el_id])) ? $options[$el_id] : $std; 
  126.  
  127. do_action('wpsf_before_field'); 
  128. do_action('wpsf_before_field_'. $el_id); 
  129. switch( $type ) { 
  130. case 'text': 
  131. $val = esc_attr(stripslashes($val)); 
  132. echo '<input type="text" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="'. $val .'" class="regular-text '. $class .'" />'; 
  133. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  134. break; 
  135. case 'textarea': 
  136. $val = esc_html(stripslashes($val)); 
  137. echo '<textarea name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" rows="5" cols="60" class="'. $class .'">'. $val .'</textarea>'; 
  138. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  139. break; 
  140. case 'select': 
  141. $val = esc_html(esc_attr($val)); 
  142. echo '<select name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" class="'. $class .'">'; 
  143. foreach($choices as $ckey=>$cval) { 
  144. echo '<option value="'. $ckey .'"'. (($ckey == $val) ? ' selected="selected"' : '') .'>'. $cval .'</option>'; 
  145. echo '</select>'; 
  146. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  147. break; 
  148. case 'radio': 
  149. $val = esc_html(esc_attr($val)); 
  150. foreach($choices as $ckey=>$cval) { 
  151. echo '<label><input type="radio" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'_'. $ckey .'" value="'. $ckey .'" class="'. $class .'"'. (($ckey == $val) ? ' checked="checked"' : '') .' /> '. $cval .'</label><br />'; 
  152. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  153. break; 
  154. case 'checkbox': 
  155. $val = esc_attr(stripslashes($val)); 
  156. echo '<input type="hidden" name="'. $this->option_group .'_settings['. $el_id .']" value="0" />'; 
  157. echo '<label><input type="checkbox" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="1" class="'. $class .'"'. (($val) ? ' checked="checked"' : '') .' /> '. $desc .'</label>'; 
  158. break; 
  159. case 'checkboxes': 
  160. foreach($choices as $ckey=>$cval) { 
  161. $val = ''; 
  162. if(isset($options[$el_id .'_'. $ckey])) $val = $options[$el_id .'_'. $ckey]; 
  163. elseif(is_array($std) && in_array($ckey, $std)) $val = $ckey; 
  164. $val = esc_html(esc_attr($val)); 
  165. echo '<input type="hidden" name="'. $this->option_group .'_settings['. $el_id .'_'. $ckey .']" value="0" />'; 
  166. echo '<label><input type="checkbox" name="'. $this->option_group .'_settings['. $el_id .'_'. $ckey .']" id="'. $el_id .'_'. $ckey .'" value="'. $ckey .'" class="'. $class .'"'. (($ckey == $val) ? ' checked="checked"' : '') .' /> '. $cval .'</label><br />'; 
  167. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  168. break; 
  169. case 'color': 
  170. $val = esc_attr(stripslashes($val)); 
  171. echo '<div style="position:relative;">'; 
  172. echo '<input type="text" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="'. $val .'" class="'. $class .'" />'; 
  173. echo '<div id="'. $el_id .'_cp" style="position:absolute;top:0;left:190px;background:#fff;"></div>'; 
  174. if($desc) echo '<p class="description">'. $desc .'</p>'; 
  175. echo '<script type="text/javascript"> 
  176. jQuery(document).ready(function($) {  
  177. var colorPicker = $("#'. $el_id .'_cp"); 
  178. colorPicker.farbtastic("#'. $el_id .'"); 
  179. colorPicker.hide(); 
  180. $("#'. $el_id .'").live("focus", function() { 
  181. colorPicker.show(); 
  182. }); 
  183. $("#'. $el_id .'").live("blur", function() { 
  184. colorPicker.hide(); 
  185. if($(this).val() == "") $(this).val("#"); 
  186. }); 
  187. }); 
  188. </script></div>'; 
  189. break; 
  190. case 'file': 
  191. $val = esc_attr($val); 
  192. echo '<input type="text" name="'. $this->option_group .'_settings['. $el_id .']" id="'. $el_id .'" value="'. $val .'" class="regular-text '. $class .'" /> '; 
  193. echo '<input type="button" class="button wpsf-browse" id="'. $el_id .'_button" value="Browse" />'; 
  194. echo '<script type="text/javascript"> 
  195. jQuery(document).ready(function($) { 
  196. $("#'. $el_id .'_button").click(function() { 
  197. tb_show("", "media-upload.php?post_id=0&type=image&TB_iframe=true"); 
  198. window.original_send_to_editor = window.send_to_editor; 
  199. window.send_to_editor = function(html) { 
  200. var imgurl = $("img", html).attr("src"); 
  201. $("#'. $el_id .'").val(imgurl); 
  202. tb_remove(); 
  203. window.send_to_editor = window.original_send_to_editor; 
  204. }; 
  205. return false; 
  206. }); 
  207. }); 
  208. </script>'; 
  209. break; 
  210. default: 
  211. break; 
  212. do_action('wpsf_after_field'); 
  213. do_action('wpsf_after_field_'. $el_id); 
  214.  
  215. /** 
  216. * Output the settings form 
  217. */ 
  218. function settings() 
  219. do_action('wpsf_before_settings'); 
  220. ?> 
  221. <form action="options.php" method="post"> 
  222. <?php do_action('wpsf_before_settings_fields'); ?> 
  223. <?php settings_fields( $this->option_group ); ?> 
  224. <?php do_settings_sections( $this->option_group ); ?> 
  225. <p class="submit"><input type="submit" class="button-primary" value="<?php _e( 'Save Changes' ); ?>" /></p> 
  226. </form> 
  227. <?php 
  228. do_action('wpsf_after_settings'); 
  229.  
  230. }