<?php

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






function woo_shrk_related_products_args( $args ) {
	global $shrk_options;
	$args['posts_per_page'] = isset( $shrk_options['woo_related_products_count'] ) ? $shrk_options['woo_related_products_count'] : 4;
	$args['columns'] = isset( $shrk_options['woo_related_products_cols'] ) ? $shrk_options['woo_related_products_cols'] : 4;
	return $args;
}

function woo_shrk_modify_pagination_args( $args ) {
	$args['prev_text'] = '';
	$args['next_text'] = '';
	return $args;
}

function woo_shrk_loop_add_to_cart_args( $args, $product ) {
	if ( !( $product->is_purchasable() && $product->is_in_stock() ) ) {
		$args['class'] = $args['class'] . ' hide'; // Hide add2cart button if not purchasable product
	}
	return $args;
}

function woo_shrk_get_category_image_id( $term_id = false, $size = 'full' ) {
    
    if ( !$term_id && is_product_category() ){
	    global $wp_query;
	    $cat = $wp_query->get_queried_object();
	    $term_id = $cat->term_id;
	}
    
    $image_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id', true );

    return $image_id ? $image_id : false;
}


function woo_shrk_get_category_image( $term_id = false, $size = 'full' ) {
    
    if ( !$term_id && is_product_category() ){
	    global $wp_query;
	    $cat = $wp_query->get_queried_object();
	    $term_id = $cat->term_id;
	}
    
    $image = $term_id ? wp_get_attachment_image_src( get_woocommerce_term_meta( $term_id, 'thumbnail_id', true ), $size ) : false;

    if ( $image ) {
	    return $image[0];
	} else {
		return false;		
	}

}

function woo_shrk_category_image( $cover ) {
	if ( function_exists('is_product_category') && is_product_category() && woo_shrk_get_category_image() ) {
		$cover = woo_shrk_get_category_image();
	}

	return $cover;
}
add_filter( 'shrk-cover-image', 'woo_shrk_category_image' );

function woo_shrk_category_image_id( $cover_id ) {
	if ( function_exists('is_product_category') && is_product_category() && woo_shrk_get_category_image_id() ) {
		$cover_id = woo_shrk_get_category_image_id();
	}

	return $cover_id;
}
add_filter( 'shrk-cover-image-id', 'woo_shrk_category_image_id' );

function woo_shrk_set_image_dimensions() {
	$catalog = array(
		'width'   => '300',  // px
		'height'  => '450',  // px
		'crop'    => 1       // true
	);

	$single = array(
		'width'   => '750',  // px
		'height'  => '1200', // px
		'crop'    => 0       // false, rel. to photoswipe zoom
	);

	// WC > 3.3
	update_option( 'woocommerce_thumbnail_image_width', 300 );
	update_option( 'woocommerce_single_image_width', 750 );

	// WC < 3.3
	update_option( 'shop_catalog_image_size',   $catalog );    // Product category thumbs
	update_option( 'shop_single_image_size',    $single );     // Single product image
}
add_action( 'shrk_theme_first_install', 'woo_shrk_set_image_dimensions' );