<?php
if ( ! defined( 'ABSPATH' ) ) {
	die( '' ); // Don't call directly
}
add_action( 'vc_build_admin_page', 'sl_testi_4_init' );
add_action( 'vc_after_init', 'sl_testi_4_shortcode' );

function sl_testi_4_init() {
	if (function_exists("vc_map")) {
		vc_map( array(
			'name'        => __( 'Testimonials. Layout#4', 'sell' ),
			'base'        => 'testimonial_query_4',
			'description' => __( 'Displays 1 testimonial per slide at carousel', 'sell' ),
			'category'    => __( 'SecretLab', 'sell' ),
			'icon'        => 'ss-vc-icon icon-arrows-h',
			'params'      => array(
				array(
					'type'			=> 'textfield',
					'heading'		=> __( 'ClassName', 'sell' ),
					'param_name'	=> 'extra_class',
					'description'	=> __( 'CSS class for carousel HTML container', 'sell' )
				),
				array(
					'type'			=> 'textfield',
					'heading'		=> __( 'Posts quantity ', 'sell' ),
					'param_name'	=> 'posts_per_page',
					'description'	=> __( 'how many posts to retrieve on page', 'sell' ),
					'value'         => 5
				),
				array(
					'type'			=> 'dropdown',
					'heading'		=> __( 'Enable\disable navigation', 'sell' ),
					'param_name'	=> 'enable_nav',
					'description'	=> __( 'set ability of navigation', 'sell' ),
					'value'         => array( 'disable' => 'false',
						'enable'  => 'true', ),
					'default'       => 'true'
				),
				array(
					'type'			=> 'dropdown',
					'heading'		=> __( 'Enable\disable autoplay', 'sell' ),
					'param_name'	=> 'autoplay',
					'description'	=> __( 'set autoplay on/off', 'sell' ),
					'value'         => array( 'disable' => 'false',
						'enable'  => 'true', ),
					'default'       => 'true'
				),
				array(
					'type'			=> 'textfield',
					'heading'		=> __( 'Duration between slides', 'sell' ),
					'param_name'	=> 'speed',
					'description'	=> __( 'time of pause between slides', 'sell' ),
					'value'         => 5000
				)

			)
		));

	}
}

function sl_testi_4_shortcode() {
 // admin area functions

}

function sl_testi_4_path( $atts, $content = null ) {
	// front-end functions
	extract( shortcode_atts( array(
		'uid'              => '',
		'posts_per_page'   => '',
		'extra_class'	   => null,
		'enable_nav'       => 'false',
		'autoplay'         => 'false',
		'speed'            => '5000'
	), $atts ) );

	global $secretlab;

	$slick_params = array( 'class'      => $extra_class ? $extra_class : 'testimonials_box_four',
		'enable_nav' => $enable_nav,
		'autoplay'   => $autoplay,
		'speed'      => $speed ? $speed : 5000,
		'dots'       => 'false',
		'arrows'     => 'true',
		'prevArrow'  => '<div class="slick-prev"></div>',
		'nextArrow'  => '<div class="slick-next"></div>',
		'sp_row'     => 1,
		'sp_show'    => 1,
		'sp_scroll'  => 1);
	$secretlab['slick'][] = $slick_params;

	$result = new WP_query('post_type=testimonial&posts_per_page='.$posts_per_page);
	$output = '';
	//Output posts
	if ( $result->posts ) :
		add_action('wp_footer', 'theguard_add_slick_carousel');
		// Main wrapper div

		$output .= '
				<div class="testimonialsarchive text-center ">
					<div class="layout4 carousel '.$slick_params['class'].'">';

		// Loop through posts
		foreach ( $result->posts as $post ) :
			// Post VARS
			$post_id          = $post->ID;
			if (function_exists("types_render_field")) {
				$client_name = types_render_field("name-of-client", array("post_id" => $post_id, "output" => "normal"));
				$client_post = types_render_field("post-of-client", array("post_id" => $post_id, "output" => "normal"));
				$client_photo = types_render_field("photo-of-client", array("post_id" => $post_id, "url" => "true"));
			} else {$client_name = $client_post = $client_photo = '';}
			// Testimonial post article start			

			// Open details div
			$output .= '<div class="item"><div class="testimonial">
								<div class="wrapper" data-anim="base wrapper">
                                    <a href="'.get_permalink($post_id).'"><img src="'.$client_photo.'" alt="'.$post->post_title.'"></a>
                                    <div class="circle" data-anim="base left"></div>
                                    <div class="circle" data-anim="base right"></div>
                                </div>
                                <strong>'.$client_name.'</strong>
                                <p class="postname">'.$client_post.'</p>
                                <div class="mention">'.$content = theguard_get_excerpt($post, null, 55).'</div>
							</div></div>';
			// End foreach loop
		endforeach;
		$output .= '</div>
					 </div>';
	endif; // End has posts check
	// Set things back to normal
	$result = null;
	wp_reset_postdata();

	// Return output
	return $output;

}

add_shortcode('testimonial_query_4', 'sl_testi_4_path');
