<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       www.redefiningtheweb.com
 * @since      1.0.0
 *
 * @package    Rtwwpcp_Woocommerce_Pdf_Catalog_Pro
 * @subpackage Rtwwpcp_Woocommerce_Pdf_Catalog_Pro/admin
 */
/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Rtwwpcp_Woocommerce_Pdf_Catalog_Pro
 * @subpackage Rtwwpcp_Woocommerce_Pdf_Catalog_Pro/admin
 * @author     RedefiningTheWeb <developer@redefiningtheweb.com>
 */
class Rtwwpcp_Woocommerce_Pdf_Catalog_Pro_Admin {
	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $rtwwpcp_plugin_name    The ID of this plugin.
	 */
	private $rtwwpcp_plugin_name;
	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $rtwwpcp_version    The current version of this plugin.
	 */
	private $rtwwpcp_version;
	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $rtwwpcp_plugin_name       The name of this plugin.
	 * @param      string    $rtwwpcp_version    The version of this plugin.
	 */
	public function __construct( $rtwwpcp_plugin_name, $rtwwpcp_version ) {
		$this->rtwwpcp_plugin_name = $rtwwpcp_plugin_name;
		$this->rtwwpcp_version = $rtwwpcp_version;
	}
	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function rtwwpcp_enqueue_styles() {
		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the rtwwpcp_run() function
		 * defined in Woocommerce_Pdf_Catalog_Pro_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Woocommerce_Pdf_Catalog_Pro_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */
		//
		$rtwwpcp_screen = get_current_screen();
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_style( $this->rtwwpcp_plugin_name, plugin_dir_url( __FILE__ ) . 'css/rtwwpcp-woocommerce-pdf-catalog-pro-admin.css', array(), $this->rtwwpcp_version, 'all' );
		wp_enqueue_style( "select2", plugins_url( 'woocommerce/assets/css/select2.css' ), array(), $this->rtwwpcp_version, 'all' );
	}
	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function rtwwpcp_enqueue_scripts() {
		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the rtwwpcp_run() function
		 * defined in Woocommerce_Pdf_Catalog_Pro_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Woocommerce_Pdf_Catalog_Pro_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */
		$rtwwpcp_screen = get_current_screen();
		
		if($rtwwpcp_screen->id == 'rtw-catalog_page_rtwwpcp_pdf_settings' || $rtwwpcp_screen->id == 'rtw-catalog_page_rtwwpcp_flipbook_settings' || $rtwwpcp_screen->id == 'product' ){
			wp_enqueue_script('media-upload');
			wp_enqueue_script('thickbox');
			wp_enqueue_style('thickbox');
			wp_enqueue_script( 'wp-color-picker');
			wp_enqueue_script( "select2", plugins_url( 'woocommerce/assets/js/select2/select2.full.min.js' ), array( 'jquery' ), $this->rtwwpcp_version, false );
			wp_register_script( $this->rtwwpcp_plugin_name, plugin_dir_url( __FILE__ ) . 'js/rtwwpcp-woocommerce-pdf-catalog-pro-admin.js', array(), $this->rtwwpcp_version, false );
			$rtwwpcp_ajax_nonce 		= wp_create_nonce( "rtwwpcp-ajax-security-string" );
			$rtwwpcp_translation_array 	= array(
										'rtwwpcp_ajaxurl' 		=> esc_url(admin_url( 'admin-ajax.php' )),
										'rtwwpcp_nonce' 		=> $rtwwpcp_ajax_nonce,
									);
			wp_localize_script( $this->rtwwpcp_plugin_name, 'rtwwpcp_global_param',$rtwwpcp_translation_array );
			wp_enqueue_script( $this->rtwwpcp_plugin_name );
			wp_enqueue_media();
	    }
	}
	/**
	 * Add Custom Menu In admin area.
	 *
	 * @since    1.0.0
	 */
	public function rtwwpcp_add_menu_page()
	{
		add_menu_page( esc_html__('RTW PDF Catalog', 'rtwwpcp-woocommerce-pdf-flipbook' ), esc_html__('RTW Catalog', 'rtwwpcp-woocommerce-pdf-flipbook'),'manage_options','rtwwpcp_settings',array($this,'rtwwpcp_add_menu_page_html'), RTWWPCP_URL.'assets/icon.png');
		add_submenu_page( 'rtwwpcp_settings', 'PDF Catalog settings', esc_html__('PDF Catalog Settings', 'rtwwpcp-woocommerce-pdf-flipbook'), 'manage_options', 'rtwwpcp_pdf_settings', array($this,'rtwwpcp_pdf_settings_page_html') );
		remove_submenu_page('rtwwpcp_settings','rtwwpcp_settings');
	}
	/**
	 * Render the html for custom submenu.
	 *
	 * @since    1.0.0
	 */
	public function rtwwpcp_pdf_settings_page_html(){
		include(RTWWPCP_DIR.'admin/partials/rtwwpcp-woocommerce-pdf-catalog-pro-admin-display.php');
	}
	/**
	 * Save admin settings.
	 *
	 * @since    1.0.0
	 */
	public function rtwwpcp_save_admin_setting()
	{
		if( isset( $_POST['rtwwpcp_pdf_submit'] ) )
		{
			$rtwwpcp_pdf_dir = RTWWPCP_PDF_DIR;
			array_map('unlink', glob("$rtwwpcp_pdf_dir/*.*"));
		}
		register_setting('rtwwpcp_header_setting','rtwwpcp_header_setting_opt');
		register_setting('rtwwpcp_footer_setting','rtwwpcp_footer_setting_opt');
		register_setting('rtwwpcp_basic_setting','rtwwpcp_basic_setting_opt');
		register_setting('rtwwpcp_css_setting','rtwwpcp_css_setting_opt');
		register_setting('rtwwpcp_watermark_setting','rtwwpcp_watermark_setting_opt');
		register_setting('rtwwpcp_format_setting','rtwwpcp_format_setting_opt');
	}
	/**
	 * Adding Meta container admin shop_order pages.
	 *
	 * @since    2.0.0
	 */ 
	public function rtwwpcp_add_meta_box( $rtwwpcp_post_id, $rtwwpcp_param )
	{
		add_meta_box( 'download_pdf', esc_html__('Download Product PDF', 'rtwwpcp-woocommerce-pdf-flipbook'), array($this, 'rtwwpcp_generate_pdf_metabox_callback'), 'product','side');
	}
	/**
	 * Adding Meta field in the meta container for PDF button in edit product pages.
	 *
	 * @since    2.0.0
	 */
	public function rtwwpcp_generate_pdf_metabox_callback($rtwwpcp_post)
	{
		$rtwwpcp_pdf_name = get_option( 'rtwwpcp_basic_setting_opt' );
		$rtw_permalink = get_admin_url().'post.php?post='.$rtwwpcp_post->ID.'&action=edit&rtwwpcp_product_id='.$rtwwpcp_post->ID;
		?>
		<div class="rtwwcpig_btn_wrap">
			<a class="rtwwcpig_btn button button-primary" id="rtwwpcp_btn" target="_blank" href="<?php echo esc_url($rtw_permalink); ?>" download><?php esc_html_e('Generate Product PDF', 'rtwwpcp-woocommerce-pdf-flipbook'); ?></a>
		</div> <?php
	}
	public function rtwwpcp_make_single_product_pdf()
	{
		if ( $_GET['rtwwpcp_product_id'] ) 
		{
			$rtwwpcp_pdf = rtwwpcp_creat_sngl_prdct_pdf($_GET['rtwwpcp_product_id']);
		}
	}
	/**
	 * Add Custom bulk action in product table.
	 *
	 * @since    1.3.0
	 */
	public function rtwwpcp_custom_action_product_table($rtwwpcp_bulk_actn)
	{
		$rtwwpcp_bulk_actn['create_pdf'] = esc_html__( 'Generate PDF', 'rtwwpcp-woocommerce-pdf-flipbook' );
		return $rtwwpcp_bulk_actn;
	}
	/**
	 * Handel custom bulk action in product table.
	 *
	 * @since    1.3.0
	 */
	public function rtwwpcp_handel_custom_action_product_table($rtwwpcp_redrct_to, $rtwwpcp_actn, $rtwwpcp_post_ids)
	{
		if ( $rtwwpcp_actn !== 'create_pdf' ) {
			return $rtwwpcp_redrct_to;
		}
		$rtwwpcp_get_setting = get_option('rtwwpcp_basic_setting_opt');
		$rtwwpcp_post_name = array();
		foreach ($rtwwpcp_post_ids as $k => $val) {
			$this->create_pdf_for_bulk_action($val);
			$rtwwpcp_product = new WC_Product($val);
			array_push($rtwwpcp_post_name, $rtwwpcp_product->get_name());
		}
		if ( class_exists('ZipArchive') ) 
		{
			$rtwwpcp_output = ob_get_contents();
			ob_clean();
			$rtwwpcp_zip = new ZipArchive;
			$rtwwpcp_archive_file_name = 'All-Product-'.time().'.zip';

			if ( isset( $rtwwpcp_get_setting['pdf_name'] ) && $rtwwpcp_get_setting['pdf_name'] == 'rtwwpcp_post_name' ) 
			{
				if ($rtwwpcp_zip->open($rtwwpcp_archive_file_name, ZipArchive::CREATE) === TRUE) 
				{
					foreach ($rtwwpcp_post_name as $rtwpcp_key => $rtwwpcp_value) 
					{
						$rtwwpcp_file = RTWWPCP_PDF_DIR.$rtwwpcp_value.'.pdf';
						$rtwwpcp_zip->addFile($rtwwpcp_file, 'rtwwpcp_pdf_zip/'.$rtwwpcp_value);
					}	
				}
			} 
			else 
			{
				if ($rtwwpcp_zip->open($rtwwpcp_archive_file_name, ZipArchive::CREATE) === TRUE) 
				{
					foreach ($rtwwpcp_post_ids as $rtwpcp_key => $rtwwpcp_value) 
					{
						$rtwwpcp_file = RTWWPCP_PDF_DIR.$rtwwpcp_value.'.pdf';
						$rtwwpcp_zip->addFile($rtwwpcp_file, 'rtwwpcp_pdf_zip/'.$rtwwpcp_value);
					}	
				}
			}
			$rtwwpcp_zip->close();
			header("Content-type: application/zip");
			header("Content-Disposition: attachment; filename=".$rtwwpcp_archive_file_name);
			header("Content-length: " . filesize($rtwwpcp_archive_file_name));
			header("Pragma: no-cache"); 
			header("Expires: 0"); 
			readfile($rtwwpcp_archive_file_name);
			unlink($rtwwpcp_archive_file_name);
			exit();
		}
	}
	/**
	 * Create HTML for Product PDF.
	 *
	 * @since    1.3.0
	 */
	Public function create_pdf_for_bulk_action($rtwwpcp_id)
	{
		ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
		$rtwwpcp_product_obj = wc_get_product($rtwwpcp_id);
		$args = array(
		    'post_type' => 'product',
		    'posts_per_page' => -1
		);
		$html_array = array();
		$wp_query = get_posts( $args );
		$rtwwpcp_format_stng = get_option('rtwwpcp_format_setting_opt');
		$rtwwpcp_get_setting = get_option('rtwwpcp_basic_setting_opt');
		if ( isset($rtwwpcp_get_setting['catalog_btn_txt']) && $rtwwpcp_get_setting['catalog_btn_txt'] != '' ) {
			$prnt_prodct_btn = $rtwwpcp_get_setting['catalog_btn_txt'];
		}else{
			$prnt_prodct_btn = "Download PDF";
		}
		if(isset($rtwwpcp_format_stng['pdf_format']) && $rtwwpcp_format_stng['pdf_format'] != ''){
			if ($rtwwpcp_format_stng['pdf_format'] == 1 ) {
			$rtwwpcp_pdf_format = $rtwwpcp_format_stng['pdf_format_1'];
			}else if($rtwwpcp_format_stng['pdf_format'] == 2){
				$rtwwpcp_pdf_format = $rtwwpcp_format_stng['pdf_format_2'];
			}else{
				$rtwwpcp_pdf_format = $rtwwpcp_format_stng['pdf_format_3'];
			}
		}else{
			$rtwwpcp_pdf_format = get_option( 'default_catalog_format', array());
		}
		if (! class_exists ( 'simple_html_dom_node' )) 
		{
			require_once (RTWWPCP_DIR .'/includes/simplehtmldom/simple_html_dom.php');
		}
		$rtwwpcp_attr_data = array();
		$rtwwpcp_color = array();
		$rtwwpcp_relate_img = array();
		$rtwwpcp_atchmnt_id = array();
		$rtwwpcp_sngl_product_format = '';
		$rtwwpcp_sngl_product_format = $rtwwpcp_pdf_format;
		$rtwwpcp_data = array();
		$rtwwpcp_data['product_id']  = $rtwwpcp_product_obj->get_id();
		$attchmnt_id = $rtwwpcp_product_obj->get_gallery_image_ids();
		foreach ($attchmnt_id as $key => $value) {
			array_push($rtwwpcp_atchmnt_id, wp_get_attachment_url( $value ));
		}
		$rtwwpcp_data['product_name']  = $rtwwpcp_product_obj->get_name();
		$attr = $rtwwpcp_product_obj->get_attributes();
		if (!empty($attr)) {
			$product_attribute_slugs = array_keys($attr);
			foreach ($attr as $key => $value) {
				if ( strpos($value->get_name(), 'pa_') !== false ) {
					$strng = str_replace('pa_', '', $value->get_name());
				}
				else{
					$strng = $value->get_name();
				}
	            $rtwwpcp_attr_data[$strng] = $value->get_options($value->get_name());
			} 
		}
		$rtwwpcp_data['related_products'] = wc_get_related_products( $rtwwpcp_product_obj->get_id(), 4 );
		$cnt = 1;
		if ( !empty($rtwwpcp_data['related_products']) ) {
			foreach ($rtwwpcp_data['related_products'] as $relat_key => $relat_val) {
				$rtwwpcp_relate_img[(wc_get_product( $relat_val ))->get_name()] = wp_get_attachment_image_src( get_post_thumbnail_id( $relat_val ), 'single-post-thumbnail' );
				$rtwwpcp_data['add_to_cart_'.$cnt] =  add_query_arg('add-to-cart', $relat_val, wc_get_cart_url()) ;
				$cnt++;
			}
		}
		$count = 1;
		foreach ($rtwwpcp_relate_img as $relat_k => $relate_v) {
			$rtwwpcp_data['relate_img_'.$count]  = $relate_v[0];
			$rtwwpcp_data['relate_prod_'.$count]  = $relat_k;
			$count++;
		}
		$image = wp_get_attachment_image_src( get_post_thumbnail_id( $rtwwpcp_product_obj->get_id() ), 'single-post-thumbnail' );
		$rtwwpcp_data['add_to_cart']  = add_query_arg('add-to-cart', $rtwwpcp_product_obj->get_id(), wc_get_cart_url()) ;
		$rtwwpcp_data['thumb_img_1']  = isset($image[0])?$image[0]:'';
		$rtwwpcp_data['thumb_img_2']  = isset($rtwwpcp_atchmnt_id[0])?$rtwwpcp_atchmnt_id[0]:'';
		$rtwwpcp_data['thumb_img_3']  = isset($rtwwpcp_atchmnt_id[1])?$rtwwpcp_atchmnt_id[1]:'';
		$rtwwpcp_data['thumb_img_4']  = isset($rtwwpcp_atchmnt_id[2])?$rtwwpcp_atchmnt_id[2]:'';
		$rtwwpcp_data['product_slug']  = $rtwwpcp_product_obj->get_slug();
		$rtwwpcp_data['product_type']  = $rtwwpcp_product_obj->get_type();
		if ( isset($rtwwpcp_format_stng['pdf_format']) && $rtwwpcp_format_stng['pdf_format'] == 2 ) 
		{
			if ( $rtwwpcp_product_obj->is_on_sale() ) 
			{
				if( $rtwwpcp_product_obj->is_type( 'variable' ) ) {
					$available_variations = $rtwwpcp_product_obj->get_available_variations();
					$percent_off = ( ( 100 * ( $available_variations[0]['display_regular_price'] - $available_variations[0]['display_price'] ) ) / $available_variations[0]['display_regular_price'] );
					$percent_off = wc_format_decimal( $percent_off, 2 );
					$rtwwpcp_data['product_price'] = '<td style="font-size:16px;padding-top: 10px;font-weight: bold;color: #212121;">'.wc_price(floor($available_variations[0]['display_price'])).' <del style="font-weight: normal;"> '.wc_price(floor($available_variations[0]['display_regular_price'])).'</del> <span style="color:#FF905A;">( '.$percent_off.'% OFF)</span></td>';
				}
				else {
					$product_sale_price  = wc_price( $rtwwpcp_product_obj->get_sale_price() );
					$product_regular_price  = wc_price( $rtwwpcp_product_obj->get_regular_price() );
					$percent_off = ( ( 100 * ( $rtwwpcp_product_obj->get_regular_price() - $rtwwpcp_product_obj->get_sale_price() ) ) / $rtwwpcp_product_obj->get_regular_price() );
					$percent_off = wc_format_decimal( $percent_off, 2 );
					$rtwwpcp_data['product_price'] = '<td style="font-size:16px;padding-top: 10px;font-weight: bold;color: #212121;">'.$product_sale_price.' <del style="font-weight: normal;"> '.$product_regular_price.'</del> <span style="color:#FF905A;">( '.$percent_off.'% OFF)</span></td>';
				}
			}
			else
			{
				$rtwwpcp_data['product_price']  = wc_price( $rtwwpcp_product_obj->get_price() );
			}
		}
		else
		{
			$rtwwpcp_data['product_price']  = wc_price( $rtwwpcp_product_obj->get_price() );
		}
		$rtwwpcp_data['product_sku']  = $rtwwpcp_product_obj->get_sku();
		$rtwwpcp_data['permalinks']  = get_permalink( $rtwwpcp_product_obj->get_id() );
		$rtwwpcp_cat = $rtwwpcp_product_obj->get_category_ids();
		foreach ($rtwwpcp_cat as $cat_key => $cat_val) {
			$rtwwpcp_data['product_category'][]  = get_term_by( 'id', $cat_val, 'product_cat' )->name;
		}
	    $rtwwpcp_data['status']  = $rtwwpcp_product_obj->get_status();
		$rtwwpcp_data['product_desc']  = $rtwwpcp_product_obj->get_description();
		$rtwwpcp_data['product_short_desc']  = $rtwwpcp_product_obj->get_short_description();
	    $args = array ('post_type' => 'product', 'post_id' => $rtwwpcp_product_obj->get_id() );
		$comments = get_comments( $args );
	    $rtwwpcp_data['product_img'] = $image[0];
	    $rtwwpcp_data['product_rating'] = wc_get_rating_html( $rtwwpcp_product_obj->get_review_count() );
		if( $rtwwpcp_data['product_rating'] == '' )
		{
			$rtwwpcp_data['product_rating'] = 'No Rating Is Available';
		}
		$rtwwpcp_data['product_review'] = array();
		if( !empty( $comments ) && is_array( $comments ) )
		{
			$count = 0;
			foreach ($comments as $key => $value) 
			{
			 	$rtwwpcp_data['product_review'][$count] = $value->comment_content;
			 	$count = ++$count;
			}
		}
		$rtwwpcp_data['avrg_rating'] = $rtwwpcp_product_obj->get_average_rating();
		if ( $rtwwpcp_sngl_product_format != '' ) 
		{
			foreach ($rtwwpcp_data as $key => $value) 
			{	
				if ( strpos($rtwwpcp_sngl_product_format, $key)!== false ) 
				{
					if ( $key == 'product_review' ) 
					{
						$strng = '';
						if( is_array($rtwwpcp_data['product_review']) && !empty($rtwwpcp_data['product_review']) )
						{
							$cont = 1;
							foreach ($rtwwpcp_data['product_review'] as $k => $val) 
							{
								$strng .= '<p>'.$cont.' : '.$val.'</p>';
								$cont = ++$cont;
							}
						}
						else
						{
							$strng = 'No Review Available';
						}
					    $rtwwpcp_sngl_product_format = str_replace('['.$key.']', $strng, $rtwwpcp_sngl_product_format);
					}
					if ( $key == 'product_category' ) 
					{
						$rtwwpcp_category_name = '';
						if ( is_array($rtwwpcp_data['product_category']) && !empty($rtwwpcp_data['product_category'] )) 
						{
							if (isset($rtwwpcp_format_stng['pdf_format']) && $rtwwpcp_format_stng['pdf_format'] == 1 ) 
							{
								$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
								$rtwwpcp_sngl_pro_cat_str = '';
								$rtwwpcp_dom = new simple_html_dom ();
								$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
								foreach ($rtwwpcp_dom->find('#rtwwpcp_cat_list') as $val) 
								{
									$rtwwpcp_sngl_pro_cat_str = $val->outertext;
								}
								$rtwwpcp_category_name .= '<tr><td><div style="width: 100%;"><table><tr>';
								foreach ($rtwwpcp_data['product_category'] as $catgry_k => $catgry_val) 
								{
									if ( $catgry_k > 0 ) 
									{
										$rtwwpcp_category_name .= '<td style="width: 4px;"></td>';
									}
									$rtwwpcp_category_name .= '<td style="width: 90px; text-align: center; padding: 4px; background-color: #009688; border-radius: 2px; color: white;font-size: 12px;">'.$catgry_val.'</td>';
								}
								$rtwwpcp_category_name .= '</tr></table></div></td></tr>';
								$rtwwpcp_dom = new simple_html_dom ();
								$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
								foreach ($rtwwpcp_dom->find('#rtwwpcp_cat_list') as $val) 
								{
									$val->outertext = $rtwwpcp_category_name;
								}
								$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
							}
							else
							{
								foreach ($rtwwpcp_data['product_category'] as $cat_k => $cat_value) 
								{
									if ($cat_key == 0) 
									{
										$rtwwpcp_category_name .= '<div style="float:left; width: 25%; text-align: center; padding: 4px; background-color: #009688; border-radius: 2px; color: white;font-size: 12px;">'.$cat_value.'</div>';
									}
									else
									{
										$rtwwpcp_category_name .= '<div style="float:left; width: 25%; text-align: center; padding: 4px; background-color: #009688; border-radius: 2px; color: white;margin-left: 5px;font-size: 12px;">'.$cat_value.'</div>';
									}
								}
								$rtwwpcp_sngl_product_format = str_replace('['.$key.']', $rtwwpcp_category_name, $rtwwpcp_sngl_product_format);
							}
						}
					}
					else
					{
						$rtwwpcp_sngl_product_format = str_replace('['.$key.']', $value, $rtwwpcp_sngl_product_format);
					}
				}
				else
				{
					$rtwwpcp_sngl_product_format = str_replace($key, ' ', $rtwwpcp_sngl_product_format);
				}
			}
			if ( strpos($rtwwpcp_sngl_product_format, '[variation]')!== false ) 
			{
				$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
				$rtwwpcp_string2 = '';
				$rtwwpcp_dom = new simple_html_dom ();
				$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
				$rtwwpcp_prod_tr = '';
				foreach ($rtwwpcp_dom->find('#rtwwpcp_variation_list') as $val) 
				{
					$rtwwpcp_prod_tr = $val->outertext;
				}
				$attr_final_text = '';
				foreach ($rtwwpcp_attr_data as $key_attr => $value_attr) 
				{ 
					if ( $rtwwpcp_format_stng['pdf_format'] == 1 ) 
					{
						$attr_final_text .= '<tr style="font-size: 16px; font-weight: bold; margin-top: 25px; color: #000000;">';
						$attr_final_text .= '<td style="font-size: 16px; font-weight: bold; margin-top: 25px; color: #000000;">'.ucfirst($key_attr);
						$attr_final_text .= '</td>';
						$attr_final_text .= '</tr>';
						if ( is_array($value_attr) ) 
						{
							$attr_string  = '';
							$attr_string .= '<tr>';
							$attr_string .= '<td style="padding-top: 10px;">';
							$attr_string .= '<table style="300px;" cellspacing="0" cellpadding="0">';
							$attr_string .= '<tr>';
							foreach ($value_attr as $k_attr => $val_attr) 
							{
								if ( $key_attr == 'color' ) 
								{
									$attr_string .= '<td style="padding-right: 15px;"><table style="border-spacing: 0;" cellspacing="0" cellpadding="0"><tr><td style="width: 40px;border: 2px solid #ddd; height: 40px;text-align: center; font-size: 14px; background-color: '.get_term( $val_attr)->slug.';"></td></tr></table></td>'; 
								}
								else if($key_attr == 'Size')
								{
									$val = substr($val_attr, 0,1);
									$attr_string .= '<td style="padding-right: 15px;"><table style="border-spacing: 0;" cellspacing="0" cellpadding="0"><tr><td style="width: 40px;border: 2px solid #2874f0; height: 40px;text-align: center; font-size: 14px; background-color: #efefefe;">'.$val.'</td></tr></table></td>'; 
								}
								else
								{
									$attr_string .= '<td style="padding-right: 15px;"><table style="border-spacing: 0;" cellspacing="0" cellpadding="0"><tr><td style="width: 40px;border: 2px solid #2874f0; height: 40px;text-align: center; font-size: 14px; background-color: #efefefe;">'.get_term( $val_attr)->name.'</td></tr></table></td>'; 
								}
							}
							$attr_string .= '</tr>';
							$attr_string .= '</table>';
							$attr_string .= '</td>';
							$attr_string .= '</tr>';
						}
						$attr_final_text .= $attr_string;
					}
					else
					{
						$attr_final_text .= '<div>';
						$attr_final_text .= '<div style="font-size: 16px;padding-top: 15px; color: #878787;">'.ucfirst($key_attr).'</div>';
						$attr_final_text .= '</div>';
						if (  is_array($value_attr) ) 
						{
							$attr_string = '';
							$attr_string .= '<div style="width: 100%;">';
							foreach ($value_attr as $k_attr => $val_attr) 
							{
								if ( $key_attr == 'color' ) 
								{
									if($k_attr == 0)
									{
										$attr_string .= '<div style="float: left;width:30px; height: 30px;text-align: center; font-size: 14px;border: 1px solid #ddd;background-color: '.get_term( $val_attr)->slug.';">
											</div>';
									}
									else
									{
										$attr_string .= '<div style="float: left; margin-left: 10px;width:30px; height: 30px;text-align: center; font-size: 14px;border: 1px solid #ddd;background-color: '.get_term( $val_attr)->slug.';">
											</div>';
									}
								}
								else if ( $key_attr == 'Size' ) 
								{
									$val = substr($val_attr, 0,1);
									$attr_string .= '<div style="float: left; padding-right: 10px;width:55px;"><div style="border: 2px solid #2874f0; border-style: dotted;width: 45px; height: 27px;text-align: center; font-size: 14px;line-height: 27px;">'.$val.'</div></div>';
								}
								else
								{
									$attr_string .= '<div style="float: left; padding-right: 10px;width:55px;"><div style="border: 2px solid #2874f0;border-style: dotted;width: 50px; height: 27px;text-align: center; font-size: 14px;line-height: 27px;">'.get_term( $val_attr)->name.'</div></div>';
								}
							}
							$attr_string .= '</div>';	
						}
						$attr_final_text .= $attr_string;
					}
				} 
			}
			$rtwwpcp_dom = new simple_html_dom ();
			$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
			foreach ($rtwwpcp_dom->find('#rtwwpcp_variation_list') as $val) 
			{
				$val->outertext = $attr_final_text;
			}
			$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
			if ( empty($rtwwpcp_relate_img) ) 
			{
				$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
				$rtwwpcp_string2 = '';
				$rtwwpcp_string = '';
				$rtwwpcp_dom = new simple_html_dom ();
				$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
				$rtwwpcp_relat_prod = '';
				foreach ($rtwwpcp_dom->find('#rtwwpcp_relat_prod tbody') as $val) 
				{
					$rtwwpcp_string2 = $val->outertext;
				}
				foreach ($rtwwpcp_dom->find('#rtwwpcp_similar_prod td') as $val) 
				{
					$rtwwpcp_string = $val->outertext;
				}
				$rtwwpcp_dom = new simple_html_dom ();
				$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
				foreach ($rtwwpcp_dom->find('#rtwwpcp_relat_prod tbody') as $val) 
				{
					$val->outertext ='';
				}
				foreach ($rtwwpcp_dom->find('#rtwwpcp_similar_prod td') as $val) 
				{
					$val->outertext ='';
				}
				$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
			}
			$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
			$img_string2 = '';
			$img_string = '';
			$rtwwpcp_dom = new simple_html_dom ();
			$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
			$relate_img = '';
			foreach ($rtwwpcp_dom->find('#rtwwcpcp_img img') as $val) 
			{
				$img_string2 = $val->getAttribute('src');
				if ( $img_string2 == '' ) 
				{
					$val->outertext ='';
				}
			}
			$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
			$rtwwpcp_sngl_product_format = htmlspecialchars_decode ( htmlentities ( $rtwwpcp_sngl_product_format, ENT_NOQUOTES, 'UTF-8', false ), ENT_NOQUOTES );
			$img_str = '';
			$str = '';
			$rtwwpcp_dom = new simple_html_dom ();
			$rtwwpcp_dom->load ( $rtwwpcp_sngl_product_format );
			foreach ($rtwwpcp_dom->find('#rtwwpcp_relat_prod tbody tr img') as $val) 
			{
				$img_str = $val->getAttribute('src');
				if ( strpos($img_str, 'img') || $img_str == '' ) 
				{
					$val->outertext ='';
				}
			}
			foreach ($rtwwpcp_dom->find('#rtwwpcp_relat_prod tr[id=rtwwpcp_relat_prod_tr]') as $val) 
			{
				$str = $val->outertext;
				if ( strpos($str, 'prod') ) 
				{
					$val->outertext ='';
				}
			}
			$rtwwpcp_sngl_product_format = $rtwwpcp_dom->save();
			if ( strpos($rtwwpcp_sngl_product_format, '[add_to_cart') ) 
			{
				$rtwwpcp_sngl_product_format = str_replace('<td id="rtwwpcp_add_to_cart" style="font-size: 16px; font-weight: normal; background-color: #d93c50; height: 30px;"><a style="color: #ffffff; text-decoration: none;" href="[add_to_cart_2]">Buy Now</a></td>', '', $rtwwpcp_sngl_product_format);
				$rtwwpcp_sngl_product_format = str_replace('<td id="rtwwpcp_add_to_cart" style="font-size: 16px; font-weight: normal; background-color: #d93c50; height: 30px;"><a style="color: #ffffff; text-decoration: none;" href="[add_to_cart_3]">Buy Now</a></td>', '', $rtwwpcp_sngl_product_format);
				$rtwwpcp_sngl_product_format = str_replace('<td id="rtwwpcp_add_to_cart" style="font-size: 16px; font-weight: normal; background-color: #d93c50; height: 30px;"><a style="color: #ffffff; text-decoration: none;" href="[add_to_cart_4]">Buy Now</a></td>', '', $rtwwpcp_sngl_product_format);
			}
		}
		$pdf_data = rtwwpcp_convert_in_pdf( $rtwwpcp_sngl_product_format,$rtwwpcp_product_obj->get_id() );
		return $pdf_data;
	}
	/**
	 * Delete pdf when post updated.
	 *
	 * @since    1.4.0
	 */
	function rtwwpcp_pdf_delete( $rtwwpge_post_id )
	{
		if ($rtwwpge_post_id) 
		{
			if( file_exists( RTWWPCP_PDF_DIR.'/'.$rtwwpcp_post_id.'.pdf' ) )
			{
				unlink(RTWWPCP_PDF_DIR.'/'.$rtwwpcp_post_id.'.pdf');
			}
			if( file_exists( RTWWPCP_PDF_DIR.'/'.$post->rtwwpcp_post_name.'.pdf' ) )
			{
				unlink(RTWWPCP_PDF_DIR.'/'.$post->rtwwpcp_post_name.'.pdf');
			}
		}
		if( file_exists( RTWWPCP_PDF_DIR.'Category-catalog.pdf' ) )
		{
			unlink(RTWWPCP_PDF_DIR.'Category-catalog.pdf');
		}
		if( file_exists( RTWWPCP_PDF_DIR.'Catalog.pdf' ) )
		{
			unlink(RTWWPCP_PDF_DIR.'Catalog.pdf');
		}	
	}
}
