<?php
/*
Template Name: 问题页模板
*/

/*
 * 旧域名自动替换为新域名
 * 会处理：正文、评论、二维码、自定义字段、srcset、背景图片等页面输出内容。
 */
$new_domain = 'https://www.tripmilu.top';

$old_domains = array(
    'https://www.gochinatravel.top',
    'http://www.gochinatravel.top',
    'https://gochinatravel.top',
    'http://gochinatravel.top',
    '//www.gochinatravel.top',
    '//gochinatravel.top',
);

ob_start(function ($html) use ($old_domains, $new_domain) {
    return str_ireplace($old_domains, $new_domain, $html);
});

get_header();

$page_id = get_queried_object_id();

/*
 * 获取客服 ID
 */
$kefu_raw = get_post_meta($page_id, 'kefus', true);

$mykefu = array_values(
    array_filter(
        array_map('absint', explode('|', (string) $kefu_raw))
    )
);

$kefu_id = 0;

if (!empty($mykefu)) {
    $kefu_id = $mykefu[array_rand($mykefu)];
}

/*
 * 默认变量，防止字段为空时报错
 */
$kftel = '';
$wechat_id = '';
$kfwxm = '';

/*
 * 获取客服相关字段
 */
if ($kefu_id) {

    $kftel = wp_strip_all_tags(
        get_post_meta($kefu_id, 'myproducts_fg', true)
    );

    $wechat_id = trim(
        wp_strip_all_tags(
            get_post_meta($kefu_id, 'myproducts_mj', true)
        )
    );

    $pic_content = trim(
        (string) get_post_meta($kefu_id, 'myproductsPic', true)
    );

    /*
     * myproductsPic 可能直接保存图片 URL，
     * 也可能保存完整 <img> 标签。
     */
    if (preg_match('#^(https?:)?//#i', $pic_content)) {
        $kfwxm = $pic_content;
    } elseif (
        preg_match(
            '#<img[^>]+(?:src|data-src)=[\'"]([^\'"]+)[\'"]#i',
            $pic_content,
            $match
        )
    ) {
        $kfwxm = $match[1];
    }

    $kfwxm = html_entity_decode(
        $kfwxm,
        ENT_QUOTES,
        get_bloginfo('charset')
    );

    /*
     * 二维码地址立即替换一次旧域名
     */
    $kfwxm = str_ireplace($old_domains, $new_domain, $kfwxm);
}
?>

<!-- question-template-new-domain-test -->

<link
    type="text/css"
    href="<?php echo esc_url(get_template_directory_uri() . '/wenda.css'); ?>"
    rel="stylesheet"
/>

<div class="container askTem">

    <?php while (have_posts()) : the_post(); ?>

        <div class="askContent">
            <?php the_content(); ?>
        </div>

        <div class="authorBox">

            <p class="ninfo">
                <b>
                    <?php
                    if (function_exists('post_views')) {
                        post_views();
                    } else {
                        echo '0';
                    }
                    ?>
                </b>
                View

                <b><?php echo esc_html(number_format_i18n(get_comments_number())); ?></b>
                Reply
            </p>

            <?php
            $nickname = get_the_author_meta('display_name');
            $email = get_the_author_meta('user_email');

            echo get_avatar($email, 40);
            echo esc_html($nickname);
            ?>

            Time：<?php echo esc_html(get_the_date('Y-m-d')); ?>

        </div>

    <?php endwhile; ?>

    <div class="mycomment">
        <?php comments_template(); ?>
        <div class="clearfix"></div>
    </div>

    <div class="mycomment">

        <?php
        $login_url = wp_login_url(get_permalink($page_id));
        $register_url = wp_registration_url();

        $args = array(
            'must_log_in' =>
                '<p class="must-log-in">' .
                '<a href="' . esc_url($login_url) . '" data-target="#mylogin" data-toggle="modal">登陆</a>' .
                '后即可回答！立即' .
                '<a data-toggle="modal" data-target="#mylogin" href="' . esc_url($register_url) . '">注册</a>' .
                '</p>',
        );

        comment_form($args);
        ?>

        <div class="clearfix"></div>
    </div>

</div>

<script type="text/javascript">
jQuery(function ($) {

    function refreshScrollFix() {
        var $scrollBar = $(".scrollBar");
        var $breadcrumbs = $(".mybreadcrumbs");

        if (
            $scrollBar.length &&
            typeof $.fn.scrollFix === "function"
        ) {
            $scrollBar.scrollFix({
                distanceTop: ($breadcrumbs.outerHeight() || 0) + 50,
                endPos: ".commentStart"
            });
        }
    }

    refreshScrollFix();

    $(window).on("load", function () {
        refreshScrollFix();
    });

    if (typeof ClipboardJS !== "undefined") {
        var clipboard = new ClipboardJS(".weixinAct");

        clipboard.on("success", function (e) {
            alert("Copy successfully - open the address book - paste the id search to add friends to contact");
            e.clearSelection();
        });

        clipboard.on("error", function () {
            alert("WeChat ID copy failed");
        });
    }

});
</script>

<div class="md-modal md-effect-13" id="modal-13">

    <div class="md-content">

        <button class="md-close">
            <i class="glyphicon glyphicon-remove"></i>
        </button>

        <div class="title-style-0">
            Please scan the QR code to add WeChat.
        </div>

        <div class="userwxBox">

            <div class="userInfo">
                <i class="fa fa-user-o"></i>
                <h2>Wechat：<?php echo esc_html($wechat_id); ?></h2>
                <span>Certified</span>
            </div>

            <div class="img">

                <?php if (!empty($kfwxm)) : ?>
                    <img
                        src="<?php echo esc_url($kfwxm); ?>"
                        alt="WeChat QR Code"
                    />
                <?php else : ?>
                    <p>二维码图片未找到</p>
                <?php endif; ?>

                <p>Open WeChat and scan to add friends</p>

            </div>

        </div>

    </div>

</div>

<?php
get_footer();

/*
 * 输出整页，并在这里统一将旧域名替换成新域名
 */
ob_end_flush();
?>