<?php
//This code will add standart WPcolorpicker to woocommerce categories
// Add term page
function stm_theme_taxonomy_add_new_meta_field() {
	$counter = 0;
	$fa_icons = stm_get_cat_icons('new_fa');
	$stm_custom_icons_type_1 = stm_get_cat_icons('type_1');
	// this will add the custom meta field to the add new term page
	?>
	<div class="form-field">
		<label for="term_meta[custom_term_meta]"><?php _e( 'Category Background Color', 'masterstudy' ); ?></label>
		<input type="text" class="stm_theme_admin_cat_colorpicker" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
		<p class="description"><?php _e( 'Enter a value for this field','masterstudy' ); ?></p>
		<label for="term_meta[custom_term_font]"><?php _e( 'Category Background font', 'masterstudy' ); ?></label>
		<input type="hidden" class="stm_theme_admin_cat_icon" name="term_meta[custom_term_font]" id="term_meta[custom_term_font]" value="">
		<div class="stm_theme_cat_chosen_icon_preview"></div>
		<div class="stm_theme_font_pack_holder">
			<button type="button" class="stm_theme_choose_fa_icons button"><?php _e( 'Choose icons from Font Awesome Pack', 'masterstudy' ); ?></button>
			<table class="form-table stm_theme_icon_font_table">
				<tr>
					<?php foreach($fa_icons as $fa_icon): $counter++; ?>
						<td>
							<i class="fa fa-<?php echo esc_attr($fa_icon); ?>" data-value="<?php echo esc_attr($fa_icon); ?>"></i>
						</td>
						<?php if($counter%15 == 0): ?>
							</tr>
							<tr>
						<?php endif; ?>
					<?php endforeach; ?>
				</tr>
			</table>
		</div>
		<div class="stm_theme_font_pack_holder">
			<button type="button" class="stm_theme_choose_custom_icons button"><?php _e( 'Choose from Master Study Icons Pack', 'masterstudy' ); ?></button>
			<table class="form-table stm_theme_icon_font_table">
				<tr>
					<?php $counter = 0; ?>
					<?php foreach($stm_custom_icons_type_1 as $stm_custom_icon_type_1): $counter++; ?>
						<td>
							<i class="fa-<?php echo esc_attr($stm_custom_icon_type_1); ?>" data-value="fa-<?php echo esc_attr($stm_custom_icon_type_1); ?>"></i>
						</td>
						<?php if($counter%15 == 0): ?>
							</tr>
							<tr>
						<?php endif; ?>
					<?php endforeach; ?>
				</tr>
			</table>
		</div>
		<p class="description"><?php _e( 'Click on button, then choose icon, which will be held for this category','masterstudy' ); ?></p>
		<p class="description"><?php _e( 'If none of this icons suits you, upload thumbnail (130*120)','masterstudy' ); ?></p>
	</div>
	<script type="text/javascript">
		jQuery(function($) {
		    $(function() {
		        $(".stm_theme_admin_cat_colorpicker").wpColorPicker();
		        
		        $('.stm_theme_font_pack_holder .button').click(function(){
					$(this).closest('.stm_theme_font_pack_holder').find('.stm_theme_icon_font_table').toggleClass('visible');
				});
				
				$('.stm_theme_icon_font_table i').click(function(){
					$('.stm_theme_icon_font_table i').closest('td').removeClass('cat_icon_chosen');
					$(this).closest('td').addClass('cat_icon_chosen');
					var chosen_icon_cat_stm_theme = $(this).attr('data-value');
					$('.stm_theme_admin_cat_icon').val(chosen_icon_cat_stm_theme);
					var cat_chosen_icon_preview = $(this).closest('td').html();
					$('.stm_theme_cat_chosen_icon_preview').html(cat_chosen_icon_preview);
				});
				
				var stm_theme_cat_current_icon = $('.stm_theme_admin_cat_icon').val();
				if(stm_theme_cat_current_icon != '') {
					$('.stm_theme_icon_font_table i[data-value='+stm_theme_cat_current_icon+']').closest('td').addClass('cat_icon_chosen');	
				}
		    });
		});
	</script>
<?php
}

add_action( 'product_cat_add_form_fields', 'stm_theme_taxonomy_add_new_meta_field', 10, 2 );

// Edit term page
function stm_theme_taxonomy_edit_meta_field($term) {
 	$counter = 0;
	$fa_icons = stm_get_cat_icons('new_fa');
	$stm_custom_icons_type_1 = stm_get_cat_icons('type_1');
	// put the term ID into a variable
	$t_id = $term->term_id;
 
	// retrieve the existing value(s) for this meta field. This returns an array
	$term_meta = get_option( "taxonomy_$t_id" ); ?>
	<tr class="form-field">
		<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Category Background Color', 'masterstudy' ); ?></label></th>
		<td>
			<input type="text" class="stm_theme_admin_cat_colorpicker" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
			<p class="description"><?php _e( 'Enter a value for this field','masterstudy' ); ?></p>
		</td>
	</tr>
	<tr class="form-field">
		<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Category Background Color', 'masterstudy' ); ?></label></th>
		<td>
			<input type="hidden" class="stm_theme_admin_cat_icon" name="term_meta[custom_term_font]" id="term_meta[custom_term_font]" value="<?php echo esc_attr( $term_meta['custom_term_font'] ) ? esc_attr( $term_meta['custom_term_font'] ) : ''; ?>">
			<div class="stm_theme_cat_chosen_icon_preview"></div>
			<div class="stm_theme_font_pack_holder">
				<button type="button" class="stm_theme_choose_fa_icons button"><?php _e( 'Choose icons from Font Awesome Pack', 'masterstudy' ); ?></button>
				<table class="form-table stm_theme_icon_font_table">
					<tr>
						<?php foreach($fa_icons as $fa_icon => $icon): $counter++; ?>
							<td>
                                <?php
                                $icon_value = array_keys($icon);
                                $icon_value = $icon_value[0];
                                ?>
								<i class="<?php echo esc_attr($icon_value); ?>" data-value="<?php echo esc_attr($icon_value); ?>" data-search="<?php echo esc_attr($icon_value); ?>"></i>
							</td>
							<?php if($counter%15 == 0): ?>
								</tr>
								<tr>
							<?php endif; ?>
						<?php endforeach; ?>
					</tr>
				</table>
			</div>
			<div class="stm_theme_font_pack_holder">
				<button type="button" class="stm_theme_choose_custom_icons button"><?php _e( 'Choose from Master Study Icons Pack', 'masterstudy' ); ?></button>
				<table class="form-table stm_theme_icon_font_table">
					<tr>
						<?php $counter = 0; ?>
						<?php foreach($stm_custom_icons_type_1 as $stm_custom_icon_type_1): $counter++; ?>
							<td>
								<i class="fa-<?php echo esc_attr($stm_custom_icon_type_1); ?>" data-value="fa-<?php echo esc_attr($stm_custom_icon_type_1); ?>"></i>
							</td>
							<?php if($counter%15 == 0): ?>
								</tr>
								<tr>
							<?php endif; ?>
						<?php endforeach; ?>
					</tr>
				</table>
			</div>
			<button type="button" class="button stm_theme_remove_cat_icon"><?php _e( 'Remove icon', 'masterstudy' ); ?></button>
		</td>
	</tr>
	<script type="text/javascript">
		jQuery(function($) {
		    $(function() {
		        $(".stm_theme_admin_cat_colorpicker").wpColorPicker();
		        
		        $('.stm_theme_font_pack_holder .button').click(function(){
					$(this).closest('.stm_theme_font_pack_holder').find('.stm_theme_icon_font_table').toggleClass('visible');
				});
				
				$('.stm_theme_icon_font_table i').click(function(){
					$('.stm_theme_icon_font_table i').closest('td').removeClass('cat_icon_chosen');
					$(this).closest('td').addClass('cat_icon_chosen');
					var chosen_icon_cat_stm_theme = $(this).attr('data-value');
					$('.stm_theme_admin_cat_icon').val(chosen_icon_cat_stm_theme);
					var cat_chosen_icon_preview = $(this).closest('td').html();
					$('.stm_theme_cat_chosen_icon_preview').html(cat_chosen_icon_preview);
				});
				
				var stm_theme_cat_current_icon = $('.stm_theme_admin_cat_icon').val();
				
				if(stm_theme_cat_current_icon != '') {
					$('.stm_theme_icon_font_table i[data-value='+stm_theme_cat_current_icon+']').closest('td').addClass('cat_icon_chosen');
					var cat_chosen_icon_preview = $('.cat_icon_chosen').closest('td').html();
					$('.stm_theme_cat_chosen_icon_preview').html(cat_chosen_icon_preview);
				}
				
				$('.stm_theme_remove_cat_icon').click(function(){
					$('.stm_theme_admin_cat_icon').val("");
					$('.stm_theme_cat_chosen_icon_preview').empty();
				})
		    });

		});
	</script>
<?php
}
add_action( 'product_cat_edit_form_fields', 'stm_theme_taxonomy_edit_meta_field', 10, 2 );

// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
	if ( isset( $_POST['term_meta'] ) ) {
		$t_id = $term_id;
		$term_meta = get_option( "taxonomy_$t_id" );
		$cat_keys = array_keys( $_POST['term_meta'] );
		foreach ( $cat_keys as $key ) {
			if ( isset ( $_POST['term_meta'][$key] ) ) {
				$term_meta[$key] = $_POST['term_meta'][$key];
			}
		}
		// Save the option array.
		update_option( "taxonomy_$t_id", $term_meta );
	}
}  
add_action( 'edited_category', 'save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_category', 'save_taxonomy_custom_meta', 10, 2 );

add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );

// Declare Woo support
add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
    add_theme_support( 'woocommerce' );
}


// Remove woo styles
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );

//Change the breadcrumb separator
add_filter( 'woocommerce_breadcrumb_defaults', 'jk_change_breadcrumb_delimiter' );
function jk_change_breadcrumb_delimiter( $defaults ) {
	// Change the breadcrumb delimeter from '/' to '>'
	$defaults['delimiter'] = '<i class="fa fa-chevron-right"></i>';
	return $defaults;
}

add_action('woocommerce_before_main_content', 'stm_woo_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'stm_woo_theme_wrapper_end', 10);

function stm_woo_theme_wrapper_start() {
  echo '';
}

function stm_woo_theme_wrapper_end() {
  echo '';
}


// Remove product count
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );


remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );


remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );


// Change out of stock text
add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);

// Our hooked in function $availablity is passed via the filter!
function custom_get_availability( $availability, $_product ) {
	if ( !$_product->is_in_stock() ) $availability['availability'] = __('No available seats', 'woocommerce');
return $availability;
}

// Change single button text add to cart
// Change single button text add to cart
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );    // 2.1 +

function woo_custom_cart_button_text() {
	return __( 'Enroll this Course', 'masterstudy' );
}


// Display 9 products per page. Goes in functions.php
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 9;' ), 20 );

add_action( 'after_setup_theme', 'stm_woo_setup' );
function stm_woo_setup() {
	add_theme_support( 'wc-product-gallery-zoom' );
	add_theme_support( 'wc-product-gallery-lightbox' );
	add_theme_support( 'wc-product-gallery-slider' );
}

add_filter('woocommerce_gallery_thumbnail_size', 'stm_woocommerce_gallery_thumbnail_size', 100, 1);

function stm_woocommerce_gallery_thumbnail_size($dims) {
    return array(160, 80);
}