<?php
/*
Based on: WooCommerce New Product Badge 
by: jameskoster
*/

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

/**
 * Check if WooCommerce is active
 **/
if ( function_exists( 'WC' ) ) {

	/**
	 * New Badge class
	 **/
	if ( ! class_exists( 'SHRK_WC_nb' ) ) {

		class SHRK_WC_nb {

			public function __construct() {
				add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'show_product_loop_new_badge' ), 30 ); 	// The new badge function
				add_filter( 'redux/options/shrk_theme_options/sections', array( $this, 'add_options' ) );
			}

	        /*-----------------------------------------------------------------------------------*/
			/* Class Functions */
			/*-----------------------------------------------------------------------------------*/

			/**
			 * Add shrk_searchwp_live_search options
			 * @param  array $sections - redux sections
			 * @return array modified sections
			 */
			function add_options( $sections ) {

				$fields = array(
						array(
							'id'      => 'woo_newbadge_enable',
							'on'      => esc_html__( 'Yes', 'ux-shop' ),
							'off'     => esc_html__( 'No', 'ux-shop' ),
							'type'    => 'switch',
							'title'   => esc_html__( 'Enable New product badge', 'ux-shop' ),
							'desc'    => esc_html__( 'Set to yes to show new product badges for the amount of days set below, or for products explicitly set as new through their meta options.', 'ux-shop' ),
							'default' => 0,
							),
						array(
							'id'       => 'woo_newbadge_newness',
							'type'     => 'text',
							'title'    => esc_html__( 'Product Newness', 'ux-shop'),
							'desc'     => esc_html__( 'Display the \'New\' flash for how many days?', 'ux-shop'),
							'validate' => 'numeric',
							'msg'      => esc_html__( 'Enter a numeric value for the number of days.', 'ux-shop'),
							'default'  => '30',
							'required' => array('woo_newbadge_enable', '=', 1),
						),
					);

				if ( class_exists( 'WC_nb' ) ) {
					$fields[] = array(
						'id'       => 'notice_wcnb_conflict',
						'type'     => 'info',
						'notice'   => true,
						'style'    => 'warning',
						'title'    => esc_html__('Plugin conflict', 'ux-shop'),
						'desc'     => esc_html__('The plugin \'WooCommerce New Product Badge\' is detected. It is suggested you deactivate it to avoid duplicate markup.', 'ux-shop'),
						'required' => array('woo_newbadge_enable', '=', 1),
					);
				}

				$sections[] = array(
					'title' => esc_html__( 'New product badge', 'ux-shop' ),
					'icon' => 'el-icon-tag',
					'fields' => $fields
				);
				return $sections;
			}

			/*-----------------------------------------------------------------------------------*/
			/* Frontend Functions */
			/*-----------------------------------------------------------------------------------*/

			// Display the new badge
			function show_product_loop_new_badge() {
				$enabled = shrk_get_theme_option( 'woo_newbadge_enable' );

				if ( !$enabled ) {
					return;
				}

				$postdate      = get_the_time( 'Y-m-d' );			// Post date
				$postdatestamp = strtotime( $postdate );			// Timestamped post date
				$newness       = shrk_get_theme_option( 'woo_newbadge_newness' ); // Newness in days as defined by option
				$override      = shrk_get_meta_option( 'woo_newbadge_enable' );

				if ( $override || ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp ) ) { // If the product was published within the newness time frame display the new badge
					echo '<span class="wc-new-badge">' . __( 'New', 'ux-shop' ) . '</span>';
				}
			}
		}

		new SHRK_WC_nb();
	}
}
