<?php
/**
 * Created by PhpStorm.
 * User: igor
 * Date: 08/08/2019
 * Time: 01:04
 */

class SG_Permalink_Settings {
	/**
	 * Hook in tabs.
	 */
	public function __construct() {
		add_action( 'admin_init', array( $this, 'settings_init' ) );
		add_action( 'admin_init', array( $this, 'settings_save' ) );
	}

	/**
	 * Init our settings
	 */
	public function settings_init() {
		add_settings_field(
			'sg_giveaway_slug',
			__( 'Giveaways Slug', 'giveasap' ),
			array( $this, 'slug_input' ),
			'permalink',
			'optional'
		);
	}

	/**
	 * Show a slug input box.
	 */
	public function slug_input() {
		$text = get_option( 'sg_giveaway_slug', null )
		?><fieldset><input id="sg_giveaway_slug" name="sg_giveaway_slug" type="text" class="regular-text code" value="<?php echo $text; ?>" placeholder="giveaway"></fieldset><?php
	}

	/**
	 * Save the settings
	 */
	public function settings_save() {
		if ( ! is_admin() )
			return;

		if ( isset( $_POST['permalink_structure'] ) || isset( $_POST['sg_giveaway_slug'] ) ):
				$value = null;
				if ( isset( $_POST[ 'sg_giveaway_slug' ] ) )
					$value = sanitize_text_field( $_POST[ 'sg_giveaway_slug' ] );
				if ( empty( $value ) )
					delete_option( 'sg_giveaway_slug' );
				else
					update_option( 'sg_giveaway_slug', $value );
			$this->flush_rules();
		endif;
	}

	/**
	 * Flush Rules
	 */
	public function flush_rules() {
		giveasap_cpt();
		flush_rewrite_rules();
	}
}

new SG_Permalink_Settings();