0

我正在为 wordpress 使用 gd 星级插件,它使用此功能来保存拇指数据:

    function add_vote_comment_thumb($id, $user, $ip, $ua, $vote) {
        global $wpdb, $table_prefix;
        $trend_date = date("Y-m-d");
        $sql_trend = sprintf("SELECT count(*) FROM %sgdsr_votes_trend WHERE vote_date = '%s' and vote_type = 'cmmthumb' and id = %s", $table_prefix, $trend_date, $id);
        $trend_data = $wpdb->get_var($sql_trend);

        $trend_added = false;
        if ($trend_data == 0) {
            $trend_added = true;
            if ($user > 0) {
                $sql = sprintf("INSERT INTO %sgdsr_votes_trend (id, vote_type, user_voters, user_votes, vote_date) VALUES (%s, 'cmmthumb', 1, %s, '%s')",
                    $table_prefix, $id, $vote, $trend_date);
                $wpdb->query($sql);
            } else {
                $sql = sprintf("INSERT INTO %sgdsr_votes_trend (id, vote_type, visitor_voters, visitor_votes, vote_date) VALUES (%s, 'cmmthumb', 1, %s, '%s')",
                    $table_prefix, $id, $vote, $trend_date);
                $wpdb->query($sql);
            }
        }

        if ($user > 0) {
            $part = $vote == 1 ? "user_recc_plus = user_recc_plus + 1" : "user_recc_minus = user_recc_minus + 1";

            if (!$trend_added) {
                $sql = sprintf("UPDATE %sgdsr_votes_trend SET user_voters = user_voters + 1, user_votes = user_votes + %s WHERE id = %s and vote_type = 'cmmthumb' and vote_date = '%s'",
                    $table_prefix, $vote, $id, $trend_date);
                $wpdb->query($sql);
            }
        } else {
            $part = $vote == 1 ? "visitor_recc_plus = visitor_recc_plus + 1" : "visitor_recc_minus = visitor_recc_minus + 1";

            if (!$trend_added) {
                $sql = sprintf("UPDATE %sgdsr_votes_trend SET visitor_voters = visitor_voters + 1, visitor_votes = visitor_votes + %s WHERE id = %s and vote_type = 'cmmthumb' and vote_date = '%s'",
                    $table_prefix, $vote, $id, $trend_date);
                $wpdb->query($sql);
            }
        }

        $sql = sprintf("UPDATE %sgdsr_data_comment SET %s, last_voted_recc = CURRENT_TIMESTAMP WHERE comment_id = %s",
            $table_prefix, $part, $id);
        $wpdb->query($sql);

wp_gdsr_dump("SAVE_THUMB_VOTE", $sql);

        $logsql = sprintf("INSERT INTO %sgdsr_votes_log (id, vote_type, user_id, vote, object, voted, ip, user_agent) VALUES (%s, 'cmmthumb', %s, %s, '', '%s', '%s', '%s')",
            $table_prefix, $id, $user, $vote, str_replace("'", "''", current_time('mysql')), $ip, $ua);
        $wpdb->query($logsql);

wp_gdsr_dump("SAVE_THUMB_LOG", $logsql);

    }

我希望这些数据也保存在 wordpress db 中的评论业力字段中。所以在函数的最后我添加:

//Integrate with comment karma system
    $up_qry = sprintf("SELECT visitor_recc_plus FROM %sgdsr_data_comment WHERE comment_id = %s", $table_prefix, $id);
    $votes_up = $wpdb->get_var($up_qry);
    $down_qry = sprintf("SELECT visitor_recc_minus FROM %sgdsr_data_comment WHERE comment_id = %s", $table_prefix, $id);
    $votes_down = $wpdb->get_var($down_qry);
    $votes = $votes_up - $votes_down;
    $votes_qry = sprintf("UPDATE %scomments SET comment_karma = $votes WHERE comment_ID = %s", $table_prefix, $id);
    $wpdb->query($votes_qry);

但这只会在访问者评分时将数据保存到comment_karma,当我以用户身份登录时,它不会将数据保存到相应的列!

我究竟做错了什么?

4

1 回答 1

0

在我看来,您添加的代码只是忽略了非访客的投票。在更大的代码片段的上下文中查看它。第一个片段进行了以下更改:

  • 登录用户投票:增量user_recc_plus
  • 登录用户投票否决:增量user_recc_minus
  • 访客投票:增加visitor_recc_plus
  • 访客投票否决:增加visitor_recc_minus

您的第二个片段似乎忽略了投票的来源。它仅根据设置业力值visitor_recc_plus - visitor_recc_minus

我没有足够的上下文给你一个明确的修复,但你不能简单地用相对变化($vote 而不是 $votes)更新业力吗?如果是这样,您只需要这样:

    $votes_qry = sprintf("UPDATE %scomments SET comment_karma = comment_karma + $vote WHERE comment_ID = %s", $table_prefix, $id);
    $wpdb->query($votes_qry);
于 2012-12-18T13:50:55.217 回答