$current_userid, 'apmeta_actionid' => $actionid, 'apmeta_type' => $type, 'apmeta_value' => $receiving_userid ); // get exteing vote if exists. $vote = ap_get_meta( $where ); // If vote already exists by user then update count of vote. if ( $vote ) { $count = $vote['apmeta_param'] + $count; $row = ap_update_meta( array( 'apmeta_param' => $count ), $where ); ap_clear_vote_count_cache( $actionid, $current_userid, $receiving_userid ); return $row; } $row = ap_add_meta( $current_userid, $type, $actionid, $receiving_userid, $count ); if ( $row !== false ) { ap_clear_vote_count_cache( $actionid, $current_userid, $receiving_userid ); do_action( 'ap_vote_casted', $current_userid, $type, $actionid, $receiving_userid ); } return $row; } /** * Add vote for post and also update post meta. * @param integer $current_userid User ID of user casting the vote. * @param string $type Type of vote, "vote_up" or "vote_down". * @param integer $actionid Post ID. * @param integer $receiving_userid User ID of user receiving the vote. * @return array|false * @since 2.5 */ function ap_add_post_vote( $current_userid, $type, $actionid, $receiving_userid, $count = 1 ) { $row = ap_add_vote( $current_userid, $type, $actionid, $receiving_userid, $count ); if ( false !== $row ) { $counts = ap_post_votes( $actionid ); update_post_meta( $actionid, ANSPRESS_VOTE_META, $counts['net_vote'] ); return $counts; } return false; } /** * Remove vote meta from DB. */ function ap_remove_vote( $type, $current_userid, $actionid, $receiving_userid ) { // Snaitize varaiables. $current_userid = (int) $current_userid; $type = sanitize_title_for_query( $type ); $actionid = (int) $actionid; $receiving_userid = (int) $receiving_userid; $row = ap_delete_meta( array( 'apmeta_type' => $type, 'apmeta_userid' => $current_userid, 'apmeta_actionid' => $actionid ) ); if ( $row !== false ) { ap_clear_vote_count_cache( $type, $actionid, $current_userid, 'type', $receiving_userid ); do_action( 'ap_vote_removed', $current_userid, $type, $actionid, $receiving_userid ); } return $row; } /** * Remove vote for post and also update post meta. * @param integer $current_userid User ID of user casting the vote. * @param string $type Type of vote, "vote_up" or "vote_down". * @param integer $actionid Post ID. * @param integer $receiving_userid User ID of user receiving the vote. * @return array|false * @since 2.5 */ function ap_remove_post_vote( $type, $current_userid, $actionid, $receiving_userid ) { $row = ap_remove_vote($type, $current_userid, $actionid, $receiving_userid ); if ( false !== $row ) { $counts = ap_post_votes( $actionid ); update_post_meta( $actionid, ANSPRESS_VOTE_META, $counts['net_vote'] ); return $counts; } return false; } /** * Retrieve vote count * If $actionid is passed then it count numbers of vote for a post * If $userid is passed then it count votes casted by a user. * If $receiving_userid is passed then it count numbers of votes received. * * @param bool|int $userid User ID of user casting the vote * @param string $type Type of vote, "vote_up" or "vote_down" * @param boolean $actionid Post ID * @param integer $receiving_userid User ID of user who received the vote * * @return int */ function ap_count_vote($userid = false, $type, $actionid = false, $receiving_userid = false) { if ( $actionid !== false ) { return ap_meta_total_count( $type, $actionid ); } elseif ( $userid !== false ) { return ap_meta_total_count( $type, false, $userid ); } elseif ( $receiving_userid !== false ) { return ap_meta_total_count( $type, false, false, false, $receiving_userid ); } return 0; } // get $post up votes function ap_up_vote($echo = false) { global $post; if ( $echo ) { echo $post->voted_up; } else { return $post->voted_up; } } // get $post down votes function ap_down_vote($echo = false) { global $post; if ( $echo ) { echo $post->voted_down; } else { return $post->voted_down; } } // get $post net votes function ap_net_vote($post = false) { if ( ! $post ) { global $post; } $net = $post->net_vote; return $net ? $net : 0; } /** * Count total vote count of a post. * @param boolean $post_id Post id. * @return integer */ function ap_net_vote_meta($post_id = false) { if ( ! $post_id ) { $post_id = get_the_ID(); } $net = get_post_meta( $post_id, ANSPRESS_VOTE_META, true ); return $net ? $net : 0; } /** * Count post vote count meta. * @param integer $post_id Post id. * @return integer */ function ap_post_votes($post_id) { $counts = ap_meta_total_count( array( 'vote_up', 'vote_down' ), $post_id, false, 'apmeta_type' ); $counts_type = array( 'vote_up' => 0, 'vote_down' => 0 ); if ( $counts ) { foreach ( $counts as $c ) { $counts_type[$c->type] = (int) $c->count; } } $vote = array(); // Voted up count. $vote['voted_up'] = $counts_type['vote_up']; // Voted down count. $vote['voted_down'] = $counts_type['vote_down']; // Net vote. $vote['net_vote'] = $counts_type['vote_up'] - $counts_type['vote_down']; return $vote; } /** * Check if user voted on given post. * * @param int $actionid * @param string $type * @param int $userid * * @return bool * * @since 2.0 */ function ap_is_user_voted($actionid, $type, $userid = false) { if ( false === $userid ) { $userid = get_current_user_id(); } if ( $type == 'vote' && is_user_logged_in() ) { global $wpdb; $query = $wpdb->prepare( 'SELECT apmeta_type as type, IFNULL(count(*), 0) as count FROM '.$wpdb->prefix.'ap_meta where (apmeta_type = "vote_up" OR apmeta_type = "vote_down") and apmeta_userid = %d and apmeta_actionid = %d GROUP BY apmeta_type', $userid, $actionid ); $key = md5( $query ); $user_done = wp_cache_get( $key, 'counts' ); if ( $user_done === false ) { $user_done = $wpdb->get_row( $query ); wp_cache_set( $key, $user_done, 'counts' ); } return $user_done; } elseif ( is_user_logged_in() ) { $done = ap_meta_user_done( $type, $userid, $actionid ); return $done > 0 ? true : false; } return false; } /** * Output voting button. * * @param int $post * * @return null|string * * @since 0.1 */ function ap_vote_btn($post = false, $echo = true) { if ( false === $post ) { global $post; } if ( 'answer' == $post->post_type && ap_opt( 'disable_voting_on_answer' ) ) { return; } if ( 'question' == $post->post_type && ap_opt( 'disable_voting_on_question' ) ) { return; } $nonce = wp_create_nonce( 'vote_'.$post->ID ); $vote = ap_is_user_voted( $post->ID, 'vote' ); $voted = $vote ? true : false; $type = $vote ? $vote->type : ''; $html = ''; $html .= '