<?php
/**
 * This file belongs to the YIT Plugin Framework.
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE (GPL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 */

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

use Dompdf\Dompdf;

/**
 * Implements the YITH_Request_Quote_Premium class.
 *
 * @class   YITH_Request_Quote_Premium
 * @package YITH
 * @since   1.0.0
 * @author  YITH
 */
if ( ! class_exists( 'YITH_Request_Quote_Premium' ) ) {

	/**
	 * Class YITH_Request_Quote_Premium
	 */
	class YITH_Request_Quote_Premium extends YITH_Request_Quote {

		/**
		 * @var bool
		 */
		private $locale = false;

		/**
		 * Returns single instance of the class
		 *
		 * @return \YITH_Request_Quote_Premium
		 * @since 1.0.0
		 */
		public static function get_instance() {
			if ( is_null( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * Constructor
		 *
		 * Initialize plugin and registers actions and filters to be used
		 *
		 * @since  1.0.0
		 * @author Emanuela Castorina
		 */
		public function __construct() {

			parent::__construct();

			$this->_run();

			//register widget
			add_action( 'widgets_init', array( $this, 'register_widgets' ) );
			add_filter( 'woocommerce_email_classes', array( $this, 'add_woocommerce_emails' ) );

			if ( 'yes' == get_option( 'ywraq_enable_pdf', 'yes' ) && ( defined( 'DOING_CRON' ) || $this->is_admin() ) ) {
				add_action( 'create_pdf', array( $this, 'generate_pdf' ), 99 );
				add_action( 'yith_ywraq_quote_template_header', array( $this, 'pdf_header' ), 10, 1 );
				add_action( 'yith_ywraq_quote_template_footer', array( $this, 'pdf_footer' ), 10, 1 );
				add_action( 'yith_ywraq_quote_template_content', array( $this, 'pdf_content' ), 10, 1 );
				add_filter( 'plugin_locale', array( $this, 'set_locale_for_pdf' ), 10, 2 );
			}

			if ( $this->is_admin() ) {
				//register metabox to the product editor
				add_action( 'admin_init', array( $this, 'add_metabox' ), 1 );
				add_action( 'save_post', array( $this, 'save_metabox_info' ), 1, 2 );
				add_filter( 'get_post_metadata', array( $this, 'get_exclusion_value' ), 10, 3 );


			} else {

				//show button in shop page
				add_action( 'woocommerce_after_shop_loop_item', array( $this, 'add_button_shop' ), 15 );
				add_filter( 'yith_ywraq_hide_price_template', array( $this, 'show_product_price' ), 0, 2 );

				if ( ! catalog_mode_plugin_enabled() ) {
					add_filter( 'woocommerce_get_price_html', array( $this, 'show_product_price' ), 0 );
					add_filter( 'woocommerce_get_variation_price_html', array( $this, 'show_product_price' ), 0 );
				}

				//check user type
				add_filter( 'yith_ywraq_before_print_button', array( $this, 'must_be_showed' ), 10, 2 );
				add_filter( 'yith_ywraq_before_print_widget', array( $this, 'raq_page_check_user' ) );
				add_filter( 'yith_ywraq_before_print_my_account_my_quotes', array( $this, 'raq_page_check_user' ) );
				add_filter( 'yith_ywraq_before_print_raq_page', array( $this, 'raq_page_check_user' ) );
				add_filter( 'yith_ywraq_raq_page_deniend_access', array( $this, 'raq_page_denied_access' ) );
			}

		}

		/**
		 * Include files and classes for the premium version.
		 *
		 * @since  2.0
		 * @author Emanuela Castorina <emanuela.castorina@yithemes.com>
		 */
		private function _run() {

			//widget
			require_once( YITH_YWRAQ_INC . 'widgets/class.yith-ywraq-list-quote-widget.php' );
			require_once( YITH_YWRAQ_INC . 'widgets/class.yith-ywraq-mini-list-quote-widget.php' );

			//Load class and functions of default form
			require_once( YITH_YWRAQ_INC . 'forms/default/class.yith-ywraq-default-form.php' );
			require_once( YITH_YWRAQ_INC . 'forms/default/functions.yith-ywraq-default-form.php' );

			//privacy
			require_once( YITH_YWRAQ_INC . 'class.yith-request-quote-privacy.php' );
			require_once( YITH_YWRAQ_INC . 'class.yith-ywraq-order-request.php' );


			if ( $this->is_admin() ) {
				require_once( YITH_YWRAQ_INC . 'class.yith-ywraq-exclusions-handler.php' );
				require_once( YITH_YWRAQ_INC . 'admin/class.yith-ywraq-exclusions-prod-table.php' );
				require_once( YITH_YWRAQ_INC . 'admin/class.yith-ywraq-exclusions-cat-table.php' );
				YITH_YWRAQ_Exclusions_Handler();
			}

			$this->_plugin_integrations();
			$this->_form_integrations();
		}

		/**
		 * Include the files and the classes if necessary.
		 *
		 * @since  2.0
		 * @author Emanuela Castorina <emanuela.castorina@yithemes.com>
		 */
		private function _plugin_integrations() {

			if ( defined( 'WCML_VERSION' ) ) {
				require_once( YITH_YWRAQ_INC . 'emails/class.yith-ywraq-multilingual-email.php' );
			}

			if ( class_exists( 'YITH_Vendors' ) ) {
				require_once( YITH_YWRAQ_INC . 'compatibility/yith-woocommerce-product-vendors.php' );
			}

			if ( class_exists( 'YITH_WAPO' ) ) {
				require_once( YITH_YWRAQ_INC . 'compatibility/yith-woocommerce-advanced-product-options.php' );
			}

			if ( class_exists( 'YITH_WCP' ) ) {
				require_once( YITH_YWRAQ_INC . 'compatibility/yith-woocommerce-composite-products.php' );
			}

		}

		/**
		 * Include the files and the classes if necessary.
		 *
		 * @since  2.0
		 * @author Emanuela Castorina <emanuela.castorina@yithemes.com>
		 */
		private function _form_integrations() {

			$form_type = get_option( 'ywraq_inquiry_form_type', 'default' );

			if ( ywraq_cf7_form_installed() && ( is_admin() || 'contact-form-7' == $form_type ) ) {
				require_once( YITH_YWRAQ_INC . 'forms/contact-form-7/class.yith-ywraq-contact-form-7.php' );
				require_once( YITH_YWRAQ_INC . 'forms/contact-form-7/functions.yith-ywraq-contact-form-7.php' );
				YITH_YWRAQ_Contact_Form_7();
			}

			if ( ywraq_yit_contact_form_installed() && ( is_admin() || 'yit-contact-form' == $form_type ) ) {
				require_once( YITH_YWRAQ_INC . 'forms/yit-contact-form/class.yith-ywraq-yit-contact-form.php' );
				YITH_YWRAQ_YIT_Contact_Form();
			}

			if ( ywraq_gravity_form_installed() && ( is_admin() || 'gravity-forms' == $form_type ) ) {
				require_once( YITH_YWRAQ_INC . 'forms/gravity-forms/ywraq-gravity-form-addons.php' );
				YWRAQ_Gravity_Forms_Add_On();
			}

			if ( is_admin() || 'default' == $form_type ) {
				YITH_YWRAQ_Default_Form();
			}

			do_action( 'ywraq_form_integration' );
		}

		/**
		 * Add the quote button in other pages is the product is simple
		 *
		 * @since   1.0.0
		 * @author  Emanuela Castorina
		 * @return  boolean|void
		 */
		public function add_button_shop() {

			$show_button = apply_filters( 'yith_ywraq-btn_other_pages', true );

			global $product;

			if ( ! $product ) {
				return false;
			}

			$type_in_loop = apply_filters( 'yith_ywraq_show_button_in_loop_product_type', array( 'simple', 'subscription', 'external' ) );

			if ( $show_button != 'yes' || ! $product->is_type( $type_in_loop ) ) {
				return false;
			}

			if ( ! function_exists( 'YITH_YWRAQ_Frontend' ) ) {
				require_once( YITH_YWRAQ_INC . 'class.yith-request-quote-frontend.php' );
				YITH_YWRAQ_Frontend();
			}

			YITH_YWRAQ_Frontend()->print_button( $product );
		}

		/**
		 * Check for which users will not see the price
		 *
		 * @since   1.0.0
		 * @author  Emanuela Castorina
		 *
		 * @param      $price
		 * @param bool $product_id
		 *
		 * @return string
		 */
		public function show_product_price( $price, $product_id = false ) {

			$hide_price = get_option( 'ywraq_hide_price' ) == 'yes';

			if ( catalog_mode_plugin_enabled() ) {
				global $YITH_WC_Catalog_Mode;
				$hide_price = $YITH_WC_Catalog_Mode->check_product_price_single( true, $product_id );

				if ( $hide_price && get_option( 'ywctm_exclude_price_alternative_text' ) != '' ) {
					$hide_price = false;
					$price      = get_option( 'ywctm_exclude_price_alternative_text' );
				}

			} elseif ( $hide_price && current_filter() == 'woocommerce_get_price_html' ) {
				$price = '';
			} elseif ( $hide_price && ! catalog_mode_plugin_enabled() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && current_filter() != 'woocommerce_get_price_html' ) {
				ob_start();

				$args = array(
					'.single_variation_wrap .single_variation'
				);

				$classes = implode( ', ', apply_filters( 'ywcraq_catalog_price_classes', $args ) );

				?>
                <style>
                    <?php echo $classes; ?>
                    {
                        display: none !important
                    }

                </style>
				<?php
				echo ob_get_clean();
			}

			return ( $hide_price ) ? '' : $price;

		}

		/**
		 * Add metabox in the product editor
		 *
		 * @since   1.0.0
		 * @author  Emanuela Castorina
		 * @return  void
		 */
		public function add_metabox() {
			global $pagenow;
			$post = isset( $_REQUEST['post'] ) ? $_REQUEST['post'] : ( isset( $_REQUEST['post_ID'] ) ? $_REQUEST['post_ID'] : 0 );
			$post = get_post( $post );

			if ( ( $post && $post->post_type == 'product' ) || ( $pagenow == 'post-new.php' && isset( $_REQUEST['post_type'] ) && $_REQUEST['post_type'] == 'product' ) ) {
				$args = require_once( YITH_YWRAQ_DIR . 'plugin-options/metabox/ywraq-metabox.php' );
				if ( ! function_exists( 'YIT_Metabox' ) ) {
					require_once( YITH_YWRAQ_DIR . 'plugin-fw/yit-plugin.php' );
				}
				$metabox = YIT_Metabox( 'yith-ywraq-metabox' );
				$metabox->init( $args );
			}

		}

		/**
		 * Check if the product is in the exclusion list
		 *
		 * @param $value
		 * @param $post_id
		 * @param $meta_key
		 *
		 * @return mixed
		 * @author Alberto Ruggiero
		 */
		function get_exclusion_value( $value, $post_id, $meta_key ) {

			if ( '_ywraq_hide_quote_button' === $meta_key && 'product' === get_post_type( $post_id ) ) {

				$exclusion_prod = explode( ',', get_option( 'yith-ywraq-exclusions-prod-list', '' ) );
				$exclusion_prod = array_map( 'absint', $exclusion_prod );

				$value = in_array( $post_id, $exclusion_prod );

			}

			return $value;
		}

		/**
		 * Add or Remove the products in the exclusion list
		 *
		 * @param $post_id
		 * @param $post
		 *
		 * @author Emanuela Castorina <emanuela.castorina@yithemes.com>
		 */
		public function save_metabox_info( $post_id, $post ) {
			if ( ( $post && $post->post_type == 'product' ) ) {
				$exclusion_prod = explode( ',', get_option( 'yith-ywraq-exclusions-prod-list', '' ) );
				$exclusion_prod = array_map( 'absint', $exclusion_prod );

				if ( ! isset( $_REQUEST['yit_metaboxes']['_ywraq_hide_quote_button'] ) ) {
					if ( in_array( $post_id, $exclusion_prod ) ) {
						$exclusion_prod = array_diff( $exclusion_prod, array( $post_id ) );
						update_option( 'yith-ywraq-exclusions-prod-list', implode( ',', $exclusion_prod ) );
					}
				} else {
					if ( ! in_array( $post_id, $exclusion_prod ) ) {
						$exclusion_prod = array_merge( $exclusion_prod, array( $post_id ) );
						update_option( 'yith-ywraq-exclusions-prod-list', implode( ',', $exclusion_prod ) );
					}
				}
			}
		}

		/**
		 * Register the widgets
		 *
		 * @since   1.0.0
		 * @author  Emanuela Castorina
		 * @return  void
		 */
		public function register_widgets() {
			register_widget( 'YITH_YWRAQ_List_Quote_Widget' );
			register_widget( 'YITH_YWRAQ_Mini_List_Quote_Widget' );
		}

		/**
		 * Refresh the quote list in the widget when a product is added or removed from the list
		 *
		 * @since   1.0.0
		 * @author  Emanuela Castorina
		 * @return  void
		 */
		public function ajax_refresh_quote_list() {
			$raq_content  = YITH_Request_Quote()->get_raq_return();
			$args         = array(
				'raq_content'      => $raq_content,
				'template_part'    => 'view',
				'title'            => isset( $_POST['title'] ) ? $_POST['title'] : '',
				'item_plural_name' => isset( $_POST['item_plural_name'] ) ? $_POST['item_plural_name'] : '',
				'item_name'        => isset( $_POST['item_name'] ) ? $_POST['item_name'] : '',
				'show_thumbnail'   => isset( $_POST['show_thumbnail'] ) ? $_POST['show_thumbnail'] : 1,
				'show_price'       => isset( $_POST['show_price'] ) ? $_POST['show_price'] : 1,
				'show_quantity'    => isset( $_POST['show_quantity'] ) ? $_POST['show_quantity'] : 1,
				'show_variations'  => isset( $_POST['show_variations'] ) ? $_POST['show_variations'] : 1,
				'widget_type'      => isset( $_POST['widget_type'] ) ? $_POST['widget_type'] : '',
			);
			$args['args'] = $args;

			wp_send_json(
				array(
					'large' => wc_get_template_html( 'widgets/quote-list.php', $args, '', YITH_YWRAQ_TEMPLATE_PATH . '/' ),
					'mini'  => wc_get_template_html( 'widgets/mini-quote-list.php', $args, '', YITH_YWRAQ_TEMPLATE_PATH . '/' ),
				)
			);

			die();
		}

		/**
		 * Refresh the number of items for the shortcode [yith_ywraq_number_items]
		 *
		 * @since   1.7.8
		 * @author  Emanuela Castorina
		 * @return  void
		 */
		public function ajax_refresh_number_items() {

			$atts = array(
				'show_url'         => $_POST['show_url'],
				'item_name'        => $_POST['item_name'],
				'item_plural_name' => $_POST['item_plural_name']
			);

			echo YITH_YWRAQ_Frontend()->shortcodes->ywraq_number_items( $atts );
		}

		/**
		 * Loads the inquiry form
		 *
		 * @param $args
		 *
		 * @since 1.0
		 */
		public function get_inquiry_form( $args ) {

			$shortcode = '';

			switch ( get_option( 'ywraq_inquiry_form_type', 'default' ) ) {
				case 'yit-contact-form':
					$shortcode = '[contact_form name="' . get_option( 'ywraq_inquiry_yit_contact_form_id' ) . '"]';
					break;
				case 'contact-form-7':
					if ( function_exists( 'icl_get_languages' ) && class_exists( 'YITH_YWRAQ_Multilingual_Email' ) ) {
						global $sitepress;
						$current_language = $sitepress->get_current_language();
						$cform7_id        = get_option( 'ywraq_inquiry_contact_form_7_id_' . $current_language );

					} else {
						$cform7_id = get_option( 'ywraq_inquiry_contact_form_7_id' );
					}

					$cform7_id = apply_filters( 'ywraq_inquiry_contact_form_7_id', $cform7_id );

					$shortcode = '[contact-form-7 id="' . $cform7_id . '"]';
					break;
				case 'gravity-forms':
					if ( ywraq_gravity_form_installed() ) {
						$gravity_form_id = YWRAQ_Gravity_Forms_Add_On()->get_selected_form_id();
						$shortcode       = '[gravityform id="' . $gravity_form_id . '" title="true" description="true" ajax="true"]';
					}
					break;
				case 'default':
					YITH_YWRAQ_Default_Form()->get_form_template( $args );
					break;
			}

			echo do_shortcode( $shortcode );

		}

		/**
		 * Filters woocommerce available mails, to add wishlist related ones
		 *
		 * @param $emails array
		 *
		 * @return array
		 * @since 1.0
		 */
		public function add_woocommerce_emails( $emails ) {
			$emails['YITH_YWRAQ_Send_Email_Request_Quote'] = include( YITH_YWRAQ_INC . 'emails/class.yith-ywraq-send-email-request-quote.php' );
			$emails['YITH_YWRAQ_Quote_Status']             = include( YITH_YWRAQ_INC . 'emails/class.yith-ywraq-quote-status.php' );
			$emails['YITH_YWRAQ_Send_Quote']               = include( YITH_YWRAQ_INC . 'emails/class.yith-ywraq-send-quote.php' );

			return $emails;
		}

		/**
		 * Loads WC Mailer when needed
		 *
		 * @return void
		 * @since 1.0
		 */
		public function load_wc_mailer() {
			add_action( 'send_raq_mail', array( 'WC_Emails', 'send_transactional_email' ), 10 );
			add_action( 'send_quote_mail', array( 'WC_Emails', 'send_transactional_email' ), 10 );
			add_action( 'change_status_mail', array( 'WC_Emails', 'send_transactional_email' ), 10 );
		}

		/**
		 * Build wishlist page URL.
		 *
		 * @param string $action
		 *
		 * @return string
		 * @since 1.0.0
		 */
		public function get_raq_url( $action = 'view' ) {
			$base_url    = '';
			$raq_page_id = get_option( 'ywraq_page_id' );

			if ( get_option( 'permalink_structure' ) ) {
				$raq_page          = get_post( $raq_page_id );
				$raq_page_slug     = $raq_page->post_name;
				$raq_page_relative = '/' . $raq_page_slug . '/' . $action . '/';

				$base_url = trailingslashit( home_url( $raq_page_relative ) );
			}

			return $base_url;

		}

		/**
		 * Check if the raq button can be showed
		 *
		 * @param boolean $value   Current filter value
		 * @param object  $product The WC Product object
		 *
		 * @return bool
		 * @since 1.0.0
		 */
		public function must_be_showed( $value = true, $product = null ) {

			if ( is_null( $product ) ) {
				// if is null get global
				global $product;
			}
			// if is null get post
			if ( ! $product ) {
				global $post;
				if ( ! $post || ! is_object( $post ) || ! is_singular() ) {
					return false;
				}
				$product = wc_get_product( $post->ID );
			}


			if ( ! is_object( $product ) || ! $this->check_user_type() || ( ! ywraq_allow_raq_out_of_stock() && $product && ! $product->is_in_stock() ) || ( ywraq_show_btn_only_out_of_stock() && $product && $product->is_in_stock() ) ) {
				return false;
			}

			return $value;
		}

		/**
		 * Check user
		 *
		 * @return bool
		 * @since 1.0.0
		 */
		public function check_user() {

			global $product;

			if ( ! $product ) {
				global $post;
				if ( ! $post || ! is_object( $post ) || ! is_singular() ) {
					return false;
				}
				$product = wc_get_product( $post->ID );
			}

			if ( ! is_object( $product ) || ! $this->check_user_type() || ( ! ywraq_allow_raq_out_of_stock() && $product && ! $product->is_in_stock() ) || ( ywraq_show_btn_only_out_of_stock() && $product && $product->is_in_stock() ) ) {
				return false;
			}

			return true;
		}

		/**
		 * Check if the raq button can be showed
		 *
		 * @return bool
		 * @since 1.0.0
		 */
		public function raq_page_check_user() {

			if ( ! $this->check_user_type() ) {
				return false;
			}

			return true;
		}

		/**
		 * Check if the current user is available to send requests
		 *
		 * @return bool
		 * @since 1.0.0
		 */
		public function check_user_type() {
			$user_type = get_option( 'ywraq_user_type' );
			$return    = false;

			if ( is_user_logged_in() && ( $user_type == 'customers' || $user_type == 'all' ) ) {
				$rules = (array) get_option( 'ywraq_user_role' );

				if ( empty( $rules ) || ! is_array( $rules ) ) {
					return false;
				}

				if ( in_array( 'all', $rules ) ) {
					return true;
				}

				$current_user = wp_get_current_user();
				$intersect    = array_intersect( $current_user->roles, $rules );
				if ( ! empty( $intersect ) ) {
					return true;
				}
			} else {
				if ( ( ! is_user_logged_in() && $user_type == 'guests' ) || $user_type == 'all' ) {
					return true;
				}
			}

			return $return;
		}

		/**
		 * @param $message string
		 *
		 * @return string
		 */
		public function raq_page_denied_access( $message ) {
			$user_type = get_option( 'ywraq_user_type' );

			if ( $user_type == 'customers' ) {
				return __( 'You must be logged in to access this page', 'yith-woocommerce-request-a-quote' );
			}

			return $message;
		}

		/**
		 * Generate the template
		 *
		 * @param $order_id
		 *
		 * @return int
		 */
		public function generate_pdf( $order_id ) {

			ob_start();

			wc_get_template( 'pdf/quote.php', array( 'order_id' => $order_id ), '', YITH_YWRAQ_TEMPLATE_PATH . '/' );

			$html = ob_get_contents();
			ob_end_clean();

			require_once( YITH_YWRAQ_DOMPDF_DIR . 'autoload.inc.php' );

			$dompdf = new DOMPDF();

			$dompdf->setPaper( 'A4', 'portrait' );

			$dompdf->set_option( 'enable_html5_parser', true );

			$dompdf->loadHtml( $html );

			$dompdf->render();

			// The next call will store the entire PDF as a string in $pdf
			$pdf = $dompdf->output();

			$file_path = $this->get_pdf_file_path( $order_id, true );

			if ( ! file_exists( $file_path ) ) {
				$file_path = $this->get_pdf_file_path( $order_id, false );
			} else {
				unlink( $file_path );
			}

			return file_put_contents( $file_path, $pdf );

		}

		/**
		 * @param $order_id
		 *
		 * @return string
		 */
		public function get_pdf_file_url( $order_id ) {
			$path = $this->create_storing_folder( $order_id );
			$url  = YITH_YWRAQ_SAVE_QUOTE_URL . $path . $this->get_pdf_file_name( $order_id );

			return apply_filters( 'ywraq_pdf_file_url', $url );
		}

		/**
		 * Return the file of pdf
		 *
		 * @param $order_id
		 *
		 * @return string
		 */
		public function get_pdf_file_name( $order_id ) {

			$pdf_file_name = 'quote_' . $order_id . '.pdf';

			$order            = wc_get_order( $order_id );
			$_ywraq_pdf_crypt = yit_get_prop( $order, '_ywraq_pdf_crypt', true );

			if ( 'yes' === $_ywraq_pdf_crypt ) {
				$ywraq_customer_email = yit_get_prop( $order, 'ywraq_customer_email', true );
				$pdf_file_name        = 'quote_' . md5( $order_id . $ywraq_customer_email ) . '.pdf';
			}

			return apply_filters( 'ywraq_pdf_file_name', $pdf_file_name, $order_id );
		}

		/**
		 * @param      $order_id
		 * @param bool $delete_file
		 *
		 * @return string
		 */
		public function get_pdf_file_path( $order_id, $delete_file = false ) {
			$path = apply_filters( 'ywraq_pdf_file_path', $this->create_storing_folder( $order_id ), $order_id );
			$file = YITH_YWRAQ_DOCUMENT_SAVE_DIR . $path . $this->get_pdf_file_name( $order_id );
			//delete the document if exists
			if ( file_exists( $file ) && $delete_file ) {
				@unlink( $file );
			}

			return $file;
		}

		/**
		 * @param $order_id
		 *
		 * @return mixed|string
		 */
		public static function create_storing_folder( $order_id ) {

			$order = wc_get_order( $order_id );
			/* Create folders for storing documents */
			$folder_pattern = '[year]/[month]/';

			$order_date = is_callable( array( $order, 'get_date_created' ) ) ? $order->get_date_created() : $order->order_date;

			$date = getdate( strtotime( $order_date ) );

			$folder_pattern = str_replace(
				array(
					'[year]',
					'[month]'
				),
				array(
					$date['year'],
					sprintf( "%02d", $date['mon'] )
				),
				$folder_pattern );

			if ( ! file_exists( YITH_YWRAQ_DOCUMENT_SAVE_DIR . $folder_pattern ) ) {
				wp_mkdir_p( YITH_YWRAQ_DOCUMENT_SAVE_DIR . $folder_pattern );
			}

			return $folder_pattern;
		}

		/**
		 * @param $order_id
		 */
		public function pdf_content( $order_id ) {
			$order = wc_get_order( $order_id );
			wc_get_template( 'pdf/quote-table.php', array( 'order' => $order ), '', YITH_YWRAQ_TEMPLATE_PATH . '/' );
		}

		/**
		 * @param $order_id
		 */
		public function pdf_header( $order_id ) {
			$order = wc_get_order( $order_id );
			wc_get_template( 'pdf/quote-header.php', array( 'order' => $order ), '', YITH_YWRAQ_TEMPLATE_PATH . '/' );
		}

		/**
		 * @param $order_id
		 */
		public function pdf_footer( $order_id ) {
			$footer_content  = get_option( 'ywraq_pdf_footer_content' );
			$show_pagination = get_option( 'ywraq_pdf_pagination' );
			wc_get_template( 'pdf/quote-footer.php', array( 'footer' => $footer_content, 'pagination' => $show_pagination, 'order_id' => $order_id ), '', YITH_YWRAQ_TEMPLATE_PATH . '/' );
		}

		/**
		 * @param $lang
		 */
		public function change_pdf_language( $lang ) {
			global $sitepress, $woocommerce;
			if ( is_object( $sitepress ) ) {
				$sitepress->switch_lang( $lang, true );
				$this->locale = $sitepress->get_locale( $lang );
				unload_textdomain( 'yith-woocommerce-request-a-quote' );
				unload_textdomain( 'woocommerce' );
				unload_textdomain( 'default' );

				load_plugin_textdomain( 'yith-woocommerce-request-a-quote', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
				$woocommerce->load_plugin_textdomain();
				load_default_textdomain();
			}
		}

		/**
		 * set correct locale code for emails
		 *
		 * @param $locale
		 * @param $domain
		 *
		 * @return bool
		 */
		function set_locale_for_pdf( $locale, $domain ) {

			if ( $domain == 'woocommerce' && $this->locale ) {
				$locale = $this->locale;
			}

			return $locale;
		}
	}

}

/**
 * Unique access to instance of YITH_Request_Quote_Premium class
 *
 * @return \YITH_Request_Quote_Premium
 */
function YITH_Request_Quote_Premium() {
	return YITH_Request_Quote_Premium::get_instance();
}

