<?php

if (!defined('ABSPATH')) {
    exit;
}
if (class_exists('WC_Email', false)) {
    return;
}
class WC_Email extends WC_Settings_API
{
    public $id;
    public $title;
    public $enabled;
    public $description;
    public $heading = '';
    public $subject = '';
    public $template_plain;
    public $template_html;
    public $template_base;
    public $recipient;
    public $object;
    public $mime_boundary;
    public $mime_boundary_header;
    public $sending;
    protected $manual = false;
    protected $customer_email = false;
    public $plain_search = array("/\r/", '/&(nbsp|#0*160);/i', '/&(quot|rdquo|ldquo|#0*8220|#0*8221|#0*147|#0*148);/i', '/&(apos|rsquo|lsquo|#0*8216|#0*8217);/i', '/&gt;/i', '/&lt;/i', '/&#0*38;/i', '/&amp;/i', '/&(copy|#0*169);/i', '/&(trade|#0*8482|#0*153);/i', '/&(reg|#0*174);/i', '/&(mdash|#0*151|#0*8212);/i', '/&(ndash|minus|#0*8211|#0*8722);/i', '/&(bull|#0*149|#0*8226);/i', '/&(pound|#0*163);/i', '/&(euro|#0*8364);/i', '/&(dollar|#0*36);/i', '/&[^&\\s;]+;/i', '/[ ]{2,}/');
    public $plain_replace = array('', ' ', '"', "'", '>', '<', '&', '&', '(c)', '(tm)', '(R)', '--', '-', '*', '£', 'EUR', '$', '', ' ');
    protected $placeholders = array();
    public $find = array();
    public $replace = array();
    public function __construct()
    {
        $this->placeholders = array_merge(array('{site_title}' => $this->get_blogname(), '{site_address}' => wp_parse_url(home_url(), PHP_URL_HOST), '{site_url}' => wp_parse_url(home_url(), PHP_URL_HOST)), $this->placeholders);
        $this->init_form_fields();
        $this->init_settings();
        if (is_null($this->template_base)) {
            $this->template_base = WC()->plugin_path() . '/templates/';
        }
        $this->email_type = $this->get_option('email_type');
        $this->enabled = $this->get_option('enabled');
        add_action('phpmailer_init', array($this, 'handle_multipart'));
        add_action('woocommerce_update_options_email_' . $this->id, array($this, 'process_admin_options'));
    }
    public function handle_multipart($mailer)
    {
        if ($this->sending && 'multipart' === $this->get_email_type()) {
            $mailer->AltBody = wordwrap(preg_replace($this->plain_search, $this->plain_replace, wp_strip_all_tags($this->get_content_plain())));
            $this->sending = false;
        }
        return $mailer;
    }
    public function format_string($string)
    {
        $find = array_keys($this->placeholders);
        $replace = array_values($this->placeholders);
        $find = array_merge((array) $this->find, $find);
        $replace = array_merge((array) $this->replace, $replace);
        $find[] = '{blogname}';
        $replace[] = $this->get_blogname();
        if (has_filter('woocommerce_email_format_string_replace') || has_filter('woocommerce_email_format_string_find')) {
            $legacy_find = $this->find;
            $legacy_replace = $this->replace;
            foreach ($this->placeholders as $find => $replace) {
                $legacy_key = sanitize_title(str_replace('_', '-', trim($find, '{}')));
                $legacy_find[$legacy_key] = $find;
                $legacy_replace[$legacy_key] = $replace;
            }
            $string = str_replace(apply_filters('woocommerce_email_format_string_find', $legacy_find, $this), apply_filters('woocommerce_email_format_string_replace', $legacy_replace, $this), $string);
        }
        return apply_filters('woocommerce_email_format_string', str_replace($find, $replace, $string), $this);
    }
    public function setup_locale()
    {
        if ($this->is_customer_email() && apply_filters('woocommerce_email_setup_locale', true)) {
            wc_switch_to_site_locale();
        }
    }
    public function restore_locale()
    {
        if ($this->is_customer_email() && apply_filters('woocommerce_email_restore_locale', true)) {
            wc_restore_locale();
        }
    }
    public function get_default_subject()
    {
        return $this->subject;
    }
    public function get_default_heading()
    {
        return $this->heading;
    }
    public function get_default_additional_content()
    {
        return '';
    }
    public function get_additional_content()
    {
        $content = $this->get_option('additional_content', '');
        return apply_filters('woocommerce_email_additional_content_' . $this->id, $this->format_string($content), $this->object, $this);
    }
    public function get_subject()
    {
        return apply_filters('woocommerce_email_subject_' . $this->id, $this->format_string($this->get_option('subject', $this->get_default_subject())), $this->object, $this);
    }
    public function get_heading()
    {
        return apply_filters('woocommerce_email_heading_' . $this->id, $this->format_string($this->get_option('heading', $this->get_default_heading())), $this->object, $this);
    }
    public function get_recipient()
    {
        $recipient = apply_filters('woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object, $this);
        $recipients = array_map('trim', explode(',', $recipient));
        $recipients = array_filter($recipients, 'is_email');
        return implode(', ', $recipients);
    }
    public function get_headers()
    {
        $header = 'Content-Type: ' . $this->get_content_type() . "\r\n";
        if (in_array($this->id, array('new_order', 'cancelled_order', 'failed_order'), true)) {
            if ($this->object && $this->object->get_billing_email() && ($this->object->get_billing_first_name() || $this->object->get_billing_last_name())) {
                $header .= 'Reply-to: ' . $this->object->get_billing_first_name() . ' ' . $this->object->get_billing_last_name() . ' <' . $this->object->get_billing_email() . ">\r\n";
            }
        } elseif ($this->get_from_address() && $this->get_from_name()) {
            $header .= 'Reply-to: ' . $this->get_from_name() . ' <' . $this->get_from_address() . ">\r\n";
        }
        return apply_filters('woocommerce_email_headers', $header, $this->id, $this->object, $this);
    }
    public function get_attachments()
    {
        return apply_filters('woocommerce_email_attachments', array(), $this->id, $this->object, $this);
    }
    public function get_email_type()
    {
        return $this->email_type && class_exists('DOMDocument') ? $this->email_type : 'plain';
    }
    public function get_content_type($default_content_type = '')
    {
        switch ($this->get_email_type()) {
            case 'html':
                $content_type = 'text/html';
                break;
            case 'multipart':
                $content_type = 'multipart/alternative';
                break;
            default:
                $content_type = 'text/plain';
                break;
        }
        return apply_filters('woocommerce_email_content_type', $content_type, $this, $default_content_type);
    }
    public function get_title()
    {
        return apply_filters('woocommerce_email_title', $this->title, $this);
    }
    public function get_description()
    {
        return apply_filters('woocommerce_email_description', $this->description, $this);
    }
    public function get_option($key, $empty_value = null)
    {
        $value = parent::get_option($key, $empty_value);
        return apply_filters('woocommerce_email_get_option', $value, $this, $value, $key, $empty_value);
    }
    public function is_enabled()
    {
        return apply_filters('woocommerce_email_enabled_' . $this->id, 'yes' === $this->enabled, $this->object, $this);
    }
    public function is_manual()
    {
        return $this->manual;
    }
    public function is_customer_email()
    {
        return $this->customer_email;
    }
    public function get_blogname()
    {
        return wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    }
    public function get_content()
    {
        $this->sending = true;
        if ('plain' === $this->get_email_type()) {
            $email_content = wordwrap(preg_replace($this->plain_search, $this->plain_replace, wp_strip_all_tags($this->get_content_plain())), 70);
        } else {
            $email_content = $this->get_content_html();
        }
        return $email_content;
    }
    public function style_inline($content)
    {
        if (in_array($this->get_content_type(), array('text/html', 'multipart/alternative'), true)) {
            ob_start();
            wc_get_template('emails/email-styles.php');
            $css = apply_filters('woocommerce_email_styles', ob_get_clean(), $this);
            $emogrifier_class = 'Pelago\\Emogrifier';
            if ($this->supports_emogrifier() && class_exists($emogrifier_class)) {
                try {
                    $emogrifier = new $emogrifier_class($content, $css);
                    do_action('woocommerce_emogrifier', $emogrifier, $this);
                    $content = $emogrifier->emogrify();
                    $html_prune = \Pelago\Emogrifier\HtmlProcessor\HtmlPruner::fromHtml($content);
                    $html_prune->removeElementsWithDisplayNone();
                    $content = $html_prune->render();
                } catch (Exception $e) {
                    $logger = wc_get_logger();
                    $logger->error($e->getMessage(), array('source' => 'emogrifier'));
                }
            } else {
                $content = '<style type="text/css">' . $css . '</style>' . $content;
            }
        }
        return $content;
    }
    protected function supports_emogrifier()
    {
        return class_exists('DOMDocument');
    }
    public function get_content_plain()
    {
        return '';
    }
    public function get_content_html()
    {
        return '';
    }
    public function get_from_name($from_name = '')
    {
        $from_name = apply_filters('woocommerce_email_from_name', get_option('woocommerce_email_from_name'), $this, $from_name);
        return wp_specialchars_decode(esc_html($from_name), ENT_QUOTES);
    }
    public function get_from_address($from_email = '')
    {
        $from_email = apply_filters('woocommerce_email_from_address', get_option('woocommerce_email_from_address'), $this, $from_email);
        return sanitize_email($from_email);
    }
    public function send($to, $subject, $message, $headers, $attachments)
    {
        add_filter('wp_mail_from', array($this, 'get_from_address'));
        add_filter('wp_mail_from_name', array($this, 'get_from_name'));
        add_filter('wp_mail_content_type', array($this, 'get_content_type'));
        $message = apply_filters('woocommerce_mail_content', $this->style_inline($message));
        $mail_callback = apply_filters('woocommerce_mail_callback', 'wp_mail', $this);
        $mail_callback_params = apply_filters('woocommerce_mail_callback_params', array($to, $subject, $message, $headers, $attachments), $this);
        $return = $mail_callback(...$mail_callback_params);
        remove_filter('wp_mail_from', array($this, 'get_from_address'));
        remove_filter('wp_mail_from_name', array($this, 'get_from_name'));
        remove_filter('wp_mail_content_type', array($this, 'get_content_type'));
        do_action('woocommerce_email_sent', $return, $this->id, $this);
        return $return;
    }
    public function init_form_fields()
    {
        $placeholder_text = sprintf(__('Available placeholders: %s', 'woocommerce'), '<code>' . esc_html(implode('</code>, <code>', array_keys($this->placeholders))) . '</code>');
        $this->form_fields = array('enabled' => array('title' => __('Enable/Disable', 'woocommerce'), 'type' => 'checkbox', 'label' => __('Enable this email notification', 'woocommerce'), 'default' => 'yes'), 'subject' => array('title' => __('Subject', 'woocommerce'), 'type' => 'text', 'desc_tip' => true, 'description' => $placeholder_text, 'placeholder' => $this->get_default_subject(), 'default' => ''), 'heading' => array('title' => __('Email heading', 'woocommerce'), 'type' => 'text', 'desc_tip' => true, 'description' => $placeholder_text, 'placeholder' => $this->get_default_heading(), 'default' => ''), 'additional_content' => array('title' => __('Additional content', 'woocommerce'), 'description' => __('Text to appear below the main email content.', 'woocommerce') . ' ' . $placeholder_text, 'css' => 'width:400px; height: 75px;', 'placeholder' => __('N/A', 'woocommerce'), 'type' => 'textarea', 'default' => $this->get_default_additional_content(), 'desc_tip' => true), 'email_type' => array('title' => __('Email type', 'woocommerce'), 'type' => 'select', 'description' => __('Choose which format of email to send.', 'woocommerce'), 'default' => 'html', 'class' => 'email_type wc-enhanced-select', 'options' => $this->get_email_type_options(), 'desc_tip' => true));
    }
    public function get_email_type_options()
    {
        $types = array('plain' => __('Plain text', 'woocommerce'));
        if (class_exists('DOMDocument')) {
            $types['html'] = __('HTML', 'woocommerce');
            $types['multipart'] = __('Multipart', 'woocommerce');
        }
        return $types;
    }
    public function process_admin_options()
    {
        parent::process_admin_options();
        $post_data = $this->get_post_data();
        if (isset($post_data['template_html_code'])) {
            $this->save_template($post_data['template_html_code'], $this->template_html);
        }
        if (isset($post_data['template_plain_code'])) {
            $this->save_template($post_data['template_plain_code'], $this->template_plain);
        }
    }
    public function get_template($type)
    {
        $type = basename($type);
        if ('template_html' === $type) {
            return $this->template_html;
        } elseif ('template_plain' === $type) {
            return $this->template_plain;
        }
        return '';
    }
    protected function save_template($template_code, $template_path)
    {
        if (current_user_can('edit_themes') && !empty($template_code) && !empty($template_path)) {
            $saved = false;
            $file = get_stylesheet_directory() . '/' . WC()->template_path() . $template_path;
            $code = wp_unslash($template_code);
            if (is_writeable($file)) {
                $f = fopen($file, 'w+');
                if (false !== $f) {
                    fwrite($f, $code);
                    fclose($f);
                    $saved = true;
                }
            }
            if (!$saved) {
                $redirect = add_query_arg('wc_error', rawurlencode(__('Could not write to template file.', 'woocommerce')));
                wp_safe_redirect($redirect);
                exit;
            }
        }
    }
    public function get_theme_template_file($template)
    {
        return get_stylesheet_directory() . '/' . apply_filters('woocommerce_template_directory', 'woocommerce', $template) . '/' . $template;
    }
    protected function move_template_action($template_type)
    {
        $template = $this->get_template($template_type);
        if (!empty($template)) {
            $theme_file = $this->get_theme_template_file($template);
            if (wp_mkdir_p(dirname($theme_file)) && !file_exists($theme_file)) {
                $core_file = $this->template_base . $template;
                $template_file = apply_filters('woocommerce_locate_core_template', $core_file, $template, $this->template_base, $this->id);
                copy($template_file, $theme_file);
                do_action('woocommerce_copy_email_template', $template_type, $this);
                ?>
				<div class="updated">
					<p><?php 
                echo esc_html__('Template file copied to theme.', 'woocommerce');
                ?></p>
				</div>
				<?php 
            }
        }
    }
    protected function delete_template_action($template_type)
    {
        $template = $this->get_template($template_type);
        if ($template) {
            if (!empty($template)) {
                $theme_file = $this->get_theme_template_file($template);
                if (file_exists($theme_file)) {
                    unlink($theme_file);
                    do_action('woocommerce_delete_email_template', $template_type, $this);
                    ?>
					<div class="updated">
						<p><?php 
                    echo esc_html__('Template file deleted from theme.', 'woocommerce');
                    ?></p>
					</div>
					<?php 
                }
            }
        }
    }
    protected function admin_actions()
    {
        if ((!empty($this->template_html) || !empty($this->template_plain)) && (!empty($_GET['move_template']) || !empty($_GET['delete_template'])) && 'GET' === $_SERVER['REQUEST_METHOD']) {
            if (empty($_GET['_wc_email_nonce']) || !wp_verify_nonce(wc_clean(wp_unslash($_GET['_wc_email_nonce'])), 'woocommerce_email_template_nonce')) {
                wp_die(esc_html__('Action failed. Please refresh the page and retry.', 'woocommerce'));
            }
            if (!current_user_can('edit_themes')) {
                wp_die(esc_html__('You don&#8217;t have permission to do this.', 'woocommerce'));
            }
            if (!empty($_GET['move_template'])) {
                $this->move_template_action(wc_clean(wp_unslash($_GET['move_template'])));
            }
            if (!empty($_GET['delete_template'])) {
                $this->delete_template_action(wc_clean(wp_unslash($_GET['delete_template'])));
            }
        }
    }
    public function admin_options()
    {
        $this->admin_actions();
        ?>
		<h2><?php 
        echo esc_html($this->get_title());
        ?> <?php 
        wc_back_link(__('Return to emails', 'woocommerce'), admin_url('admin.php?page=wc-settings&tab=email'));
        ?></h2>

		<?php 
        echo wpautop(wp_kses_post($this->get_description()));
        ?>

		<?php 
        do_action('woocommerce_email_settings_before', $this);
        ?>

		<table class="form-table">
			<?php 
        $this->generate_settings_html();
        ?>
		</table>

		<?php 
        do_action('woocommerce_email_settings_after', $this);
        ?>

		<?php 
        if (current_user_can('edit_themes') && (!empty($this->template_html) || !empty($this->template_plain))) {
            ?>
			<div id="template">
				<?php 
            $templates = array('template_html' => __('HTML template', 'woocommerce'), 'template_plain' => __('Plain text template', 'woocommerce'));
            foreach ($templates as $template_type => $title) {
                $template = $this->get_template($template_type);
                if (empty($template)) {
                    continue;
                }
                $local_file = $this->get_theme_template_file($template);
                $core_file = $this->template_base . $template;
                $template_file = apply_filters('woocommerce_locate_core_template', $core_file, $template, $this->template_base, $this->id);
                $template_dir = apply_filters('woocommerce_template_directory', 'woocommerce', $template);
                ?>
					<div class="template <?php 
                echo esc_attr($template_type);
                ?>">
						<h4><?php 
                echo wp_kses_post($title);
                ?></h4>

						<?php 
                if (file_exists($local_file)) {
                    ?>
							<p>
								<a href="#" class="button toggle_editor"></a>

								<?php 
                    if (is_writable($local_file)) {
                        ?>
									<a href="<?php 
                        echo esc_url(wp_nonce_url(remove_query_arg(array('move_template', 'saved'), add_query_arg('delete_template', $template_type)), 'woocommerce_email_template_nonce', '_wc_email_nonce'));
                        ?>" class="delete_template button">
										<?php 
                        esc_html_e('Delete template file', 'woocommerce');
                        ?>
									</a>
								<?php 
                    }
                    ?>

								<?php 
                    printf(esc_html__('This template has been overridden by your theme and can be found in: %s.', 'woocommerce'), '<code>' . esc_html(trailingslashit(basename(get_stylesheet_directory())) . $template_dir . '/' . $template) . '</code>');
                    ?>
							</p>

							<div class="editor" style="display:none">
								<textarea class="code" cols="25" rows="20"
								<?php 
                    if (!is_writable($local_file)) {
                        ?>
									readonly="readonly" disabled="disabled"
								<?php 
                    } else {
                        ?>
									data-name="<?php 
                        echo esc_attr($template_type) . '_code';
                        ?>"<?php 
                    }
                    ?>><?php 
                    echo esc_html(file_get_contents($local_file));
                    ?></textarea>
							</div>
						<?php 
                } elseif (file_exists($template_file)) {
                    ?>
							<p>
								<a href="#" class="button toggle_editor"></a>

								<?php 
                    $emails_dir = get_stylesheet_directory() . '/' . $template_dir . '/emails';
                    $templates_dir = get_stylesheet_directory() . '/' . $template_dir;
                    $theme_dir = get_stylesheet_directory();
                    if (is_dir($emails_dir)) {
                        $target_dir = $emails_dir;
                    } elseif (is_dir($templates_dir)) {
                        $target_dir = $templates_dir;
                    } else {
                        $target_dir = $theme_dir;
                    }
                    if (is_writable($target_dir)) {
                        ?>
									<a href="<?php 
                        echo esc_url(wp_nonce_url(remove_query_arg(array('delete_template', 'saved'), add_query_arg('move_template', $template_type)), 'woocommerce_email_template_nonce', '_wc_email_nonce'));
                        ?>" class="button">
										<?php 
                        esc_html_e('Copy file to theme', 'woocommerce');
                        ?>
									</a>
								<?php 
                    }
                    ?>

								<?php 
                    printf(esc_html__('To override and edit this email template copy %1$s to your theme folder: %2$s.', 'woocommerce'), '<code>' . esc_html(plugin_basename($template_file)) . '</code>', '<code>' . esc_html(trailingslashit(basename(get_stylesheet_directory())) . $template_dir . '/' . $template) . '</code>');
                    ?>
							</p>

							<div class="editor" style="display:none">
								<textarea class="code" readonly="readonly" disabled="disabled" cols="25" rows="20"><?php 
                    echo esc_html(file_get_contents($template_file));
                    ?></textarea>
							</div>
						<?php 
                } else {
                    ?>
							<p><?php 
                    esc_html_e('File was not found.', 'woocommerce');
                    ?></p>
						<?php 
                }
                ?>
					</div>
				<?php 
            }
            ?>
			</div>

			<?php 
            wc_enqueue_js("jQuery( 'select.email_type' ).on( 'change', function() {\n\n\t\t\t\t\tvar val = jQuery( this ).val();\n\n\t\t\t\t\tjQuery( '.template_plain, .template_html' ).show();\n\n\t\t\t\t\tif ( val != 'multipart' && val != 'html' ) {\n\t\t\t\t\t\tjQuery('.template_html').hide();\n\t\t\t\t\t}\n\n\t\t\t\t\tif ( val != 'multipart' && val != 'plain' ) {\n\t\t\t\t\t\tjQuery('.template_plain').hide();\n\t\t\t\t\t}\n\n\t\t\t\t}).trigger( 'change' );\n\n\t\t\t\tvar view = '" . esc_js(__('View template', 'woocommerce')) . "';\n\t\t\t\tvar hide = '" . esc_js(__('Hide template', 'woocommerce')) . "';\n\n\t\t\t\tjQuery( 'a.toggle_editor' ).text( view ).on( 'click', function() {\n\t\t\t\t\tvar label = hide;\n\n\t\t\t\t\tif ( jQuery( this ).closest(' .template' ).find( '.editor' ).is(':visible') ) {\n\t\t\t\t\t\tvar label = view;\n\t\t\t\t\t}\n\n\t\t\t\t\tjQuery( this ).text( label ).closest(' .template' ).find( '.editor' ).slideToggle();\n\t\t\t\t\treturn false;\n\t\t\t\t} );\n\n\t\t\t\tjQuery( 'a.delete_template' ).on( 'click', function() {\n\t\t\t\t\tif ( window.confirm('" . esc_js(__('Are you sure you want to delete this template file?', 'woocommerce')) . "') ) {\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn false;\n\t\t\t\t});\n\n\t\t\t\tjQuery( '.editor textarea' ).on( 'change', function() {\n\t\t\t\t\tvar name = jQuery( this ).attr( 'data-name' );\n\n\t\t\t\t\tif ( name ) {\n\t\t\t\t\t\tjQuery( this ).attr( 'name', name );\n\t\t\t\t\t}\n\t\t\t\t});");
        }
    }
}