<?php
if (! defined('ABSPATH')) exit; // Exit if accessed directly
global $yeepdf_settings_backend_main;
class Yeepdf_Settings_Builder_PDF_Backend
{
    function __construct()
    {
        add_action('admin_enqueue_scripts', array($this, 'style'));
        add_action('admin_head', array($this, 'add_font'));
        add_action('init', array($this, 'create_posttype'));
        add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
        add_filter('get_sample_permalink_html', array($this, 'remove_permalink'));
        add_action('save_post_yeepdf', array($this, 'save_metabox'), 10, 2);
        add_filter('admin_body_class', array($this, 'body_class'));
        add_action('admin_footer', array($this, "add_page_templates"));
        add_filter('post_row_actions', array($this, "duplicate_post_link"), 10, 2);
        add_action("yeepdf_builder_tab__editor_before", array($this, "yeepdf_builder_tab__editor"), 1);
        add_action("yeepdf_header_settings", array($this, "add_header_settings"), 10);
        add_action("yeepdf_footer_settings", array($this, "add_header_settings"), 10);
        add_action("yeepdf_watermark_text_settings", array($this, "add_header_settings"), 10);
        add_action("yeepdf_watermark_img_settings", array($this, "add_header_settings"), 10);
    }
    function add_header_settings($pdfs)
    {
        echo wp_kses_post($this->get_text_pro());
    }
    function get_text_pro()
    {
        $text = '<div class="pro_disable pro_disable_fff">Upgrade to pro version</div>';
        return $text;
    }
    public static function check_pro()
    {
        return true;
    }
    function yeepdf_builder_tab__editor($post)
    {
        $post_id = $post->ID;
        $sizes = array(
            "A Sizes" => array(
                "A0" => "A0 (841 x 1189mm)",
                "A1" => "A1 (594 x 841mm)",
                "A2" => "A2 (420 x 594mm)",
                "A3" => "A3 (297 x 420mm)",
                "A4" => "A4 (210 x 297mm)",
                "A5" => "A5 (148 x 210mm)",
                "A6" => "A6 (105 x 148mm)",
                "A7" => "A7 (74 x 105mm)",
                "A8" => "A8 (52 x 74mm)",
                "A9" => "A9 (37 x 52mm)",
                "A10" => "A10 (26 x 37mm)",
            ),
            "B sizes" => array(
                "B0" => "B0 (1414 x 1000mm)",
                "B1" => "B1 (1000 x 707mm)",
                "B2" => "B2 (707 x 500mm)",
                "B3" => "B3 (500 x 353mm)",
                "B4" => "B4 (353 x 250mm)",
                "B5" => "B5 (250 x 176mm)",
                "B6" => "B6 (176 x 125mm)",
                "B7" => "B7 (125 x 88mm)",
                "B8" => "B8 (88 x 62mm)",
                "B9" => "B9 (62 x 44mm)",
                "B10" => "B10 (44 x 31mm)",
            ),
            "Custom Sizes" => apply_filters("yeepdf_custom_sizes", array()),
        );
        $list_tempates = array();
        $args = array(
            'numberposts' => -1,
            'post_type'   => 'yeepdf',
            'exclude'   => array($post_id)
        );
        $post_list = get_posts($args);
        foreach ($post_list as $post) {
            $list_tempates[$post->ID] = $post->post_title;
        }
        $list_fonts = Yeepdf_Settings_Main::get_list_fonts();
        $pdfs = get_post_meta($post_id, "_builder_pdf_settings", true);
        $font_family = get_post_meta($post_id, "_builder_pdf_settings_font_family", true);
        if (!$font_family) {
            $font_family = "dejavusans";
        }
        if (!is_array($pdfs)) {
            $pdfs = array("dpi" => 96, "size" => "A4", "orientation" => "P", "show_page" => "");
        }
?>
        <div class="builder__editor--item builder__editor--item-settings">
            <label><?php esc_html_e("Settings", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
                    ?></label>
            <div class="yeepdf_setting_group">
                <div class="yeepdf_setting_row">
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("DPI", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
                                                                ?></label>
                        <input name="builder_pdf_settings[dpi]" type="text" class="yeepdf_setting_input" value="<?php echo esc_attr($pdfs["dpi"]) ?>">
                    </div>
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Orientation", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
                                                                ?></label>
                        <select name="builder_pdf_settings[orientation]" class="yeepdf_setting_input">
                            <option value="P"><?php esc_html_e("Portrait", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
                                                ?></option>
                            <option <?php selected($pdfs["orientation"], "L") ?> value="L"><?php esc_html_e("Landscape", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
                                                                                            ?></option>
                        </select>
                    </div>
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Paper Size", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
                                                                ?></label>
                        <select name="builder_pdf_settings[size]" class="yeepdf_setting_input">
                            <?php
                            foreach ($sizes as $group => $options) {
                                echo wp_kses_post('<optgroup label="' . $group . '">');
                                foreach ($options as $key => $value) {
                                    $check = "";
                                    if ($pdfs["size"] == $key) {
                                        $check = "selected";
                                    }
                            ?>
                                    <option <?php echo esc_attr($check) ?> value="<?php echo esc_attr($key) ?>"><?php echo esc_html($value) ?></option>
                            <?php
                                }
                                echo wp_kses_post('</optgroup>');
                            }
                            ?>
                        </select>
                    </div>
                </div>
                <div class="yeepdf_setting_row">
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Font family", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                ?></label>
                        <select class="font_family yeepdf_setting_input" name="builder_pdf_settings_font_family">
                            <?php
                            foreach ($list_fonts as $font => $vl) {
                            ?>
                                <option style="font-family: <?php echo esc_attr($font) ?>" <?php selected($font_family, $font) ?> value="<?php echo esc_attr($font) ?>"><?php echo esc_attr($font) ?></option>
                            <?php
                            }
                            $google_fonts = get_option("pdf_custom_fonts", array());
                            foreach ($google_fonts as $font => $vl) {
                            ?>
                                <option style="font-family: '<?php echo esc_attr($font) ?>'" <?php selected($font_family, $font) ?> value="<?php echo esc_attr($font) ?>"><?php echo esc_attr($font) ?></option>
                            <?php
                            }
                            ?>
                        </select>
                    </div>
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Font size", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                ?></label>
                        <input type="number" class="font-size-main">
                    </div>
                    <div class="yeepdf_settings_group-wrapper">
                        <?php Yeepdf_Editor::get_color_pick(esc_html__("Font color", 'pdf-for-woocommerce')) //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                        ?>
                    </div>
                </div>
                <div class="yeepdf_setting_row">
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Header Template", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                ?></label>
                        <?php
                        do_action("yeepdf_header_settings", $pdfs, $list_tempates);
                        ?>
                    </div>
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Footer Template", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                ?></label>
                        <?php
                        do_action("yeepdf_footer_settings", $pdfs, $list_tempates);
                        ?>
                    </div>
                </div>
                <div class="yeepdf_setting_row">
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Watermark text", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                ?></label>
                        <?php
                        do_action("yeepdf_watermark_text_settings", $pdfs);
                        ?>
                    </div>
                </div>
                <div class="yeepdf_setting_row">
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Watermark image", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                ?></label>
                        <?php
                        do_action("yeepdf_watermark_img_settings", $pdfs);
                        ?>
                    </div>
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Upload image", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                ?></label>
                        <input type="button" class="upload-editor--image-ok button button-primary" value="Upload">
                    </div>
                </div>
                <div class="yeepdf_setting_row">
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Custom CSS", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                ?></label>
                        <?php
                        $css = "";
                        if (isset($pdfs["css"])) {
                            $css = $pdfs["css"];
                        }
                        ?>
                        <textarea rows="4" name="builder_pdf_settings[css]" class="setting-custom-css"><?php echo esc_textarea($css) ?></textarea>
                    </div>
                </div>
                <div class="yeepdf_setting_row">
                    <div class="yeepdf_settings_group-wrapper">
                        <label class="yeepdf_checkbox_label"><?php esc_html_e("Disable Active Form", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                ?></label>
                        <?php
                        $css = "";
                        if (isset($pdfs["disable_active_form"]) && $pdfs["disable_active_form"] == "yes") {
                            $disable_active_form = "yes";
                        } else {
                            $disable_active_form = "no";
                        }
                        ?>
                        <div class="yeepdf_setting_input-wrapper">
                            <label class="yeepdf-switch">
                                <input name="builder_pdf_settings[disable_active_form]" type="checkbox" <?php checked($disable_active_form, "yes") ?> value="yes">
                                <span class="yeepdf-slider yeepdf-round"></span>
                            </label>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php
    }
    function email_builder_main($post)
    {
        $post_id = $post->ID;
        wp_nonce_field("_yeepdf_check_nonce", "_yeepdf_check_nonce");
    ?>
        <div id="builder-header">
            <div id="header-right">
                <div class="header-right-l">
                    <?php do_action("yeepdf_head_settings", $post) ?>
                    <div class="button-icon yeepdf-builder-choose-shortcodes" title="Shortcodes">
                        <span class="dashicons dashicons-shortcode"></span>
                    </div>
                    <div class="button-icon yeepdf-builder-choose-blank" title="Blank The Template">
                        <span class="dashicons dashicons-media-default"></span>
                    </div>
                </div>
                <div class="header-right-r">
                    <div class="" title="Templates">
                        <a href="#" class="button yeepdf-email-choose-template">
                            <?php esc_html_e("Templates", "pdf-for-woocommerce") //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
                            ?></a>
                    </div>
                    <div class="" title="Import Template">
                        <a href="#" class="button yeepdf-email-import"><span class="dashicons dashicons-upload"></span></a>
                    </div>
                    <div class="" title="Export Template">
                        <a href="#" class="button yeepdf-email-export"><span
                                class="dashicons dashicons-download"></span></a>
                    </div>
                    <div class="">
                        <?php
                        $oder_id = 0;
                        if (class_exists("Yeepdf_Woocommerce_Backend")) {
                            $oder_id = get_option("_yeepdf_woocommerce_demo");
                            $url = add_query_arg(array("pdf_preview" => "preview", "preview" => 1, "id" => $post_id, "woo_order" => $oder_id), get_home_url());
                        } else {
                            $url = add_query_arg(array("pdf_preview" => "preview", "preview" => 1, "id" => $post_id), get_home_url());
                        }
                        ?>
                        <a class="button yeepdf-email-choose-preview " target="_blank" href="<?php echo esc_url(wp_nonce_url($url, "yeepdf")) ?>"><?php esc_html_e("Preview", "pdf-for-woocommerce") //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
                                                                                                                                                    ?></a>
                    </div>
                    <div class="">
                        <a href="#" class="button button-yeepdf-save button-primary-ok"><?php esc_html_e("Save", "pdf-for-woocommerce") //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
                                                                                        ?></a>
                    </div>
                </div>
            </div>
            <div class="clear"></div>
        </div>
        <div class="yeepdf-builder-container">
            <div class="email-builder-side">
                <div class="builder__right">
                    <div class="builder__widget">
                        <div class="builder_main_info">
                            <div class="builder_main_info_text">
                                <?php esc_attr_e("YeePDF - PDF Customizer", "pdf-for-woocommerce") //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 
                                ?>
                            </div>
                            <div class="builder_main_info_icon" title="Go To Dashboard">
                                <a href="<?php echo esc_url(get_admin_url() . "edit.php?post_type=yeepdf") ?>"><span class="dashicons dashicons-wordpress"></span></a>
                            </div>
                        </div>
                        <ul class="builder__tab">
                            <li class="tab__block_elements"><a class="active" id="#tab__block"><span><?php esc_html_e("Elements", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                                                        ?></span> </a></li>
                            <li><a class="" id="#tab__editor"><span><?php esc_html_e("Editor", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                    ?></span></a></li>
                        </ul>
                        <div class="tab__inner">
                            <div class="tab__content active" id="tab__block">
                                <div class="builder__widget--inner">
                                    <div class="builder__widget_tab builder__widget_genaral">
                                        <div class="builder__widget_tab_title"><span
                                                class="builder__widget_tab_title_t"><?php esc_attr_e("Genaral", "pdf-for-woocommerce") //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                                    ?></span><span
                                                class="builder__widget_tab_title_icon dashicons dashicons-arrow-down-alt2"></span><span
                                                class="builder__widget_tab_title_icon dashicons dashicons-arrow-up-alt2"></span>
                                        </div>
                                        <ul class="momongaPresets momongaPresets_data">
                                            <?php do_action("yeepdf_builder_block") ?>
                                        </ul>
                                    </div>
                                </div>
                                <div class="builder__widget--inner">
                                    <div class="builder__widget_tab builder__widget_columns">
                                        <div class="builder__widget_tab_title"><span
                                                class="builder__widget_tab_title_t"><?php esc_attr_e("Columns", "pdf-for-woocommerce") //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                                    ?></span><span
                                                class="builder__widget_tab_title_icon dashicons dashicons-arrow-down-alt2"></span><span
                                                class="builder__widget_tab_title_icon dashicons dashicons-arrow-up-alt2"></span><span
                                                class="builder__widget_tab_title_icon dashicons dashicons-arrow-up-alt2"></span>
                                        </div>
                                        <ul class="builder-row-tool momongaPresets_data">
                                            <?php do_action("yeepdf_builder_tab_block_row") ?>
                                        </ul>
                                    </div>
                                </div>
                                <?php do_action("yeepdf_builder_tab_block_addons") ?>
                            </div>
                            <div class="tab__content" id="tab__editor">
                                <div class="builder__editor">
                                    <?php do_action("yeepdf_builder_tab__editor", $post) ?>
                                </div>
                            </div>
                            <div class="builder_main_footer">
                                <div class="builder_main_footer_text">
                                    <a href="<?php echo esc_url(get_dashboard_url()) ?>"><span
                                            class="dashicons dashicons-arrow-left-alt"></span>
                                        <?php esc_attr_e("BACK TO DASHBOARD", "pdf-for-woocommerce") //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                        ?></a>
                                </div>
                                <div class="builder_main_footer_icon">
                                    <a href="#"
                                        class="button button-primary yeepdf_button_settings"><?php esc_attr_e("SETTINGS", "pdf-for-woocommerce") //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                                                ?></a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="email-builder-main" data-type="main">
                <div class="email-builder-main-change_backgroud" data-type="main"><i class="pdf-creator-icon icon-pencil"></i>
                    <?php esc_html_e("Settings PDF", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                    ?></div>
                <div class="builder__list builder__list--js">
                    <div class="builder-row-container builder__item">
                        <div style="background-color: #ffffff" data-background_full="not" data-type="row1"
                            class="builder-row-container-row builder-row-container-row1">
                            <div class="builder-row builder-row-empty">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
        $data_js = get_post_meta($post_id, 'data_email', true);
        if (is_array($data_js)) {
            $data_js = wp_json_encode($data_js);
        }
        ?>
        <textarea name="data_email" class="data_email hidden"><?php echo esc_textarea($data_js) ?></textarea>
        <script type="text/javascript">
            <?php
            $data = array();
            $datas = apply_filters("yeepdf_builder_block_html", $data);
            ?>
            var wp_builder_pdf = <?php echo wp_json_encode($datas) ?>
        </script>
        <style type="text/css">
            <?php
            do_action("yeepdf_add_css", $post_id);
            ?>
        </style>
        <?php
        wp_enqueue_media();
    }
    function style()
    {
        global $post;
        $add_libs = false;
        if ((isset($post->post_type) && $post->post_type == "yeepdf") || (isset($_GET["post_type"]) && $_GET["post_type"] == "yeepdf")) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
            $add_libs = true;
        }
        $add_libs = apply_filters("yeepdf_add_libs", $add_libs);
        if ($add_libs) {
            $ver = "7.0.0";
            //$ver = time();
            wp_enqueue_script('jquery');
            wp_enqueue_style('yeepdf-font', YEEPDF_CREATOR_BUILDER_URL . "backend/css/pdfcreator.css", array(), $ver);
            wp_enqueue_style('yeepdf-momonga', YEEPDF_CREATOR_BUILDER_URL . "backend/css/momonga.css", array("wp-jquery-ui-dialog", "wp-color-picker"), $ver);
            wp_enqueue_style('yeepdf-main', YEEPDF_CREATOR_BUILDER_URL . "backend/css/main.css", array(), $ver);
            wp_register_script('yeepdf_pdf_code_toggle', YEEPDF_CREATOR_BUILDER_URL . "backend/src/tinymce-ace.js", array());
            wp_register_script('yeepdf_main', YEEPDF_CREATOR_BUILDER_URL . "backend/src/main.js", array(), $ver);
            wp_register_script('yeepdf_builder', YEEPDF_CREATOR_BUILDER_URL . "backend/src/builder.js", array("yeepdf_main"), $ver);
            wp_register_script('yeepdf_editor', YEEPDF_CREATOR_BUILDER_URL . "backend/src/set_editor.js", array("yeepdf_main"), $ver);
            wp_register_script('table_resize', YEEPDF_CREATOR_BUILDER_URL . "backend/libs/table-resize/jquery.resizableColumns.js", array("jquery"), $ver, true);
            wp_enqueue_script('yeepdf_script', YEEPDF_CREATOR_BUILDER_URL . "backend/src/script.js", array("jquery", "yeepdf_main", "jquery-ui-core", "jquery-ui-dialog", "jquery-ui-sortable", "jquery-ui-draggable", "jquery-ui-droppable", "wp-color-picker", "wp-tinymce", "yeepdf_editor", "yeepdf_builder", "jquery-effects-core", "jquery-effects-scale", "yeepdf_pdf_code_toggle", "thickbox", "table_resize"), $ver);
            $list_fonts = Yeepdf_Settings_Main::get_list_fonts();
            $google_fonts = get_option("pdf_custom_fonts", array());
            $font_formats = "";
            foreach ($list_fonts as $k => $vl) {
                $font_formats =  $font_formats . $k . "=" . $k . ";";
            }
            foreach ($google_fonts as $k => $vl) {
                $font_formats =  $font_formats . $k . "=" . $k . ";";
            }
            $builder_shorcode = apply_filters("yeepdf_builder_shortcode", array());
            $shortcodes = Yeepdf_Builder_PDF_Shortcode::list_shortcodes();
            $builder_shorcode_re = "";
            $i = 0;
            foreach ($builder_shorcode as $k => $v) {
                if (strpos($k, "{") === 0) {
                    $k = str_replace(array("{", "}"), "", $k);
                    if ($i == 0) {
                        $builder_shorcode_re .= "\{" . $k . "\}";
                    } else {
                        $builder_shorcode_re .= "|\{" . $k . "\}";
                    }
                } else {
                    $k = str_replace(array("[", "]"), "", $k);
                    if ($i == 0) {
                        $builder_shorcode_re .= "\[" . $k . "\]";
                    } else {
                        $builder_shorcode_re .= "|\[" . $k . "\]";
                    }
                }
                $i++;
            }
            wp_localize_script(
                'yeepdf_script',
                'yeepdf_script',
                array(
                    'ajax_url' => admin_url('admin-ajax.php'),
                    'youtube_play_src' => 'pdf-for-woocommerce' . "images/youtube_play.png",
                    'yeepdf_url_plugin' => YEEPDF_CREATOR_BUILDER_URL,
                    'shortcodes' =>  $shortcodes,
                    'google_font_font_formats' => $font_formats,
                    'home_url' => get_home_url(),
                    'builder_shorcode' => $builder_shorcode,
                    'builder_shorcode_re' => $builder_shorcode_re,
                    'imagick_installed' => class_exists('Imagick'),
                )
            );
        }
    }
    function add_font()
    {
        global $post_type;
        if ("yeepdf" == $post_type || (isset($_GET["page"]) && $_GET["page"] == "yeepdf-settings")) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
            $upload_dir = wp_upload_dir();
            $path_main = $upload_dir['basedir'] . '/pdfs/fonts/';
            $defaultConfig     = (new Mpdf\Config\ConfigVariables())->getDefaults();
            $fontDirs = $defaultConfig['fontDir'];
            $fonts = Yeepdf_Settings_Main::get_list_fonts();
            $google_fonts = get_option("pdf_custom_fonts", array());
        ?>
            <style type="text/css">
                @font-face {
                    font-family: 'fontawesome';
                    font-style: normal;
                    font-weight: 400;
                    font-display: block;
                    src: url(<?php echo esc_url(YEEPDF_CREATOR_BUILDER_URL) ?>"vendor/mpdf/mpdf/ttfonts/fontawesome.ttf") format("truetype");
                }

                .fontawesome {
                    font-family: "fontawesome";
                }

                <?php foreach ($fonts as $key => $value) {
                    foreach ($value as $k => $vl) {
                        if (is_readable($fontDirs[0] . "/" . $vl)) {
                            $url = YEEPDF_CREATOR_BUILDER_URL . "vendor/mpdf/mpdf/ttfonts/" . $vl;
                        } else {
                            $url = false;
                        }
                        if ($url != "") {
                            $font_weight = "normal";
                            $font_style = "normal";
                            if ($k == "B") {
                                $font_weight = "bold";
                            } elseif ($k == "I") {
                                $font_style = "italic";
                            } elseif ($k == "BI") {
                                $font_style = "italic";
                                $font_weight = "bold";
                            }
                ?>@font-face {
                    font-family: '<?php echo esc_attr($key) ?>';
                    src: url(<?php echo esc_url($url);
                                ?>);
                    font-style: <?php echo esc_attr($font_style) ?>;
                    font-weight: <?php echo esc_attr($font_weight) ?>;
                }

                <?php
                        }
                    }
                }
                foreach ($google_fonts as $key => $value) {
                    foreach ($value as $k => $vl) {
                        if (is_readable($path_main . $vl)) {
                            $url = $upload_dir["baseurl"] . "/pdfs/fonts/" . $vl;
                        } else {
                            $url = false;
                        }
                        if ($url != "") {
                            $font_weight = "normal";
                            $font_style = "normal";
                            if ($k == "B") {
                                $font_weight = "bold";
                            } elseif ($k == "I") {
                                $font_style = "italic";
                            } elseif ($k == "BI") {
                                $font_style = "italic";
                                $font_weight = "bold";
                            }
                ?>@font-face {
                    font-family: '<?php echo esc_attr($key) ?>';
                    src: url(<?php echo esc_url($url);
                                ?>);
                    font-style: <?php echo esc_attr($font_style) ?>;
                    font-weight: <?php echo esc_attr($font_weight) ?>;
                }

                <?php
                        }
                    }
                }
                ?>
            </style>
        <?php
        }
    }
    function create_posttype()
    {
        register_post_type(
            'yeepdf',
            array(
                'labels' => array(
                    'name' => esc_html__('PDF Templates', 'pdf-for-woocommerce'), //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                    'add_new' => esc_html__('New Template', 'pdf-for-woocommerce'), //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                    'new_item' => esc_html__('New Template', 'pdf-for-woocommerce'), //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                    'add_new_item' => esc_html__('New Template', 'pdf-for-woocommerce'), //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                    'singular_name' => esc_html__('yeepdfs', 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                ),
                'public' => false,
                'has_archive' => true,
                'supports'    => array('title'),
                'show_in_menu' => true,
                'show_ui' => true,
                'show_in_rest' => true,
                'menu_icon'           => 'dashicons-email',
                'menu_position' => 100,
                'exclude_from_search' => true,
                'publicly_queryable' => false,
                'query_var' => false
            )
        );
    }
    function yeepdf_allow_custom_css($styles)
    {
        $styles[] = 'position';
        $styles[] = 'z-index';
        $styles[] = 'top';
        $styles[] = 'right';
        $styles[] = 'bottom';
        $styles[] = 'left';
        $styles[] = 'width';
        $styles[] = 'min-width';
        $styles[] = 'max-width';
        $styles[] = 'height';
        $styles[] = 'min-height';
        $styles[] = 'max-height';
        $styles[] = 'display';
        $styles[] = 'overflow';
        $styles[] = 'float';
        $styles[] = 'clear';
        $styles[] = 'margin';
        $styles[] = 'margin-top';
        $styles[] = 'margin-right';
        $styles[] = 'margin-bottom';
        $styles[] = 'margin-left';
        $styles[] = 'padding';
        $styles[] = 'padding-top';
        $styles[] = 'padding-right';
        $styles[] = 'padding-bottom';
        $styles[] = 'padding-left';

        $styles[] = 'border';
        $styles[] = 'border-collapse';
        $styles[] = 'border-top';
        $styles[] = 'border-right';
        $styles[] = 'border-bottom';
        $styles[] = 'border-left';
        $styles[] = 'border-radius';
        $styles[] = 'border-color';
        $styles[] = 'border-style';
        $styles[] = 'border-width';
        $styles[] = 'background';
        $styles[] = 'background-color';
        $styles[] = 'background-image';
        $styles[] = 'color';
        $styles[] = 'opacity';
        $styles[] = 'box-shadow';
        $styles[] = 'font-family';
        $styles[] = 'font-size';
        $styles[] = 'font-weight';
        $styles[] = 'font-style';
        $styles[] = 'text-align';
        $styles[] = 'text-transform';
        $styles[] = 'text-decoration';
        $styles[] = 'line-height';
        $styles[] = 'letter-spacing';
        $styles[] = 'white-space';
        $styles[] = 'cursor';
        $styles[] = 'vertical-align';
        return $styles;
    }
    function save_metabox($post_id, $post)
    {
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }
        if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
            return;
        }
        if (!($post instanceof WP_Post) || $post->post_type !== 'yeepdf') {
            return;
        }
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }
        if (!isset($_POST['_yeepdf_check_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_yeepdf_check_nonce'])), '_yeepdf_check_nonce')) {
            return;
        }
        if (isset($_POST['data_email'])) {
            $raw = wp_unslash($_POST['data_email']); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
            $raw = is_string($raw) ? trim($raw) : '';
            if ($raw !== '') {
                $decoded = json_decode($raw, true);
                // Only accept valid JSON (array/object). Reject scalars and invalid JSON.
                if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
                    //add_filter('safe_style_css', array($this, 'yeepdf_allow_custom_css'));
                    //$clean_data = map_deep($decoded, 'wp_kses_post');
                    $clean_data = $decoded;
                    //remove_filter('safe_style_css', array($this, 'yeepdf_allow_custom_css'));
                    update_post_meta($post_id, 'data_email', $clean_data);
                } else {
                }
            }
        }
        if (isset($_POST['builder_pdf_settings_font_family'])) {
            $builder_pdf_settings_font_family = sanitize_text_field(wp_unslash($_POST['builder_pdf_settings_font_family']));
            update_post_meta($post_id, '_builder_pdf_settings_font_family', $builder_pdf_settings_font_family);
        }
        if (isset($_POST['builder_pdf_settings'])) {
            $datas = array();
            if (is_array($_POST["builder_pdf_settings"])) { //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
                foreach ($_POST["builder_pdf_settings"] as $key => $value) { //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
                    $datas[sanitize_key($key)] = sanitize_textarea_field(wp_unslash($value));
                }
                update_post_meta($post_id, '_builder_pdf_settings', $datas);
            }
        }
    }
    function remove_permalink($link)
    {
        global $post_type;
        if ('yeepdf' === $post_type) {
            return "";
        } else {
            return $link;
        }
    }
    function add_meta_boxes()
    {
        add_meta_box(
            'email-builder-main',
            esc_html__('Builder PDF', 'pdf-for-woocommerce'), //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
            array($this, 'email_builder_main'),
            'yeepdf',
            'normal',
            'default'
        );
    }
    function body_class($classes)
    {
        global $post_type;
        $screen = get_current_screen();
        if ('yeepdf' == $post_type && $screen->id == 'yeepdf') {
            return  $classes . " post-php"; //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
        } else {
            return  $classes;
        }
    }
    function add_page_templates()
    {
        ?>
        <div id="yeepdf-email-templates" style="display:none">
            <div class="list-view-templates">
                <?php
                $args = array(
                    "json" => "",
                    "img" => YEEPDF_CREATOR_BUILDER_URL . "backend/demo/template1/1.png",
                    "title" => "Email templates",
                    "cat" => array(),
                    "id" => 0,
                );
                do_action("builder_yeepdfs");
                ?>
            </div>
        </div>
        <div id="yeepdf-builder-shortcodes-templates" style="display:none">
            <div class="list-view-short-templates">
                <?php
                $shortcodes = Yeepdf_Builder_PDF_Shortcode::list_shortcodes();
                foreach ($shortcodes as $shortcode_k => $shortcode_v) {
                ?>
                    <h3><?php echo esc_html($shortcode_k) ?></h3>
                    <?php
                    foreach ($shortcode_v as $k => $v) {
                        if (is_array($v)) {
                    ?>
                            <h4><?php echo esc_html($k) ?></h4>
                            <?php
                            foreach ($v as $k_i => $v_i) {
                            ?>
                                <div class="list-view-short-templates-r">
                                    <div class="list-view-short-templates-k" title="Click to copy">
                                        <?php
                                        if (strpos($k_i, "{") === false) {
                                            echo esc_html("[" . $k_i . "]");
                                        } else {
                                            echo esc_html($k_i);
                                        }
                                        ?>
                                    </div>
                                    <div class="list-view-short-templates-v">
                                        <?php echo esc_html($v_i) ?>
                                    </div>
                                </div>
                            <?php
                            }
                        } else {
                            ?>
                            <div class="list-view-short-templates-r">
                                <div class="list-view-short-templates-k" title="Click to copy">
                                    <?php
                                    if (strpos($k, "{") === false) {
                                        echo esc_html("[" . $k . "]");
                                    } else {
                                        echo esc_html($k);
                                    }
                                    ?>
                                </div>
                                <div class="list-view-short-templates-v">
                                    <?php echo esc_html($v) ?>
                                </div>
                            </div>
                <?php
                        }
                    }
                }
                ?>
            </div>
        </div>
    <?php
    }
    public static function item_demo($args1)
    {
        $defaults = array(
            "json" => "",
            "img" => YEEPDF_CREATOR_BUILDER_URL . "backend/demo/template1/1.png",
            "title" => "PDF templates",
            "url" => "#",
            "id" => 0,
            "cat" => array(),
        );
        $args = wp_parse_args($args1, $defaults);
        $domain = "https://pdf.add-ons.org/";
        $url_view = $domain . "?pdf_preview=preview&id=" . $args["id"] . "&woo_order=18";
        $url_design = $domain . "?templates_id=" . $args["id"];
    ?>
        <div class="grid-item" data-file="<?php echo esc_url($args["json"]) ?>">
            <img src="<?php echo esc_url($args["img"]) ?>">
            <div class="demo_content">
                <div class="demo-title"><?php echo esc_html($args["title"]) ?></div>
                <div class="demo-tags"><?php echo esc_html(implode(", ", $args["cat"])) ?></div>
                <div class="yeepdf-email-actions">
                    <div class="demo-fl">
                        <a class="button yeepdf-email-actions-import"
                            href="#"><?php esc_html_e("Import", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                        ?></a>
                        <a target="_blank" class="button yeepdf-email-actions-design"
                            href="<?php echo esc_url($url_design) ?>"><?php esc_html_e("Design", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                        ?></a>
                    </div>
                    <div class="demo-fr">
                        <a target="_blank" class="button yeepdf-email-actions-view"
                            href="<?php echo esc_url($url_view) ?>"><?php esc_html_e("Preview", 'pdf-for-woocommerce') //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
                                                                    ?></a>
                    </div>
                    <div class="clear"></div>
                </div>
            </div>
        </div>
<?php
    }
    function duplicate_post_link($actions, $post)
    {
        if ($post->post_type == 'yeepdf' && current_user_can('edit_posts')) {
            $actions['preview_template'] = '<a target="_blank" href="' . esc_url(get_home_url() . "?pdf_preview=preview&id=" . $post->ID) . '" title="Preview" rel="Preview">Preview</a>';
        }
        return $actions;
    }
}
$yeepdf_settings_backend_main = new Yeepdf_Settings_Builder_PDF_Backend;
