<?php
/**
 * Class Doc8_Settings
 *
 * The class use for controlling and outputting the different document settings.
 *
 * @package DoC8
 * @version 1.0.0
 */
class Doc8_Settings {
	public function __construct() {
		register_setting( 'doc8_settings', 'doc8_settings', array(
			'sanitize_callback' => array( $this, 'sanitize' ),
		) );

		// Flush rewrite rules after options are updated.
		add_action( 'updated_option', array( $this, 'maybe_flush_rewrite_rules' ), 10, 2 );

		$this->register_slugs_settings();
		$this->register_heading_settings();
		$this->register_orderby_settings();
		$this->register_template_settings();
	}

	public function register_slugs_settings() {
		// Set slugs sections
		add_settings_section( 'doc8_settings_slugs', __( 'Slugs', 'docu' ), array( $this, 'slugs_settings_heading' ), 'doc8_settings' );

		// Add slugs fields
		add_settings_field(
			'doc8_slug',
			__( 'Base Slug', 'docu' ),
			array( $this, 'input_field' ),
			'doc8_settings',
			'doc8_settings_slugs',
			array(
				'type' => 'text',
				'name' => 'doc8_slug',
				'default' => 'doc',
				'desc' => __( 'Use as the main slug that controls the permalink structure of a document.', 'docu' ),
			)
		);
		add_settings_field(
			'doc8_category_slug',
			__( 'Category Slug', 'docu' ),
			array( $this, 'input_field' ),
			'doc8_settings',
			'doc8_settings_slugs',
			array(
				'type' => 'text',
				'name' => 'doc8_category_slug',
				'default' => 'category',
				'desc' => __( 'Use as the category slug that controls the permalink structure of a document category.', 'docu' ),
			)
		);
	}

	public function register_heading_settings() {
		// Set heading options
		add_settings_section( 'doc8_heading', __( 'Heading', 'docu' ), array( $this, 'heading_description' ), 'doc8_settings' );

		// Set heading fields
		add_settings_field(
			'doc8_heading_title',
			__( 'Title', 'docu' ),
			array( $this, 'input_field' ),
			'doc8_settings',
			'doc8_heading',
			array(
				'type' => 'text',
				'name' => 'doc8_heading_title',
				'default' => __( 'Documents', 'docu' ),
			)
		);
		add_settings_field(
			'doc8_heading_tagline',
			__( 'Brief Description', 'docu' ),
			array( $this, 'input_field' ),
			'doc8_settings',
			'doc8_heading',
			array(
				'type' => 'textarea',
				'name' => 'doc8_heading_tagline',
			)
		);
	}

	public function register_orderby_settings() {
		// Sequence order
		add_settings_section( 'doc8_orderby', __( 'Order Sequence', 'docu' ), array( $this, 'orderby_heading' ), 'doc8_settings' );
		add_settings_field(
			'doc8_orderby',
			__( 'Orderby', 'docu' ),
			array( $this, 'input_field' ),
			'doc8_settings',
			'doc8_orderby',
			array(
				'type' => 'select',
				'name' => 'doc8_orderby',
				'options' => array(
					'post_date' => __( 'Default (date)', 'docu' ),
					'menu_order' => __( 'Menu order', 'docu' ),
				),
				'default' => 'menu_order',
			)
		);
		add_settings_field(
			'doc8_order',
			__( 'Order', 'docu' ),
			array( $this, 'input_field' ),
			'doc8_settings',
			'doc8_orderby',
			array(
				'type' => 'select',
				'name' => 'doc8_order',
				'options' => array(
					'asc' => __( 'Ascending (ASC)', 'docu' ),
					'desc' => __( 'Decending (ASC)', 'docu' ),
				),
				'default' => 'desc',
			)
		);

		add_settings_field(
			'doc8_per_page',
			__( 'Documents per page', 'docu' ),
			array( $this, 'input_field' ),
			'doc8_settings',
			'doc8_orderby',
			array(
				'type' => 'text',
				'name' => 'doc8_per_page',
				'default' => get_option( 'posts_per_page' ),
				'desc' => __( 'The number of primary documents to display per page.', 'docu' ),
			)
		);
	}

	public function register_template_settings() {
		add_settings_section( 'doc8_templates', __( 'Templating', 'doc8' ), array( $this, 'template_heading' ), 'doc8_settings' );

		add_settings_field(
			'doc8_default',
			__( 'Default', 'docu' ),
			array( $this, 'input_field' ),
			'doc8_settings',
			'doc8_templates',
			array(
				'type' => 'radio',
				'name' => 'doc8_template',
				'id' => 'doc8-default',
				'default' => 'default',
				'label' => __( 'Use the current theme\'s archive and singular templates to print the documents.', 'docu' ),
			)
		);

		add_settings_field(
			'doc8_standard',
			__( 'Standard', 'docu' ),
			array( $this, 'input_field' ),
			'doc8_settings',
			'doc8_templates',
			array(
				'type' => 'radio',
				'name' => 'doc8_template',
				'id' => 'doc8-standard',
				'default' => 'standard',
				'label' => __( 'Use the custom standard template to print the documents.', 'docu' ),
			)
		);
	}

	public function sanitize( $input ) {

		return $input;
	}

	public function maybe_flush_rewrite_rules( $option ) {
		if ( 'doc8_settings' != $option ) {
			return;
		}

		// Call register again to ensure slugs are properly registered and read!
		doc8_register();
		flush_rewrite_rules();
	}

	public function slugs_settings_heading() {
		printf( '<p class="description">%s</p>', __( 'Use to control the base permalink structure for document pages.', 'docu' ) );
	}

	public function heading_description() {
		printf( '<p class="description">%s</p>', __( 'Use as heading on document pages.', 'docu' ) );
	}

	public function orderby_heading() {
		printf( '<p class="description">%s</p>', __( 'The order to which the documents are listed.', 'docu' ) );
	}

	public function template_heading() {
		printf( '<p class="description">%s</p>', __( 'Use to display the documents.', 'docu' ) );
	}

	/**
	 * Helper method to generate and output form field.
	 *
	 * @param array $input
	 */
	public function input_field( $input ) {
		$input = wp_parse_args( $input, array(
			'type' => 'text',
			'default' => '',
			'options' => array(),
		));

		$type = $input['type'];
		$value = doc8_get_setting( $input['name'], $input['default'] );
		$name = 'doc8_settings[' . $input['name'] . ']';
		$id = ! empty($input['id']) ? $input['id'] : 'doc8';

		switch ( $type ) {
			default :
				printf( '<input type="%1$s" name="%2$s" value="%3$s" id="%4$s" class="regular-text" />', $type, $name, $value, $id );

				break;

			case 'radio' :
			case 'checkbox' :
				printf( '<input type="%1$s" name="%2$s" value="%3$s" id="%4$s" %5$s />', $type, $name, $input['default'], $id, checked( $value, $input['default'], false ) );

				if ( ! empty($input['label']) ) {
					printf( '<label for="%1$s">%2$s</label>', $id, $input['label'] );
				}

				break;


			case 'textarea' :
				printf( '<textarea name="%1$s" class="widefat" rows="5">%2$s</textarea>', $name, esc_textarea( $value ) );
				break;

			case 'select' :
				$options = '';

				foreach ( $input['options'] as $key => $val ) {
					$options .= sprintf( '<option value="%1$s"%2$s>%3$s</option>', esc_attr( $key ), selected( $value, $key, false ), $val );
				}

				printf( '<select name="%1$s" class="regular-text">%2$s</select>', $name, $options );
				break;
		}

		if ( ! empty($input['desc']) ) {
			printf( '<p class="description">%s</p>', $input['desc'] );
		}
	}
}
new Doc8_Settings();
