/**
 * Rebuild scores for all votes
 */
function bbpvotes_rebuild_scores()
{
    $post_ids = array();
    $default_query_args = array('post_type' => bbpvotes()->supported_post_types, 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids');
    $votes_up_query_args = wp_parse_args(array('meta_key' => bbpvotes()->metaname_post_vote_up), $default_query_args);
    $votes_down_query_args = wp_parse_args(array('meta_key' => bbpvotes()->metaname_post_vote_down), $default_query_args);
    $votes_up_query = new WP_Query($votes_up_query_args);
    $votes_down_query = new WP_Query($votes_down_query_args);
    foreach ($votes_up_query->posts as $id) {
        $post_ids[] = $id;
    }
    foreach ($votes_down_query->posts as $id) {
        $post_ids[] = $id;
    }
    $post_ids = array_unique($post_ids);
    foreach ($post_ids as $id) {
        $post_score = 0;
        $vote_count = 0;
        $votes = bbpvotes_get_votes_for_post($id);
        foreach ($votes as $user_id => $vote) {
            $post_score += $vote;
            $vote_count++;
        }
        update_post_meta($id, bbpvotes()->metaname_post_vote_score, $post_score);
        update_post_meta($id, bbpvotes()->metaname_post_vote_count, $vote_count);
    }
}
function bbpvotes_get_post_votes_log($post_id = 0)
{
    if (!($votes = bbpvotes_get_votes_for_post($post_id))) {
        return;
    }
    $r = "\n\n" . '<div id="bbpvotes-post-votes-log-' . esc_attr($post_id) . '" class="bbpvotes-post-votes-log">' . "\n\n";
    if (!bbpvotes()->options['anonymous_vote']) {
        foreach ($votes as $user_id => $score) {
            $user_id = bbp_get_user_id($user_id);
            if (!($user = get_userdata($user_id))) {
                continue;
            }
            if ($score > 0) {
                $title = sprintf(esc_html__('%1$s voted up', 'bbpvotes'), $user->display_name);
                $icon = '<i class="bbpvotes-avatar-icon-vote bbpvotes-avatar-icon-plus fa fa-plus-square"></i>';
            } else {
                $title = sprintf(esc_html__('%1$s voted down', 'bbpvotes'), $user->display_name);
                $icon = '<i class="bbpvotes-avatar-icon-vote bbpvotes-avatar-icon-minus fa fa-minus-square"></i>';
            }
            $user_avatar = get_avatar($user_id, 30);
            $user_vote_link = sprintf('<a title="%1$s" href="%2$s">%3$s</a>', $title, esc_url(bbp_get_user_profile_url($user_id)), $user_avatar . $icon);
            $r .= apply_filters('bbpvotes_get_post_votes_log_user', $user_vote_link, $user_id, $score);
        }
    } else {
        $votes_str = array();
        if ($votes_up = bbpvotes_get_votes_up_for_post($post_id)) {
            $votes_up_count = count($votes_up);
            $votes_up_count_display = bbpvotes_number_format($votes_up_count);
            $votes_str[] = sprintf(_n('%s vote up', '%s votes up', $votes_up_count), '<span class="bbpvotes-score">' . $votes_up_count_display . '</span>');
        }
        if ($votes_down = bbpvotes_get_votes_down_for_post($post_id)) {
            $votes_down_count = count($votes_down);
            $votes_down_count_display = bbpvotes_number_format($votes_down_count);
            $votes_str[] = sprintf(_n('%s vote down', '%s votes down', $votes_down_count), '<span class="bbpvotes-score">' . $votes_down_count_display . '</span>');
        }
        $votes_str = implode(' ' . __('and', 'bbpvotes') . ' ', $votes_str);
        $r .= sprintf(__('This reply has received %1$s.', 'bbpvotes'), $votes_str);
    }
    $r .= "\n" . '</div>' . "\n\n";
    return apply_filters('bbpvotes_get_post_votes_log', $r, $post_id);
}
Esempio n. 3
0
 function display_topic_score()
 {
     if (!($votes = bbpvotes_get_votes_for_post(bbp_get_topic_id()))) {
         return;
     }
     $score = bbpvotes_get_votes_score_for_post(bbp_get_topic_id());
     $score_display = bbpvotes_number_format($score);
     printf('<span class="bbpvotes-topic-score">%1$s</span>', sprintf(__('Score: %s pts', 'bbpvotes'), '<span class="bbpvotes-score">' . $score_display . '</span>'));
 }