<?php
class WPCTP_Testimonials_Post_Type {
	function __construct() {
		add_action( 'init', array( $this, 'wpctp_testimonial_post_type' ) );
		add_action( 'add_meta_boxes', array( $this, 'wpctp_add_meta_box' ) );
		add_action( 'save_post', array( $this, 'wpctp_save_meta_box' ) ); 
		add_action( 'template_redirect', array( $this, 'wpctp_template_redirect' ) );
	}
	function wpctp_testimonial_post_type() {
		register_post_type(
			'wpcargo_testimonial',
			array(
				'labels' => array(
					'name' 				=> __( 'WPCargo Testimonials', 'wpctp' ),
					'singular_name' 	=> __( 'WPCargo Testimonial', 'wpctp' ),
					'add_new' 			=> __( 'Add Testimonial', 'wpctp' ),
					'add_new_item' 		=> __( 'Add New Testimonial', 'wpctp' ),
					'edit_item' 		=> __( 'Edit Testimonial', 'wpctp' ),
					'all_items'			=> __( 'Testimonials', 'wpctp' ),
					'menu_name' 		=> __( 'WPCargo Testimonials', 'wpctp' ),
					'view_item' 		=> false,
					'featured_image'	=> __( 'Testimonial Author Image', 'wpctp' ),
					'set_featured_image'	=> __( 'Set Author Image', 'wpctp' ),
					'remove_featured_image'	=> __( 'Remove Author Image', 'wpctp' ),
					'use_featured_image'	=> __( 'Use as Author Image', 'wpctp' ),
				),
				'public'		 		=> true,
				'hierarchical' 			=> false,
				'rewrite' 				=> false,
				'query_var' 			=> false,
				'show_in_nav_menus' 	=> false,
				'exclude_from_search' 	=> true,
				'supports' 				=> array( 'title', 'editor', 'revisions' ),
			)
		);
	}
	function wpctp_template_redirect(){
		if( get_post_type() == 'wpcargo_testimonial' ){
			wp_redirect( get_bloginfo( 'url' ) );
			exit();
		}
	}
	function wpctp_add_meta_box(){
		add_meta_box(
			'wpctp_meta_box',
			__( 'WPCargo Extra Fields', 'wpctp' ),
			array( $this, 'wpctp_meta_box_callback' ),
			'wpcargo_testimonial',
			'normal'
		);
	}
	function wpctp_meta_box_callback( $post ) {
		wp_nonce_field( 'wpctp_testimonial_meta_box', 'wpctp_testimonial_meta_box_nonce' );
		$img_url = WPCTP_IMAGES_URL . '/no-image.png';
		if( get_post_meta( $post->ID, 'cover_image', true ) ){
			$img_url = wp_get_attachment_image_url( get_post_meta( $post->ID, 'cover_image', true ), 'full' );
		}
		?>
        <table>
        	<tbody>
            	<tr>
                	<td><h3><?php _e( 'Position', 'wpctp' ); ?></h3></td>
                	<td><input type="text" id="wpcargo_testimonial_position" name="wpcargo_testimonial_position" autocomplete="off" value="<?php echo get_post_meta( $post->ID, 'position', true ); ?>" /></td>
                </tr>
            	<tr>
                	<td><h3><?php _e( 'Country', 'wpctp' ); ?></h3></td>
                	<td><input type="text" id="wpcargo_testimonial_country" name="wpcargo_testimonial_country" autocomplete="off" value="<?php echo get_post_meta( $post->ID, 'country', true ); ?>" /></td>
                </tr>
            </tbody>
        </table>
        <?php
    }
	function wpctp_save_meta_box( $post_id ) {
		if ( ! isset( $_POST['wpctp_testimonial_meta_box_nonce'] ) ) {
			return;
		}
		if ( ! wp_verify_nonce( $_POST['wpctp_testimonial_meta_box_nonce'], 'wpctp_testimonial_meta_box' ) ) {
			return;
		}
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}
		if ( isset( $_POST['post_type'] ) && 'wpctp_testimonial' == $_POST['post_type'] ) {
			if ( ! current_user_can( 'edit_page', $post_id ) ) {
				return;
			}
		} else {
			if ( ! current_user_can( 'edit_post', $post_id ) ) {
				return;
			}
		}
		$position = sanitize_text_field( $_POST['wpcargo_testimonial_position'] );
		$country = sanitize_text_field( $_POST['wpcargo_testimonial_country'] );	
		update_post_meta( $post_id, 'position', $position );
		update_post_meta( $post_id, 'country', $country );
	}
}
new WPCTP_Testimonials_Post_Type;