<?php
/**
 * Testimonial Register
 *
 * @package All In One Custom Post
 * @since All In One Custom Post 1.0.0 
 */
if ( !defined( 'ABSPATH' ) ) exit;

// Register testimonial clients
function vr_register_post_testimonials() {

	register_post_type( 'vr_testimonials', 
	array( 
		'menu_icon' 	=> 'dashicons-format-quote',
		'supports' 		=> array( 'title', 'thumbnail', 'author' ),
		'public' 		=> true,
		'show_ui' 		=> true,
		'labels'        => array(
			'name'                	=> __( 'Testimonials', PLUGIN_NAME ),
			'singular_name'       	=> __( 'Testimonial', PLUGIN_NAME ),
			'add_new'             	=> __( 'Add New Testimonial', PLUGIN_NAME ),
			'add_new_item'        	=> __( 'Add New Testimonial', PLUGIN_NAME ),
			'menu_name'           	=> __( 'Testimonials', PLUGIN_NAME ),
			'parent_item_colon'   	=> __( 'Parent Testimonial', PLUGIN_NAME ),
			'all_items'           	=> __( 'All Testimonials', PLUGIN_NAME ),
			'view_item'           	=> __( 'View Testimonial', PLUGIN_NAME ),
			'edit_item'           	=> __( 'Edit Testimonial', PLUGIN_NAME ),
			'update_item'         	=> __( 'Update Testimonial', PLUGIN_NAME ),
			'search_items'        	=> __( 'Search Testimonial', PLUGIN_NAME ),
			'not_found' 			=> __(  'No Testimonial found', PLUGIN_NAME ),
			'not_found_in_trash'	=> __(  'No Testimonial found in Trash', PLUGIN_NAME ),
			'parent' 				=> __(  'Parent Testimonial', PLUGIN_NAME ),
		)
	));

	register_taxonomy( 
		'vr_testimonials_categories',
		'vr_testimonials', 
		array(
			'labels' => array( 
				'name' 				=> __( 'Testimonials Categories', PLUGIN_NAME ),
				'singular_name' 	=> __( 'Category', PLUGIN_NAME ),
				'search_items'  	=> __( 'Search Categories', PLUGIN_NAME ),
				'all_items' 		=> __( 'All Categories', PLUGIN_NAME ),
				'parent_item' 		=> __( 'Parent Category', PLUGIN_NAME ),
				'parent_item_colon' => __( 'Parent Category:', PLUGIN_NAME ),
				'edit_item' 		=> __( 'Edit Category', PLUGIN_NAME ),
				'update_item' 		=> __( 'Update Category', PLUGIN_NAME ),
				'add_new_item' 		=> __( 'Add New Category', PLUGIN_NAME ),
				'new_item_name'		=> __( 'New Category Name', PLUGIN_NAME ),
				'menu_name' 		=> __( 'Categories', PLUGIN_NAME ),
			 ),
			'hierarchical' => true, 
			'public' => true,
			'rewrite' => true
		)
	);

	register_taxonomy( 
		'vr_testimonials_tags',
		'vr_testimonials',
		array(  
			'labels' => array(
				'name' 				=> __( 'Testimonials Tags', PLUGIN_NAME ),
				'singular_name' 	=> __( 'Tag', PLUGIN_NAME ),
				'search_items'  	=> __( 'Search Tags', PLUGIN_NAME ),
				'all_items' 		=> __( 'All Tags', PLUGIN_NAME ),
				'parent_item' 		=> __( 'Parent Tag', PLUGIN_NAME ),
				'parent_item_colon'	=> __( 'Parent Tag:', PLUGIN_NAME ),
				'edit_item' 		=> __( 'Edit Tag', PLUGIN_NAME ),
				'update_item' 		=> __( 'Update Tag', PLUGIN_NAME ),
				'add_new_item' 		=> __( 'Add New Tag', PLUGIN_NAME ),
				'new_item_name' 	=> __( 'New Tag Name', PLUGIN_NAME ),
				'menu_name' 		=> __( 'Tags', PLUGIN_NAME ),
			 ),
			'hierarchical' => false, 
			'public' => true, 
			'rewrite' => true
		)
	);

}
add_action( 'init', 'vr_register_post_testimonials' );
$vr_testimonials_metabox = new VP_Metabox( plugin_dir_path( __FILE__ ) . 'testimonials_metabox.php' );

// Testimonials Shortcode
function vr_shortcode_testimonials( $atts ) {

	extract( shortcode_atts(array( 
		'id' 				=> '',
		'posts_per_page' 	=> -1,
		'category'			=> '',
		'module'			=> '',
		'column'			=> '',
		'ani'				=> '',
		'animation'			=> ''
	), $atts ));

	global $wp_query;
	$wp_query = new WP_Query(array( 
		'p'					            => esc_attr( $id ),
		'posts_per_page' 	            => esc_attr( $posts_per_page ),
		'vr_testimonials_categories'	=> esc_attr( $category ),
		'post_type' 		            => 'vr_testimonials',
		'post_status'		            => 'publish',
		'orderby'			            => 'date'
	));

	if ( $animation != '' ) { $animation = 'data-animation="' . $animation . '"'; $ani = 'fx-start'; }
	if ( $module == 'slider' ) { $column = 'col-md-12'; }

	ob_start(); ?>

	<?php if ( $module == 'slider' ) : ?>
			
		<div class="testimonial-slider clearfix <?php echo sanitize_html_class( $ani ); ?>" <?php echo wp_strip_all_tags( $animation ); ?>>
			<div class="slider-body">
				<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
					<div class="slider-item">
						
						<article class="testimonial-grid">
							<div class="testimonial-body">
								<blockquote><i class="fa fa-quote-left"></i><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.quote' ) ); ?><i class="fa fa-quote-right"></i></blockquote>
							</div>
							<div class="testimonial-foot clearfix">
								<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
									<div class="thumbnail">
										<img width="100" height="100" src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 100, 100, true, true, true ) ); ?>" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
									</div>
								<?php endif; ?>
								<cite>
									<?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.name' ) ); ?> / 
									<?php if ( vp_metabox( 'vr_testimonials.url' ) != '' ) : ?>
										<a href="<?php echo esc_url( vp_metabox( 'vr_testimonials.url' ) ); ?>"><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></a>
									<?php else : ?>
										<small><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></small>
									<?php endif; ?>
								</cite>
							</div>
						</article>

					</div>
				<?php endwhile; ?>
			</div>
		</div>
			
	<?php elseif ( $module == 'list' ) : ?>
			
		<div class="row row-equal fx">
		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
			
			<article class="testimonial-list column-equal clearfix <?php echo wp_strip_all_tags( $column ); ?> <?php echo sanitize_html_class( $ani ); ?>" <?php echo wp_strip_all_tags( $animation ); ?>>
				<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
					<div class="thumbnail">
						<img width="100" height="100" src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 100, 100, true, true, true ) ); ?>" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
					</div>
				<?php endif; ?>
				<div class="testimonial-body">
					<blockquote><i class="fa fa-quote-left"></i><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.quote' ) ); ?><i class="fa fa-quote-right"></i></blockquote>
					<cite>
						<?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.name' ) ); ?> 
						<?php if ( vp_metabox( 'vr_testimonials.url' ) != '' ) : ?>
							<a class="label-main" href="<?php echo esc_url( vp_metabox( 'vr_testimonials.url' ) ); ?>"><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></a>
						<?php else : ?>
							<small><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></small>
						<?php endif; ?>
					</cite>
				</div>
			</article>
		
		<?php endwhile; ?>
		</div>
			
	<?php elseif ( $module == 'widget' ) : ?>
		
		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
	
			<article class="testimonial-grid column-equal">
				<div class="testimonial-body">
					<blockquote><i class="fa fa-quote-left"></i><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.quote' ) ); ?><i class="fa fa-quote-right"></i></blockquote>
				</div>
				<div class="testimonial-foot clearfix">
					<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
						<div class="thumbnail">
							<img width="100" height="100" src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 100, 100, true, true, true ) ); ?>" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
						</div>
					<?php endif; ?>
					<cite>
						<?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.name' ) ); ?> / 
						<?php if ( vp_metabox( 'vr_testimonials.url' ) != '' ) : ?>
							<a class="label-main" href="<?php echo esc_url( vp_metabox( 'vr_testimonials.url' ) ); ?>"><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></a>
						<?php else : ?>
							<small><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></small>
						<?php endif; ?>
					</cite>
				</div>
			</article>
		
		<?php endwhile; ?>
			
	<?php else : ?>
		
		<div class="row row-equal fx">
		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
	
			<article class="testimonial-grid <?php echo wp_strip_all_tags( $column ); ?> <?php echo sanitize_html_class( $ani ); ?>" <?php echo wp_strip_all_tags( $animation ); ?>>
				<div class="testimonial-body">
					<blockquote><i class="fa fa-quote-left"></i><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.quote' ) ); ?><i class="fa fa-quote-right"></i></blockquote>
				</div>
				<div class="testimonial-foot clearfix">
					<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
						<div class="thumbnail">
							<img width="100" height="100" src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 100, 100, true, true, true ) ); ?>" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
						</div>
					<?php endif; ?>
					<cite>
						<?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.name' ) ); ?> / 
						<?php if ( vp_metabox( 'vr_testimonials.url' ) != '' ) : ?>
							<a class="label-main" href="<?php echo esc_url( vp_metabox( 'vr_testimonials.url' ) ); ?>"><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></a>
						<?php else : ?>
							<small><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></small>
						<?php endif; ?>
					</cite>
				</div>
			</article>
		
		<?php endwhile; ?>
		</div>

	<?php endif; ?>

	<?php
	$html = ob_get_clean();
	wp_reset_query();
	return $html;
}
add_shortcode( 'shortcode_testimonials', 'vr_shortcode_testimonials' );
