<?php
/*
* Plugin-Name: Product Designer App
* Plugin-URI: https://productdesignerapp.com
* Author: productdesignerapp
* Front Functions
*/

if (!defined('ABSPATH'))
{
    exit;
}

/*
* Active shortcode studio page
*/
add_shortcode('pdapp-studio-page', 'pdapp_studio_page_active');
if(!function_exists('pdapp_studio_page_active'))
{
	function pdapp_studio_page_active($atts)
	{
		global $current_user;
		if(is_admin() || pdapp_get('_locale') != '')
		{
			return false;
		}
		
		$pdappProductID = (int) pdapp_get('product_id');
		if($pdappProductID == 0 && isset($atts['id']))
		{
			$pdappProductID = (int) $atts['id'];
		}
		
		if($pdappProductID == 0)
		{
			global $wpdb;
			$row = $wpdb->get_row($wpdb->prepare("SELECT * FROM %i", PDAPP_DB_WOO_PRODUCTS), ARRAY_A);
			if(!isset($row['pid']))
			{
				return "<h4>". pdapp_lang('product_design_not_found') ."</h4>";
			}

			$pdappProductID = $row['pid'];
		}

		$product = wc_get_product($pdappProductID);
		if(!is_object($product))
		{
			return "<h4>". pdapp_lang('product_design_not_found') ."</h4>";
		}
		
		$pdappLangDefault = pdapp_lang_code();
		$pdappToken = pdapp_create_token();
		$pdappNonce = wp_create_nonce(PDAPP_FRONT_NONCE);
		$pdappBackUrl = get_permalink($pdappProductID);
		$pdappCustomerMail = isset($current_user->user_email) ? $current_user->user_email : '';
		
		$pdappPrice = $product->get_price();

		$pdapphtml = "<h4>". pdapp_lang('active_theme_plugin_msg') ."</h4>";
		$pdapptheme = pdapp_get_theme();
		$pdappthemePath = PDAPP_PLUGIN_DIR .'templates/'. $pdapptheme .'/index.php';

		if(file_exists($pdappthemePath))
		{
			ob_start();
			require_once($pdappthemePath);
			$pdapphtml = ob_get_contents();
			ob_end_clean();
		}

		return $pdapphtml;
	}
}

/*
* Button design on page product detail
*/
add_action('woocommerce_after_shop_loop_item', 'pdapp_customize_button', 10);
add_action('woocommerce_after_add_to_cart_quantity', 'pdapp_customize_button', 30);
if(!function_exists('pdapp_customize_button'))
{
	function pdapp_customize_button()
	{
		$productID = get_the_ID();

		$productDesign = pdapp_woo_get_product(["pid"=>$productID]);
		if(isset($productDesign['did']))
		{
			$designID = $productDesign['did'];
			$product = pdapp_get_product($designID);
			
			if(isset($product['id']))
			{
				$pageID = pdapp_get_page_studio();
				if($pageID != '')
				{
					if(function_exists('pll_get_post'))
					{
						$pageID = pll_get_post($pageID);
					}
					$pdappLink = get_permalink($pageID);
					$pdappLink = add_query_arg( ['product_id' => $productID], $pdappLink );
					echo '<a class="button pdapp-customize-product-btn" onclick="return pdappLoadCustomizeProduct(this);" href="'. esc_url($pdappLink) .'">'. esc_html(pdapp_lang('btn_customize')) .'</a>';
					
					if(isset($product['advanced']) && isset($product['advanced']['cartdesign']) && $product['advanced']['cartdesign'] != 1)
					{
						$pdappStype = '#product-'. esc_html($productID) .' .cart .single_add_to_cart_button, .product-'. esc_html($productID) .' .cart .single_add_to_cart_button, .product-'. esc_html($productID) .' .cart .quantity, #product-'. esc_html($productID) .' .cart .quantity, #product-'. esc_html($productID) .' .add_to_cart_button, #post-'. esc_html($productID) .' .cart .single_add_to_cart_button, #post-'. esc_html($productID) .' .cart .quantity, .post-'. esc_html($productID) .' .cart .single_add_to_cart_button, .post-'. esc_html($productID) .' .cart .quantity, .post-'. esc_html($productID) .' .add_to_cart_button{display: none !important;}';
						wp_enqueue_style('pdapp-style-button', PDAPP_PLUGIN_URL . 'assets/css/pdapp-inline.css', array(), PDAPP_VERSION.'-'. time(), 'all');
						wp_add_inline_style('pdapp-style-button', $pdappStype);
					}
				}
			}
		}
	}
}
?>