<?php

/**
 * Shop Shortcode
 */


// [progression_shop heading_pro="" menu_description_pro="" etc...]
add_shortcode( 'progression_shop', 'progression_shop_func' );
function progression_shop_func( $atts, $content = null ) { // New function parameter $content is added!
   extract( shortcode_atts( array(
		
	  'progression_studios_shop_layout' => 'progression-shop-default-layout',
	  'progression_studios_shop_columns' => '3',
	  'progression_studios_shop_gap' => '20px',
	  'shop_post_count' => '12',
	  'shop_pagination_type' => 'pagination_off',
	  'shop_filter_categories' => '',
	  'shop_heading_size' => '',
	  'shop_price_size' => '',
	  'shop_sale_products' => '',
	  'progression_shop_sort' => 'default',
	  
	  
		 
   ), $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 = $shop_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 ( $shop_sale_products ) {
		$shop_sale_option = '>';
	}
	else {
		$shop_sale_option = '>=';
	}
		
	
	 if ( $shop_filter_categories ) {
		 
		 
		 
		if ( $progression_shop_sort == 'default' ) {
	  	$blogloop = new WP_Query(
	  		array(
				
	  	        'post_type' => 'product',
	  			  'paged' => $paged,
				  'orderby'   => 'date',
	  	        'posts_per_page' => $shop_post_count,
    				'tax_query' => array(
    				        // Note: tax_query expects an array of arrays!
    				        array(
    				            'taxonomy' => 'product_cat', // 
    				            'field'    => 'slug',
    				            'terms'    => $arrayIds,
    				        )
    				 ),
					 'meta_query'     => array(
						 'relation' => 'OR',
						 array( // Simple products type
							 'key'           => '_sale_price',
							 'value'         => 0,
							 'compare'       => $shop_sale_option,
							 'type'          => 'numeric'
						 ),
						 array( // Variable products type
							 'key'           => '_min_variation_sale_price',
							 'value'         => 0,
							 'compare'       => $shop_sale_option,
							 'type'          => 'numeric'
						 )
					 )
					
	  		)
	   	);	
			
		}
		
		
		if ( $progression_shop_sort == 'top-sellers' ) {
	  	$blogloop = new WP_Query(
	  		array(
				
	  	        'post_type' => 'product',
	  			  'paged' => $paged,
	  	        'posts_per_page' => $shop_post_count,
				'orderby' =>'meta_value_num', 
				'meta_key' => 'total_sales',
    				'tax_query' => array(
    				        // Note: tax_query expects an array of arrays!
    				        array(
    				            'taxonomy' => 'product_cat', // 
    				            'field'    => 'slug',
    				            'terms'    => $arrayIds,
    				        )
    				 ),
					 'meta_query'     => array(
						 'relation' => 'OR',
						 array( // Simple products type
							 'key'           => '_sale_price',
							 'value'         => 0,
							 'compare'       => $shop_sale_option,
							 'type'          => 'numeric'
						 ),
						 array( // Variable products type
							 'key'           => '_min_variation_sale_price',
							 'value'         => 0,
							 'compare'       => $shop_sale_option,
							 'type'          => 'numeric'
						 )
					 )
					
	  		)
	   	);	
			
		}
		
		
		
		if ( $progression_shop_sort == 'top-rated' ) {
	  	$blogloop = new WP_Query(
	  		array(
				
	  	        'post_type' => 'product',
	  			  'paged' => $paged,
	  	        'posts_per_page' => $shop_post_count,
				'orderby' =>'meta_value_num', 
				'meta_key' => '_wc_average_rating',
    				'tax_query' => array(
    				        // Note: tax_query expects an array of arrays!
    				        array(
    				            'taxonomy' => 'product_cat', // 
    				            'field'    => 'slug',
    				            'terms'    => $arrayIds,
    				        )
    				 ),
					 'meta_query'     => array(
						 'relation' => 'OR',
						 array( // Simple products type
							 'key'           => '_sale_price',
							 'value'         => 0,
							 'compare'       => $shop_sale_option,
							 'type'          => 'numeric'
						 ),
						 array( // Variable products type
							 'key'           => '_min_variation_sale_price',
							 'value'         => 0,
							 'compare'       => $shop_sale_option,
							 'type'          => 'numeric'
						 )
					 )
					
	  		)
	   	);	
			
		}
			
			
			
		 
	}
	else {
		
		
		if ( $progression_shop_sort == 'default' ) {
			
	 	$blogloop = new WP_Query(
		
	 		array(
	 	        'post_type' => 'product',
	 			  'paged' => $paged,
				  'orderby'   => 'date',
	 	        'posts_per_page' => $shop_post_count,
				 'meta_query'     => array(
					 'relation' => 'OR',
					 array( // Simple products type
						 'key'           => '_sale_price',
						 'value'         => 0,
						 'compare'       => $shop_sale_option,
						 'type'          => 'numeric'
					 ),
					 array( // Variable products type
						 'key'           => '_min_variation_sale_price',
						 'value'         => 0,
						 'compare'       => $shop_sale_option,
						 'type'          => 'numeric'
					 )
				 )
					 
					 
	 		)
	  	);	
		
		}
		
		
		
		
		
		if ( $progression_shop_sort == 'top-sellers' ) {
			
	 	$blogloop = new WP_Query(
		
	 		array(
	 	        'post_type' => 'product',
	 			  'paged' => $paged,
	 	        'posts_per_page' => $shop_post_count,
					'orderby' =>'meta_value_num', 
					'meta_key' => 'total_sales',
				 'meta_query'     => array(
					 'relation' => 'OR',
					 array( // Simple products type
						 'key'           => '_sale_price',
						 'value'         => 0,
						 'compare'       => $shop_sale_option,
						 'type'          => 'numeric'
					 ),
					 array( // Variable products type
						 'key'           => '_min_variation_sale_price',
						 'value'         => 0,
						 'compare'       => $shop_sale_option,
						 'type'          => 'numeric'
					 )
				 )
					 
					 
	 		)
	  	);	
		
		}
		
		
		if ( $progression_shop_sort == 'top-rated' ) {
			
	 	$blogloop = new WP_Query(
		
	 		array(
	 	        'post_type' => 'product',
	 			  'paged' => $paged,
	 	        'posts_per_page' => $shop_post_count,
					'orderby' =>'meta_value_num', 
					'meta_key' => '_wc_average_rating',
				 'meta_query'     => array(
					 'relation' => 'OR',
					 array( // Simple products type
						 'key'           => '_sale_price',
						 'value'         => 0,
						 'compare'       => $shop_sale_option,
						 'type'          => 'numeric'
					 ),
					 array( // Variable products type
						 'key'           => '_min_variation_sale_price',
						 'value'         => 0,
						 'compare'       => $shop_sale_option,
						 'type'          => 'numeric'
					 )
				 )
					 
					 
	 		)
	  	);	
		
		}
		
		
		
	}
	 
 	

	
	?>


		<div class="woocommerce progression-studios-shop-vc">
			<div class="products">
				<div class="progression-shop-masonry-overflow <?php echo esc_attr($progression_studios_shop_layout) ?>">
					<div class="progression-masonry-margins" style="margin-top:-<?php echo esc_attr($progression_studios_shop_gap) ?>; margin-left:-<?php echo esc_attr($progression_studios_shop_gap) ?>; margin-right:-<?php echo esc_attr($progression_studios_shop_gap) ?>;">
						<div class="progression-shop-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_shop_columns) ?>">
									<?php include(locate_template('template-parts/visual-composer/content-product.php')); ?>
								</div>
							<?php endwhile; // end of the loop. ?>
							
							<div class="clearfix-pro"></div>
						</div>
					</div>
				</div><!-- close .progression-masonry-overflow -->
		
		
				<script type="text/javascript">
				jQuery(document).ready(function($) {
					'use strict';
	
					/* Default Isotope Load Code */
					var $container = $('.progression-shop-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_shop_columns) ?>',
							},hiddenStyle: {
								opacity: 0
							},
							visibleStyle: {
								opacity: 1
							}
				 		});
					});
					/* END Default Isotope Code */
					
					
	
				});
				</script>
	
	
			<div class="clearfix-pro"></div>
			<?php if($shop_pagination_type == 'default_pagination_pro' ): ?><?php progression_studios_show_pagination_links_blog(); ?><?php endif ?>
				
		
		</div><!-- close .products -->
	</div><!-- close .progression-studios-shop-vc -->
	
	<?php wp_reset_postdata();?>
		
	<?php
	
	return $output_pro. ob_get_clean();
	
}


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

      "params" => array(
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Shop Columns", "pro-elements" ),
            "param_name" => "progression_studios_shop_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" => __( "Shop Post Count", "pro-elements" ),
            "param_name" => "shop_post_count",
				"std"         => '12',
         ),
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Shop Pagination", "pro-elements" ),
            "param_name" => "shop_pagination_type",
				"value"       => array(
					'No Pagination'				=> 'pagination_off',
					'Default Pagination'   		=> 'default_pagination_pro',
			),
			"std"         => 'pagination_off',
         ),
			
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Shop Gap", "pro-elements" ),
            "param_name" => "progression_studios_shop_gap",
				"value"       => array(
					'0px' => '0px',
					'5px' => '5px',
					'10px' => '10px',
					'15px' => '15px',
					'20px' => '20px',
					'25px' => '25px',
					'30px' => '30px',
			),
			"std"         => '20px',
         ),
			
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Shop Sorting", "pro-elements" ),
            "param_name" => "progression_shop_sort",
				"value"       => array(
					'Sort by Latest Products'				=> 'default',
					'Sort by Top Selling'   				=> 'top-sellers',
					'Sort by Top Rated'   					=> 'top-rated',
					
			),
			"std"         => 'default',
         ),
			
         array(
            "type" => "checkbox",
				"class" => "",
            "heading" => __( "Display products on sale", "pro-elements" ),
            "param_name" => "shop_sale_products",
				"std"         => '',
         ),
			
			
         array(
            "type" => "textfield",
				"class" => "",
            "heading" => __( "Narrow by Categories by ID", "pro-elements" ),
				"description" => __( "Enter category IDs to display a specific category. Add-in multiple category IDS seperated by a comma to use multiple categories. (Leave blank to pull in all categories).", "pro-elements" ),
            "param_name" => "shop_filter_categories",
				"std"         => '',
         ),
			
			
         
		 
         
			
			
         array(
            "type" => "dropdown",
				"class" => "",
            "heading" => __( "Shop Post Layout", "pro-elements" ),
            "param_name" => "progression_studios_shop_layout",
				"value"       => array(
				        'Overlay'   				=> 'progression-shop-default-layout',
				        'Underneath'   	=> 'progression-shop-underneath-layout',
				),
				"std"         => 'progression-shop-default-layout',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),
			


         array(
            "type" => "textfield",
				"class" => "",
            "heading" => __( "Heading Font Size", "pro-elements" ),
            "param_name" => "shop_heading_size",
				"std"         => '',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),
			
         array(
            "type" => "textfield",
				"class" => "",
            "heading" => __( "Price Font Size", "pro-elements" ),
            "param_name" => "shop_price_size",
				"std"         => '',
				'group' => __( 'Design Options', 'pro-elements' ),
         ),


			
      )
   ) );
}