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

新增短代码reply #1060

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2387,6 +2387,42 @@ function register_shortcodes() {
return ob_get_clean();
});
}
// reply 短代码
add_shortcode('reply', function($atts, $content = null) {
extract(shortcode_atts(array(
"notice" => '<p class="reply-to-read" style="text-align:center; border:2px solid #0000af; border-style:dashed; border-radius:4px; padding:5px; margin:10px;">
<strong style="color: #ff6033;">温馨提示:</strong>此处内容需要您<strong><a href="#respond" title="点击进行评论">评论</a></strong>或<strong><a href="' . esc_url(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 esc_html(do_shortcode($content));
}
// 如果未登录,检查是否评论过
$email = isset($_COOKIE['comment_author_email_' . COOKIEHASH])
? str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH])
: null;
// 检查 email 格式是否有效
if (empty($email) || !is_email($email)) {
return $notice;
}
global $wpdb;
$post_id = get_the_ID();
// 使用 prepare() 进行转义
$email = esc_sql($email);
// 构建 SQL 查询
$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 esc_html(do_shortcode($content));
} else {
return $notice;
}
});
add_action('init', 'register_shortcodes');
//code end

Expand Down Expand Up @@ -2644,4 +2680,4 @@ function iterator_to_string(Iterator $iterator): string
$content .= $item;
}
return $content;
}
}