settings = include( $settings_file ); $this->option_group = preg_replace("/[^a-z0-9]+/i", "", basename( $settings_file, '.php' )); if( $option_group ) $this->option_group = $option_group; add_action('admin_init', array(&$this, 'admin_init')); add_action('admin_notices', array(&$this, 'admin_notices')); add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts')); } /** * Get the option group for this instance * * @return string the "option_group" */ function get_option_group() { return $this->option_group; } /** * Registers the internal WordPress settings */ function admin_init() { register_setting( $this->option_group, $this->option_group .'_settings', array(&$this, 'settings_validate') ); $this->process_settings(); } /** * Displays any errors from the WordPress settings API */ function admin_notices() { //settings_errors(); } /** * Enqueue scripts and styles */ function admin_enqueue_scripts() { wp_enqueue_style('farbtastic'); wp_enqueue_style('thickbox'); wp_enqueue_script('jquery'); wp_enqueue_script('farbtastic'); wp_enqueue_script('media-upload'); wp_enqueue_script('thickbox'); } /** * Adds a filter for settings validation * * @param array the un-validated settings * @return array the validated settings */ function settings_validate( $input ) { return apply_filters( $this->option_group .'_settings_validate', $input ); } /** * Displays the "section_description" if speicified in $wpsf_settings * * @param array callback args from add_settings_section() */ function section_intro( $args ) { if(!empty($this->settings)){ foreach($this->settings as $section){ if($section['section_id'] == $args['id']){ if(isset($section['section_description']) && $section['section_description']) echo '

'. $section['section_description'] .'

'; break; } } } } /** * Processes $wpsf_settings and adds the sections and fields via the WordPress settings API */ function process_settings() { if(!empty($this->settings)){ usort($this->settings, array(&$this, 'sort_array')); foreach($this->settings as $section){ if(isset($section['section_id']) && $section['section_id'] && isset($section['section_title'])){ add_settings_section( $section['section_id'], $section['section_title'], array(&$this, 'section_intro'), $this->option_group ); if(isset($section['fields']) && is_array($section['fields']) && !empty($section['fields'])){ foreach($section['fields'] as $field){ if(isset($field['id']) && $field['id'] && isset($field['title'])){ add_settings_field( $field['id'], $field['title'], array(&$this, 'generate_setting'), $this->option_group, $section['section_id'], array('section' => $section, 'field' => $field) ); } } } } } } } /** * Usort callback. Sorts $wpsf_settings by "section_order" * * @param mixed section order a * @param mixed section order b * @return int order */ function sort_array( $a, $b ) { return $a['section_order'] > $b['section_order']; } /** * Generates the HTML output of the settings fields * * @param array callback args from add_settings_field() */ function generate_setting( $args ) { $section = $args['section']; $defaults = array( 'id' => 'default_field', 'title' => 'Default Field', 'desc' => '', 'std' => '', 'type' => 'text', 'choices' => array(), 'class' => '' ); $defaults = apply_filters( 'wpsf_defaults', $defaults ); extract( wp_parse_args( $args['field'], $defaults ) ); $options = get_option( $this->option_group .'_settings' ); $el_id = $this->option_group .'_'. $section['section_id'] .'_'. $id; $val = (isset($options[$el_id])) ? $options[$el_id] : $std; do_action('wpsf_before_field'); do_action('wpsf_before_field_'. $el_id); switch( $type ){ case 'text': $val = esc_attr(stripslashes($val)); echo ''; if($desc) echo '

'. $desc .'

'; break; case 'textarea': $val = esc_html(stripslashes($val)); echo ''; if($desc) echo '

'. $desc .'

'; break; case 'select': $val = esc_html(esc_attr($val)); echo ''; if($desc) echo '

'. $desc .'

'; break; case 'radio': $val = esc_html(esc_attr($val)); foreach($choices as $ckey=>$cval){ echo '
'; } if($desc) echo '

'. $desc .'

'; break; case 'checkbox': $val = esc_attr(stripslashes($val)); echo ''; echo ''; break; case 'checkboxes': foreach($choices as $ckey=>$cval){ $val = ''; if(isset($options[$el_id .'_'. $ckey])) $val = $options[$el_id .'_'. $ckey]; elseif(is_array($std) && in_array($ckey, $std)) $val = $ckey; $val = esc_html(esc_attr($val)); echo ''; echo '
'; } if($desc) echo '

'. $desc .'

'; break; case 'color': $val = esc_attr(stripslashes($val)); echo '
'; echo ''; echo '
'; if($desc) echo '

'. $desc .'

'; echo '
'; break; case 'file': $val = esc_attr($val); echo ' '; echo ''; echo ''; break; case 'editor': wp_editor( $val, $el_id, array( 'textarea_name' => $this->option_group .'_settings['. $el_id .']' ) ); if($desc) echo '

'. $desc .'

'; break; case 'custom': echo $std; break; default: break; } do_action('wpsf_after_field'); do_action('wpsf_after_field_'. $el_id); } /** * Output the settings form */ function settings() { do_action('wpsf_before_settings'); ?>
option_group ); ?> option_group ); ?>