<?php

if ( function_exists( 'WC' ) && version_compare( WC()->version, '3.3.0' ) >= 0 ) {
	add_filter( 'woocommerce_gallery_thumbnail_size', 'woo_shrk_smallthumb_fix');
	add_action( 'after_setup_theme', 'shrk_set_wc33_thumbnails', 15 );
	add_action( 'after_setup_theme', 'shrk_copy_wc33_migrate_settings' );
} 

/**
 * Handle WooCommerce deprecated functions
 */






function woo_shrk_get_gallery_image_ids( $product ) {
	if ( version_compare( WC()->version, '2.7.0' ) >= 0 ) {
		return $product->get_gallery_image_ids();		// After 2.7.0
	} else {
		return $product->get_gallery_attachment_ids();
	}
}

function woo_shrk_get_product_type( $product ) {
	if ( method_exists( $product, 'get_type' ) ) {
	    $type = $product->get_type();
	} else {
	    $type = $product->product_type;
	}

	return $type;
} 

/**
 * Add custom thumbnail image size
 * when needed
 */
function shrk_set_wc33_thumbnails() {
	if ( shrk_get_theme_option( 'woo_custom_smallthumb_size' ) ) {
		$size = array();

		$size['width']  = shrk_get_theme_option( 'woo_custom_smallthumb_width' );
		$size['height'] = shrk_get_theme_option( 'woo_custom_smallthumb_height' );
		$size['crop']   = shrk_get_theme_option( 'woo_custom_smallthumb_crop' );

		add_image_size( 'shrk_shop_thumb', $size['width'], $size['height'], $size['crop'] );
	}
}

/**
 * Change applied thumbnail size for small single product thumbnails
 */
function woo_shrk_smallthumb_fix( $size ) {
	if ( shrk_get_theme_option( 'woo_custom_smallthumb_size' ) ) {
		$size = 'shrk_shop_thumb';
	}
	return $size;
}

/**
 * Migrate some pre WC3.3 settings to theme's redux options
 * & set some of the new WC 3.3 settings based on redux options
 */
function shrk_copy_wc33_migrate_settings() {
	global $shrk_options;
	
	if ( !class_exists( 'ReduxFrameworkInstances' ) || ( defined('DOING_AJAX') && DOING_AJAX ) ) {
		return;
	}

	if ( get_option( 'shrk_theme_wc33_settings_migrated', false ) ) {
		return;
	}

	$redux = ReduxFrameworkInstances::get_instance( 'shrk_theme_options' );
	
	if ( $redux && method_exists( $redux,'set' ) ) {
		$old_thumbnail_size = get_option( 'shop_thumbnail_image_size', array() );
		$old_catalog_size = get_option( 'shop_catalog_image_size', array() );

		if ( !empty( $old_thumbnail_size ) ) {
			$redux->set( 'woo_custom_smallthumb_width', $old_thumbnail_size['width'] );	
			$redux->set( 'woo_custom_smallthumb_height', $old_thumbnail_size['height'] );	
			$redux->set( 'woo_custom_smallthumb_crop', $old_thumbnail_size['crop'] );

			if ( $old_thumbnail_size['width'] == $old_catalog_size['width'] && 
				 $old_thumbnail_size['height'] == $old_catalog_size['height'] && 
				 $old_thumbnail_size['crop'] == $old_catalog_size['crop']
			) {
				$redux->set( 'woo_custom_smallthumb_size', false );
			} else {
				$redux->set( 'woo_custom_smallthumb_size', true );
			}
		}
	}

	// Set Shop Columns grid's 1st value to 'Products per row'
	if ( isset( $shrk_options['woo_shop_columns_grid'] ) && !empty( $shrk_options['woo_shop_columns_grid'][0] ) && $shrk_options['woo_shop_columns_grid'][0]['opr'] == 'min' ) {
		update_option('woocommerce_catalog_columns', $shrk_options['woo_shop_columns_grid'][0]['cols'] );
	}

	// Calculate rows per page by products per row and posts_per_page
	if ( isset( $shrk_options['woo_shop_columns_grid'] ) && !empty( $shrk_options['woo_shop_columns_grid'][0] ) && $shrk_options['woo_shop_columns_grid'][0]['opr'] == 'min' ) {
		$cols   = absint( $shrk_options['woo_shop_columns_grid'][0]['cols'] );
		$cols   = $cols ? $cols : 1;
		$pppage = absint( get_option( 'posts_per_page' ) );
		$rows   = absint( ceil( $pppage / $cols ) );
		update_option('woocommerce_catalog_rows', $rows );
	}

	update_option( 'shrk_theme_wc33_settings_migrated', true );
}

