<?php
add_action( 'vc_before_init', 'rockon_gallery_club_WITHVS' );
function rockon_gallery_club_WITHVS() {
	$taxonomy = 'portfolio_categories';
	$categories = get_terms( $taxonomy, array( 'parent' => 0, ) );
	$catasclass = array();
	$catasclass['select'] = 'null';
	foreach($categories as $category)
	{
		$catasclass[$category->name] = $category->slug;
	}
   vc_map( array(
      "name" => __("Rockon Show Post As Gallery",'rockon'),
      "base" => "rockon_gallery_shortchode",
      "class" => "",
      "category" => __('Content','rockon'),
      'admin_enqueue_js' => array(get_template_directory_uri().'/vc_extend/bartag.js'),
      'admin_enqueue_css' => array(get_template_directory_uri().'/vc_extend/bartag.css'),
      "params" => array(
         array(
            "type" => "textfield",
            "holder" => "div",
            "class" => "",
            "heading" => __("Title",'rockon'),
            "param_name" => "title",
            "value" => __("Gallery 2 Column",'rockon'),
            "description" => __("Title text Here.",'rockon')
         )/*,
		 array(
            "type" => "textfield",
            "holder" => "div",
            "class" => "",
            "heading" => __("Indicate For Sharing",'rockon'),
            "param_name" => "share",
            "value" => __("SHARE THIS IMAGE",'rockon'),
            "description" => __("change text for showing sharing icon.",'rockon')
         )*/,
         array(
            "type" => "dropdown",
            "holder" => "div",
            "class" => "",
            "heading" => __("Number Of Column",'rockon'),
            "param_name" => "no_of_column",
            "value" => array('2','3','4'),
            "description" => __("Choose no of column.",'rockon')
         ),
		  array(
            "type" => "dropdown",
            "holder" => "div",
            "class" => "",
            "heading" => __("Number Of Post",'rockon'),
            "param_name" => "no_of_post",
            "value" => array('6','12','18','24','30','36','42','48'),
            "description" => __("Choose no of post.",'rockon')
         ),
		 array(
            "type" => "dropdown",
            "holder" => "div",
            "class" => "",
            "heading" => __("Post Type (Either select post type or the post category, one at a time)",'rockon'),
            "param_name" => "post_type",
            "value" => array('null'=>'Select','services'=>'Services','portfolio'=>'Portfolio','disc_jockey_team'=>'Disc Jockey Team','post'=>'Post'),
            "description" => ''
         ),
		 array(
            "type" => "dropdown",
            "holder" => "div",
            "class" => "",
            "heading" => __("Portfolio Category (Either select post type or the portfolio category, one at a time)",'rockon'),
            "param_name" => "category",
            "value" => $catasclass,
            "description" => ''
         )
      )
   ) );
}
add_shortcode( 'rockon_gallery_shortchode', 'rockon_gallery_club_post_func' );
function rockon_gallery_club_post_func( $atts ) { // New function parameter $content is added!
   extract( shortcode_atts( array(
      'title' => 'something',
	  'no_of_column' => '2',
	  'no_of_post' => '8',
	  'post_type' => 'portfolio',
	  'category' => 'null'
   ), $atts ) );
   global $post;
   $result = '';
   $src = $openpostdesc = $a = $fancyaudio = $image1 = $image2 = '';
   $i = $j = 0;
   $result .= '<div class="clearfix"></div>
	<div class="rock_main_gallery">
		<div class="main_gallery">
			<div id="photo_tab" class="main_gallery_tab_content animated fadeInDown">';
	if($category != 'null'){
		$args = array(
		  'post_type' => $post_type,
		  'tax_query' => array(
				array(
					'taxonomy' => 'portfolio_categories',
					'field' => 'slug',
					'terms' => $category,
				)
			),
		  'posts_per_page' => $no_of_post
		);	
	}else{
		$args = array( 'post_type' => $post_type, 'posts_per_page' => $no_of_post); 
	}
	$query = new WP_Query( $args );
	if($query->have_posts()):	
		while($query->have_posts()) : $query->the_post();
			if($i == $j){
				$result .= '<div class="row">';
				if($no_of_column == '2'){
					$j = $j + 2;
				}elseif($no_of_column == '3'){
					$j = $j + 3;
				}elseif($no_of_column == '4'){
					$j = $j + 4;
				}
			}
			if (has_post_thumbnail($post->ID)){	
				$image = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) , 'full' );
				if($no_of_column == '2'){
					$thumb_w = '560';
					$thumb_h = '420';
					$image1 = aq_resize($image, $thumb_w, $thumb_h, true);
					$thumb_w = '630';
					$thumb_h = '470';
					$image2 = aq_resize($image, $thumb_w, $thumb_h, true);
				}elseif($no_of_column == '3'){
					$thumb_w = '371';
					$thumb_h = '280';	
					$image1 = aq_resize($image, $thumb_w, $thumb_h, true);
					$thumb_w = '630';
					$thumb_h = '470';
					$image2 = aq_resize($image, $thumb_w, $thumb_h, true);
				}elseif($no_of_column == '4'){
					$thumb_w = '277';
					$thumb_h = '210';
					$image1 = aq_resize($image, $thumb_w, $thumb_h, true);
					$thumb_w = '630';
					$thumb_h = '470';
					$image2 = aq_resize($image, $thumb_w, $thumb_h, true);
				}
			}
			if($no_of_column == '2'){
				$result .='<div class="col-lg-6 col-md-6 col-sm-6 main_gallery_item">';
			}elseif($no_of_column == '3'){
				$result .='<div class="col-lg-4 col-md-4 col-sm-4 main_gallery_item">';
			}elseif($no_of_column == '4'){
				$result .='<div class="col-lg-3 col-md-3 col-sm-3 main_gallery_item">';
			}
				$result .= '<div class="rock_club_photo_slider_item">
					  <div class="rock_club_photo_item">';
			//$image = get_the_post_thumbnail( $post->ID, 'rockon-common-size' );
			$audiosrc = get_post_meta( $post->ID, 'rockon_audio_src', true );
			if (has_post_thumbnail($post->ID)){	
				$result .= '<img src="'.esc_url($image1).'" alt="">';
				$src = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) , 'full' );
			}else{
				$result .= '<img src="'.ROCKON_PATH.'/images/no_image.jpg" alt="">';
				$src = ROCKON_PATH.'/images/no_image.jpg"';
			}
			if(!empty($audiosrc)){
				$filetype = wp_check_filetype($audiosrc);
				$type = $filetype['type'];
				if($type == 'audio/mpeg'){
					$thumb_w = '525';
					$thumb_h = '280';
					$src = aq_resize($src, $thumb_w, $thumb_h, true);
					$a = '<a class="fancybox" href="#rockongallery_audio'.$i.'">';
					$fancyaudio = '<div class="rock_audio_player" style="display:none;" id="rockongallery_audio'.$i.'">
<div class="rock_audio_player_title"><span class="track_artist">'.__(get_the_title($post->ID),'rockon').'</span></div>
<div class="rock_audio_player_track_image"> <img src="'.esc_url($src).'" alt=""/>
  <div class="rock_audio_player_track_image_overlay">'.__(get_the_title($post->ID),'rockon').'</div>
</div>
<div class="audio-player">
  <audio class="rock_player" controls><source src="'.esc_url($audiosrc).'" type="audio/mpeg"></audio>
</div>
</div>';
				}else{
					$a = '<a class="fancybox-video iframe" href="https:'.$audiosrc.'">';
				}
			}else{
				$a = '<a class="fancybox" data-fancybox-group="group1" href="'.esc_url($src).'">';	
			}		
				$result .= '<div class="rock_club_photo_overlay">
						  <div class="photo_link animated fadeInDown">'.$a.' <i class="fa fa-search-plus"></i></a> <a class="main_gallery_item_link" id="rockon_sha_photo'.$i.'"><i class="fa fa-link"></i></a> </div>
						  <a class="rock_club_photo_detail animated fadeInUp" href="'.esc_url(get_the_permalink($post->ID)).'">'.__(get_the_title($post->ID),'rockon').'</a> </div>
					  </div>
					</div>';
					
			$result .= '</div>';
			$result .= $fancyaudio;
			$fancyaudio = '';
			if(!empty($audiosrc)){
					$filetype = wp_check_filetype($audiosrc);
					$type = $filetype['type'];
				if($type == 'audio/mpeg'){
						$openpostdesc .= '<div class="main_gallery_item_popup rockon_sha_photo'.$i.'">
					<div class="row">
					  <div class="col-lg-12 col-md-12 col-sm-12">
							<div class="col-lg-7 col-md-7 col-sm-10 col-lg-offset-0 col-md-offset-0 col-sm-offset-1">';
						if (has_post_thumbnail($post->ID))
						{	
							$openpostdesc .= '<img src="'.esc_url($image2).'" alt="">';
							$src = wp_get_attachment_image( $post->ID, 'full' );
						}else{
							$openpostdesc .= '<img src="'.ROCKON_PATH.'/images/no_image.jpg" alt="">';
							$src = ROCKON_PATH.'/images/no_image.jpg"';
						}	
						 $openpostdesc .= '<div class="audio-player">
                      <audio class="rock_player" controls><source src="'.esc_url($audiosrc).'" type="audio/mpeg"></audio>
                    </div>';	
				   $openpostdesc .=  '</div><div class="col-lg-5 col-md-5 col-sm-10 col-lg-offset-0 col-md-offset-0 col-sm-offset-1"> <a href="">
						  <h1>'.__(get_the_title($post->ID),'rockon').'</h1>
						  </a>
						  <p>'.__(get_excerpt(300),'rockon').'</p>
						  <a href="'.esc_url(get_the_permalink($post->ID)).'" class="btn btn-arrow btn-4a icon-arrow-right">Read More</a>
						</div>
					  </div>
					</div>
					<a class="main_gallery_item_popup_close">X</a> </div>';
				}else{
				$openpostdesc .= '<div class="main_gallery_item_popup rockon_sha_photo'.$i.'">
				<div class="row">
				  <div class="col-lg-12 col-md-12 col-sm-12">
                  <div class="gallery_video">
                    <video width="640" height="400" style="width: 100%; height: 100%;">
                      <source type="video/youtube" src="'.esc_url( 'https:'.$audiosrc).'" />
                    </video>
                  </div>
				  </div>
				</div>
				<a class="main_gallery_item_popup_close">X</a> </div>';
				}
			}else{
				$openpostdesc .= '<div class="main_gallery_item_popup rockon_sha_photo'.$i.'">
				<div class="row">
				  <div class="col-lg-12 col-md-12 col-sm-12">
					<div class="col-lg-7 col-md-7 col-sm-10 col-lg-offset-0 col-md-offset-0 col-sm-offset-1">';
					if (has_post_thumbnail($post->ID))
					{	
						$openpostdesc .= '<img src="'.esc_url($image2).'" alt="">';
						$src = wp_get_attachment_image( $post->ID, 'full' );
					}else{
						$openpostdesc .= '<img src="'.ROCKON_PATH.'/images/no_image.jpg" alt="">';
						$src = ROCKON_PATH.'/images/no_image.jpg"';
					}		 
			   $openpostdesc .=  '</div><div class="col-lg-5 col-md-5 col-sm-10 col-lg-offset-0 col-md-offset-0 col-sm-offset-1"> <a href="">
					  <h1>'.__(get_the_title($post->ID),'rockon').'</h1>
					  </a>
					  <p>'.__(get_excerpt(300),'rockon').'</p>
					<a href="'.esc_url(get_the_permalink($post->ID)).'" class="btn btn-arrow btn-4a icon-arrow-right">Read More</a>
					</div>
				  </div>
				</div>
				<a class="main_gallery_item_popup_close">X</a> </div>';
			}
		if(($i+1) == $j){
			$result .= $openpostdesc;	
			$openpostdesc = '';
			$result .= '</div>';
		}
			$i++;
		endwhile; 
	endif;
	if(($i+1) <= $j){
		$result .= $openpostdesc;	
		$openpostdesc = '';
		$result .= '</div>';
	}
	$result .= '
			</div>
		</div>
	</div>';
	
	return $result;
   
}
?>