// 生成分享链接AJAX
add_action('wp_ajax_create_share_link', 'create_share_link_func');
add_action('wp_ajax_nopriv_create_share_link', 'create_share_link_func');
function create_share_link_func(){
    global $wpdb;
    $tid = intval($_POST['tid']);
    $now = time();
    $expire = $now + 7*24*3600;
    $finger = get_finger();
    $code = md5($finger.$tid.$now.rand(1000,9999));

    // 判断是否存在未过期记录
    $exist = $wpdb->get_row($wpdb->prepare("SELECT id FROM share_log WHERE share_uid=%s AND target_id=%d AND expire_time>$now", $finger, $tid));
    if($exist){
        $wpdb->update("share_log", ['expire_time'=>$expire], ['id'=>$exist->id]);
        $getcode = $wpdb->get_var($wpdb->prepare("SELECT share_code FROM share_log WHERE id=%d", $exist->id));
        $code = $getcode;
    }else{
        $wpdb->insert("share_log", [
            'share_uid' => $finger,
            'share_code' => $code,
            'target_id' => $tid,
            'need_num' => 1,
            'unlock_num' => 0,
            'is_unlock' => 0,
            'expire_time' => $expire,
            'create_time' => $now
        ]);
    }
    $url = home_url("/?p=$tid&s=$code");
    echo json_encode(['code'=>200,'url'=>$url]);
    exit;
}

// 查询解锁状态AJAX
add_action('wp_ajax_check_share_unlock', 'check_share_unlock_func');
add_action('wp_ajax_nopriv_check_share_unlock', 'check_share_unlock_func');
function check_share_unlock_func(){
    global $wpdb;
    $tid = intval($_POST['tid']);
    $now = time();
    $finger = get_finger();
    $row = $wpdb->get_row($wpdb->prepare("SELECT is_unlock FROM share_log WHERE share_uid=%s AND target_id=%d AND expire_time>$now ORDER BY id DESC LIMIT 1", $finger, $tid));
    $unlock = $row ? $row->is_unlock : 0;
    echo json_encode(['is_unlock'=>$unlock]);
    exit;
}

// 全局指纹函数
function get_finger(){
    $ua = $_SERVER['HTTP_USER_AGENT'];
    $ip = $_SERVER['REMOTE_ADDR'];
    return md5($ua.$ip);
}<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://4.ds889.xyz/wp-sitemap.xsl" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://4.ds889.xyz/</loc><lastmod>2024-08-12T16:56:49+08:00</lastmod></url><url><loc>https://4.ds889.xyz/sample-page/</loc><lastmod>2025-03-06T19:42:40+08:00</lastmod></url><url><loc>https://4.ds889.xyz/forums-2/</loc><lastmod>2024-07-21T14:06:33+08:00</lastmod></url><url><loc>https://4.ds889.xyz/user-sign/</loc><lastmod>2025-03-06T19:49:45+08:00</lastmod></url><url><loc>https://4.ds889.xyz/forums/</loc><lastmod>2025-03-06T19:49:46+08:00</lastmod></url><url><loc>https://4.ds889.xyz/newposts/</loc><lastmod>2025-03-06T19:56:59+08:00</lastmod></url><url><loc>https://4.ds889.xyz/user/</loc><lastmod>2025-03-06T19:57:29+08:00</lastmod></url><url><loc>https://4.ds889.xyz/zhuanti/</loc><lastmod>2025-03-06T19:57:29+08:00</lastmod></url><url><loc>https://4.ds889.xyz/archives/</loc><lastmod>2025-03-06T19:57:29+08:00</lastmod></url><url><loc>https://4.ds889.xyz/tags/</loc><lastmod>2025-03-06T19:57:29+08:00</lastmod></url><url><loc>https://4.ds889.xyz/sample-page-2/</loc><lastmod>2024-07-21T14:03:08+08:00</lastmod></url><url><loc>https://4.ds889.xyz/user-sign-2/</loc><lastmod>2024-07-21T14:06:32+08:00</lastmod></url><url><loc>https://4.ds889.xyz/user-sign-2-2/</loc><lastmod>2024-07-26T15:05:26+08:00</lastmod></url><url><loc>https://4.ds889.xyz/newposts-2/</loc><lastmod>2024-07-29T01:54:50+08:00</lastmod></url></urlset>
