function bbpvotes_get_score_link($args = '') { // Parse arguments against default values $r = bbp_parse_args($args, array('id' => 0, 'link_before' => '', 'link_after' => '', 'sep' => ' | ', 'text' => esc_html__('Vote up', 'bbpvotes')), 'get_post_vote_score_link'); $post = get_post((int) $r['id']); $post_type = $post->post_type; $score = bbpvotes_get_votes_score_for_post($post->ID); $score_display = bbpvotes_number_format($score); $votes_count = bbpvotes_get_votes_count_for_post($post->ID); $votes_count_display = bbpvotes_number_format($score); $r['text'] = sprintf(__('Score: %1$s', 'bbpvotes'), $score_display); $r['title'] = sprintf(__('Votes count: %1$s', 'bbpvotes'), $votes_count_display); $link_classes = array('bbpvotes-post-vote-link', 'bbpvotes-post-score-link'); if (!$votes_count) { $link_classes[] = 'bbpvotes-post-no-score'; } $retval = $r['link_before'] . '<a href="#" title="' . $r['title'] . '"' . bbpvotes_classes_attr($link_classes) . '>' . $r['text'] . '</a>' . $r['link_after']; return apply_filters('bbpvotes_get_score_link', $retval, $r); }
function bbpvotes_post_vote_ajax() { $result = array('success' => false); if (!isset($_POST['post_id'])) { return false; } $action = $_POST['action']; $post_id = $_POST['post_id']; if ($action == 'bbpvotes_post_vote_up') { $nonce = 'vote-up-post_' . $post_id; } else { if ($action == 'bbpvotes_post_vote_down') { $nonce = 'vote-down-post_' . $post_id; } } if (!wp_verify_nonce($_POST['_wpnonce'], $nonce)) { return false; } if ($action == 'bbpvotes_post_vote_up') { $vote = bbpvotes()->do_post_vote($post_id, true); } else { if ($action == 'bbpvotes_post_vote_down') { $vote = bbpvotes()->do_post_vote($post_id, false); } } if (!is_wp_error($vote)) { $result['success'] = true; $score = bbpvotes_get_votes_score_for_post($post_id); $score_display = bbpvotes_number_format($score); $votes_count = bbpvotes_get_votes_count_for_post($post_id); $vote_count_display = bbpvotes_number_format($votes_count); $result['score_text'] = sprintf(__('Score: %1$s', 'bbpvotes'), $score_display); $result['score_title'] = sprintf(__('Votes count: %1$s', 'bbpvotes'), $vote_count_display); } else { $result['message'] = $vote->get_error_message(); } header('Content-type: application/json'); echo json_encode($result); die; }
/** * Delete user votes by vote type (up or down) and update post score. * @param type $user_id * @param type $up */ function delete_user_votes_type($user_id, $up = true) { //get individual posts if ($up) { //up votes $meta_name = $this->metaname_post_vote_up; } else { //down votes $meta_name = $this->metaname_post_vote_down; } $user_votes_args = array('post_type' => $this->supported_post_types, 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => array(array('key' => $meta_name, 'value' => $user_id))); $user_voted_query = new WP_Query($user_votes_args); foreach ($user_voted_query->posts as $id) { $post_score = bbpvotes_get_votes_score_for_post($id); $votes_count = bbpvotes_get_votes_count_for_post($id); if ($up) { //up votes $post_score--; } else { //down votes $post_score++; } $votes_count--; if (delete_post_meta($id, $meta_name, $user_id)) { update_post_meta($id, $this->metaname_post_vote_score, $post_score); update_post_meta($id, $this->metaname_post_vote_count, $votes_count); } } }
function post_column_content($column_name, $post_id) { $post_type = get_post_type(); if (!in_array($post_type, bbpvotes()->supported_post_types)) { return; } $output = ''; switch ($column_name) { //score case $this->column_name_score: if ($score = bbpvotes_get_votes_score_for_post($post_id)) { $output = $score; } break; } echo $output; }