<?php
/**
 * Theme Options for Vertigo
 *
 * @package Vertigo
 */

/**
 * Init plugin options to white list our options and add css for options page
 */
function vertigo_theme_options_init() {
	register_setting( 'vertigo_options', 'vertigo_theme_options', 'vertigo_theme_options_validate' );

	// Register our settings field group
	add_settings_section(
		'general', // Unique identifier for the settings section
		'', // Section title (we don't want one)
		'__return_false', // Section callback (we don't want anything)
		'theme_options' // Menu slug, used to uniquely identify the page; see vertigo_theme_options_add_page()
	);

	add_settings_field( 'colorpicker', __( 'Accent Color', 'vertigo' ), 'vertigo_settings_field_colorpicker', 'theme_options', 'general' );
	add_settings_field( 'font', __( 'Font', 'vertigo' ), 'vertigo_settings_field_font', 'theme_options', 'general' );
}
add_action( 'admin_init', 'vertigo_theme_options_init' );

/**
 * Load up the menu page
 */
function vertigo_theme_options_add_page() {
	add_theme_page( __( 'Theme Options', 'vertigo' ), __( 'Theme Options', 'vertigo' ), 'edit_theme_options', 'theme_options', 'vertigo_theme_options_render_page' );
}
add_action( 'admin_menu', 'vertigo_theme_options_add_page' );

/**
 * Change the capability required to save the 'vertigo_options' options group.
 *
 * @see vertigo_theme_options_init() First parameter to register_setting() is the name of the options group.
 * @see vertigo_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
 *
 * @param string $capability The capability used for the page, which is manage_options by default.
 * @return string The capability to actually use.
 */
function vertigo_option_page_capability( $capability ) {
	return 'edit_theme_options';
}
add_filter( 'option_page_capability_vertigo_options', 'vertigo_option_page_capability' );

/**
 * Add JS for theme options form
 */
function vertigo_enqueue_theme_options_scripts() {
	wp_enqueue_style( 'vertigo-theme-options', get_template_directory_uri() . '/inc/theme-options.css', false, '1.0', 'all' );
	wp_enqueue_script( 'vertigo-theme-options', get_template_directory_uri() . '/js/jscolor/jscolor.js' );
}
add_action( 'admin_print_scripts-appearance_page_theme_options', 'vertigo_enqueue_theme_options_scripts' );

/**
 * Renders the colorpicker setting field.
 */
function vertigo_settings_field_colorpicker() {
	$options = vertigo_get_theme_options();
	?>
	<input type="text" name="vertigo_theme_options[accent_color]" id="accent-color" value="<?php echo esc_attr( $options['accent_color'] ); ?>" class="color { pickerPosition:'right' }" />
	<label class="description" for="accent-color"><?php _e( 'Change the accent color by entering a HEX color number. (ie: <code>EE3322</code>)', 'vertigo' ) ?></label>
	<?php
}

/**
 * Renders the font setting field.
 */
function vertigo_settings_field_font() {
	$options = vertigo_get_theme_options();
	?>
	<label for="vertigo-font">
		<input type="checkbox" name="vertigo_theme_options[vertigo_font]" id="vertigo-font" value="true" <?php checked( $options['vertigo_font'], 'true' ); ?> />
		<?php _e( 'Enable Hitchcock custom font (Note: this font only supports basic Latin uppercase letters, numerals, and some punctuation.)', 'vertigo' ); ?>
		<img src="<?php echo get_template_directory_uri(); ?>/inc/images/hitchcock.gif" alt="Hitchcock" id="hitchcock-sample"/>
	</label>
	<?php
}

/**
 * Calculate default option values
 *
 * @return array
 */
function vertigo_default_options() {
	$default_options = array(
		'accent_color' => 'ee3322',
		'vertigo_font' => ( '1' == get_option( 'lang_id' ) ) ? 'true' : 'false'
	);

	return apply_filters( 'vertigo_default_options', $default_options );
}

/**
 * Renders the Theme Options administration screen.
 */
function vertigo_theme_options_render_page() {
	?>
	<div class="wrap">
		<h2><?php printf( __( '%s Theme Options', 'vertigo' ), wp_get_theme() ); ?></h2>
		<?php settings_errors(); ?>

		<form method="post" action="options.php">
			<?php
				settings_fields( 'vertigo_options' );
				do_settings_sections( 'theme_options' );
			?>

			<p class="submit">
				<?php submit_button( null, 'primary', 'submit', false ); ?>
				<?php submit_button( __( 'Reset Options', 'vertigo' ), 'secondary', 'vertigo_theme_options[reset]', false, array( 'id' => 'reset' ) ); ?>
			</p>
		</form>
	</div>
	<?php
}

/**
 * Sanitize and validate form input
 *
 * @param array options
 * @return array sanitized options
 */
function vertigo_theme_options_validate( $input ) {
	$defaults = vertigo_default_options();

	// Check accent color input format
	// Valid = hexadecimal 3 or 6 digits
	$accent_color = preg_replace( '/[^0-9a-fA-F]/', '', $input['accent_color'] );
	if ( 6 == strlen( $accent_color ) || 3 == strlen( $accent_color ) )
		$input['accent_color'] = $accent_color;
	else
		$input['accent_color'] = $defaults['accent_color'];

	// Check that Vertigo font checkbox value is either true or false
	if ( ! isset( $input['vertigo_font'] ) )
	$input['vertigo_font'] = ( $input['vertigo_font'] == 'true' ? 'true' : 'false' );

	// Reset to default options
	if ( ! empty( $input['reset'] ) ) {
		$defaults = vertigo_default_options();
		foreach ( $input as $field => $value ) {
			if ( isset( $defaults[$field] ) )
				$input[$field] = $defaults[$field];
			else
				unset( $input[$field] );
		}
	}

	return $input;
}