<?php
/*
Plugin Name: Settings API Test
Description: Plugin to test the settings api
Author: prettyboymp
License: GPLv2 or later
*/
class Test_Settings_1 {
	
	private $settings_page_key;

	public function  __construct() {
		$this->settings_page_key = 'test-settings-page';
	}

	public function initialize() {
		add_action('admin_menu', array($this, '_admin_menu'));
		add_action('admin_init', array($this, '_admin_init'));
	}

	public function _admin_init() {
	}

	public function _admin_menu() {
		$hook = add_menu_page('Test Settings', 'Test', 'manage_options', $this->settings_page_key, array($this, '_print_test_settings_page'));

		add_action('load-'.$hook, array($this, '_on_load_settings_page_1'));
	}

	public function _on_load_settings_page_1() {
		global $current_screen;

		add_contextual_help( $current_screen, '<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' ) . '</p>' );

		add_settings_section( 'section-a', 'Section A', '', $this->settings_page_key );
		add_settings_field( 'foo', 'Foo', array( $this, 'display_generic_select_field' ), $this->settings_page_key, 'section-a', array( 'options' => array( 1 => 'Foo', 2 => 'Bar', 3 => 'Quasi' ) ), array( $this, 'sanitize_generic_select_field' ), 'foobar' );
		add_settings_field( 'bar', 'Bar', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-a', array( ), array( $this, 'sanitize_generic_text_field' ), 'foobar' );
		add_settings_field( 'someint', 'Some Int', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-a', array( ), array( $this, 'sanitize_generic_int_field' ), 'foobar' );

		add_settings_section( 'section-b', 'Section B', '', $this->settings_page_key );
		add_settings_field( 'cheese', 'Cheese', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-b', array( ), array( $this, 'sanitize_generic_text_field' ) );
		add_settings_field( 'wine', 'Wine', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-b', array( ), array( $this, 'sanitize_generic_text_field' ) );

		add_settings_section( 'section-c', 'Section C', '', $this->settings_page_key );
		add_settings_field( 'mayo', 'mayo', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-c', array( ), array( $this, 'sanitize_generic_text_field' ) );
		add_settings_field( 'mustard', 'Mustard', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-c', array( ), array( $this, 'sanitize_generic_text_field' ) );

		if( isset( $_REQUEST[ 'action' ] ) ) {
			check_admin_referer( $this->settings_page_key . '-options' );
			save_settings_page( $this->settings_page_key );
		}

	}

	public function display_generic_text_field( $args, $current_value, $field, $section_id, $page_id ) {
		?>
		<input type="text" name="<?php echo esc_attr( $field[ 'id' ] );?>" value="<?php echo esc_attr( $current_value );?>" />
		<?php
	}

	public function display_generic_integer_field( $args, $current_value, $field, $section_id, $page_id ) {
		?>
		<input type="text" id="<?php echo esc_attr( $field[ 'id' ] )?>" name="<?php echo esc_attr( $field[ 'id' ] );?>" value="<?php echo esc_attr( $current_value );?>" />
		<?php
	}

	public function display_generic_select_field( $args, $current_value, $field, $section_id, $page_id ) {
		if( isset( $args[ 'options' ] ) && is_array( $args[ 'options' ] ) ) {
			?>
			<select id="<?php echo esc_attr( $field[ 'id' ] )?>" name="<?php echo esc_attr( $field[ 'id' ] );?>">
				<?php foreach( $args[ 'options' ] as $key => $name ) :?>
					<option value="<?php echo esc_attr( $key )?>"<?php selected( $key, $current_value )?>><?php echo esc_html( $name )?></option>
				<?php endforeach;?>
			</select>
			<?php
		}
	}

	public function sanitize_generic_text_field( $new_value, $old_value ) {
		return strip_tags($new_value);
	}

	public function sanitize_generic_select_field( $new_value, $old_value, $field ) {
		if( isset( $field[ 'args' ][ 'options' ] ) && in_array( $new_value, array_keys( $field[ 'args' ][ 'options' ] ) ) )
			return $new_value;

		add_settings_error( $field[ 'id' ], 'invalid-option', sprintf( 'An invalid option was selected for %s', $field[ 'title' ] ) );
		return $old_value;
	}

	public function sanitize_generic_int_field( $new_value ) {
		return intval( $new_value );
	}


	public function _print_test_settings_page() {
		$title = __( 'Test Settings' );
		$page_key = $this->settings_page_key;
		$description = __( 'Some description text' );
		?>
		<div class="wrap">
			<?php screen_icon();?>
			<h2><?php echo $title?></h2>
			<?php settings_errors( $page_key );?>
			<?php if( $description )
				echo "<p>{$description}</p>";?>
			<form action="" method="post">
				<?php settings_fields( $this->settings_page_key ); //would like to rename this function ?>
				<?php do_settings_sections( $this->settings_page_key );?>
				<p class="submit">
					<input type="submit" value="Save Changes" class="button-primary" name="Submit">
				</p>
			</form>
		</div>
		<?php
	}
}
add_action( 'init', array( new Test_Settings_1(), 'initialize' ) );
add_action( 'init', array( new Test_Settings_1(), 'initialize' ) ); //kicking off two instances to be sure that multiple registrations don't interfer

