<?php
/*
* Plugin Name: DH Woocommerce Product Labels
* Plugin URI: http://teenvl.net/
* Description: Add visually-appealing labels to any product images. 
* Version: 1.0.2
* Author: DH Zoanku
* Author URI: http://teenvl.net/
* License: License GNU General Public License version 2 or later;
* Copyright 2013  DH Zoanku
*/
if (!function_exists('dhwc_is_active')){
	/**
	 * Check woocommerce plugin is active
	 *
	 * @return boolean .TRUE is active
	 */
	function dhwc_is_active(){
		$plugin = 'woocommerce/woocommerce.php';

		if (in_array( $plugin, (array) get_option( 'active_plugins', array() ) ))
			return true;
			
		if ( !is_multisite() )
			return false;

		$plugins = get_site_option( 'active_sitewide_plugins');
		if ( isset($plugins[$plugin]) )
			return true;

		return false;
	}
}

if (dhwc_is_active()) {
	
	/**
	 * dhwcpl_init function
	 */
	function dhwcpl_init(){
		load_plugin_textdomain( 'dhwcpl', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
	}
	add_action( 'init', 'dhwcpl_init' );
	
	/**
	 * dhwcpl_init_taxonomy function
	 */
	function dhwcpl_init_taxonomy(){
	
		register_taxonomy('product_label',
		apply_filters('dhwc_taxonomy_objects_product_label', array('product') ),
			apply_filters('dhwc_taxonomy_args_product_label', array(
				'hierarchical'=>false,
				'update_count_callback'=> '_update_post_term_count',
				'label'=> __('Product Labels','dhwcpl'),
				'labels' => array(
				'name' 				=> __('Product Labels','dhwcpl'),
				'singular_name' 	=> __('Product Label','dhwcpl'),
				'menu_name'			=> _x( 'Labels', 'Admin menu name', 'dhwcpl' ),
				'search_items' 		=> __('Search Product Labels','dhwcpl'),
				'all_items' 		=> __('All Product Labels','dhwcpl'),
				'parent_item' 		=> __('Parent Product Label','dhwcpl'),
				'parent_item_colon' => __('Parent Product Label:','dhwcpl'),
				'edit_item' 		=> __('Edit Product Label','dhwcpl'),
				'update_item' 		=> __('Update Product Label','dhwcpl'),
				'add_new_item' 		=> __('Add New Product Label','dhwcpl'),
				'new_item_name' 	=> __('New Product Label Name','dhwcpl')
			),
			'show_ui'=> true,
			'show_in_nav_menus'=> true,
			'query_var'=> is_admin(),
			'capabilities'=> array(
				'manage_terms'=> 'manage_product_terms',
				'edit_terms'=> 'edit_product_terms',
				'delete_terms'=> 'delete_product_terms',
				'assign_terms'=> 'assign_product_terms'
					),
					'rewrite' =>true
			) )
		);
	}
	add_action('woocommerce_register_taxonomy', 'dhwcpl_init_taxonomy');
	
	/**
	 * dhwcpl_get_display function
	 */
	function dhwcpl_get_display(){
		return apply_filters('dhwcpl_display',array(
			'all'=>__('All','dhwcpl'),
			'loop'=>__('Loop Product','dhwcpl'),
			'single'=>__('Single Product','dhwcpl')
		));
	}
	/**
	 * dhwcpl_get_format function
	 */
	function dhwcpl_get_format(){
		return apply_filters('dhwcpl_format',array(
				'image'=>__('Image','dhwcpl'),
				'text'=>__('Text','dhwcpl'),
				'custom'=>__('Custom','dhwcpl')
		));
	}
	
	/**
	 * dhwcpl_get_image_url function
	 */
	function dhwcpl_get_image_url($label_id,$size = 'full'){
		$image_id = get_woocommerce_term_meta($label_id, 'image_id', true);
		if ($image_id)
			return current(wp_get_attachment_image_src($image_id, $size));
		return false;
	}
	
	if (is_admin()){
		include_once dirname(__FILE__).'/includes/dhwc-product-labels-admin.php';
	}else{
		/**
		 * dhwcpl_print_styles function
		 */
		function dhwcpl_print_styles(){
			wp_enqueue_style('dhwcpl',WP_PLUGIN_URL.'/'.dirname(plugin_basename(__FILE__)).'/assets/css/style.css');
		}
		
		add_action('wp_print_styles', 'dhwcpl_print_styles');
	}

	/**
	 * dhwcpl_hide_sale_flash function
	 */
	function dhwcpl_hide_sale_flash($content){
		return false;
	}
	
	add_filter('woocommerce_sale_flash','dhwcpl_hide_sale_flash',100);
	
	/**
	 * dhwcpl_product_label function
	 */
	function dhwcpl_product_label($product_label){
		$html = "";
		$style = 'style="';
		$format = get_woocommerce_term_meta($product_label->term_id,'format',true);
			
		$top = get_woocommerce_term_meta($product_label->term_id, 'top', true);
		if ($top != '') {
			$style .="top:".$top.';';
		}
		$right = get_woocommerce_term_meta($product_label->term_id, 'right', true);
		if ($right != '') {
			$style .='right:'.$right.';';
		}
		$bottom = get_woocommerce_term_meta($product_label->term_id, 'bottom', true);
		if ($bottom != '' ) {
			$style .= 'bottom:'.$bottom.';';
		}
		$left = get_woocommerce_term_meta($product_label->term_id, 'left', true);
		if ($left != '' ) {
			$style .='left:'.$left.';';
		}
			
		$image_tag = '';
		if ($image = dhwcpl_get_image_url($product_label->term_id)) {
			$image_tag = '<img src="'.$image.'" alt="'.$product_label->name.'"/>';
		}
			
		$label = '';
		if ($format == 'text') {
			$label .= '<span class="label">';
			$label .= esc_html__($product_label->name);
			$label .= '</span>';
		}elseif ($format == 'custom'){
			$label .= wpautop(get_woocommerce_term_meta($product_label->term_id, 'product_label_content', true));
		}
		$style .='"';
			
		$html .='<span class="product-label label-'.$product_label->term_id.'" '.$style.'>';
		$html .= $label.$image_tag;
		$html .= '</span>';
		
		return $html;
	}
	/**
	 * dhwcpl_shop_loop_item function
	 */
	function dhwcpl_shop_loop_item(){
		global $product;
		$upload_dir = wp_upload_dir();
		$product_labels_ids = get_post_meta( $product->id, 'product_labels', true );
		$product_labels = array();
		$html = "";
		if ($product_labels_ids) {
			$product_labels = (array) get_terms('product_label', array( 'hide_empty' => 0, 'orderby' => 'id','include'=>$product_labels_ids));
			if (is_array($product_labels) && count($product_labels) > 0) {
				foreach ($product_labels as $product_label){
					$display = get_woocommerce_term_meta($product_label->term_id,'display',true);
					
					if ($display == 'single')
						continue;
					
					$html .= dhwcpl_product_label($product_label);
				}
			}
		}
		echo $html;
	}
	
	/**
	 * dhwcpl_single_product function
	 */
	function dhwcpl_single_product(){
		global $product;
		$upload_dir = wp_upload_dir();
		$product_labels_ids = get_post_meta( $product->id, 'product_labels', true );
		$product_labels = array();
		$html = "";
		if ($product_labels_ids) {
			$product_labels = (array) get_terms('product_label', array( 'hide_empty' => 0, 'orderby' => 'id','include'=>$product_labels_ids));
			if (is_array($product_labels) && count($product_labels) > 0) {
				foreach ($product_labels as $product_label){
					$display = get_woocommerce_term_meta($product_label->term_id,'display',true);
						
					if ($display == 'loop')
						continue;
					$html .= dhwcpl_product_label($product_label);
				}
			}
		}
		echo  $html;
	}
	
	add_action( 'woocommerce_before_shop_loop_item_title', 'dhwcpl_shop_loop_item', 50 );
	add_action( 'woocommerce_product_thumbnails', 'dhwcpl_single_product',50 );
}
