<?php 
/*-----------------------------------------------------------------------------------*/
/* ADDS CUSTOM PERMALINK STRUCTURE FOR PORTFOLIO AND GALLERY
/*-----------------------------------------------------------------------------------*/

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'KKTFWP_Portfolio_Gallery_Permalink_Settings', false ) ) {
	
	class KKTFWP_Portfolio_Gallery_Permalink_Settings {
		
		public function __construct() {
			
			add_action( 'admin_init', array( $this , 'settings_init' ) );
			add_action( 'admin_init', array( $this , 'settings_save' ) );
			
			add_filter( 'kktfwp_portfolio_slug', array ( $this, 'run_filter' ), 10 );
			add_filter( 'kktfwp_portfolio_cat_slug', array ( $this, 'run_filter' ), 15 );
			add_filter( 'kktfwp_gallery_slug', array ( $this, 'run_filter'), 20 );
			add_filter( 'kktfwp_gallery_cat_slug', array ( $this, 'run_filter'), 25 );
			
		}
		 
		/* Settings Init */
		public function settings_init() {
					 
			/* Create settings section */
			add_settings_section(
				'kktfwp_custom_slugs',                   // Section ID
				esc_html__( 'Portfolio and Gallery URLs structure' , 'toppic' ),  // Section title
				array( $this, 'custom_slugs_description' ), // Section callback function
				'permalink'                          // Settings page slug
			);
		 
			add_settings_field(
				'kktfwp_portfolio-slug',       // Field ID
				esc_html__( 'Portfolio base' , 'toppic' ),       // Field title 
				array( $this, 'slug_callback' ), // Field callback function
				'permalink',                    // Settings page slug
				'kktfwp_custom_slugs',              // Section ID
				array( 
					'id' => 'kktfwp_portfolio-slug', 
					'option_name' => 'kktfwp_portfolio_slug' 
				)
			); 			
						
			add_settings_field(
				'kktfwp_gallery-slug',       // Field ID
				esc_html__( 'Gallery base' , 'toppic' ),       // Field title 
				array( $this, 'slug_callback' ), // Field callback function
				'permalink',                    // Settings page slug
				'kktfwp_custom_slugs',               // Section ID
				array( 
					'id' => 'kktfwp_gallery-slug', 
					'option_name' => 'kktfwp_gallery_slug' 
				)
			);
			
			add_settings_field(
				'kktfwp_portfolio-cat-slug',       // Field ID
				esc_html__( 'Portfolio category base' , 'toppic' ),       // Field title 
				array( $this, 'slug_callback' ), // Field callback function
				'permalink',                    // Settings page slug
				'kktfwp_custom_slugs',              // Section ID
				array( 
					'id' => 'kktfwp_portfolio-cat-slug', 
					'option_name' => 'kktfwp_portfolio_cat_slug' 
				)
			); 
			
			add_settings_field(
				'kktfwp_gallery-cat-slug',       // Field ID
				esc_html__( 'Gallery category base' , 'toppic' ),       // Field title 
				array( $this, 'slug_callback' ), // Field callback function
				'permalink',                    // Settings page slug
				'kktfwp_custom_slugs',               // Section ID
				array( 
					'id' => 'kktfwp_gallery-cat-slug', 
					'option_name' => 'kktfwp_gallery_cat_slug' 
				)
			);		
		}
		
		public function settings_save() {
			if ( ! is_admin() && ! current_user_can( 'manage_options' ) ) {
				return;
			}

			if ( isset( $_POST['permalink_structure'] ) ) {
				
				$permalinks = array();

				$permalinks['kktfwp_portfolio_slug']    	= $this -> sanitize_permalink( $_POST['kktfwp_portfolio_slug'] );
				$permalinks['kktfwp_portfolio_cat_slug']    = $this -> sanitize_permalink( $_POST['kktfwp_portfolio_cat_slug'] );
				$permalinks['kktfwp_gallery_cat_slug']  	= $this -> sanitize_permalink( $_POST['kktfwp_gallery_cat_slug'] );			
				$permalinks['kktfwp_gallery_slug']      	= $this -> sanitize_permalink( $_POST['kktfwp_gallery_slug'] );

				foreach ( $permalinks as $key=>$val ) {
					update_option( $key, $val );
				}
			}	
		}	

		public function run_filter( $permalink ) {
			
			switch ( $permalink ) {
				case 'portfolio':
					$portfolio = get_option( 'kktfwp_portfolio_slug' );	
					break;
				case 'gallery':
					$portfolio = get_option( 'kktfwp_gallery_slug' );	
					break;
				case 'portfolio-type':
					$portfolio = get_option( 'kktfwp_portfolio_cat_slug' );	
					break;
				case 'gallery-type':
					$portfolio = get_option( 'kktfwp_gallery_cat_slug' );	
					break;
			}
			
			if ( !empty( $portfolio ) ) {
				$permalink = $portfolio;
			}			
			
			return $permalink;			
		}

		/* Sanitize permalink values before insertion into DB. */	
		public function sanitize_permalink( $string ) {			
			$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
			return preg_replace('/[^A-Za-z0-9\-_]/', '', $string); // Removes special chars.			
		}
		 
		/* Setting Section Description */
		public function custom_slugs_description(){
			echo wpautop( sprintf( __( 'If you like, you may enter custom structures for your portfolio and gallery URLs here. For example, using <code>work</code> as your portfolio base or <code>album</code> as your gallery base would make your links like <code>%1s/work/project-name/</code> or <code>%1s/album/gallery-name/</code>. If you leave these blank the defaults will be used.' ), get_option( 'home' ), get_option( 'home' ) ) ); 
		}
		 
		/* Settings Field Callback */
		public function slug_callback( $args ){
			
			$id = $args['id'];
			$option_name = $args['option_name'];
			?>        
				<input name="<?php echo $option_name ?>" id="<?php echo esc_attr( $id ) ?>" value="<?php echo esc_attr( get_option( $option_name ) ) ?>" class="regular-text code" type="text" /> 
			<?php
		}
	}
}

return new KKTFWP_Portfolio_Gallery_Permalink_Settings();