<?php

/**
 * Hook in on activation
 */
global $pagenow;

if ( is_admin() && isset( $_GET['activated'] ) && $pagenow == 'themes.php' ) {
	add_action( 'init', 'theme_woocommerce_image_dimensions', 1 );
}

/**
 * Define image sizes
 */
function theme_woocommerce_image_dimensions() {
  	$catalog = array(
		'width' 	=> '400',	// px
		'height'	=> '400',	// px
		'crop'		=> 1 		// true
	);

	$single = array(
		'width' 	=> '400',	// px
		'height'	=> '400',	// px
		'crop'		=> 1 		// true
	);

	$thumbnail = array(
		'width' 	=> '90',	// px
		'height'	=> '90',	// px
		'crop'		=> 1 		// true
	);

	// Image sizes
	update_option( 'shop_catalog_image_size', $catalog ); 		// Product category thumbs
	update_option( 'shop_single_image_size', $single ); 		// Single product image
	update_option( 'shop_thumbnail_image_size', $thumbnail ); 	// Image gallery thumbs
}

// WooCommerce CSS
//
function theme_woocommerce_enqueue_scripts() {
	wp_enqueue_style('theme-woocommerce', THEME_URI . '/tdframework/addons/woocommerce/theme-woocommerce.css');

	// Remove Woocommerce Styling
	wp_dequeue_style('woocommerce-layout');
	wp_dequeue_style('woocommerce-smallscreen');
	wp_dequeue_style('woocommerce-general');

	wp_enqueue_script( 'jquery' );

	wp_enqueue_script(
		'woo-theme-js',
		THEME_URI . '/tdframework/addons/woocommerce/theme-woocommerce.js',
		array('jquery'),
		'',
		true
	);
}
add_action('wp_enqueue_scripts', 'theme_woocommerce_enqueue_scripts', 999999);

// Adds WooCommerce layout option page
//
function theme_woocommerce_layoutoptions() {
	global $core_theme_options_handler;
	global $core_layout_default;

	$options = new CoreOptionGroup('woocommerce-layouts', __('Woocommerce', THEME_SLUG), __('Use this page to define the layouts of WooCommerce pages.', THEME_SLUG), THEME_URI. '//tdframework/addons/woocommerce/images/options-woocommerce.png');
	$core_theme_options_handler->group_add($options);

	$layouts = array(
		'layout-woocommerce-shop' => __('Shop page', THEME_SLUG),
		'layout-woocommerce-category' => __('Product category page', THEME_SLUG),
		'layout-woocommerce-tag' => __('Product tag page', THEME_SLUG),
		'layout-woocommerce-product' => __('Product page', THEME_SLUG),
		//'layout-woocommerce-cart' => __('Cart page', THEME_SLUG),
		//'layout-woocommerce-checkout' => __('Checkout page', THEME_SLUG),
		//'layout-woocommerce-account' => __('Account page', THEME_SLUG),
	);
	foreach ($layouts as $key => $value) {
		$section = new CoreOptionSection($key, $value);
		$options->section_add($section);
		$section->option_add(new CoreOption($key, null, 'layout', null, $core_layout_default));

		// Slider
		$section->option_add(new CoreOption('slider_'.$key, __('Slider', THEME_SLUG), 'sliders', __('The slider will be displayed at the top of the', THEME_SLUG) . ' ' . $value . ' .'));

		$section->option_add(new CoreOption($key.'_background', __('Background', THEME_SLUG), 'image'));

		$section->option_add(new CoreOption($key.'_colorscheme', __('Color scheme', THEME_SLUG), 'colorschemes-list'));

		$section->option_add(new CoreOption('custom_content_' .$key , __('Slogan block', THEME_SLUG), 'text-multiline', __('Any HTML put here will be included in it\'s own block above the content.', THEME_SLUG)));

		$section->option_add(new CoreOption('custom_content_' .$key.'_bg' , __('Slogan background', THEME_SLUG), 'image', __('A background for the', THEME_SLUG) . ' '.$value . ' '. __('slogan block above the content.', THEME_SLUG)));
	}
}
add_action('after_setup_theme', 'theme_woocommerce_layoutoptions');

// Returns the right layout for the current WooCommerce page
//
function theme_woocommerce_layout($layout) {
	if (!is_woocommerce()) {
		return $layout;
	}

	if (is_shop()) {
		return core_options_get('layout-woocommerce-shop');
	} elseif (is_product_category()) {
		return core_options_get('layout-woocommerce-category');
	} elseif (is_product_tag()) {
		return core_options_get('layout-woocommerce-tag');
	} elseif (is_product()) {
		return core_options_get('layout-woocommerce-product');
	} elseif (is_cart()) {
		return core_options_get('layout-woocommerce-cart');
	} elseif (is_checkout()) {
		return core_options_get('layout-woocommerce-checkout');
	} elseif (is_account_page()) {
		return core_options_get('layout-woocommerce-account');
	} else {
		return $layout;
	}
}
add_filter('core_layout', 'theme_woocommerce_layout', 5);

// Get Woocommerce Sliders
function theme_woocommerce_slider($slider){
	if ( !is_woocommerce() )
		return $slider;

	if (is_shop()){
		$post_type = 'theme';
		$slug = '_layout-woocommerce-shop';
	}

	if (is_product_category()){
		$post_type = 'theme';
		$slug = '_layout-woocommerce-category';
	}

	if (is_product_tag()){
		$post_type = 'theme';
		$slug = '_layout-woocommerce-tag';
	}

	if (is_product()){
		$post_type = 'theme';
		$slug = '_layout-woocommerce-product';
	}

	if (is_cart()){
		$post_type = 'theme';
		$slug = '_layout-woocommerce-cart';
	}

	if (is_checkout()){
		$post_type = 'theme';
		$slug = '_layout-woocommerce-checkout';
	}

	if (is_account_page()){
		$post_type = 'theme';
		$slug = '_layout-woocommerce-account';
	}

	return core_options_get('slider'.$slug, $post_type);
}
add_filter('slider_area', 'theme_woocommerce_slider');

// Get Woocommerce background image
function theme_woocommerce_get_background($backgroundimage){
	if ( !is_woocommerce() )
		return $backgroundimage;

	if (is_shop()){
		$backgroundimage = core_options_get('layout-woocommerce-shop_background', 'theme');
	}

	if (is_product_category()){
		$backgroundimage = core_options_get('layout-woocommerce-category_background', 'theme');
	}

	if (is_product_tag()){
		$backgroundimage = core_options_get('layout-woocommerce-tag_background', 'theme');
	}

	if (is_product()){
		$backgroundimage = core_options_get('layout-woocommerce-product_background', 'theme');
	}

	if (is_cart()){
		$backgroundimage = core_options_get('layout-woocommerce-cart_background', 'theme');
	}

	if (is_checkout()){
		$backgroundimage = core_options_get('layout-woocommerce-checkout_background', 'theme');
	}

	if (is_account_page()){
		$backgroundimage = core_options_get('layout-woocommerce-account_background', 'theme');
	}

	return $backgroundimage;
}
add_filter('layout_background', 'theme_woocommerce_get_background');

//Get Woocommerce colorscheme
function theme_woocommerce_colorscheme($scheme){
	if( !is_woocommerce())
		return $scheme;

	if (is_shop()){
		$scheme = core_options_get('layout-woocommerce-shop_colorscheme', 'theme');
	}

	if (is_product_category()){
		$scheme = core_options_get('layout-woocommerce-category_colorscheme', 'theme');
	}

	if (is_product_tag()){
		$scheme = core_options_get('layout-woocommerce-tag_colorscheme', 'theme');
	}

	if (is_product()){
		$scheme = core_options_get('layout-woocommerce-product_colorscheme', 'theme');
	}

	if (is_cart()){
		$scheme = core_options_get('layout-woocommerce-cart_colorscheme', 'theme');
	}

	if (is_checkout()){
		$scheme = core_options_get('layout-woocommerce-checkout_colorscheme', 'theme');
	}

	if (is_account_page()){
		$scheme = core_options_get('layout-woocommerce-account_colorscheme', 'theme');
	}
	return $scheme;
}
add_filter('color_scheme', 'theme_woocommerce_colorscheme');

// Get Woocommerce custom content
function theme_woocommerce_custom_content($content){
	if( !is_woocommerce())
		return $content;

	if (is_shop()){
		$content = core_options_get('custom_content_layout-woocommerce-shop', 'theme');
	}

	if (is_product_category()){
		$content = core_options_get('custom_content_layout-woocommerce-category', 'theme');
	}

	if (is_product_tag()){
		$content = core_options_get('custom_content_layout-woocommerce-tag', 'theme');
	}

	if (is_product()){
		$content = core_options_get('custom_content_layout-woocommerce-product', 'theme');
	}

	if (is_cart()){
		$content = core_options_get('custom_content_layout-woocommerce-cart', 'theme');
	}

	if (is_checkout()){
		$content = core_options_get('custom_content_layout-woocommerce-checkout', 'theme');
	}

	if (is_account_page()){
		$content = core_options_get('custom_content_layout-woocommerce-account', 'theme');
	}
	return $content;
}
add_filter('custom_content', 'theme_woocommerce_custom_content');

// Get woocommerce custom content background
function theme_woocommerce_custom_content_bg($slogan_bg){
	if( !is_woocommerce() )
		return $slogan_bg;

	if (is_shop()){
		$slogan_bg = core_options_get('custom_content_layout-woocommerce-shop_bg', 'theme');
	}

	if (is_product_category()){
		$slogan_bg = core_options_get('custom_content_layout-woocommerce-category_bg', 'theme');
	}

	if (is_product_tag()){
		$slogan_bg = core_options_get('lcustom_content_ayout-woocommerce-tag_bg', 'theme');
	}

	if (is_product()){
		$slogan_bg = core_options_get('custom_content_layout-woocommerce-product_bg', 'theme');
	}

	if (is_cart()){
		$slogan_bg = core_options_get('custom_content_layout-woocommerce-cart_bg', 'theme');
	}

	if (is_checkout()){
		$slogan_bg = core_options_get('custom_content_layout-woocommerce-checkout_bg', 'theme');
	}

	if (is_account_page()){
		$slogan_bg = core_options_get('custom_content_layout-woocommerce-account_bg', 'theme');
	}
	return $slogan_bg;
}
add_filter('slogan_background', 'theme_woocommerce_custom_content_bg');

// Displays the WooCommerce cart, also updates the cart fragment on AJAX callbacks
//
function theme_woocommerce_cart($fragments=null) {
	global $woocommerce;

	if (is_array($fragments))
		ob_start();

	// Cart display
	echo '<div class="cart-contents">';

	//echo '<img src="', THEME_URI, '/images/icon-menu-sub.png"><span class="text">';
	echo '<i class="icon-caret-down fa fa-caret-down"></i> <span class="text">';
	echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, THEME_SLUG), $woocommerce->cart->cart_contents_count);
	echo ' - ';
	echo $woocommerce->cart->get_cart_total();
	//echo '</span><img src="', THEME_URI, '/images/woocommerce/icon-cart.png">';
	echo '</span> <i class="icon-shopping-cart fa fa-shopping-cart"></i>';


	// Output dropdown cart
	echo '<div class="theme-woocommerce-cart-dropdown">';

	// Articles
	echo '<ul class="cart_list">';
	if (sizeof($woocommerce->cart->cart_contents) > 0) {
		$i = 0;
		foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) {

			$i++;
			if ($i == 1) {
				$rowclass = ' class="cart_oddrow"';
			} else {
				$rowclass = ' class="cart_evenrow"';
				$i = 0;
			}

			$_product = $cart_item['data'];
			if ($_product->exists() && $cart_item['quantity'] > 0) {
				echo '<li' . $rowclass . '>';

				// Thumbnail
				echo '<div class="dropdowncartimage">';
				echo '<a href="' . get_permalink($cart_item['product_id']) . '">';

				if (has_post_thumbnail($cart_item['product_id']))
					echo get_the_post_thumbnail($cart_item['product_id'], 'shop_thumbnail');
				else
					echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/placeholder.png" alt="Placeholder" width="' . $woocommerce->get_image_size('shop_thumbnail_image_width') . '" height="' . $woocommerce->get_image_size('shop_thumbnail_image_height') . '">';

				echo '</a>';
				echo '</div>';

				// Product title
				echo '<div class="dropdowncartproduct">';
				echo '<a href="' . get_permalink($cart_item['product_id']) . '">';
				echo apply_filters('woocommerce_cart_widget_product_title', $_product->get_title(), $_product);

				if ($_product instanceof woocommerce_product_variation && is_array($cart_item['variation']))
					echo woocommerce_get_formatted_variation($cart_item['variation']);

				echo '</a>';
				echo '</div>';

				// Product quantity
				echo '<div class="dropdowncartquantity">';
				echo '<span class="quantity">' . $cart_item['quantity'] . ' &times; ' . woocommerce_price($_product->get_price()) . '</span>';
				echo '</div>';
				echo '<div class="clear"></div>';

				echo '</li>';
			}
		}

		// No articles present
	} else {
		echo '<li class="empty">' . __('No products in the cart.', THEME_SLUG) . '</li>';
	}
	echo '</ul>';

	// Totals
	if (sizeof($woocommerce->cart->cart_contents) > 0) {
		echo '<p class="total"><strong>';

		if (get_option('js_prices_include_tax') == 'yes')
			_e('Total', 'woothemes');
		else
			_e('Subtotal', 'woothemes');

		echo ': </strong>' . $woocommerce->cart->get_cart_total();

		echo '</p>';

		// Buttons
		do_action('woocommerce_widget_shopping_cart_before_buttons');
		echo '<div class="buttons">
			  <a href="' . $woocommerce->cart->get_cart_url() . '" class="button small">' . __('View Cart &rarr;', THEME_SLUG).'</a>
			  <a href="' . $woocommerce->cart->get_checkout_url() . '" class="button small">' . __('Checkout &rarr;', THEME_SLUG).'</a>
			  </div>';
	}

	echo '</div>';
	echo '</div>';


	// Return fragment with updated cart
	if (is_array($fragments)) {
		$fragments['div.cart-contents'] = ob_get_clean();
		return $fragments;
	}
}
add_filter('add_to_cart_fragments', 'theme_woocommerce_cart');

function theme_woocommerce_cart_colors(){
	$colors = array(
		// theme-woocommerce-cart color
		'color_menu_text' => array(
			'.theme-woocommerce-cart',
			'.theme-woocommerce-cart a.button',
			//'.cart_list a',
			'.dropdowncartproduct a'
		),

		'color_menu_text_hover' => array(
			'.cart_list a:hover',
			'.dropdowncartproduct a:hover'
		),

		// woocommerce buttons
		'color_button_text'           => array('input.button'),
		'color_button_text_hover'     => array('input.button:hover'),
	);

	$bgcolors = array(
		'color_menu_background' => array(
			'.theme-woocommerce-cart-dropdown'
		),

		'color_button' => array(
			'input.button',
		),

		'color_button_hover' => array(
			'input.button:hover',
		),
	);

	apply_colors('color', $colors);
	apply_colors('background-color', $bgcolors);
}
add_action('core_custom_css', 'theme_woocommerce_cart_colors');

// Additional WooCommerce settings
//
function theme_woocommerce_page_settings($options)
{
	$options[] = array(
		'name' => 'Column and Product Count',
		'type' => 'title',
		'desc' => 'These settings control the number of columns and products in product listings.',
		'id'   => 'column_options'
	);

	$options[] = array(
		'name' => 'Column Count',
		'desc' => 'The amount of product columns on an overview page.',
		'id' => 'core_woocommerce_column_count',
		'css' => 'min-width: 100px;',
		'std' => '3',
		'type' => 'select',
		'options' => array
		(
			'1' => '1',
			'2' => '2',
			'3' => '3',
			'4' => '4',
			'6' => '6',
			'12' => '12',
		),
	);

	$itemcount = array('-1' => 'All');
	for($i = 3; $i < 101; $i++)
		$itemcount[$i] = $i;

	$options[] = array(
		'name' => 'Product Count',
		'desc' => 'The amount of products on an overview page.',
		'id' => 'core_woocommerce_product_count',
		'css' => 'min-width: 100px;',
		'std' => '24',
		'type' => 'select',
		'options' => $itemcount,
	);

	$options[] = array(
		'name' => __('Related Items', THEME_SLUG),
		'desc' => __('The number of related items.', THEME_SLUG),
		'id' => 'core_woocommerce_related_item_count',
		'css' => 'min-width: 100px;',
		'std' => '4',
		'type' => 'select',
		'options' => array
		(
			'1' => '1',
			'2' => '2',
			'3' => '3',
			'4' => '4',
			'6' => '6',
		),
	);

	$options[] = array(
		'type' => 'sectionend',
		'id' => 'column_options'
	);

	return $options;
}
add_filter('woocommerce_product_settings', 'theme_woocommerce_page_settings', 5);

// Text replacements
function theme_woocommerce_addcart_button_text() {
	return __('Add to cart', THEME_SLUG);
}
add_filter('single_add_to_cart_text', 'theme_woocommerce_addcart_button_text');

function theme_woocommerce_viewcart_button_text() {
	return __('View cart', THEME_SLUG);
}
add_filter('single_view_cart_text', 'theme_woocommerce_viewcart_button_text');

// Disable WooCommerce Add to Cart buttons in the product loop
function woocommerce_template_loop_add_to_cart() {
}

// Move sorting form to top
remove_action('woocommerce_pagination', 'woocommerce_catalog_ordering', 20);

// Disable WooCommerce breadcrumbs
remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);

// Move sorting form to top
//remove_action('woocommerce_pagination', 'woocommerce_catalog_ordering', 20);
//add_action('woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 20);

// Number of columns on a product page
function theme_woocommerce_column_count() {
	return get_option('core_woocommerce_column_count');
}
add_filter('loop_shop_columns', 'theme_woocommerce_column_count');

// Number of items to loop through on a product page
function theme_woocommerce_loop_count() {
	return get_option('core_woocommerce_product_count');
}
add_filter('loop_shop_per_page', 'theme_woocommerce_loop_count');

// Redefine woocommerce_output_related_products()
function woocommerce_output_related_products() {
	$columns = get_option('core_woocommerce_related_item_count');
	woocommerce_related_products($columns,$columns); // Display related products columns
}

function woocommerce_related_products_limit() {
  global $product, $woocommerce_loop;
  $columns = get_option('core_woocommerce_related_item_count');
	$args['posts_per_page'] = $columns;
	return $args;
}
//add_filter( 'woocommerce_related_products_args', 'woocommerce_related_products_limit' );

// Redefine woocommerce_output_upsells()
function woocommerce_output_upsells() {
	$columns = get_option('core_woocommerce_related_item_count');
	woocommerce_upsell_display($columns,$columns); // Display up sell products columns
}

// Redefine woocommerce_output_cross_sells()
function woocommerce_output_cross_sells() {
	$columns = get_option('core_woocommerce_related_item_count');
	woocommerce_cross_sell_display($columns,$columns); // Display up sell products columns
}

// Add custom post class to product items
function theme_woocommerce_post_class($classes){
	global $woocommerce_loop;
	if ( is_woocommerce() ) {
		$columns = $woocommerce_loop['columns'];
		$classes[] = core_theme_get_column($columns);
	}

	return $classes;
}
add_filter('post_class', 'theme_woocommerce_post_class');


/**
 * Sensei plugin support
 */
add_action( 'after_setup_theme', 'theme_woo_sensei_support' );
function theme_woo_sensei_support() {

	if ( ! function_exists( 'is_sensei' ) ) {
		return;
	}

    add_theme_support( 'sensei' );
}

add_action( 'wp', 'theme_woo_sensei_init' );
function theme_woo_sensei_init(){

	if ( ! function_exists( 'is_sensei' ) ) {
		return;
	}

	global $woothemes_sensei;
	
	remove_action( 'sensei_before_main_content', array( $woothemes_sensei->frontend, 'sensei_output_content_wrapper' ), 10 );
	remove_action( 'sensei_after_main_content', array( $woothemes_sensei->frontend, 'sensei_output_content_wrapper_end' ), 10 );

	// remove default sensei titles
	//remove_action( 'sensei_course_single_title', array( $woothemes_sensei->frontend , 'sensei_single_title' ), 10 );
	//remove_action( 'sensei_lesson_single_title', array( $woothemes_sensei->frontend , 'sensei_single_title' ), 10 );
	//remove_action( 'sensei_quiz_single_title', array( $woothemes_sensei->frontend, 'sensei_single_title' ), 10 );
	//remove_action( 'sensei_message_single_title', array( $woothemes_sensei->frontend, 'sensei_single_title' ), 10 );

	// add custom  body  wrappers
	//add_action('sensei_before_main_content', 'theme_woo_sensei_wrapper_start', 10);
	//add_action('sensei_after_main_content', 'theme_woo_sensei_wrapper_end', 10);

}

function theme_woo_sensei_wrapper_start() {
    echo '<div id="content-sensei" class="sensei content-area">';
}

function theme_woo_sensei_wrapper_end() {
    echo '</div><!-- #content-sensei -->'; ?>
	<?php core_theme_hook_after_content(); ?>

    </div><!-- ends here #wrapper -->
    <?php //core_theme_hook_after_wrapper(); ?>

<!-- </div>ends here #container -->
<?php //core_theme_hook_after_container(); ?>


	<?php //core_theme_hook_before_footer(); ?>
<?php 
}


function theme_woo_sensei_set_layout($layouts){
	if ( ! function_exists( 'is_sensei' ) ) {
		return $layouts;
	}

	$layouts = array_merge( $layouts, array( 'layout-sensei' => __('Sensei', THEME_SLUG) ) );

	return $layouts;
}
add_filter( 'custom_layouts', 'theme_woo_sensei_set_layout' );


function theme_woo_sensei_layout($layouts){
	if ( function_exists( 'is_sensei' ) && is_sensei() ) {
		return core_options_get('layout-sensei');
	}

	return $layouts;
}
add_filter( 'core_layout', 'theme_woo_sensei_layout', 9999 );




