<?php
/**
 * Shortcode Title: Featured projects
 * Shortcode: featured_projects_2
 * Usage: [featured_projects_2]
 */
add_shortcode('featured_projects_2', 'ts_featured_projects_2_func');

function ts_featured_projects_2_func( $atts, $content = null ) {

	global $post;

    extract(shortcode_atts(array(
		'header' => '',
		'category' => '',
		'limit' => 10,
		),
	$atts));

	$html = '';
	$args = array(
		'numberposts'     => "",
		'posts_per_page'  => $limit,
		'meta_query' => array(array('key' => '_thumbnail_id')), //get posts with thumbnails only
		'offset'          => 0,
		'orderby'         => 'date',
		'order'           => 'DESC',
		'include'         => '',
		'exclude'         => '',
		'meta_key'        => '',
		'meta_value'      => '',
		'post_type'       => 'portfolio',
		'post_mime_type'  => '',
		'post_parent'     => '',
		'paged'				=> 1,
		'post_status'     => 'publish'
	);

	if (!empty($category)) {
		$args['tax_query'] = array(
			array(
				'taxonomy' => 'portfolio-categories',
				'field' => 'id',
				'terms' => explode(',',$category)
			),
		);
	}

	$the_query = new WP_Query( $args );

	if ( $the_query->have_posts() )
	{
		$list = '';
		while ( $the_query->have_posts() )
		{
			$the_query->the_post();
			if (has_post_thumbnail($post->ID))
			{
				$image = ts_get_resized_post_thumbnail($post->ID,'featured-projects-2',get_the_title());
				$image_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
				$image_src = '';
				if (isset($image_src_array[0])) {
					$image_src = $image_src_array[0];
				}
			}
			else
			{
				continue;
			}
			
			$terms = wp_get_post_terms( $post -> ID, 'portfolio-categories', $args );
			$term_slugs = array();
			$term_names = array();
			if (count($terms) > 0):
				foreach ($terms as $term):
					$term_slugs[] = $term -> slug;
					$term_names[] = $term -> name;
				endforeach;
			endif;
			
			$a1 = '<a draggable="false" href="'. get_permalink() .'" class="link">';
			$a2 = '</a>';

			$list .= '
				<article class="featured-project item-con-t1 '.implode(' ', $term_slugs).'">
				  <a rel="prettyPhoto[featured_projects_2]" draggable="false" href="'.$image_src.'" class="link">'.$image.'<div class="overlay"></div><div class="triangle"></div></a>
				  <div class="project-body">
					'.$a1.'
						<h2>'.ts_get_shortened_string_by_letters(get_the_title(),20,true).'</h2>
						<h3>'.implode(' ', $term_names).'</h3>
					'.$a2.'
					<span class="likes icon-heart" data-post-id="'.$post -> ID.'">'.ts_get_theme_likes($post -> ID).'</span>
				  </div>
				</article>';
		}
		if (!empty($list))
		{
			
			$terms = get_terms( 'portfolio-categories', array('orderby' => 'name') );
			
			$terms_html = '';
			foreach ($terms as $term) {
				$terms_html .= '<li><a href="#" data-filter=".'.$term -> slug.'">'.$term -> name.'</a></li>';
			}
			
			$html = '
				<div class="featured-projects-2-container gallery-container">
					<div class="fp-filter gallery-filters">
						<span>'.__('Sort Portfolio', 'circles').'</span>
						<ul>
							<li><a href="#" class="selected" data-filter="*">'.__('All', 'circles').'</a></li>
							'.$terms_html.'
						</ul>
					</div>
					<div class="clear"></div>
					<div class="featured-projects-2 gallery">
						'.$list.'
					</div>
				</div>
				<div class="clear"></div>';
		}
		// Restor original Query & Post Data
		wp_reset_query();
		wp_reset_postdata();
	}
	return $html;
}