PIX主题全站增加GO外链跳转提示

经过改造,PIX主题已经全站外链支持跳转提示。这里因为涉及文件较多,不建议新手小白改代码,可以安装相关的插件实现跳转功能。

跳转逻辑:外链分为文章POST内外链,侧边栏WIDGET外链,友链LINK外链和评论COMMNET外链,首先判断链接是否是外链(目标网址非本站域名判定为外链),如是则进行GO跳转提示。考虑到跳转后网址公开,所以对GO文件的跳转URL并未进行任何加密。

GO跳转的自我感受弊端:如果自己的站缓慢,则跳转也缓慢,甚至导致所有链接全挂,所以慎重!

强烈建议涉及文件修改前进行备份!!!强烈建议涉及文件修改前进行备份!!!强烈建议涉及文件修改前进行备份!!!

演示图如下:

PIX主题全站增加GO外链跳转提示-似水流年

改造开始:

1、在网站根目录下(注意不是主题目录)新建go.php,加入以下代码:

<?php
if (
    strlen($_SERVER['REQUEST_URI']) > 384 ||
    strpos($_SERVER['REQUEST_URI'], "eval(") ||
    strpos($_SERVER['REQUEST_URI'], "base64")
) {
    @header("HTTP/1.1 414 Request-URI Too Long");
    @header("Status: 414 Request-URI Too Long");
    @header("Connection: Close");
    @exit;
}
//通过QUERY_STRING取得完整的传入数据,然后取得url=之后的所有值,兼容性更好

@session_start();
$t_url = !empty($_SESSION['GOLINK']) ? $_SESSION['GOLINK'] : preg_replace('/^url=(.*)$/i', '$1', $_SERVER["QUERY_STRING"]);
//数据处理
if (!empty($t_url)) {
    //判断取值是否加密
    if ($t_url == base64_encode(base64_decode($t_url))) {
        $t_url =  base64_decode($t_url);
    }
    //防止xss
    $t_url =  htmlspecialchars($t_url);

    //对取值进行网址校验和判断
    preg_match('/^(http|https|thunder|qqdl|ed2k|Flashget|qbrowser):\/\//i', $t_url, $matches);
    if ($matches) {
        $url = $t_url;
        $title = '页面加载中,请稍候...';
    } else {
        preg_match('/\./i', $t_url, $matche);
        if ($matche) {
            $url = 'http://' . $t_url;
            $title = '页面加载中,请稍候...';
        } else {
            $url = 'http://' . $_SERVER['HTTP_HOST'];
            $title = '参数错误,正在返回首页...';
        }
    }
} else {
    $title = '参数缺失,正在返回首页...';
    $url = 'http://' . $_SERVER['HTTP_HOST'];
}
?>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="robots" content="noindex, nofollow" />
    <title><?php echo $title; ?></title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="renderer" content="webkit"/>
  <meta name="force-rendering" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
    <link rel="shortcut icon" type="image/ico" href="/favicon.ico">
    <script>
  function link_jump()
  {
    //禁止其他网站使用我们的跳转页面
    var MyHOST = new RegExp("<?php echo $_SERVER['HTTP_HOST']; ?>");
    if (!MyHOST.test(document.referrer)) {
       location.href="https://" + MyHOST;
    }
    location.href="<?php echo $url; ?>";
  }
    //延时1S跳转,可自行修改延时时间
  //setTimeout(link_jump, 1500);
  //延时50S关闭跳转页面,用于文件下载后不会关闭跳转页的问题
 setTimeout(function(){window.opener=null;window.close();}, 50000);
  </script>
<style>
      *,:after,:before{box-sizing:border-box}body.reader-black-font,body.reader-black-font .history-mode .view-area,body.reader-black-font .history-mode .view-area pre,body.reader-black-font .main .kalamu-area,body.reader-black-font .main .markdown .text,body.reader-black-font input,body.reader-black-font select,body.reader-black-font textarea{font-family:-apple-system,SF UI Text,Arial,PingFang SC,Hiragino Sans GB,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif}body{background:url(https://www.vxras.com/wp-content/uploads/2023/09/hx_bg@1x.png);}.ext-link__wrapper{position:absolute;width:620px;padding:40px 0;border-radius:20px;text-align:center;top:118px;left:50%;-webkit-transform:translateX(-50%);-ms-transform:translateX(-50%);transform:translateX(-50%);background-color:rgba(255, 255, 255, 0.5)!important;box-shadow:0 0 20px 0 #FFD1D8;overflow:hidden;background-image: url(https://s1.hdslb.com/bfs/seed/jinkela/short/mini-login-v2/img/22_open.4ea5f239.png),url(https://s1.hdslb.com/bfs/seed/jinkela/short/mini-login-v2/img/33_open.f7d7f655.png);background-position: 0 100%,100% 100%;background-repeat: no-repeat,no-repeat;background-size: 20%;}.title{font-size:22px;color:#2f2f2f;}.sub-title{font-size:16px;color:#888;margin-top:8px}.link-bd{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-align:center;-webkit-align-items:center;-ms-flex-align:center;align-items:center;width:460px;margin:12px auto 0;padding:10px;border-radius:50px;background:#ffebfb;border:1px solid #f5dcdc;zoom:1}.link-bd:after,.link-bd:before{content:" ";display:table}.link-bd .link-bd__icon{-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;display:flex;align-items:center;width:40px;justify-content:center;height:40px;line-height:40px;font-size:20px;text-align:center;border-radius:2px}.link-btn{text-align:center;font-size:0;margin-top:24px}.link-bd .link-bd__text{font-size:14px;color:#ff74b2;margin-left:10px;overflow:hidden;-o-text-overflow:ellipsis;text-overflow:ellipsis;white-space:nowrap}.link-btn__text{display:inline-block;width:144px;height:44px;line-height:43px;border-radius:22px;font-size:14px;color:#ff74b2;border:1px solid #ff74b2;cursor:pointer;text-decoration:none}.link-btn__text:hover{color:#fff;background:#ff74b2}
.alert-footer {
  margin: 0 auto;
  height: 90px;
  padding:30px 60px;
    display:inline-block;
}
.alert-footer-icon {
  float: left;
padding:10px 10px;
  
    text-align:right;
    
}
.alert-footer-text {
  float: right;
  border-left: 2px solid #EEE;
  padding: 3px 0 0 5px;
  height: 60px;
  color: #0B85CC;
  font-size: 12px;
  text-align: left;
  
}
.alert-footer-text p {
  color: #7A7A7A;
  font-size: 22px;
  line-height: 18px;
  margin-top: 0px;
}
.ext-link__wrapper>img{
    width:20%;
}

@media screen and (max-width: 768px){
 .link-bd,.ext-link__wrapper{
     width:90%;
 }   
.link-btn__text{
    width:40%;
}
    
}

</style>
</head>
<body>    
<div class="ext-link__wrapper">
        <?php echo '<img src="https://images.my1981.cn/wp-content/uploads/2023/09/logo.png" alt="Logo">'; ?>
        <div class="title">即将离开似水流年,跳转到外链网站!</div>
        <div class="sub-title">宝贝!似水流年不能保证所有外链网站安全,需要您自行判断!是否继续?</div>
        <div class="link-bd speedlist">
            <div class="link-bd__icon"><svg t="1698155189995" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="13737" width="32" height="32"><path d="M398.45868 673.486553m-350.513448 0a350.513447 350.513447 0 1 0 701.026895 0 350.513447 350.513447 0 1 0-701.026895 0Z" fill="#FFD9D9" p-id="13738"></path><path d="M875.908068 100.146699m-100.146699 0a100.146699 100.146699 0 1 0 200.293399 0 100.146699 100.146699 0 1 0-200.293399 0Z" fill="#FFD9D9" p-id="13739"></path><path d="M875.908068 203.047433h-726.063569a12.518337 12.518337 0 0 1-12.518338-12.518338V80.86846a12.518337 12.518337 0 0 1 12.518338-12.518338h726.063569a12.518337 12.518337 0 0 1 12.518338 12.518338v109.660635a12.518337 12.518337 0 0 1-12.518338 12.518338z m-714.045965-25.036675h701.026895V93.386797h-701.026895zM398.45868 662.470416a75.110024 75.110024 0 0 1-51.57555-23.033741 61.59022 61.59022 0 0 1-9.764304-85.375061l108.408802-108.408802a12.518337 12.518337 0 0 1 17.776039 0 12.017604 12.017604 0 0 1 0 17.525672l-108.659168 108.659169c-10.76577 10.76577-6.259169 33.799511 10.01467 50.073349s39.057213 20.78044 50.073349 10.01467L544.672861 500.733496c10.76577-10.76577 6.259169-33.549144-10.01467-50.073349a12.518337 12.518337 0 0 1 17.776039-17.776039 61.59022 61.59022 0 0 1 9.764303 85.375061l-130.691443 130.941809a46.818582 46.818582 0 0 1-33.04841 13.269438z" fill="#2D2F33" p-id="13740"></path><path d="M507.368215 490.718826a12.267971 12.267971 0 0 1-8.762836-3.755501 61.339853 61.339853 0 0 1-10.01467-85.124694l130.941809-130.941809a61.339853 61.339853 0 0 1 85.124695 10.014669 61.59022 61.59022 0 0 1 10.01467 85.375062L606.012714 475.696822a12.518337 12.518337 0 0 1-17.776039 0 12.518337 12.518337 0 0 1 0-17.525673l108.659169-108.659169a25.036675 25.036675 0 0 0 5.508068-21.030806 54.830318 54.830318 0 0 0-15.272372-29.042543c-16.273839-16.023472-39.057213-20.530073-50.073349-9.764303l-130.691443 130.691442c-10.76577 11.016137-6.259169 33.799511 10.01467 50.07335a12.518337 12.518337 0 0 1 0 17.525672 12.768704 12.768704 0 0 1-9.013203 2.754034zM875.908068 853.500244h-726.063569a12.518337 12.518337 0 0 1-12.518338-12.518337v-109.660636a12.518337 12.518337 0 0 1 12.518338-12.518337h726.063569a12.518337 12.518337 0 0 1 12.518338 12.518337v109.660636a12.518337 12.518337 0 0 1-12.518338 12.518337z m-714.045965-25.036674h701.026895v-84.623961h-701.026895z" fill="#2D2F33" p-id="13741"></path></svg></div>
            <div class="link-bd__text"><?php echo $url; ?></div>
        </div>
        <div class="link-btn">
            <a href="javascript:void(0);" onclick="javascript:window.location.href='<?php echo $url; ?>'" class="link-btn__text">前往外链网站</a>
      <a onclick="location.replace('//<?php echo $_SERVER['HTTP_HOST']; ?>')"  class="link-btn__text" style="margin-left:10px;">回到似水流年</a>
        </div>
    </div>

</body>
</html>

2、编辑主题目录下的functions.php或者func.php,在最后加入如下代码,以支持POST文章内外链。

//文章内外链添加go跳转
function the_content_nofollow($content) {
    preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/', $content, $matches);
    if ($matches) {
        foreach ($matches[2] as $val) {
            if (strpos($val, '://')!== false && strpos($val, home_url()) === false &&!preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i', $val)) {
                // 判断如果目标链接域名不是 my1981.cn 才进行跳转
                if (strpos($val, 'my1981.cn') === false) {
                    $content = str_replace("href=\"$val\"", "href=\"".home_url()."/go.php?url=$val\" ", $content);
                }
            }
        }
    }
    return $content;
}
add_filter('the_content', 'the_content_nofollow', 999);

3、编辑pix/inc/pix-widget.php,大概127行找到"//最近评论",把整段更换为如下代码,以支持侧边栏WIDGET外链。

//最近评论
function cst_widget_com($number,$type,$title){
    $html = '';
    $arr = array(
        'number' => $number,
        'post_type' => $type,
        'orderby' => 'comment_date',
        'order' => 'DESC',
    );

    $lists = get_comments($arr);
    if(isset($lists) && is_array($lists)){
        foreach($lists as $list){
            $user_id = $list->user_id;
            $name = $list->comment_author;
            $original_link = $list->comment_author_url;
            $link = $original_link;
            if (strpos($original_link, 'my1981.cn') === false) {
                $link = get_option('home').'/go.php?url='.$original_link;
            }
            $avatar = get_avatar($list->comment_author_email, 100);

            $body = $list->comment_content;
            $time = timeago(get_gmt_from_date($list->comment_date));

            $html.= '<li class="wid_comment_item">
                        <div class="left"><a href="'.$link.'" target="_blank">'.$avatar.'</a></div>
                        <div class="right">
                            <a href="'.$link.'" class="name" target="_blank">'.$name.'</a>
                            <div class="body">'.$body.'</div>
                            <div class="meta">'.$time.'</div>
                        </div>
                    </li>';
        }
    }

    echo '<div class="wid_title">'.$title.'</div><ul class="items wid_comment">'.$html.'</ul>';
}

4、编辑pix/inc/pix-fn.php,大概340行搜索“//友链”,将整段替换成以下代码(至“//server评论消息推送”前结束),以支持LINK外链。注意,这段代码以下文章修改过:

给友链网站加一个实时快照 给友链网站加一个实时快照 从钟MM的博客评论下,无意间进入到了三十里海河的一篇文章《使用 WordPress mShots 截图 API》,感觉非常的炫酷,用了一个小时,基于PIX主题的友链,更新了代码,实现了快照抓取。 原理... 时间:2024/8/9 分类:技术相关
// 友链
function link_item() {
    $arr = get_op('linkscat_show');
    $linkcats = get_terms( array(
        'taxonomy'     => 'link_category',
        'include'      => $arr,
        'count'        => true,
        'hide_empty'   => 1,
        'orderby'      => 'include',
    ) );
    $output = '';
    foreach($linkcats as $linkcat){
        $id = $linkcat->term_id;
        $output.= '<div class="link_group_content"><div class="link_cat_name"><i class="ri-bookmark-line"></i>'.$linkcat->name.'</div><div id="link_'.$id.'" class="link_group">';
            $bookmarks = get_bookmarks( 'orderby=date&category='.$id);
            if (!empty($bookmarks) ) {
                foreach ( $bookmarks as $bookmark ) {
                    $img_type = $bookmark->link_image? $bookmark->link_image : '';
                    $preg = "/^http(s)?:\\/\\/.+/";
                    $avatar = '<img src="'.THEME_URL.'https://images.my1981.cn/img/avatar.png">';
                    if(!empty($img_type)){
                        if ( preg_match($preg,@$img_type) ) {
                            $avatar = '<img alt="avatar" src="'. $bookmark->link_image.'" srcset="'. $bookmark->link_image.'" class="avatar avatar-80" height="80" width="80">';
                        } else {
                            $avatar = get_avatar( $bookmark->link_image, 80 );
                        }
                    }

                    // 判断链接是否需要跳转
                    if (strpos($bookmark->link_url, '://')!== false && strpos($bookmark->link_url, home_url()) === false &&!preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i', $bookmark->link_url) && strpos($bookmark->link_url, 'my1981.cn') === false) {
                        $bookmarkLink = home_url(). "/go.php?url=". $bookmark->link_url;
                    } else {
                        $bookmarkLink = $bookmark->link_url;
                    }

                    $output.= '<div class="item" title="'. $bookmark->link_description.'">';
                    $output.= '<div class="link-avatar"><a href="'. $bookmarkLink.'" target="_blank" class="hover-trigger">'. $avatar.'</a></div>';
                    $output.= '<div class="info">';
                    $output.= '<h3 class="name"><a href="'. $bookmarkLink.'" target="_blank" class="hover-trigger">'. $bookmark->link_name.'</a></h3>';
                    $output.= '<div class="meta button"><a href="'. $bookmarkLink.'" target="_blank"><i class="iconfont icon-zhuanfa_3"></i></a></div>';
                    $output.= '<div class="description hover-trigger">'. $bookmark->link_description.'</div></div>';
                    $output.= '</div>';
                }
            }
            $output.= '</div></div>';
    }

    // Add custom script to handle the hover functionality with optimizations
    $output.= '
    <style>
       .hover-preview {
            position: absolute;
            width: 400px;
            height: 320px;
            display: none;
            border: 1px solid #ddd;
            box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
            z-index: 1000;
        }
    </style>
    <script>
        function initializeHoverPreview() {
            const hoverTriggers = document.querySelectorAll(".hover-trigger");
            let preview = document.querySelector(".hover-preview");

            if (!preview) {
                preview = document.createElement("img");
                preview.className = "hover-preview";
                document.body.appendChild(preview);
            }

            hoverTriggers.forEach(trigger => {
                let debounceTimer;

                trigger.addEventListener("mouseover", function(e) {
                    const url = e.target.closest("a").href;
                    const previewUrl = `https://s0.wordpress.com/mshots/v1/` + encodeURIComponent(url) + `?w=400&h=320`;

                    if (preview.src!== previewUrl) {
                        preview.src = previewUrl;
                    }
                    preview.style.display = "block";
                });

                trigger.addEventListener("mousemove", function(e) {
                    clearTimeout(debounceTimer);
                    debounceTimer = setTimeout(function() {
                        let previewX = e.pageX + 2;
                        let previewY = e.pageY + 2;

                        if (previewX + preview.width > window.innerWidth) {
                            previewX = e.pageX - preview.width - 2;
                        }

                        if (previewY + preview.height > window.innerHeight) {
                            previewY = e.pageY - preview.height - 2;
                        }

                        preview.style.left = previewX + "px";
                        preview.style.top = previewY + "px";
                    }, 50);
                });

                trigger.addEventListener("mouseout", function() {
                    preview.style.display = "none";
                });
            });
        }

        document.addEventListener("DOMContentLoaded", initializeHoverPreview);
        document.addEventListener("pjax:end", initializeHoverPreview);
    </script>';

    return $output;
}

5、编辑pix/inc/pix-comment.php,这个文件改的地方比较多,可以全部代码替换为如下代码,以支持COMMENT外链。

<?php

//评论表情
function smile_img(){
    $smile = '';
    $arr = array('&#128515;','&#128522','&#128521','&#128530','&#128536','&#128076','&#128077','&#128150','&#128139','&#128079','&#128514','&#128540','&#9996','&#127873','&#9749','&#127801','&#129318','&#128064','&#128518','&#129314','&#128520','&#128123','&#128586','&#128549','&#128566','&#128528','&#128548','&#128561','&#128591','&#128170');

    foreach($arr as $v){
        $smile.= '<a class="smile_btn">'.$v.'</a>';
    }

    return $smile;

}

if ( version_compare( $GLOBALS['wp_version'], '4.4-alpha', '<' ) ) {
    wp_die('请升级到 4.4 以上版本');
}

if(!function_exists('fa_ajax_comment_err')) :

    function fa_ajax_comment_err($a) {
        header('HTTP/1.0 500 Internal Server Error');
        header('Content-Type: text/plain;charset=UTF-8');
        echo $a;
        exit;
    }

endif;

if(!function_exists('fa_ajax_comment_callback')) :

    function fa_ajax_comment_callback(){
        $comment_merge = $_POST['comment'].$_POST['comment_media'];
        // 判断链接是否需要跳转
        if (strpos($comment_merge, '://')!== false && strpos($comment_merge, home_url()) === false &&!preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i', $comment_merge) && strpos($comment_merge, 'my1981.cn') === false) {
            $comment_merge = str_replace("href=\"$comment_merge\"", "href=\"".home_url()."/go.php?url=$comment_merge\" ", $comment_merge);
        }
        $_POST['comment'] = $comment_merge;
        $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
        if ( is_wp_error( $comment ) ) {
            $data = $comment->get_error_data();
            if (! empty( $data ) ) {
                fa_ajax_comment_err($comment->get_error_message());
            } else {
                exit;
            }
        }
        $user = wp_get_current_user();
        $user_id = $user->ID;
        do_action('set_comment_cookies', $comment, $user);
        $GLOBALS['comment'] = $comment; //根据你的评论结构自行修改,如使用默认主题则无需修改
        $avatar = get_avatar( $comment, 50 );
        $url = get_comment_author_url();

        $comment_parent = $_POST['comment_parent'];
        $post = get_post($_POST['comment_post_ID']);
        $post_author = $post->post_author;
        

        //Events::add($_POST['comment_post_ID'],$user_id,$to_id,'reply',$_POST['comment'],'0',$comment_parent,$remark);
       ?>
        <li id="li-comment-<?php comment_ID()?>" <?php comment_class();?>>
                <div id="comment-<?php comment_ID();?>" class="comment_body contents">	
                    <div class="profile">
                        <a href="<?php echo $url?>" target="_blank"><?php echo pix_comment_ava($user_id,$comment)?></a>
                    </div>	
                    <div class="com_right">				
                            <section class="commeta">
                                <div class="left">
                                    <h4 class="author"><a href="<?php echo $url?>" target="_blank"><?php echo pix_comment_name($user_id);?></a></h4>	
                                    <time itemprop="datePublished" datetime="<?php echo get_comment_date( 'c' );?>">待审核</time>									
                                </div>
                                <div class="right">
                                    <div class="info">
                                    </div>	
                                </div>
                            </section>
                        <div class="body">
                            <?php comment_text();?>
                        </div>
                    </div>    
                </div>
        <?php die();
    }

endif;

add_action('wp_ajax_nopriv_ajax_comment', 'fa_ajax_comment_callback');
add_action('wp_ajax_ajax_comment', 'fa_ajax_comment_callback');

/*-----------------------------------------------------------------------------------*/
/* COMMENT FORMATTING
/*-----------------------------------------------------------------------------------*/

if(!function_exists('cst_comment_format')){
    function cst_comment_format($comment, $args, $depth){
        $GLOBALS['comment'] = $comment;
        $user_id = $comment->user_id;
        $avatar = get_avatar( $comment, 50 );
        $url = get_comment_author_url();
        // 判断链接是否需要跳转
        if (strpos($url, '://')!== false && strpos($url, home_url()) === false &&!preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i', $url) && strpos($url, 'my1981.cn') === false) {
            $url = home_url(). "/go.php?url=". $url;
        }
       ?>
            <li id="li-comment-<?php comment_ID()?>" <?php comment_class();?>>
                <div id="comment-<?php comment_ID();?>" class="comment_body contents">	
                    <div class="profile">
                        <a href="<?php echo $url?>" target="_blank"><?php echo pix_comment_ava($user_id,$comment)?></a>
                    </div>	
                    <div class="com_right">				
                            <section class="commeta">
                                <div class="left">
                                    <h4 class="author"><a href="<?php echo $url?>" target="_blank"><?php echo pix_comment_name($user_id);?></a><?php is_master($user_id);?></h4>
                                    <time itemprop="datePublished" datetime="<?php echo get_comment_date( 'c' );?>"> · <?php echo timeago(get_gmt_from_date(get_comment_date('Y-m-d G:i:s')));?></time>									
                                </div>
                                <div class="right">
                                    <div class="info"> 
                                        <?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth'], 'reply_text' => '<i class="ri-chat-1-line"></i>')));?>                               
                                    </div>	
                                </div>
                            </section>
                        <div class="body">
                            <?php comment_text();?>
                        </div>
                        <div class="comment_list_footer">
                            
                        </div> 
                    </div>    
                </div>				
        <?php
    }
}

//ajax评论分页
add_action('wp_ajax_nopriv_ajax_comment_page_nav', 'ajax_comment_page_nav');
add_action('wp_ajax_ajax_comment_page_nav', 'ajax_comment_page_nav');
function ajax_comment_page_nav(){
    global $post,$wp_query, $wp_rewrite;
    $postid = $_POST["post_id"];
    $pageid = $_POST["paged"];
    $comments = get_comments('post_id='.$postid.'&orderby=comment_date&order=desc');
    $post = get_post($postid);
    if( 'desc'!= get_option('comment_order')){
        $comments = array_reverse($comments);
    }
    $wp_query->is_singular = true;
    $baseLink = '';
    if ($wp_rewrite->using_permalinks()) {
        $baseLink = '&base='. user_trailingslashit(get_permalink($postid). 'comment-page-%#%', 'commentpaged');
    }
    echo '<ul class="comment-list" >';
        wp_list_comments('page='. $pageid. '&per_page='. get_option('comments_per_page'). '&callback=cst_comment_format', $comments);//如果你的主题使用了回调函数,则要设置
    echo '</ul>';
    echo '<nav class="commentnav" data-fuck="'.$postid.'">';
    paginate_comments_links('total='. get_comment_pages_count($comments).  '&current='. $pageid. '&prev_text=<i class="ri-arrow-left-s-line"></i>&next_text=<i class="ri-arrow-right-s-line"></i>');
    echo '</nav>';
    die;
}

// 评论添加@
function cst_comment_add_at( $comment_text, $comment = '') {
    if( $comment->comment_parent > 0) {
        $parent_id = $comment->comment_parent;
        $comment_parent = get_comment($parent_id);
        $comment_content = $comment_parent->comment_content;
        // 判断链接是否需要跳转
        if (strpos($comment_content, '://')!== false && strpos($comment_content, home_url()) === false &&!preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i', $comment_content) && strpos($comment_content, 'my1981.cn') === false) {
            $comment_content = str_replace("href=\"$comment_content\"", "href=\"".home_url()."/go.php?url=$comment_content\" ", $comment_content);
        }
        $comment_text = '<a class="parents_at" uk-tooltip="title: '.$comment_content.'; pos: top" href="#comment-'. $comment->comment_parent. '"></a> '. $comment_text;
    }
    
    return $comment_text;
  }
  add_filter( 'comment_text', 'cst_comment_add_at', 20, 2);
  
/**
 * 评论高亮作者
 */
function is_master($user_id = '') {
    if( empty($user_id) ) return;
    if( $user_id == 1 ) {
    echo '<i class="ri-bear-smile-line" uk-tooltip="title: 管理员; pos: top"></i>';
    }
}

//评论昵称
function pix_comment_name($user_id = ''){
    if( $user_id == 1 ) {
        return get_nickname();
    } else {
        return comment_author();
    }
}

//评论头像
function pix_comment_ava($user_id = '',$comment){
    if( $user_id == 1 ) {
        return get_user_avatar();
    } else {
        return get_avatar( $comment, 50 );
    }
}

//ajax加载话题评论
function load_t_comment(){
    global $post,$wp_query, $wp_rewrite;
    $pid = $_POST['pid'];
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $comments = get_comments(array('post_id'=>$pid,'status'=> 'approve'));
    $number = get_option('comments_per_page');
    $pages = get_comment_pages_count($comments);
    $pageid = 1;

    if( 'desc'!= get_option('comment_order')){
        $comments = array_reverse($comments);
        $pageid = $pages;
    }
    $wp_query->is_singular = true;
    $baseLink = '';
    if ($wp_rewrite->using_permalinks()) {
        $baseLink = '&base='. user_trailingslashit(get_permalink($postid). 'comment-page-%#%', 'commentpaged');
    }

    echo '<ul class="comment-list uk-animation-slide-bottom-small">';
        if($comments){
            wp_list_comments('page='. $pageid. '&per_page='. get_option('comments_per_page'). '&callback=cst_comment_format', $comments);//如果你的主题使用了回调函数,则要设置
        } else {
            echo '<p class="nodata"><i class="ri-ghost-line"></i>空空如也!</p>';
        }
     
    echo '</ul>';
    if($pages > 1){
        echo '<nav class="commentnav" data-fuck="'.$pid.'">';
        paginate_comments_links('total='. get_comment_pages_count($comments).  '&current='. $pageid. '&prev_text=<i class="ri-arrow-left-s-line"></i>&next_text=<i class="ri-arrow-right-s-line"></i>');
        echo '</nav>';
    }

    die();
}
add_action('wp_ajax_nopriv_load_t_comment', 'load_t_comment');
add_action('wp_ajax_load_t_comment', 'load_t_comment');

//评论表情
function show_smile_btn(){
    $html = '<div class="comment_smile_box">
                <a class="com_smile_btn"><i class="ri-emotion-line"></i></a>
                <div class="com_smile_show shadow" uk-drop="mode: click;toggle:.com_smile_btn"><div class="inner round8">'.smile_img().'</div></div>
             </div>';

    return $html;       
}

//访客评论头像获取
function comment_visitor( $user_id, $author_name, $author_email, $avatar_size ) {
    $user_info = get_userdata($user_id);
    if ( $user_id ) { // 用户
        $user = get_userdata( $user_id );
        $avatar = get_avatar_url( $user->user_email, array( 'size' => $avatar_size ) );
        // 判断链接是否需要跳转
        if (strpos($avatar, '://')!== false && strpos($avatar, home_url()) === false &&!preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i', $avatar) && strpos($avatar, 'my1981.cn') === false) {
            $avatar = home_url(). "/go.php?url=". $avatar;
        }
        $condition = '<a class="edit-profile login_avatar" href="'. wp_logout_url( cst_get_curl() ).'" target="_top" >'.pix_comment_ava($user_id,$author_email).'<small>'.pix_comment_name($user_id).', 登出</small></a>';
    }
    elseif ( $author_name ) { // 访客
        $avatar = get_avatar_url( $author_email, array( 'size' => $avatar_size ) );
        // 判断链接是否需要跳转
        if (strpos($avatar, '://')!== false && strpos($avatar, home_url()) === false &&!preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i', $avatar) && strpos($avatar, 'my1981.cn') === false) {
            $avatar = home_url(). "/go.php?url=". $avatar;
        }
        $condition = '<a class="edit-profile edit-card"><img src="'. $avatar.'" height="50" width="50" class="v-avatar avatar avatar-50"><small>'.$author_name.', 修改信息</small></a>';
    }
    else { // 匿名
        $avatar = get_bloginfo( 'template_directory' ).'/img/avatar.png';
        $condition = '<a class="edit-profile edit-card"><img src="'. $avatar.'"  height="50" width="50" class="v-avatar avatar avatar-50"><small>点击填写昵称和邮箱,方可发布评论</small></a>';
    }

    echo $condition;
}

//ajax获取评论头像
add_action('wp_ajax_nopriv_ajax_avatar_get', 'ajax_avatar_get');
add_action('wp_ajax_ajax_avatar_get', 'ajax_avatar_get');
function ajax_avatar_get(){
    $email = isset($_POST['email'])? $_POST['email'] : false;
    $name = isset($_POST['name'])? $_POST['name'] : '神秘访客';
    if($email){
        $res = get_avatar_url( $email, array( 'size'=>50 ) );
        $avatar = preg_replace("/http:\/\/(www|\d).gravatar.com\/avatar\//","https://sdn.geekzu.org/avatar/",$res);
     echo json_encode(array('avatar'=>$avatar,'name'=>$name?$name:'神秘访客'));
     exit();
    } else {
        return;
    }
}

//防垃圾评论
function refused_spam_comments( $comment_data ) { 
    $pattern = '/[一-龥]/u'; 
    if(!preg_match($pattern,$comment_data['comment_content'])) { 
        fa_ajax_comment_err('评论中必须含中文!'); 
    } 
    return( $comment_data ); 
    } 
    add_filter('preprocess_comment','refused_spam_comments');
    
// WordPress禁止日文评论
function pix_comment_jp_post( $incoming_comment ) {
    $jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
    if(preg_match($jpattern, $incoming_comment['comment_content'])){
        fa_ajax_comment_err( "评论中禁止发日文字符!" );
    }
    return( $incoming_comment );
    }
add_filter('preprocess_comment', 'pix_comment_jp_post');

//机器人验证
function no_robot_check(){
    $html = '';
    $html = '<div class="comment-form-validate">
                <input class="pix-checkbox-radio" type="checkbox" name="no-robot">
                <label for="default" class="comment_check_style"></label>			
            </div>';
    return  $html;       
}

function pix_robot_comment(){
    if ( !$_POST['no-robot'] && !is_user_logged_in()) {
        fa_ajax_comment_err(__('请解锁后再提交评论'));
    }
  }
if(get_op('com_robot')){add_action('pre_comment_on_post', 'pix_robot_comment');}

改造结束!

您已阅读完《技术相关(共31篇)》目录的第 30 篇。请继续阅读本文后3篇文章:

阅读本目录下更多其它文章

历史上本站今天曾发布的文章:

Comments | 6 条评论
  • 魏叔叔

    大佬 可否把片刻页面嗯评论修改为摊开显示

    • 似水流年

      @魏叔叔 那样一个片刻要占很大一个篇幅啊?

    • 似水流年

      文章是这样,底部评论都摊开了,但是这一页面只有一篇文章啊!一个片刻如果评论的多,可能这一页就只显示一个片刻了,不知道你的意思,我理解的对不对?

      • 魏叔叔

        对 就是这样 但是可以参考https://0ru.cn/这个主题的评论 间距小一点 评论摊开三四条 如果很多剩下的用更多来点击加载

        • 似水流年

          这个恐怕要重构pix主题的评论框架了,那就改的不是一两处了。😂

          • 魏叔叔

            那就坐等老付的pro,看会不会有这样的更新

消息盒子
# 您需要首次评论以获取消息 #
# 您需要首次评论以获取消息 #

只显示最新10条未读和已读信息