charset) ) $charset_collate = "DEFAULT CHARACTER SET ".$wpdb->charset; $sql[] = "CREATE TABLE ".$wpdb->base_prefix."up_down_post_vote_totals ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, post_id bigint(20) NOT NULL, vote_count_up bigint(20) NOT NULL DEFAULT '0', vote_count_down bigint(20) NOT NULL DEFAULT '0', KEY post_id (post_id), CONSTRAINT UNIQUE (post_id) ) ".$charset_collate.";"; $sql[] = "CREATE TABLE ".$wpdb->base_prefix."up_down_post_vote ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, post_id bigint(20) unsigned NOT NULL, voter_id varchar(32) NOT NULL DEFAULT '', vote_value int(11) NOT NULL DEFAULT '0', KEY post_id (post_id), KEY voter_id (voter_id), KEY post_voter (post_id, voter_id), CONSTRAINT UNIQUE (post_id, voter_id) ) ".$charset_collate.";"; $sql[] = "CREATE TABLE ".$wpdb->base_prefix."up_down_comment_vote_totals ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, comment_id bigint(20) unsigned NOT NULL, post_id bigint(20) unsigned NOT NULL, vote_count_up bigint(20) NOT NULL DEFAULT '0', vote_count_down bigint(20) NOT NULL DEFAULT '0', KEY post_id (post_id), KEY comment_id (comment_id), CONSTRAINT UNIQUE (comment_id) ) ".$charset_collate.";"; $sql[] = "CREATE TABLE ".$wpdb->base_prefix."up_down_comment_vote ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, comment_id bigint(20) unsigned NOT NULL, post_id bigint(20) unsigned NOT NULL, voter_id varchar(32) NOT NULL DEFAULT '', vote_value int(11) NOT NULL DEFAULT '0', KEY comment_id (comment_id), KEY post_id (post_id), KEY voter_id (voter_id), KEY post_voter (post_id, voter_id), KEY comment_voter (comment_id, voter_id), CONSTRAINT UNIQUE (comment_id, voter_id) ) ".$charset_collate.";"; dbDelta ($sql); # If old style post vote logging table exists, port records over to new logging table if( $wpdb->get_var("SHOW TABLES LIKE '".$wpdb->base_prefix."up_down_post_votes'") == $wpdb->base_prefix."up_down_post_votes" ) { // Port post vote logs $result_query = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".$wpdb->base_prefix."up_down_post_votes")); foreach ( $result_query as $value ) { $wpdb->insert ($wpdb->base_prefix."up_down_post_vote", array ('post_id' => $value->post_id, 'voter_id' => $value->voter_id, 'vote_value' => $value->vote_value)); } //Drop old post log table $wpdb->query( 'DROP TABLE IF EXISTS '.$wpdb->base_prefix."up_down_post_votes"); } # If old style comment vote logging table exists, port records over to new logging table if( $wpdb->get_var("SHOW TABLES LIKE '".$wpdb->base_prefix."up_down_comment_votes'") == $wpdb->base_prefix."up_down_comment_votes" ) { // Port comment vote logs $result_query = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".$wpdb->base_prefix."up_down_comment_votes")); foreach ( $result_query as $value ) { $wpdb->insert ($wpdb->base_prefix."up_down_comment_vote", array ('comment_id' => $value->comment_id, 'post_id' => $value->post_id, 'voter_id' => $value->voter_id, 'vote_value' => $value->vote_value)); } //Drop old comment log table $wpdb->query( 'DROP TABLE IF EXISTS '.$wpdb->base_prefix."up_down_comment_votes"); } update_option ("updown_db_version", $updown_db_version); if (!get_option ("updown_guest_allowed")) update_option ("updown_guest_allowed", "not allowed"); if (!get_option ("updown_counter_type")) update_option ("updown_counter_type", "plusminus"); if (!get_option ('updown_vote_text')) update_option ("updown_vote_text", "vote"); if (!get_option ('updown_votes_text')) update_option ("updown_votes_text", "votes"); } public function init_plugin() { $this->load_vote_client_resources(); } public function load_vote_client_resources() { if ( !is_admin() ) { // different styles available switch (get_option ("updown_css")) { case "simple": wp_register_style ( 'updownupdown', plugins_url ( '/style/updownupdown-simple.css', __FILE__)); break; default: wp_register_style( 'updownupdown', plugins_url( '/style/updownupdown.css', __FILE__)); } wp_enqueue_style( 'updownupdown' ); wp_register_script( 'updownupdown', plugins_url( '/js/updownupdown.js', __FILE__), array( 'jquery' ), '1.0' ); wp_enqueue_script('updownupdown'); } } public function add_ajax_url() { echo ''; } function guest_allowed() { return get_option ("updown_guest_allowed") == "allowed"; } public function get_user_id() { if (is_user_logged_in()) return get_current_user_id(); // guests user-id = md5 hash of it's IP if ($this->guest_allowed()) return md5 ($_SERVER['REMOTE_ADDR']); return 0; } public function get_post_votes_total ( $post_id ) { global $wpdb; if ( !$post_id ) return false; $result_query = $wpdb->get_results($wpdb->prepare(" SELECT vote_count_up, vote_count_down FROM ".$wpdb->base_prefix."up_down_post_vote_totals WHERE post_id = %d", $post_id)); return ( count($result_query) == 1 ? array( "up" => $result_query[0]->vote_count_up, "down" => $result_query[0]->vote_count_down ) : array( "up" => 0, "down" => 0 )); } public function get_comment_votes_total( $comment_id ) { global $wpdb; if ( !$comment_id ) return false; $result_query = $wpdb->get_results($wpdb->prepare(" SELECT vote_count_up, vote_count_down FROM ".$wpdb->base_prefix."up_down_comment_vote_totals WHERE comment_id = %d", $comment_id)); return ( count($result_query) == 1 ? array( "up" => $result_query[0]->vote_count_up, "down" => $result_query[0]->vote_count_down ) : array( "up" => 0, "down" => 0 )); } public function get_post_user_vote( $user_id, $post_id ) { global $wpdb; return $wpdb->get_var($wpdb->prepare(" SELECT vote_value FROM ".$wpdb->base_prefix."up_down_post_vote WHERE voter_id = %s AND post_id = %d", $user_id, $post_id)); } public function get_comment_user_vote( $user_id, $comment_id ) { global $wpdb; return $wpdb->get_var($wpdb->prepare(" SELECT vote_value FROM ".$wpdb->base_prefix."up_down_comment_vote WHERE voter_id = %s AND comment_id = %d", $user_id, $comment_id)); } public function render_vote_badge( $vote_up_count = 0, $vote_down_count = 0, $votable = true, $existing_vote = 0 ) { $img_up_status = ''; $img_down_status = ''; $vote_label = ''; $up_classnames = ''; $down_classnames = ''; $down_classnames = ''; $votable = (is_user_logged_in() || $this->guest_allowed()) && $votable; if ( $existing_vote > 0 ) $img_up_status = '-on'; elseif ( $existing_vote < 0 ) $img_down_status = '-on'; $up_img_src = plugins_url( '/images/arrow-up'.$img_up_status.'.png', __FILE__); $down_img_src = plugins_url( '/images/arrow-down'.$img_down_status.'.png', __FILE__); $vote_total_count = $vote_up_count - $vote_down_count; $vote_total_count_num = $vote_up_count + $vote_down_count; if ( $vote_down_count > 0 ) { $vote_down_count *= -1; $down_classnames = ' updown-active'; } if ( $vote_up_count > 0 ) { $vote_up_count = "+" . $vote_up_count; $up_classnames = ' updown-active'; } if (!get_option ("updown_counter_sign") || get_option ("updown_counter_sign") == "yes") $updown_classnames .= ' updown-count-sign'; else $updown_classnames .= ' updown-count-unsign'; if ($vote_total_count > 0) { if (!get_option ("updown_counter_sign") || get_option ("updown_counter_sign") == "yes") $vote_total_count = "+" . $vote_total_count; $updown_classnames .= ' updown-pos-count'; // $updown_style = 'style="color:#5ebc40" '; } else if ($vote_total_count < 0) { if (get_option ("updown_counter_sign") == "no") $vote_total_count = substr ($vote_total_count, 1); $updown_classnames .= ' updown-neg-count'; // $updown_style = 'style="color:#bb5853" '; } if ( $vote_up_count == 0 && $vote_down_count == 0 && $votable ) { $vote_up_count = ''; $vote_down_count = ''; $vote_total_count = ''; } if ($votable) $vote_label = get_option ('updown_vote_text'); else $vote_label .= get_option ('updown_votes_text'); if ($votable) echo '