* @license GPL-2.0+ * @link http://rahularyan.com * @copyright 2014 Rahul Aryan */ class anspress_vote { /** * Instance of this class. */ protected static $instance = null; /** * Return an instance of this class. * @return object A single instance of this class. */ public static function get_instance() { // If the single instance hasn't been set, set it now. if (null == self::$instance) { self::$instance = new self; } return self::$instance; } /** * Initialize the plugin by setting localization and loading public scripts * and styles. */ public function __construct() { add_action( 'the_post', array($this, 'ap_append_vote_count') ); add_action( 'wp_ajax_ap_vote_on_post', array($this, 'ap_vote_on_post') ); add_action( 'wp_ajax_ap_add_to_favourite', array($this, 'ap_add_to_favourite') ); add_action( 'wp_ajax_nopriv_ap_add_to_favourite', array($this, 'ap_add_to_favourite_nopriv') ); } // fired on uninstall public function uninstall() { if ( ap_opt('clear_databse')) { global $wpdb; $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'ap_vote'); } } function ap_append_vote_count($post){ if($post->post_type == 'question' || $post->post_type == 'answer'){ global $wpdb; //voted up count if(is_object($post)){ $post->voted_up = $wpdb->get_var( "SELECT IFNULL(count(*), 0) FROM " .$wpdb->prefix ."ap_vote where (type = 'vote_post' and `value` = '1') and actionid = $post->ID"); //voted down count $post->voted_down = $wpdb->get_var( "SELECT IFNULL(count(*), 0) FROM " .$wpdb->prefix ."ap_vote where (type = 'vote_post' and `value` = '-1') and actionid = $post->ID"); // net vote $post->net_vote = $post->voted_up - $post->voted_down; //closed count $post->closed = ap_post_close_vote(); //flagged count $post->flag = ap_post_flag_count(); //favourite count $post->favourite = ap_post_favourite(); $post->voted_closed = ap_is_user_voted_closed(); $post->flagged = ap_is_user_flagged(); $post->favourited = $wpdb->get_var( "SELECT count(*) FROM " .$wpdb->prefix ."ap_vote where (type = 'favourite' and `value` = '1') and (`userid` = ".get_current_user_id()." and actionid = $post->ID)"); //if current logged in user voted if(is_user_logged_in()){ $user_voted = $wpdb->get_var( "SELECT `value` FROM " .$wpdb->prefix ."ap_vote where `type` = 'vote_post' and ( `userid` = ".get_current_user_id()." and `actionid` = $post->ID)"); $post->user_voted = strlen($user_voted) ? $user_voted : 0; } } } } // process ajax voting request function ap_vote_on_post(){ $args = explode('-', sanitize_text_field($_POST['args'])); if(wp_verify_nonce( $args[2], 'vote_'.$args[1] )){ global $wpdb; $value = $args[0] == 'up' ? 1 : -1; $is_voted = ap_is_user_voted($args[1]); if($is_voted){ // if user already voted and click that again then reverse if($is_voted == $value){ $row = $wpdb->delete($wpdb->prefix.'ap_vote', array('userid'=> get_current_user_id(), 'actionid' =>$args[1], 'type'=> 'vote_post'), array('%d', '%d', '%s') ); $counts = ap_post_votes($args[1]); //update post meta update_post_meta($args[1], ANSPRESS_VOTE_META, $counts['net_vote']); do_action('ap_undo_vote', $args[1], $value); echo $row.'_'.'undo_'.$args[0].'_'.$counts['net_vote'] ; }else{ echo '0_'. __('Please undo your previous vote','ap'); } }else{ $row = $wpdb->insert( $wpdb->prefix.'ap_vote', array('userid'=> get_current_user_id(), 'actionid' =>$args[1], 'type'=> 'vote_post', 'value' => $value), array('%d', '%d', '%s', '%d') ); $counts = ap_post_votes($args[1]); //update post meta update_post_meta($args[1], ANSPRESS_VOTE_META, $counts['net_vote']); if($value > 0) do_action('ap_voted_up', $args[1]); else do_action('ap_voted_down', $args[1]); echo $row.'_'.'voted_'.$args[0].'_'.$counts['net_vote'] ; } }else{ echo '0'. __('Please try again', 'ap'); } die(); } // add to favourite ajax action function ap_add_to_favourite(){ $args = explode('-', sanitize_text_field($_POST['args'])); if(wp_verify_nonce( $args[1], 'favourite_'.$args[0] )){ global $wpdb; $is_favourited = ap_is_user_favourited($args[0]); if($is_favourited){ // if already in favourite list then remove $row = $wpdb->delete($wpdb->prefix.'ap_vote', array('userid'=> get_current_user_id(), 'actionid' =>$args[0], 'type'=> 'favourite'), array('%d', '%d', '%s') ); $counts = ap_post_favourite($args[0]); //update post meta update_post_meta($args[0], ANSPRESS_FAV_META, $counts); echo $row.'_'.'removed_'.$counts.'_'.__('Add to favourite list', 'ap') ; }else{ $row = $wpdb->insert( $wpdb->prefix.'ap_vote', array('userid'=> get_current_user_id(), 'actionid' =>$args[0], 'type'=> 'favourite', 'value' => 1), array('%d', '%d', '%s', '%d') ); $counts = ap_post_favourite($args[0]); //update post meta update_post_meta($args[0], ANSPRESS_FAV_META, $counts); echo $row.'_'.'added_'.$counts.'_'.__('Remove from favourite list', 'ap') ; } }else{ echo '0_'.__('Please try again', 'ap'); } die(); } function ap_add_to_favourite_nopriv(){ echo '0_'.__('Please login or register to add this question to favourite', 'ap'); die(); } } // 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; } function ap_post_votes($postid){ global $wpdb; $vote = array(); //voted up count $vote['voted_up'] = $wpdb->get_var( "SELECT IFNULL(count(*), 0) FROM " .$wpdb->prefix ."ap_vote where (type = 'vote_post' and `value` = '1') and actionid = $postid"); //voted down count $vote['voted_down'] = $wpdb->get_var( "SELECT IFNULL(count(*), 0) FROM " .$wpdb->prefix ."ap_vote where (type = 'vote_post' and `value` = '-1') and actionid = $postid"); // net vote $vote['net_vote'] = $vote['voted_up'] - $vote['voted_down']; return $vote; } //check if user voted on the post function ap_is_user_voted($postid){ if(is_user_logged_in()){ global $wpdb; $user_voted = $wpdb->get_var( "SELECT `value` FROM " .$wpdb->prefix ."ap_vote where `type` = 'vote_post' and ( `userid` = ".get_current_user_id()." and `actionid` = $postid)"); return strlen($user_voted) ? $user_voted : false; } return false; } //checck if user added post to favourite function ap_is_user_favourited($postid){ if(is_user_logged_in()){ global $wpdb; return $wpdb->get_var( "SELECT count(*) FROM " .$wpdb->prefix ."ap_vote where (type = 'favourite' and `value` = '1') and (`userid` = ".get_current_user_id()." and actionid = $postid)"); } return; } function ap_post_favourite($postid = false){ //favourite count global $post; global $wpdb; $postid = $postid ? $postid : $post->ID; return $wpdb->get_var( "SELECT count(*) FROM " .$wpdb->prefix ."ap_vote where (type = 'favourite' and `value` = '1') and actionid = $postid"); } // voting html function ap_vote_html($post = false){ if(!$post_id) global $post; $nonce = wp_create_nonce( 'vote_'.$post->ID ); ?>
ID ); $title = (!$post->favourited) ? (__('Add to favourite list', 'ap')) : (__('Remove from favourite list', 'ap')); ?> ID; return $wpdb->get_var( "SELECT count(*) FROM " .$wpdb->prefix ."ap_vote where (type = 'close' and `value` = '1') and actionid = $postid"); } //check if user voted for close function ap_is_user_voted_closed($postid = false){ if(is_user_logged_in()){ global $post; global $wpdb; $postid = $postid ? $postid : $post->ID; return $wpdb->get_var( "SELECT count(*) FROM " .$wpdb->prefix ."ap_vote where type = 'close' and (`userid` = ".get_current_user_id()." and actionid = $postid)"); } return; } // closing vote html function ap_close_vote_html(){ global $post; $nonce = wp_create_nonce( 'close_'.$post->ID ); $title = (!$post->voted_closed) ? (__('Vote for closing', 'ap')) : (__('Undo your vote', 'ap')); ?> voted_closed > 0 ? ''.$post->voted_closed.'' : ''); ?> delete($wpdb->prefix.'ap_vote', array('userid'=> get_current_user_id(), 'actionid' =>$args[0], 'type'=> 'close'), array('%d', '%d', '%s') ); $counts = ap_post_close_vote($args[0]); //update post meta update_post_meta($args[0], ANSPRESS_CLOSE_META, $counts); echo $row.'_'.'removed_'.__('close','ap').'('.$counts.')_'. __('Vote for closing', 'ap'); }else{ $row = $wpdb->insert( $wpdb->prefix.'ap_vote', array('userid'=> get_current_user_id(), 'actionid' =>$args[0], 'type'=> 'close', 'value' => 1), array('%d', '%d', '%s', '%d') ); $counts = ap_post_close_vote($args[0]); //update post meta update_post_meta($args[0], ANSPRESS_CLOSE_META, $counts); echo $row.'_'.'added_'.__('close','ap').'('.$counts.')_'. __('Undo your vote', 'ap'); } }else{ echo '0'.__('Please try again', 'ap'); } die(); } /* ---------------Flag btn------------------- ------------------------------------------- */ // count flags on the post function ap_post_flag_count($postid=false){ global $post; global $wpdb; $postid = $postid ? $postid : $post->ID; return $wpdb->get_var( "SELECT count(*) FROM " .$wpdb->prefix ."ap_vote where type = 'flag' and actionid = $postid"); } //check if user flagged on post function ap_is_user_flagged($postid = false){ if(is_user_logged_in()){ global $post; global $wpdb; $postid = $postid ? $postid : $post->ID; return $wpdb->get_var( "SELECT count(*) FROM " .$wpdb->prefix ."ap_vote where type = 'flag' and (`userid` = ".get_current_user_id()." and actionid = $postid)"); } return; } // flag button html function ap_flag_btn_html(){ global $post; $nonce = wp_create_nonce( 'flag_'.$post->ID ); $title = (!$post->flagged) ? (__('Flag this post', 'ap')) : (__('You have flagged this post', 'ap')); ?> flag > 0 ? ''.$post->flag.'':''); ?> insert( $wpdb->prefix.'ap_vote', array('userid'=> get_current_user_id(), 'actionid' =>$args[0], 'type'=> 'flag', 'value' => $note_id), array('%d', '%d', '%s', '%d') ); else $row = $wpdb->insert( $wpdb->prefix.'ap_vote', array('userid'=> get_current_user_id(), 'actionid' =>$args[0], 'type'=> 'flag', 'note' => $other_note), array('%d', '%d', '%s', '%s') ); $counts = ap_post_flag_count($args[0]); //update post meta update_post_meta($args[0], ANSPRESS_FLAG_META, $counts); echo $row.'_flagged_'.__('flag','ap').'('.$counts.')_'. __('You have flagged this post', 'ap'); } }else{ echo '0'.__('Please try again', 'ap'); } die(); }