<?php
if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
	/**
	 * Extended class to integrate testimonial slider with
	 * visual composer
	 */
    class WPBakeryShortCode_themesflat_teammember_slider extends WPBakeryShortCodesContainer {
    }
}

/**
 * Register filter for append custom class name
 * that generated from visual-composer
 */
add_filter( 'themesflat/shortcode/themesflat_teammember_class', 'themesflat_teammember_shortcodes_class', 10, 3 );
add_action( 'vc_before_init', 'themesflat_team_member_shortcode_params' );

add_filter( 'themesflat/shortcode/themesflat_teammember_atts', 'themesflat_teammember_shortcode_atts' );

function themesflat_teammember_shortcodes_class($atts) {
	if (function_exists('vc_shortcode_custom_css_class')) {
		$vcclass = vc_shortcode_custom_css_class( $atts['css'], ' ' );
	}
	$class[] = 'themesflat-team';
	$class[] =$vcclass;
	$class[] = $atts['style'];
	$class[] = $atts['class'];
	return $class;
}

function themesflat_teammember_shortcode_atts( $atts ) {
	$icons_params = themesflat_available_icons('icon');
	$atts = array_merge($atts,$icons_params);
	return $atts;
}

function themesflat_team_member_shortcode_params() {
	/**
	 * Map the testimonial slider shortcode
	 */
	vc_map( array(
		'name'                    => esc_html__( 'Themesflat: Team Member Slider', 'fo' ),
		'base'                    => 'themesflat_teammember_slider',
		'as_parent'               => array( 'only' => 'themesflat_member' ), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)
		'icon'        => themesflat_icon,
		'content_element'         => true,
		'show_settings_on_create' => false,
		'category'                => esc_html__( 'fo', 'fo' ),
		'params'                  => array(			
			array(
				'type' => 'textfield',
				'heading' => esc_html__( 'Margin', 'fo' ),
				'param_name' => 'margin',
				'value' => '30',
				'description' => esc_html__( 'Margin Item For Slide', 'fo' )
			),
			array(
				'type' => 'textfield',
				'heading' => esc_html__( 'Slides Per View', 'fo' ),
				'param_name' => 'slides_per_view',
				'value' => '1',
				'description' => esc_html__( 'The number of items you want to see on the screen.', 'fo' )
			),
			array(
				'type' => 'checkbox',
				'heading' => esc_html__( 'Autoplay', 'fo' ),
				'param_name' => 'autoplay',
				'value' => array( esc_html__( 'Yes, Please', 'fo' ) => 1 )
			),
			array(
				'type' => 'checkbox',
				'heading' => esc_html__( 'Show Pagination Control', 'fo' ),
				'param_name' => 'show_control',
				'value' => array( esc_html__( 'Yes, Please', 'fo' ) => 1 )
			),
			array(
				'type' => 'checkbox',
				'heading' => esc_html__( 'Show Prev/Next Buttons', 'fo' ),
				'param_name' => 'show_direction',
				'value' => array( esc_html__( 'Yes, Please', 'fo' ) => 1 )
			),			
			array(
				'type' => 'textfield',
				'heading' => esc_html__( 'Extra Class Name', 'fo' ),
				'param_name' => 'class',
				'description' => esc_html__( 'Your Custom Class', 'fo' )
			),

			array(
				'type' => 'css_editor',
				'param_name' => 'css',
				'group' => esc_html__( 'Design Options', 'fo' )
			)
		),
		'js_view' => 'VcColumnView'
	) );

	/**
	 * Map the member item
	 */
	$icons_params = themesflat_map_icons('icon','Icon for readmore');
	vc_map( array(
		'base'        => 'themesflat_member',
		'name'        => esc_html__( 'Themesflat: Team Member', 'fo' ),
		'icon'        => themesflat_icon,
		'category'    => esc_html__( 'fo', 'fo' ),
		'params'      => array_merge(
			array(
				array(
					'type'        => 'textfield',
					'heading'     => esc_html__( 'Name', 'fo' ),
					'param_name'  => 'name'
				),

				array(
					'type'       => 'attach_image',
					'heading'    => esc_html__( 'Image', 'fo' ),
					'param_name' => 'image'
				),

				array(
					'type'             => 'textfield',
					'heading'          => esc_html__( 'Subtitle', 'fo' ),
					'param_name'       => 'subtitle',
				),

				array(
					'type'       => 'textarea',
					'heading'    => esc_html__( 'Content', 'fo' ),
					'param_name' => 'content'
				),

				array(
					'type'       => 'dropdown',
					'heading'    => esc_html__( 'Style', 'fo' ),
					'param_name' => 'style',
					'value'		=> array(
							'Grid'	=> 'grid',
							'List'	=> 'list'
						),
					'std'	=> 'grid'
				),


				array(
					'type'       => 'textfield',
					'heading'    => esc_html__( 'Phone', 'fo' ),
					'param_name' => 'phone'
				),


				array(
					'type'       => 'textfield',
					'heading'    => esc_html__( 'Email', 'fo' ),
					'param_name' => 'email'
				),


				array(
					'type'       => 'textfield',
					'heading'    => esc_html__( 'Experience', 'fo' ),
					'param_name' => 'experience'
				),

				array(
					'type'       => 'textfield',
					'heading'    => esc_html__( 'Facebook URL', 'fo' ),
					'param_name' => 'facebook'
				),

				array(
					'type'       => 'textfield',
					'heading'    => esc_html__( 'Twitter URL', 'fo' ),
					'param_name' => 'twitter'
				),

				array(
					'type'       => 'textfield',
					'heading'    => esc_html__( 'Google URL', 'fo' ),
					'param_name' => 'google'
				),

				array(
					'type'       => 'textfield',
					'heading'    => esc_html__( 'LikeIn URL', 'fo' ),
					'param_name' => 'likedin'
				),

				array(
					'type'       => 'textfield',
					'heading'    => esc_html__( 'Pinterest URL', 'fo' ),
					'param_name' => 'pinterest'
				),

				array(
					'type'       => 'textfield',
					'heading'    => esc_html__( 'Instagram URL', 'fo' ),
					'param_name' => 'instagram'
				),

				array(
					'type' => 'textfield',
					'heading' => esc_html__( 'Read More Link', 'fo' ),
					'param_name' => 'link',
					'description' => esc_html__( 'Enter custom url for read more button', 'fo' )
				),

				array(
					'type' => 'textfield',
					'heading' => esc_html__( 'Read More Text', 'fo' ),
					'param_name' => 'text',
					'description' => esc_html__( 'Enter custom text for read more button', 'fo' ),
					'value' => esc_html__( 'View Profile', 'fo' )
				)),
			$icons_params,
			array(

				array(
					'type'       => 'textfield',
					'heading'    => esc_html__( 'Extra Class', 'fo' ),
					'param_name' => 'class'
				),

				array(
					'type' => 'css_editor',
					'param_name' => 'css',
					'group' => esc_html__( 'Design Options', 'fo' )
				)
			)
		)
	) );
}

add_shortcode( 'themesflat_member', 'themesflat_shortcode_member' );
add_shortcode( 'themesflat_teammember_slider', 'themesflat_shortcode_teammember_slider' );

/**
 * Testimonial shortcode handle
 * 
 * @param   string  $atts  Shortcode attributes
 * @return  void
 */
function themesflat_shortcode_member( $atts, $content = null ) {
	$atts = vc_map_get_attributes( 'themesflat_member', $atts );
	extract (apply_filters( 'themesflat/shortcode/themesflat_member_atts',$atts));
	$icon_name = themesflat_shortcode_icon_name('icon_',$icon_type);
	$icon_value = !empty($icon_name) ? $atts[$icon_name] : '';

	$member_image = '';
	$member_info ='';
	$class = apply_filters( 'themesflat/shortcode/themesflat_teammember_class',  $atts );
	
	if ( ! empty( $atts['subtitle'] ) )
		$member_info.= sprintf( '<div class="team-subtitle">%s</div>', wp_kses_post( $atts['subtitle'] ) );

	$member_info .= sprintf( '<h4 class="team-name">%s</h4>', wp_kses_post( $atts['name'] ) );
	$member_extra_info = '';
	$extra_info = array('phone' => esc_html__('Phone'),'email' => esc_html__('Email'),'experience' => esc_html__('Experience'));
	if ( !empty($phone) || !empty($email) || !empty($experience)) {
		$member_extra_info = '<ul class="themesflat_member_extra_info">';
		foreach ($extra_info as $key => $value) {
			if(!empty($atts[$key])) {
				$member_extra_info .= "<li><span>".esc_html($value).":</span>".esc_html__($atts[$key])."</li>";
			}
		}
		$member_extra_info .= '</ul>';
	}

	$social_links = '';

	if ( ! empty( $atts['facebook'] ) )
		$social_links.= sprintf( ' <a href="%s" data-title="Facebook" class="facebook"><i class="fa fa-facebook"></i></a>', esc_url( $atts['facebook'] ) );

	if ( ! empty( $atts['twitter'] ) )
		$social_links.= sprintf( ' <a href="%s" data-title="Twitter" class="twitter"><i class="fa fa-twitter"></i></a>', esc_url( $atts['twitter'] ) );
		if ( ! empty( $atts['google'] ) )
		$social_links.= sprintf( ' <a href="%s" data-title="google" class="google"><i class="fa fa-google-plus"></i></a>', esc_url( $atts['google'] ) );

	if ( ! empty( $atts['likedin'] ) )
		$social_links.= sprintf( ' <a href="%s" data-title="likedin" class="likedin"><i class="fa fa-linkedin"></i></a>', esc_url( $atts['likedin'] ) );

	if ( ! empty( $atts['pinterest'] ) )
		$social_links.= sprintf( ' <a href="%s" data-title="pinterest" class="pinterest"><i class="fa fa-pinterest-p"></i></a>', esc_url( $atts['pinterest'] ) );

	if ( ! empty( $atts['instagram'] ) )
		$social_links.= sprintf( ' <a href="%s" data-title="Instagram" class="instagram"><i class="fa fa-instagram"></i></a>', esc_url( $atts['instagram'] ) );
	$box_readmore = '';
	$button_class = ($atts['style'] == 'grid'? 'no-background' :'');

	if ( ! empty( $atts['link'] ) && ! empty( $atts['text'] ) ) {
		$box_readmore = sprintf( '
				<a class="themesflat-button %3$s" href="%1$s">%2$s<i class="readmore-icon %4$s"></i></a>
			', esc_url( $atts['link'] ), esc_html( $atts['text'] ),esc_attr($button_class),esc_attr($icon_value ));
	}
	$box_social_links = '';
	if ( ! empty( $social_links ) )
		$social_links = sprintf( '<div class="social-links"><span>%s</span>%s</div>',esc_html__('Social:'), $social_links);

		$box_social_links = sprintf( '<div class="box-social-links">%s %s</div>', $social_links,$box_readmore );

	$box_social_links_grid = ($style == 'grid' ? $box_social_links : '');
	$box_social_links_list = ($style == 'list' ? $box_social_links : '');

	if ( ! empty( $atts['image'] ) ) {
		if ( is_numeric( $atts['image'] ) && $images = wp_get_attachment_image_src( $atts['image'], 'full' ) )
			$atts['image'] = array_shift( $images );

		$class[] = 'has-image';
		$member_image = sprintf( '
			<div class="team-image item-hover">
				<img src="%s" alt="%s" />
				%s
			</div>
		', esc_attr( $atts['image'] ), esc_attr( $atts['name'] ),$box_social_links_grid );
	}

	return sprintf( '
		<div class="%s themesflat-hover">
			%s			
			<div class="team-info">	
				%s			
				<div class="team-desc">%s</div>
				%s
				%s
			</div>
		</div>
	', esc_attr( implode( ' ', $class ) ), wp_kses_post($member_image), wp_kses_post($member_info), wp_kses_post($content),wp_kses_post($member_extra_info) ,wp_kses_post($box_social_links_list)
	 );
}

/**
 * This function will be use to handle testimonial slider
 * shortcode
 * 
 * @param   string  $atts     Shortcode attributes
 * @param   string  $content  Shortcode content
 * @return  string
 */
function themesflat_shortcode_teammember_slider( $atts, $content = null ) {
	$atts = vc_map_get_attributes( 'themesflat_teammember_slider', $atts );
	extract (apply_filters( 'themesflat/shortcode/themesflat_teammember_slider_atts',$atts));
	$config = $atts;

	unset( $config['class'] );
	unset( $config['css'] );

	$class = apply_filters( 'themesflat/shortcode/themesflat_testimonial_slider_class', array( 'testimonial-slider','themesflat_enable_slider', $atts['class'] ), $atts );

	// Enqueue shortcode assets
	wp_enqueue_script( 'themesflat-carousel' );
	
	return sprintf( '
		<div class="%s" data-margin="%s" data-slides_per_view="%s" data-autoplay="%s" data-show_control="%s" data-show_direction="%s">			
			%s				
		</div>
	', implode( ' ', $class ), esc_attr( $margin ) , esc_attr( $slides_per_view ), esc_attr( $autoplay ), esc_attr( $show_control ), esc_attr( $show_direction ),  do_shortcode( $content ) );
}