<?php

/**
 * Blog Shortcode
 */


// [progression_blog heading_pro="" menu_description_pro="" etc...]
add_shortcode( 'progression_blog', 'progression_blog_func' );
function progression_blog_func( $atts, $content = null ) { // New function parameter $content is added!
   extract( shortcode_atts( array(
		
	  'progression_studios_blog_layout' => 'progression-blog-underneath-layout',
	  'progression_studios_blog_columns' => '4',
	  'progression_studios_blog_gap' => '0px',
	  'blog_post_count' => '4',
	  'blog_pagination_type' => 'pagination_off',
	  'blog_filter_categories' => '',
	  'blog_heading_size' => '',
	  'blog_hide_category' => '',
	  'blog_backgrond_color' => '#000000',
	  'blog_lighter_fonts' => 'true',
	  
	  'blog_hide_date' => '',
	  'blog_hide_author' => 'true',
	  'blog_hide_comments' => 'true',
	  
	  'blog_hide_excerpt' => '',
	  
		 
   ), $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 = $blog_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 ( $blog_filter_categories ) {
		 
	 	$blogloop = new WP_Query(
	 		array(
	 	        'post_type' => 'post',
				  'post__not_in' => get_option( 'sticky_posts' ),
				  'ignore_sticky_posts' => 1,
	 			  'paged' => $paged,
	 	        'posts_per_page' => $blog_post_count,
  				'tax_query' => array(
  				        // Note: tax_query expects an array of arrays!
  				        array(
  				            'taxonomy' => 'category', // 
  				            'field'    => 'slug',
  				            'terms'    => $arrayIds,
  				        )
  				 ),
	 		)
	  	);
		 
	}
	else {
			 
		 	$blogloop = new WP_Query(
		 		array(
		 	        'post_type' => 'post',
					  'post__not_in' => get_option( 'sticky_posts' ),
					  'ignore_sticky_posts' => 1,
		 			  'paged' => $paged,
		 	        'posts_per_page' => $blog_post_count,
		 		)
		  	);
			 
		 	
	}
	
	?>

		<div class="progression-studios-blog-vc">

			
			
				
				<div class="progression-masonry-overflow <?php echo esc_attr($progression_studios_blog_layout) ?> <?php if($blog_lighter_fonts == 'true'  ) : ?>lighter-fonts-blog-pr<?php endif ?>">
					<div class="progression-masonry-margins" style="margin-left:-<?php echo esc_attr($progression_studios_blog_gap) ?>; margin-right:-<?php echo esc_attr($progression_studios_blog_gap) ?>;">
						<div class="progression-blog-masonry-<?php echo esc_attr($idpro) ?>">
							<?php while($blogloop->have_posts()): $blogloop->the_post();	?>
								<div <?php if($blog_backgrond_color): ?>style="background:<?php echo esc_attr($blog_backgrond_color) ?>;"<?php endif ?> class="progression-masonry-item progression-masonry-col-<?php echo esc_attr($progression_studios_blog_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.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-blog-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_blog_columns) ?>',
							},hiddenStyle: {
								opacity: 0
							},
							visibleStyle: {
								opacity: 1
							}
				 		});
					});
					/* END Default Isotope Code */
					
					
				});
				</script>
	

			<div class="clearfix-pro"></div>
			<?php if($blog_pagination_type == 'default_pagination_pro' ): ?><?php progression_studios_show_pagination_links_blog(); ?><?php endif ?>
				
			
	</div><!-- close .progression-studios-blog-vc -->

	<?php wp_reset_postdata();?>
		
	<?php
	
	return $output_pro. ob_get_clean();
	
}


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

      "params" => array(
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Blog Columns", "pro-elements" ),
            "param_name" => "progression_studios_blog_columns",
				"value"       => array(
					'1 Column'   	=> '1',
					'2 Columns'  	=> '2',
					'3 Columns'		=> '3',
					'4 Columns'  	=> '4',
					'5 Colums'  	=> '5',
			),
			"std"         => '1',
         ),
			
			
         array(
            "type" => "textfield",
				"class" => "",
            "heading" => __( "Blog Post Count", "pro-elements" ),
            "param_name" => "blog_post_count",
				"std"         => '12',
         ),
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Blog Pagination", "pro-elements" ),
            "param_name" => "blog_pagination_type",
				"value"       => array(
					'No Pagination'				=> 'pagination_off',
					'Default Pagination'   		=> 'default_pagination_pro',
			),
			"std"         => 'pagination_off',
         ),
			
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Blog Post Gap", "pro-elements" ),
            "param_name" => "progression_studios_blog_gap",
				"value"       => array(
					'0px' => '0px',
					'5px' => '5px',
					'10px' => '10px',
					'15px' => '15px',
					'20px' => '20px',
					'25px' => '25px',
					'30px' => '30px',
			),
			"std"         => '0px',
         ),
			

			
         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" => "blog_filter_categories",
				"std"         => '',
         ),
		 
        
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Blog Post Layout", "pro-elements" ),
            "param_name" => "progression_studios_blog_layout",
				"value"       => array(
				        'Default'   				=> 'progression-blog-default-layout',
				        'Underneath'   	=> 'progression-blog-underneath-layout',
				),
				"std"         => 'progression-blog-underneath-layout',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),
			
         array(
            "type" => "colorpicker",
				"class" => "",
            "heading" => __( "Blog Background", "pro-elements" ),
            "param_name" => "blog_backgrond_color",
				"std"         => '#000000',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),
			
         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Check to make fonts lighter (Use when creating a dark background color)", "pro-elements" ),
            "param_name" => "blog_lighter_fonts",
				'group' => __( 'Design Options', 'pro-elements' ),
				"std"         => 'true',
         ),
			
         array(
            "type" => "textfield",
				"class" => "",
            "heading" => __( "Heading Font Size", "pro-elements" ),
            "param_name" => "blog_heading_size",
				"std"         => '',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),
			
         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Check to hide Category", "pro-elements" ),
            "param_name" => "blog_hide_category",
				'group' => __( 'Design Options', 'pro-elements' ),
				"std"         => '',
         ),
			
         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Check to hide Date", "pro-elements" ),
            "param_name" => "blog_hide_date",
				'group' => __( 'Design Options', 'pro-elements' ),
         ),
			
         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Check to hide Author", "pro-elements" ),
            "param_name" => "blog_hide_author",
				'group' => __( 'Design Options', 'pro-elements' ),
				"std"         => 'true',
         ),
			
         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Check to hide Comments", "pro-elements" ),
            "param_name" => "blog_hide_comments",
				'group' => __( 'Design Options', 'pro-elements' ),
				"std"         => 'true',
         ),
			
         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Check to hide Excerpt", "pro-elements" ),
            "param_name" => "blog_hide_excerpt",
				'group' => __( 'Design Options', 'pro-elements' ),
				"std"         => '',
         ),
		

			
      )
   ) );
}