settings = $settings ? $settings : array(); $this->name = $name; $this->group = $group; $this->main = $main; $this->permission = $permission; $this->icon = $icon; $this->position = $position; $this->html = $html; $this->join = $join; add_action('admin_init', array($this, 'admin_init')); add_action('admin_menu', array($this, 'admin_menu')); } public function admin_init() { foreach ($this->settings as $setting) { $setting = (object) $setting; $type = $setting->type; $type = $type == 'select' ? 'string' : $type; $type = $type == 'text' ? 'string' : $type; $type = $type == 'url' ? 'string' : $type; $type = $type == 'password' ? 'string' : $type; register_setting($this->group, $setting->id, array( 'type' => $type, 'description' => $setting->title, 'default' => property_exists($setting, 'default') ? $setting->default : null, 'sanitize_callback' => array($this , 'sanitize_callback') ) ); } } public function sanitize_callback($input) { return sanitize_textarea_field($input); } public function admin_menu() { if ($this->main) { add_menu_page( $this->name, $this->name, $this->permission, self::create_key($this->group, $this->join), array($this, 'menu_page'), $this->icon, $this->position ); } else { add_submenu_page( self::create_key($this->group, $this->join), $this->name, $this->name, $this->permission, self::create_key($this->group.' '.$this->name, $this->join), array($this, 'menu_page') ); } } public static function create_key($key, $join = '-') { return urlencode(str_replace(' ',$join,strtolower($key))); } public static function notice($message, $type = 'success') { echo '

'.esc_html($message).'

'; } public static function check_nounce($key) { if (isset($_POST[$key])) { if (wp_verify_nonce(sanitize_key(wp_unslash($_POST[$key])), plugin_basename( __FILE__ ))) { return true; } else { self::notice('Security check failed, please login and try again.'); } } return false; } public static function echo_nounce($key) { wp_nonce_field(plugin_basename( __FILE__ ), $key); } public static function settings($setting) { //TODO: add select $allow = array( 'tr' => array( 'valign' => array(), 'class' => array() ), 'th' => array( 'scope' => array() ), 'td' => array(), 'div' => array( 'style' => array() ), 'select' => array( 'id' => array(), 'name' => array() ), 'input' => array( 'id' => array(), 'name' => array(), 'type' => array(), 'checked' => array(), 'value' => array(), 'style' => array(), 'placeholder' => array() ), 'textarea' => array( 'id' => array(), 'name' => array(), 'rows' => array(), 'cols' => array() ), 'option' => array( 'value' => array(), 'selected' => array() ), 'label' => array( 'for' => array(), 'name' => array() ) ); $setting = (object) $setting; if ($setting->type == 'boolean') { echo wp_kses(self::settings_checkbox($setting), $allow); } elseif ($setting->type == 'select') { echo wp_kses(self::settings_select($setting), $allow); } elseif ($setting->type == 'text') { echo wp_kses(self::settings_text($setting), $allow); } elseif ($setting->type == 'title') { echo wp_kses(self::settings_row($setting), $allow); } else { echo wp_kses(self::settings_input($setting), $allow); } } public static function settings_row($setting, $html = '') { return ' '.esc_html($setting->title).($setting->type=='text'&&property_exists($setting,'description')?'
'.esc_html($setting->description).'
':'').' '.$html.' '; } public static function settings_text($setting) { $html = ''; return self::settings_row($setting, $html); } public static function settings_select($setting) { $assoc = self::associative($setting->values); $current = get_option($setting->id); $html = ''; return self::settings_row($setting, $html); } public static function settings_checkbox($setting) { $html = 'id) == '1' ? ' checked="checked"' : '').' />'; $html .= ''; return self::settings_row($setting, $html); } public static function settings_input($setting) { $html = ''; return self::settings_row($setting, $html); } public static function checkbox_request($key) { return isset($_REQUEST[$key]) && $_REQUEST[$key] != '' ? 1 : 0; } public static function associative($array) { return array_keys($array) !== range(0, count($array) - 1); } public function menu_page() { echo '

'.esc_html($this->name).'

'; echo $this->html; $nounce = $this->group; if (self::check_nounce($nounce)) { foreach ($this->settings as $setting) { $setting = (object) $setting; $key = $setting->id; $value = isset($_REQUEST[$key]) ? stripslashes($_REQUEST[$key]) : ''; if ($setting->type == 'boolean') { update_option($key, self::checkbox_request($key)); } else if ($setting->type == 'text') { update_option($key, sanitize_textarea_field($value)); } else { update_option($key, sanitize_text_field($value)); } } self::notice('Saved.'); } ?>
settings as $setting) { self::settings((object) $setting); } ?>