'default_field', 'title' => 'Default Field', 'desc' => '', 'std' => '', 'type' => 'text', 'placeholder' => '', 'choices' => array(), 'class' => '' ); /** * Constructor * * @param string path to settings file * @param string optional "option_group" override */ public function __construct( $settings_file, $option_group = '' ) { if( is_admin() ) { global $pagenow; if( !is_file($settings_file) ) return; require_once( $settings_file ); $this->option_group = preg_replace("/[^a-z0-9]+/i", "", basename($settings_file, '.php')); if( $option_group ) $this->option_group = $option_group; $this->construct_settings(); add_action( 'admin_init', array( $this, 'admin_init') ); add_action( 'wpsf_do_settings_sections_'.$this->option_group, array( $this, 'do_tabless_settings_sections'), 10 ); if( isset( $_GET['page'] ) && $_GET['page'] === $this->settings_page['slug'] ) { if( $pagenow !== "options-general.php" ) add_action( 'admin_notices', array( $this, 'admin_notices') ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts') ); } if( $this->has_tabs() ) { add_action( 'wpsf_before_settings_'.$this->option_group, array( $this, 'tab_links' ) ); add_action( 'wpsf_before_settings_'.$this->option_group, array( $this, 'tab_styles' ) ); add_action( 'wpsf_before_settings_'.$this->option_group, array( $this, 'tab_scripts' ) ); remove_action( 'wpsf_do_settings_sections_'.$this->option_group, array( $this, 'do_tabless_settings_sections'), 10 ); add_action( 'wpsf_do_settings_sections_'.$this->option_group, array( $this, 'do_tabbed_settings_sections'), 10 ); } } } /** * Construct Settings * * @return array */ public function construct_settings() { $this->settings_wrapper = array(); $this->settings_wrapper = apply_filters( 'wpsf_register_settings_'.$this->option_group, $this->settings_wrapper ); if( !is_array($this->settings_wrapper) ){ return new WP_Error( 'broke', __( 'WPSF settings must be an array' ) ); } // If "sections" is set, this settings group probably has tabs if( isset( $this->settings_wrapper['sections'] ) ) { $this->tabs = (isset( $this->settings_wrapper['tabs'] )) ? $this->settings_wrapper['tabs'] : array(); $this->settings = $this->settings_wrapper['sections']; // If not, it's probably just an array of settings } else { $this->settings = $this->settings_wrapper; } $this->settings_page['slug'] = sprintf( '%s-settings', str_replace('_', '-', $this->option_group ) ); } /** * Get the option group for this instance * * @return string the "option_group" */ public function get_option_group() { return $this->option_group; } /** * Registers the internal WordPress settings */ public function admin_init() { register_setting( $this->option_group, $this->option_group .'_settings', array( $this, 'settings_validate') ); $this->process_settings(); } /** * Add Settings Page * * @param array $args */ public function add_settings_page( $args ) { $defaults = array( 'parent_slug' => false, 'page_slug' => "", 'page_title' => "", 'menu_title' => "", 'capability' => 'manage_options' ); $args = wp_parse_args( $args, $defaults ); $this->settings_page['title'] = $args['page_title']; if( $args['parent_slug'] ) { add_submenu_page( $args['parent_slug'], $this->settings_page['title'], $args['menu_title'], $args['capability'], $this->settings_page['slug'], array( $this, 'settings_page_content' ) ); } else { add_menu_page( $this->settings_page['title'], $args['menu_title'], $args['capability'], $this->settings_page['slug'], array( $this, 'settings_page_content' ) ); } } /** * Settings Page Content */ public function settings_page_content() { ?>
'. $section['section_description'] .'
'; break; } } } } /** * Processes $this->settings and adds the sections and fields via the WordPress settings API */ private 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']) ){ $page_name = ( $this->has_tabs() ) ? sprintf( '%s_%s', $this->option_group, $section['tab_id'] ) : $this->option_group; add_settings_section( $section['section_id'], $section['section_title'], array( $this, 'section_intro'), $page_name ); 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'), $page_name, $section['section_id'], array('section' => $section, 'field' => $field) ); } } } } } } } /** * Usort callback. Sorts $this->settings by "section_order" * * @param mixed section order a * @param mixed section order b * @return int order */ public 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() */ public function generate_setting( $args ) { $section = $args['section']; $this->setting_defaults = apply_filters( 'wpsf_defaults_'.$this->option_group, $this->setting_defaults ); extract( wp_parse_args( $args['field'], $this->setting_defaults ) ); $options = get_option( $this->option_group .'_settings' ); $el_id = sprintf( '%s_%s', $section['section_id'], $id ); $val = (isset($options[$el_id])) ? $options[$el_id] : $std; do_action( 'wpsf_before_field_'.$this->option_group ); do_action( 'wpsf_before_field__'.$this->option_group. $el_id ); switch( $type ){ case 'text': $val = esc_attr(stripslashes($val)); echo ''; if($desc) echo ''. $desc .'
'; break; case 'password': $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 ''. $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 ''. $desc .'
'; break; case 'color': $val = esc_attr(stripslashes($val)); echo ''. $desc .'
'; echo ''. $desc .'
'; break; case 'custom': echo $std; break; default: break; } do_action( 'wpsf_after_field_'.$this->option_group ); do_action( 'wpsf_after_field__'.$this->option_group. $el_id ); } /** * Output the settings form */ public function settings() { do_action( 'wpsf_before_settings_'.$this->option_group ); ?> option_group ); } /** * Tabless Settings sections */ public function do_tabless_settings_sections() { do_settings_sections( $this->option_group ); } /** * Tabbed Settings sections */ public function do_tabbed_settings_sections() { $i = 0; foreach ( $this->tabs as $tab_data ) { ?>