/* 添加样式文件 */
function e_reply_encryption_enqueue_style()
{
    wp_enqueue_style('reply-content-style', plugins_url('/style.css', __FILE__), array(), '1.0.0', 'all');
}
add_action('wp_enqueue_scripts', 'e_reply_encryption_enqueue_style');
 
/* 主体方法,判断是否评论过当前文章 */
function e_reply_encryption($atts, $content = null) {
    if (is_comment()) {
        return '<div class="e-encryption">' . $content . '</div>';
    } else {
        return '
        <div class="e-encryption" name="e-encryption">
            <label>评论后查看隐藏内容</label>
            <button class="btn btn-primary btn-sm shadow-sm" type="button" aria-label="Comment" tooltip="评论" onclick="reply_go_to_comment()">
                <span class="btn-inner--icon">回复</span>
            </button>
        </div>
        <script>
        function reply_go_to_comment(){
          /* 根据主题进行修改 */
          gotoHash("#post_comment" , 600);
          $("#post_comment_content").focus();
        }
        </script>
        ';
    }
}
 
/* 判断是否评论过 */
function is_comment() {
    $post_id = get_the_ID();
    $email = null;
    $user_ID = wp_get_current_user()->ID;
    $user_name = wp_get_current_user()->display_name;
 
    if ( $user_ID > 0 ) {
        $email = get_userdata( $user_ID )->user_email;
    } else if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
        $email = str_replace( '%40', '@', $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] );
    } else {
        return false;
    }
    if ( empty( $email ) && empty( $user_name ) ) {
        return false;
    }
 
    global $wpdb;
    $pid = $post_id;
    $query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$pid} and `comment_approved`='1' and (`comment_author_email`='{$email}' or `comment_author`='{$user_name}') LIMIT 1";
    if ( $wpdb->get_results( $query ) ) {
        return true;
    }
}
/* 添加[reply][/reply] 形式的简码实现文章内使用*/
add_shortcode('reply', 'e_reply_encryption');