<?php

/**
 * Register size guide post type
 */
class prkSizeGuideCPT {

	public function __construct() {
		add_action( 'init', array( $this, 'registerTypes' ) );
		add_filter( 'manage_prk_size_guide_posts_columns', array($this, 'prk_size_guide_set_column') );
		add_action( 'manage_prk_size_guide_posts_custom_column' , array($this,'prk_size_guide_display_column'), 10, 2 );
		register_activation_hook( dirname(__FILE__) . '/prkSizeGuidePlugin.php', array( $this, 'examplePosts' ) );

	}

	public function registerTypes() {

		if ( ! prkSizeGuidePlugin::hasWooCommerce() ) {
			return;
		}

		$labels = array(
			'name'               => __( 'راهنمای سایز بندی', 'post type general name', 'parskala' ),
			'singular_name'      => __( 'سایز بندی', 'post type singular name', 'parskala' ),
			'menu_name'          => __( 'راهنمای سایز بندی', 'admin menu', 'parskala' ),
			'name_admin_bar'     => __( 'Size guide', 'add new on admin bar', 'parskala' ),
			'add_new'            => __( 'افزودن جدید', 'size guide', 'parskala' ),
			'add_new_item'       => __( 'Add new size guide', 'parskala' ),
			'new_item'           => __( 'New size guide', 'parskala' ),
			'edit_item'          => __( 'Edit size guide', 'parskala' ),
			'view_item'          => __( 'View size guide', 'parskala' ),
			'all_items'          => __( 'All size guides', 'parskala' ),
			'search_items'       => __( 'Search size guides', 'parskala' ),
			'not_found'          => __( 'No size guides found.', 'parskala' ),
			'not_found_in_trash' => __( 'No size guides found in trash.', 'parskala' )
		);

		$args = array(
			'hierarchical'       => false,
			'labels'             => $labels,
			'public'             => true,
			'publicly_queryable' => false,
			'show_ui'            => true,
            'show_in_nav_menus'  => false,
			'show_in_menu'       => true,
			'query_var'          => true,
			'rewrite'            => array( 'slug' => 'size-guide' ),
			'capability_type'    => 'page',
			'has_archive'        => false,
			'supports'           => array( 'title', 'editor' )
		);

		register_post_type( 'prk_size_guide', $args );

	}

	/*
	 * Sort Column
	 *
	 */

	public function prk_size_guide_set_column( $columns ){
		unset($columns['date']);
		$columns['prk_shortcode'] = __( 'Shortcode (Pages only) ', 'parskala' );
		$columns['date'] = __( 'Date', '' );

		return $columns;
	}

	/*
	 * Set Column Shortcode
	 * Default Shortcode value
	 *
	 */

	public function prk_size_guide_display_column( $columns, $post_id ){
	    $btn_val = '""';
	    $btn = "true";
	    $id = $post_id;
		switch( $columns ){
			case 'prk_shortcode' :
				echo "<input type = 'text' value = '[prk_size_guide postid = ".$id." button=".$btn." button_value=".$btn_val."]' style='width:100%; max-width:350px;'>";
				break;
		}
	}

	/**
	 * Add custom post types
	 */

	public function examplePosts() {

		$this->registerTypes();

		if ( count( get_posts( array( 'post_type' => 'prk_size_guide' ) ) ) == 0 ) {

			//sample women
			$women = array(
				'post_content' => __('<h3>Measuring Tips to Assure The Best Fit</h3>
            <img class="alignright size-full wp-image-2101" style="float:right" src="' . PRK_SIZEGUIDE_ASSETS . 'img/size-guide-woman.jpg" alt="silhouette-woman"/><h4>Bust</h4>
            With arms at sides, place tape measure under your arms and run it around the fullest part of the bustline and across the shoulder blades.
            <br><h4>Waist</h4>
            Find the natural crease of your waist by bending to one side. Run tape measure around your natural waistline, keeping one finger between the tape and your body for a comfortable fit.
            <br><h4>Hips</h4>
            With feet together, run tape measure around the fullest part of your hips/seat, about 7 to 8 inches below your waistline.
            <br><h4>Inseam</h4>
            For full-length pants, run tape measure along the inside of your leg, from just below the crotch to about 1 inch below the ankle.', 'parskala'),
				'post_name'    => 'sample-women-size-guide',
				'post_title'   => __('Sample women size guide', 'parskala'),
				'post_status'  => 'publish',
				'post_type'    => 'prk_size_guide',
			);

			$womenID = wp_insert_post( $women );

			$womenTitle   = __('Premier Designer And Contemporary Apparel Sizing', 'parskala');
			$womenTable   = array(
				array( 'سایز', 'نیم‌تنه', 'دور کمر', 'باسن' ),
				array( '4', '30', '22.75', '32.75' ),
				array( '6', '31', '23.75', '33.75' ),
				array( '8', '32', '24.75', '34.75' ),
				array( '10', '34', '26.75', '36.75' ),
				array( '12', '36', '28.75', '38.75' ),
				array( '14', '38', '30.75', '40.75' ),
				array( '16', '40', '32.75', '42.75' ),
				array( '18', '43', '35.75', '45.75' )
			);
			$womenCaption = __('All dimensions are given in inches', 'parskala');

			$meta_table    = array();
			$meta_table[0] = array(
				'title'   => $womenTitle,
				'table'   => $womenTable,
				'caption' => $womenCaption
			);

			update_post_meta( $womenID, '_prk_sizeguide', $meta_table );

			//sample men
			$men = array(
				'post_content' => __('<h3>Measuring Tips to Assure The Best Fit</h3>
            <img class="alignright size-full wp-image-2101" style="float:right;" src="' . PRK_SIZEGUIDE_ASSETS . 'img/size-guide-man.jpg" alt="silhouette-woman"/><h4>Bust</h4>
            With arms at sides, place tape measure under your arms and run it around the fullest part of the bustline and across the shoulder blades.
            <br><h4>Waist</h4>
            Find the natural crease of your waist by bending to one side. Run tape measure around your natural waistline, keeping one finger between the tape and your body for a comfortable fit.
            <br><h4>Hips</h4>
            With feet together, run tape measure around the fullest part of your hips/seat, about 7 to 8 inches below your waistline.
            <br><h4>Inseam</h4>
            For full-length pants, run tape measure along the inside of your leg, from just below the crotch to about 1 inch below the ankle.', 'parskala'),
				'post_name'    => 'sample-men-size-guide',
				'post_title'   => __('Sample men size guide', 'parskala'),
				'post_status'  => 'publish',
				'post_type'    => 'prk_size_guide',
			);

			$menID = wp_insert_post( $men );

			$menTitle   = __('Premier Designer And Contemporary Apparel Sizing', 'parskala');
			$menTable   = array(
				array( 'سایز', 'نیم‌تنه', 'دور کمر', 'باسن' ),
				array( '4', '30', '22.75', '32.75' ),
				array( '6', '31', '23.75', '33.75' ),
				array( '8', '32', '24.75', '34.75' ),
				array( '10', '34', '26.75', '36.75' ),
				array( '12', '36', '28.75', '38.75' ),
				array( '14', '38', '30.75', '40.75' ),
				array( '16', '40', '32.75', '42.75' ),
				array( '18', '43', '35.75', '45.75' )
			);
			$menCaption = __('All dimensions are given in inches', 'parskala');

			$meta_table    = array();
			$meta_table[0] = array(
				'title'   => $menTitle,
				'table'   => $menTable,
				'caption' => $menCaption
			);

			update_post_meta( $menID, '_prk_sizeguide', $meta_table );

		}

	}

}

new prkSizeGuideCPT();
