<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}






function shrk_swiper_scripts() {
	global $shrk_theme_info;
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

	$deps = function_exists( 'vc_map' ) ? array( 'wpb_composer_front_js' ) : array();
	wp_register_script( 'shrk-swiper' , SHRK_THEME_MODULES_URI . 'swiper/js/swiper' . $suffix . '.js', $deps, $shrk_theme_info['Version'] );
}
add_action( 'wp_enqueue_scripts', 'shrk_swiper_scripts', 10 );

function shrk_sc_swiper_get_atts( $atts, $classes ) {

	$data_atts   = $style_rules = array();
	$output      = '';
	$spv         = false; // Slides per view
	extract( $atts );


	//swiper attributes
	if ( !empty( $swiper_mode ) && $swiper_mode == 'vertical' ) {
		$spv = '1';
	} else {
		$spv = 'auto';
	}

	if ( !empty( $swiper_spv ) ) { 
		$spv = $swiper_spv;
	}

	if ( !empty( $swiper_mode ) ) { 
		$data_atts[] = 'data-swiper-direction="' . esc_attr( $swiper_mode ) . '"'; 
	}

	if ( !empty( $swiper_effect ) ) { 
		$data_atts[] = 'data-swiper-effect="' . esc_attr( $swiper_effect ) . '"'; 
	}

	if ( $partial_view == 'yes' ) {	
		$data_atts[] = 'data-swiper-partialmaxwidth="' . esc_attr( $partial_max_width ) . '"';	
	}

	if ( !empty( $show_scrollbar ) && $show_scrollbar == 'yes' ) {
		$data_atts[] = 'data-swiper-showscrollbar="true"';
	}

	if ( !empty( $auto_load_slides ) && $auto_load_slides == 'yes' ) { 
		$data_atts[] = 'data-swiper-autoloadslides="true"'; 
		$classes[]   = 'swiper-auto-load-slides';
	}

	if ( !empty( $hide_pagination ) ) {
		$classes[]   = $hide_pagination == 'yes' ? 'swiper-hide-pagination' : 'swiper-show-pagination';
	}

	if ( !empty( $swiper_autoplay ) && $swiper_autoplay == 'yes' ) { 
		$data_atts[] = 'data-swiper-autoplay="' . esc_attr( $swiper_speed ) . '"'; 
	}

	if ( !empty( $swiper_animation_speed ) ) { 
		$data_atts[] = 'data-swiper-speed="' . esc_attr( absint( $swiper_animation_speed ) ) . '"'; 
	}

	if ( (!empty( $wrap ) && $wrap == 'yes') || (!empty( $swiper_loop ) && $swiper_loop == 'yes') ) { 
		$data_atts[] = 'data-swiper-loop="true"'; 
	}

	// Swiper height
	if ( !empty( $slider_height ) && ( !empty($slider_height_mode) && $slider_height_mode == 'custom' )  ) {
		$style_rules[] = "height: " . esc_attr( $slider_height ) . ";";
	}

	// Swiper bg
	if ( !empty( $slider_bg_color_inline ) ) {
		$style_rules[] = "background-color: " . esc_attr( $slider_bg_color_inline ) . ";";
	}

	// Slider controls color scheme
	if ( !empty( $swiper_controls_colors ) && $swiper_controls_colors == 'dark' ) {
		$classes[]   = 'dark-controls';
	}

	if ( !empty( $swiper_controls_transp ) && $swiper_controls_transp == 'yes' ) {
		$classes[]   = 'transp-controls';
	}

	if ( !empty( $arrows_hidden_xs ) && $arrows_hidden_xs == 'yes' ) {
		$classes[]   = 'arrows-hidden-xs';
	}

	if ( !empty( $pagination_only_xs ) && $pagination_only_xs == 'yes' ) {
		$classes[]   = 'pagination-shown-only-xs';
	}

	if ( !empty($slider_height_mode) && $slider_height_mode == 'windowheight_mh' ) {
		$data_atts[] = 'data-swiper-heightmode="windowheight_mh"'; 
	}

	if ( !empty( $swiper_autoheight ) ) {
		$data_atts[] = 'data-swiper-autoheight="false"'; 
	}

	if ( !empty( $space_between_slides ) || '0' === $space_between_slides ) {
		$data_atts[] = 'data-swiper-spacebetween="' . esc_attr( $space_between_slides ) . '"';
	}

	if ( !empty( $slides_grid_width ) && intval($slides_grid_width) <> 12 ) {
		$classes[] = 'swiper-multi-cols';
	}

	if ( !empty( $inner_half_image ) && $inner_half_image == 'yes' ) {
		$classes[]   = 'half-image';
	}

	if ( !empty( $inner_transp_content ) && $inner_transp_content == 'yes' ) {
		$classes[]   = 'transp-content';
	}

	if ( !empty( $inner_transp_content_mob ) && $inner_transp_content_mob == 'yes' ) {
		$classes[]   = 'transp-content-mobile';
	}

	if ( !empty( $swiper_breakpoints ) ) {
		$data_atts[] = 'data-swiper-breakpoints="' . ($swiper_breakpoints == 'disable' ? 'false' : $swiper_breakpoints) . '"';
	}

	//Calc the num of visible slides
	if ( $partial_view == 'yes' ) {	
		$slides_visible = ceil(100 / intval($partial_max_width));
	} else {
		$slides_visible = 12 / intval($slides_grid_width);		
	}

	//No of slides to preload
	if ( !empty( $preload_first_x_slides ) && $preload_first_x_slides == 'auto' ) {
		$preload_first_x_slides = $slides_visible;
	}

	if ( $spv ) {
		$data_atts[] = 'data-swiper-slidesperview="' . esc_attr( $spv ) . '"';
	}

	$data_atts[] = 'data-swiper-slidespreloadbg="' . esc_attr( $slides_visible ) . '"';
	
	if ( !empty($atts['category_id']) ) $data_atts[] = 'data-swiper-qry-category_id="' . esc_attr( $atts['category_id'] ) . '"';
	if ( !empty($atts['page']) )        $data_atts[] = 'data-swiper-qry-page="' . esc_attr( $atts['page'] ) . '"';
	if ( !empty($atts['per_page']) )    $data_atts[] = 'data-swiper-qry-per_page="' . esc_attr( $atts['per_page'] ) . '"';
	if ( !empty($atts['orderby']) )     $data_atts[] = 'data-swiper-qry-orderby="' . esc_attr( $atts['orderby'] ) . '"';
	if ( !empty($atts['order']) )       $data_atts[] = 'data-swiper-qry-order="' . esc_attr( $atts['order'] ) . '"';
	if ( !empty($atts['cover_atts']['thumb_fb']) ) $data_atts[] = 'data-swiper-qry-thumb_fb="' . esc_attr( $atts['cover_atts']['thumb_fb'] ) . '"';
	if ( !empty($atts['cover_atts']['thumb_only']) ) $data_atts[] = 'data-swiper-qry-thumb_only="' . esc_attr( $atts['cover_atts']['thumb_only'] ) . '"';
	
	return 'class="' . implode(' ', $classes) . '" ' . implode(' ', $data_atts) . ' style="'. implode(' ', $style_rules) .'"';
}

function shrk_swiper_product_loop( $query_args, $atts ) {
	global $product, $woocommerce_loop;

	$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts ) );
	$cover_atts = $atts['cover_atts'];

	if ( $atts['get_array'] === true ) { // return each slide as array element

		$arr = array();

		if ( $products->have_posts() ) : 

			while ( $products->have_posts() ) : $products->the_post(); 
				ob_start();
				include( wc_locate_template( 'content-slide-product.php' ) );
				$arr[] = ob_get_clean();
			endwhile; // end of the loop. 

		endif;

		$output = $arr;

	} else {

		ob_start();

		if ( $products->have_posts() ) : 

			while ( $products->have_posts() ) : $products->the_post(); 
				include( wc_locate_template( 'content-slide-product.php' ) );
			endwhile; // end of the loop. 

		endif;

		$output = ob_get_clean();

	}

	$products_count = $products->found_posts;

	woocommerce_reset_loop();
	wp_reset_postdata();
	return array( 'content' => $output, 'count' => $products_count );

}

function shrk_swiper_product_category( $atts ) {
	$atts = shortcode_atts( array(
		'per_page'    => '2',
		'orderby'     => 'date',
		'order'       => 'desc',
		'category_id' => '',  // id
		'operator'    => 'IN', 
		'page'        => '1',
		'get_array'   => false,
		'cover_atts'  => null,
	), $atts );

	if ( ! $atts['category_id'] ) {
		return '';
	}

	// Default ordering args
	$ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'], $atts['order'] );
	$meta_query    = WC()->query->get_meta_query();
	$query_args    = array(
		'post_type'				=> 'product',
		'post_status' 			=> 'publish',
		'ignore_sticky_posts'	=> 1,
		'orderby' 				=> $ordering_args['orderby'],
		'order' 				=> $ordering_args['order'],
		'posts_per_page' 		=> $atts['per_page'],
		'offset'				=> (intval($atts['page']) - 1) * intval($atts['per_page']),
		'meta_query' 			=> $meta_query,
		'tax_query' 			=> array(
			array(
				'taxonomy' 		=> 'product_cat',
				'terms' 		=> $atts['category_id'],
				'field' 		=> 'id',
				'operator' 		=> $atts['operator']
			)
		)
	);

	if ( isset( $ordering_args['meta_key'] ) ) {
		$query_args['meta_key'] = $ordering_args['meta_key'];
	}

	$return = shrk_swiper_product_loop( $query_args, $atts );

	// Remove ordering query arguments
	WC()->query->remove_ordering_args();

	return $return;
}

?>