From 84b10f42e5dfd0b20dc03d71295ccaa7f45e2b94 Mon Sep 17 00:00:00 2001 From: Hexa <111950052+Deepseaon@users.noreply.github.com> Date: Mon, 20 Jan 2025 22:15:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=9F=AD=E4=BB=A3=E7=A0=81re?= =?UTF-8?q?ply?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions.php | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/functions.php b/functions.php index f3c98fdb..6c6ff0c8 100644 --- a/functions.php +++ b/functions.php @@ -2387,6 +2387,42 @@ function register_shortcodes() { return ob_get_clean(); }); } + // reply 短代码 + add_shortcode('reply', function($atts, $content = null) { + extract(shortcode_atts(array( + "notice" => '

+ 温馨提示:此处内容需要您评论登录后才能查看, 评论后请 点击刷新!

' + ), $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 @@ -2644,4 +2680,4 @@ function iterator_to_string(Iterator $iterator): string $content .= $item; } return $content; -} \ No newline at end of file +}