<?php

/**
 * Portfolio Shortcode
 */


// [progression_portfolio heading_pro="" menu_description_pro="" etc...]
add_shortcode( 'progression_portfolio', 'progression_portfolio_func' );
function progression_portfolio_func( $atts, $content = null ) { // New function parameter $content is added!
   extract( shortcode_atts( array(
		
	  'progression_studios_portfolio_layout' => 'progression-default-layout',
	  'progression_studios_portfolio_columns' => '3',
	  'portfolio_cropping_image' => 'true',
	  'progression_studios_portfolio_gap' => '0px',
	  'portfolio_post_count' => '12',
	  'portfolio_pagination_type' => 'default_pagination_pro',
	  'portfolio_filter_categories' => '',
	  'portfolio_filtering' => '',
	  'portfolio_filter_text' => 'All',
	  'portfolio_disable_text_hvr' => '',
	  'portfolio_heading_size' => '',
	  'portfolio_disable_cat_hvr' => '',
	  'portfolio_background_color' => '',
		 
   ), $atts ) );
   
    $output_pro = '';
	
	
	STATIC $idpro = 0;
	$idpro++;
	
	ob_start();
	?>
	

	
	<?php
	if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {   $paged = get_query_var('page'); } else {  $paged = 1; }
	
	global $blogloop;
	global $post;
	
	$postIds = $portfolio_filter_categories; // get custom field value
    $arrayIds = explode(',', $postIds); // explode value into an array of ids
    if(count($arrayIds) <= 1) // if array contains one element or less, there's spaces after comma's, or you only entered one id
    {
        if( strpos($arrayIds[0], ',') !== false )// if the first array value has commas, there were spaces after ids entered
        {
            $arrayIds = array(); // reset array
            $arrayIds = explode(', ', $postIds); // explode ids with space after comma's
        }
	 }

	 
	 if ( $portfolio_filter_categories ) {
		 
	 	$blogloop = new WP_Query(
	 		array(
	 	        'post_type' => 'portfolio',
	 			  'paged' => $paged,
	 	        'posts_per_page' => $portfolio_post_count,
  				'tax_query' => array(
  				        // Note: tax_query expects an array of arrays!
  				        array(
  				            'taxonomy' => 'portfolio-category', // 
  				            'field'    => 'slug',
  				            'terms'    => $arrayIds,
  				        )
  				 ),
	 		)
	  	);
		 
	}
	else {
			 
		 	$blogloop = new WP_Query(
		 		array(
		 	        'post_type' => 'portfolio',
		 			  'paged' => $paged,
		 	        'posts_per_page' => $portfolio_post_count,
		 		)
		  	);
			 
		 	
	}
	
	?>


		<div class="progression-studios-portfolio-vc">
			
			<?php if($portfolio_filtering == 'true' ): ?>
				<ul class="progression-filter-button-group progression-filter-group-<?php echo esc_attr($idpro) ?>">
					<?php if($portfolio_filter_categories): ?>
			
						<?php
							$i = 0;

							$args = array(
							    'hide_empty'             => '0',
							    'slug'              => $arrayIds,
							); 
				
							$terms = get_terms( 'portfolio-category', $args );
							if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
								echo '<li class="is-checked" data-filter="*">' . $portfolio_filter_text .'</li> ';	
				
								foreach ( $terms as $term ) { 
								if ($i == 0) {
								echo '<li data-filter=".'.$term->slug.'">' .$term->name .'</li> ';	
								} else if ($i > 0)  {
								echo '<li data-filter=".'.$term->slug.'">' .$term->name .'</li> ';	
								}
								$i++;
								}
							}	
						?>
					<?php else: ?>
						<?php
							$i = 0;
							$terms = get_terms( 'portfolio-category', 'hide_empty=0' );
							if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
								echo '<li class="is-checked" data-filter="*">' . $portfolio_filter_text .'</li> ';	
				
								foreach ( $terms as $term ) { 
								if ($i == 0) {
								echo '<li data-filter=".'.$term->slug.'">' .$term->name .'</li> ';	
								} else if ($i > 0)  {
								echo '<li data-filter=".'.$term->slug.'">' .$term->name .'</li> ';	
								}
								$i++;
								}
							}	
						?>
					<?php endif ?>
				</ul>
				<div class="clearfix-pro"></div>
			<?php endif ?>
			
			
				
				<div class="progression-masonry-overflow <?php echo esc_attr($progression_studios_portfolio_layout) ?> <?php if($portfolio_disable_text_hvr == 'true' ): ?>progression-disable-text-hover<?php endif ?> <?php if($portfolio_disable_cat_hvr == 'true' ): ?>progression-disable-category-hover<?php endif ?>">
					<div class="progression-masonry-margins" style="margin-left:<?php echo esc_attr($progression_studios_portfolio_gap) ?>; margin-right:<?php echo esc_attr($progression_studios_portfolio_gap) ?>;">
						<div class="progression-masonry-<?php echo esc_attr($idpro) ?>">
							<?php while($blogloop->have_posts()): $blogloop->the_post();	?>
								<div class="progression-masonry-item progression-masonry-col-<?php echo esc_attr($progression_studios_portfolio_columns) ?> <?php  $terms = get_the_terms( $post->ID , 'portfolio-category' );  if ( !empty( $terms ) ) : 	foreach ( $terms as $term ) { 	$term_link = get_term_link( $term, 'portfolio-category' ); if( is_wp_error( $term_link ) ) continue; echo " ". $term->slug ; }  endif; ?>">
									<?php include(locate_template('template-parts/visual-composer/content-portfolio.php')); ?>
								</div>
							<?php endwhile; // end of the loop. ?>
						</div>
					</div>
				</div><!-- close .progression-masonry-overflow -->
		
		
				<script type="text/javascript">
				jQuery(document).ready(function($) {
					'use strict';
	
					/* Default Isotope Load Code */
					var $container = $('.progression-masonry-<?php echo esc_attr($idpro) ?>').isotope();
					$container.imagesLoaded( function() {
						$(".progression-masonry-item").addClass('opacity-progression');
						$container.isotope({
							itemSelector: '.progression-masonry-item',
							transitionDuration: '0.4s',

							masonry: {
							    columnWidth: '.progression-masonry-col-<?php echo esc_attr($progression_studios_portfolio_columns) ?>',
							},hiddenStyle: {
								opacity: 0
							},
							visibleStyle: {
								opacity: 1
							}
				 		});
					});
					/* END Default Isotope Code */
					
					<?php if($portfolio_filtering == 'true' ): ?>
					$('.progression-filter-group-<?php echo esc_attr($idpro) ?>').on( 'click', 'li', function() {
					  var filterValue = $(this).attr('data-filter');
					  $container.isotope({ filter: filterValue });
					});
		
			    	  $('.progression-filter-group-<?php echo esc_attr($idpro) ?>').each( function( i, buttonGroup ) {
			    		var $buttonGroup = $( buttonGroup );
			    		$buttonGroup.on( 'click', 'li', function() {
			    		  $buttonGroup.find('.is-checked').removeClass('is-checked');
			    		  $( this ).addClass('is-checked');
			    		});
			    	  });
					<?php endif ?>
	
				});
				</script>
	
	
			<div class="clearfix-pro"></div>
			<?php if($portfolio_pagination_type == 'default_pagination_pro' ): ?><?php progression_studios_show_pagination_links_blog(); ?><?php endif ?>
				
			
	</div><!-- close .progression-studios-portfolio-vc -->
	
	<?php wp_reset_postdata();?>
		
	<?php
	
	return $output_pro. ob_get_clean();
	
}


add_action( 'vc_before_init', 'progression_portfolio_integrateVC' );
function progression_portfolio_integrateVC() {
   vc_map( array(
      "name" => __( "Pro Portfolio", "pro-elements" ),
	  "description" => __( "Portfolio post list", "pro-elements" ),
      "base" => "progression_portfolio",
	  "weight" => 100,
      "class" => "",
      "category" => __( "Pro Elements", "pro-elements"),
	  'icon' => 'vc-icon',

      "params" => array(
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Portfolio Columns", "pro-elements" ),
            "param_name" => "progression_studios_portfolio_columns",
				"value"       => array(
					'1 Column'   	=> '1',
					'2 Columns'  	=> '2',
					'3 Columns'		=> '3',
					'4 Columns'  	=> '4',
					'5 Colums'  	=> '5',
			),
			"std"         => '3',
         ),
			
			
         array(
            "type" => "textfield",
				"class" => "",
            "heading" => __( "Portfolio Post Count", "pro-elements" ),
            "param_name" => "portfolio_post_count",
				"std"         => '12',
         ),
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Portfolio Pagination", "pro-elements" ),
            "param_name" => "portfolio_pagination_type",
				"value"       => array(
					'No Pagination'				=> 'pagination_off',
					'Default Pagination'   		=> 'default_pagination_pro',
			),
			"std"         => 'default_pagination_pro',
         ),
			
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Portfolio Gap", "pro-elements" ),
            "param_name" => "progression_studios_portfolio_gap",
				"value"       => array(
					'0px' => '0px',
					'5px' => '5px',
					'10px' => '10px',
					'15px' => '15px',
					'20px' => '20px',
					'25px' => '25px',
					'30px' => '30px',
			),
			"std"         => '0px',
         ),
			
			
         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Crop Portfolio Images", "pro-elements" ),
            "param_name" => "portfolio_cropping_image",
				"std"         => 'true',
         ),
			
			
         array(
            "type" => "textfield",
				"class" => "",
            "heading" => __( "Narrow by Categories", "pro-elements" ),
				"description" => __( "Enter category slugs to display a specific category. Add-in multiple category slugs seperated by a comma to use multiple categories. (Leave blank to pull in all categories).", "pro-elements" ),
            "param_name" => "portfolio_filter_categories",
				"std"         => '',
         ),
		 
         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Turn on Category Filter", "pro-elements" ),
            "param_name" => "portfolio_filtering",
				"std"         => '',
         ),
		 
         array(
            "type" => "textfield",
				"class" => "",
            "heading" => __( "Category Filter Text for All Posts", "pro-elements" ),
            "param_name" => "portfolio_filter_text",
				"std"         => 'All',
				'dependency' => array(
					'element' => 'portfolio_filtering',
					'not_empty' => true,
				),
         ),
			
			
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Portfolio Post Layout", "pro-elements" ),
            "param_name" => "progression_studios_portfolio_layout",
				"value"       => array(
				        'Overlay'   				=> 'progression-default-layout',
				        'Underneath'   	=> 'progression-underneath-layout',
				),
				"std"         => 'progression-default-layout',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),
			

         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Portfolio disable all text", "pro-elements" ),
            "param_name" => "portfolio_disable_text_hvr",
				"std"         => '',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),
			
         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Portfolio disable category", "pro-elements" ),
            "param_name" => "portfolio_disable_cat_hvr",
				"std"         => '',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),
			
         array(
            "type" => "textfield",
				"class" => "",
            "heading" => __( "Heading Font Size", "pro-elements" ),
            "param_name" => "portfolio_heading_size",
				"std"         => '',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),
			
         array(
            "type" => "colorpicker",
				"class" => "",
            "heading" => __( "Portfolio Background Color", "pro-elements" ),
            "param_name" => "portfolio_background_color",
				"std"         => '',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),


			
      )
   ) );
}