给Hosico主题增加一个取消收藏功能

115

知言老大偷懒了,竟然只写了收藏,没写取消收藏的路由~~

function tl_exec_common_service_common_post_unfavorite__auth($params)
{
    // 提取并验证文章 ID
    $post_id = isset($params['postId']) ? intval($params['postId']) : 0;
    if ($post_id <= 0) {
        return tl_create_common_error(__('无效的文章 ID', TD));
    }

    // 获取当前用户 ID
    $uid = get_current_user_id();
    $key = 'tl_favorite_post';

    global $wpdb;
    $prefix = $wpdb->prefix;
    $table = $prefix . 'usermeta';

    // 从用户元数据中移除收藏的文章
    $result = delete_user_meta($uid, $key, $post_id);

    // 同步文章总收藏量至文章的 postmeta 方便排序
    $sql = sprintf("SELECT COUNT(*) FROM $table WHERE `meta_key`='%s' AND `meta_value`='%s'", $key, $post_id);
    $count = $wpdb->get_var($sql);
    update_post_meta($post_id, 'tl_post_favorites', $count);

    return tl_create_common_response($result);
}

发表评论 (1)

后再参与讨论