<?php
/*
Plugin Name: Example Options
Plugin URI: http://bueltge.de/
Description: Options in WordPress 2.8 and higher, register_setting() API
Author: Frank B&uuml;ltge
Author URI: http://bueltge.de/
*/

if ( !function_exists('add_action') ) {
	header('Status: 403 Forbidden');
	header('HTTP/1.1 403 Forbidden');
	exit();
}

if ( function_exists('add_action') ) {
	//WordPress definitions
	if ( !defined('WP_CONTENT_URL') )
		define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
	if ( !defined('WP_CONTENT_DIR') )
		define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
	if ( !defined('WP_PLUGIN_URL') )
		define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
	if ( !defined('WP_PLUGIN_DIR') )
		define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');
	if ( !defined('PLUGINDIR') )
		define( 'PLUGINDIR', 'wp-content/plugins' );
	if ( !defined('WP_LANG_DIR') )
		define('WP_LANG_DIR', WP_CONTENT_DIR . '/languages');

	// plugin definitions
	define( 'FB_MSO_BASENAME', plugin_basename(__FILE__) );
	define( 'FB_MSO_TEXTDOMAIN', 'mysampleoptions' );
}

if ( !class_exists( 'MySampleOptions' ) ) {
	class MySampleOptions {

		// constructor
		function MySampleOptions() {
			add_action( 'admin_init',
									array(&$this, 'example_options_init')
								);
			add_action( 'admin_menu',
									array(&$this, 'example_options_add_page')
								);
		}
		
		// Plugin options initialisieren
		function example_options_init() {
			register_setting( 'example_options',
												'example_name',
												array(&$this, 'example_options_validate')
											);
		}
		
		// Menu page Seite hinzufuegen
		function example_options_add_page() {
			add_options_page( __( 'Example Options', FB_MSO_TEXTDOMAIN ),
												__( 'Example Options with WP 2.8',
														FB_MSO_TEXTDOMAIN ),
												'manage_options', 'example_options',
												array(&$this, 'example_options_do_page')
											);
		}
		
		// HTML, Simple Options Page
		function example_options_do_page() {
			?>
			<div class="wrap">
				<h2><?php _e( 'Example Options with WP 2.8',
											FB_MSO_TEXTDOMAIN ); ?></h2>
				<form method="post" action="options.php">
					
					<?php
					settings_fields('example_options');
					$options = get_option('example_name');
					?>
					
					<table class="form-table">
						<tr valign="top">
							<th scope="row">
								<?php _e( 'A example checkbox', FB_MSO_TEXTDOMAIN ); ?>
							</th>
							<td><input name="example_name[myoption1]" type="checkbox" 
										value="1" <?php checked('1',
										$options['myoption1']); ?> /></td>
						</tr>
						<tr valign="top">
							<th scope="row">A example text</th>
							<td><input type="text" name="example_name[mytext]"
											value="<?php echo $options['mytext']; ?>" /></td>
						</tr>
					</table>
					
					<p class="submit">
						<input type="submit" class="button-primary"
											value="<?php _e( 'Save Changes',
																				FB_MSO_TEXTDOMAIN ); ?>" />
					</p>
					
				</form>
			</div>
			<?php	
		}
		
		// Sanitize und validiere input
		// (Akzeptiert ein array, return ein sanitized array)
		function example_options_validate($input) {
			// erster Wert muss 0 oder 1 sein
			$input['option1'] = ( $input['option1'] == 1 ? 1 : 0 );
			
			// Wert mit text
			$input['sometext'] =  wp_filter_nohtml_kses($input['sometext']);
			
			return $input;
		}
	
	}
		
	// instance class
	$MySampleOptions = new MySampleOptions();
}