Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

考虑简码功能添加一个回复后可见的功能 #1026

Open
Deepsea-CN opened this issue Dec 17, 2024 · 3 comments
Open

考虑简码功能添加一个回复后可见的功能 #1026

Deepsea-CN opened this issue Dec 17, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@Deepsea-CN
Copy link

Deepsea-CN commented Dec 17, 2024

这是什么方面的想法?

简码功能

这个想法具体是要实现什么?
免插件实现隐藏内容回复后可见的功能

这个想法实现后有什么帮助?
有些文章或者有些分享的内容可能会希望隐藏某一部分,需求回复/登录后再给访客展示,可以通过简码的形式直接在主题内支持

是否有具体的实现思路?
根据网上的wp隐藏内容回复后看见的代码修改优化得到以下版本,对登录用户显示,对访客检查是否评论

function reply_to_read( $atts , $content = null ) {
    extract(shortcode_atts( array(
        "notice" => '<p class="reply-to-read" style="text-align:center; border:2px solid #f00; border-style:dotted; border-radius:4px; padding:5px; margin:10px;">
            <strong style="color: red;">温馨提示:</strong>此处内容需要您<strong><a href="#respond" title="点击进行评论">回复评论</a></strong>或<strong><a href="'.wp_login_url(get_permalink()).'" title="点击登录">登录</a></strong>后才能查看, 评论后请 <strong><a href="javascript:location.reload()" title="点击刷新">刷新!</a></strong></p>'
    ), $atts));
    $user_ID = (int) wp_get_current_user()->ID;
    // 如果用户已登录,直接显示内容
    if ( $user_ID > 0 ) {
        return do_shortcode( $content );
    }
    // 如果未登录,检查是否评论过
    $email = isset($_COOKIE['comment_author_email_' . COOKIEHASH]) 
             ? str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]) 
             : null;

    if ( empty($email) ) {
        return $notice;
    }
    global $wpdb;
    $post_id = get_the_ID();
    $query = $wpdb->prepare(
        "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID` = %d AND `comment_approved` = '1' AND `comment_author_email` = %s LIMIT 1",
        $post_id,
        $email
    );
    if ( $wpdb->get_results($query) ) {
        return do_shortcode($content);
    } else {
        return $notice;
    }
}
add_shortcode('reply', 'reply_to_read');

补充信息:
需要的博主也可以自行添加到function.php中使用,顺便有没有佬看看sql查询拼接有没有安全漏洞

@Deepsea-CN Deepsea-CN added the enhancement New feature or request label Dec 17, 2024
Copy link

@Deepsea-CN,感谢你给 sakurairo 提出了 issue。ヾ(≧▽≦*)o
请根据 issue 模版确保背景信息的完善,我们将调查并尽快回复你。

@mirai-mamori
Copy link
Owner

可以pr

@Deepsea-CN
Copy link
Author

好的,发pr了,我本地自行测试可以使用,就是前端样式比较普通

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants