· 8 years ago · Jul 10, 2018, 03:46 AM
1<?php
2/*
3This script is designed to be run from wordpress. It will work if placed in the root directory of a wordpress install.
4Revision: 4
5
6-Added Widget
7-Fixed Guest Voting
8-Modified to use get_options
9-Fixed Widget, now excludes deleted posts
10
11*/
12
13//Run this to create an entry for a post in the voting system. Will check if the post exists. If it doesn't, it will create an entry.
14function SetPost($post_ID) {
15 global $wpdb;
16
17 //prevents SQL injection
18 $p_ID = $wpdb->escape($post_ID);
19
20 //Check if entry exists
21 $id_raw = $wpdb->get_var("SELECT ID FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
22 if ($id_raw != '') {
23 //entry exists, do nothing
24 } else {
25 //entry does not exist
26 $wpdb->query("INSERT INTO ".$wpdb->prefix."votes (post, votes, guests, usersinks, guestsinks) VALUES(".$p_ID.", '', '', '', '') ") or die(mysql_error());
27 }
28}
29
30//Run this to create an entry for a user in the voting system. Will check if the user exists. If it doesn't, it will create an entry.
31function SetUser($user_ID) {
32 global $wpdb;
33
34 //prevents SQL injection
35 $u_ID = $wpdb->escape($user_ID);
36
37 //Check if entry exists
38 $id_raw = $wpdb->get_var("SELECT ID FROM ".$wpdb->prefix."votes_users WHERE user='".$u_ID."'");
39 if ($id_raw != '') {
40 //entry exists, do nothing
41 } else {
42 //entry does not exist
43 $wpdb->query("INSERT INTO ".$wpdb->prefix."votes_users (user, votes, sinks) VALUES(".$u_ID.", '', '') ") or die(mysql_error());
44 }
45}
46
47//Returns the vote count
48function GetVotes($post_ID, $percent = false) {
49 global $wpdb;
50
51 //prevents SQL injection
52 $p_ID = $wpdb->escape($post_ID);
53
54 //Create entries if not existant
55 SetPost($p_ID);
56
57 //Gets the votes
58 $votes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
59 $sinks_raw = $wpdb->get_var("SELECT usersinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
60 $guestvotes_raw = $wpdb->get_var("SELECT guests FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
61 $guestsinks_raw = $wpdb->get_var("SELECT guestsinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
62/* Deprecated
63 $uservotes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes_users WHERE user='".$u_ID."'");
64 $usersinks_raw = $wpdb->get_var("SELECT sinks FROM ".$wpdb->prefix."votes_users WHERE user='".$u_ID."'");
65*/
66
67 //Put it in array form
68 $votes = explode(",", $votes_raw);
69 $sinks = explode(",", $sinks_raw);
70 $guestvotes = explode(",", $guestvotes_raw);
71 $guestsinks = explode(",", $guestsinks_raw);
72/* Deprecated
73 $uservotes = explode(",", $uservotes_raw);
74 $usersinks = explode(",", $usersinks_raw);
75*/
76 $uservotes = 0;
77 $usersinks = 0;
78
79 $initial = 0; //Initial no. of votes [will be placed at -1 when all posts receive votes]
80
81
82
83 //The mathematics
84 if ($percent == true) {
85 // make $votecount into a percent
86 $totalcount = count($votes) + count($sinks) + count($guestvotes) + count($guestsinks) + count($uservotes) + count($usersinks) + get_option('voteiu_initialoffset') - 6;
87 // the -6 is because count('') returns 1, so if there is no votes at all, 1 is returned. -6 offsets that
88 $forcount = count($votes) + count($guestvotes) + count($uservotes) + get_option('voteiu_initialoffset') - 3;
89 $againstcount = count($sinks) + count($guestsinks) + count($usersinks) - 3;
90 if ($totalcount > 0) {
91 $votecount = number_format(100*($forcount / $totalcount), 0) . "%";
92 } else {
93 return false;
94 }
95 return $votecount;
96 // uncomment this line below if you want to test
97 //return count($votes) . " " . count($sinks) . " " . count($guestvotes) . " " . count($guestsinks) . " " . count($uservotes) . " " . count($usersinks) . " " . get_option('voteiu_initialoffset') . " " . $p_ID;
98 } else {
99 // wihtout percent mode, $votecount is number of total positive votes (votes minus sinks)
100 $votecount = count($votes) - count($sinks) + count($guestvotes) - count($guestsinks) + count($uservotes) - count($usersinks) + $initial;
101 return $votecount + get_option('voteiu_initialoffset');
102 }
103}
104
105function GetPostVotes($post_ID) {
106 global $wpdb;
107
108 //prevents SQL injection
109 $p_ID = $wpdb->escape($post_ID);
110
111 //Create entries if not existant
112 SetPost($p_ID);
113
114 //Gets the votes
115 $votes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
116
117 //Put it in array form
118 $votes = explode(",", $votes_raw);
119 return $votes + get_option('voteiu_initialoffset');
120}
121
122function GetPostSinks($post_ID) {
123 global $wpdb;
124
125 //prevents SQL injection
126 $p_ID = $wpdb->escape($post_ID);
127
128 //Create entries if not existant
129 SetPost($p_ID);
130
131 //Gets the votes
132 $sinks_raw = $wpdb->get_var("SELECT usersinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
133
134 //Put it in array form
135 $sinks = explode(",", $sinks_raw);
136 return $sinks + get_option('voteiu_initialoffset');
137}
138
139//Returns a series of information
140function GetBarVotes($post_ID) {
141
142 //Some minor configuration
143 $max_displayed_votes = 40;
144 $vote_threshold = 30;
145
146 $votes = GetVotes($post_ID);
147 $votemax = $max_displayed_votes;
148 $votebreak = 30; //votes at which bar changes color
149 $bar[0] = 0; //The length of the bar
150 $bar[1] = 0; //The state of the bar
151 if ($votes > $votemax && $votes > -1) {
152 $bar[0] = $votemax;
153 } else {
154 if ($votes > -1) {
155 $bar[0] = $votes;
156 } else {
157 $bar[0] = 0;
158 }
159 }
160 if ($votes > $votebreak) {
161 $bar[1] = 1;
162 }
163 return $bar;
164}
165
166//Checks if the user voted
167function UserVoted($post_ID, $user_ID) {
168 global $wpdb;
169
170 //prevents SQL injection
171 $p_ID = $wpdb->escape($post_ID);
172 $u_ID = $wpdb->escape($user_ID);
173
174 //Create entry if not existant
175 SetPost($p_ID);
176
177 //Gets the votes
178 $votes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
179 $sinks_raw = $wpdb->get_var("SELECT usersinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
180
181 //Put it in array form
182 $votes = explode(",", $votes_raw);
183 $sinks = explode(",", $sinks_raw);
184
185 $voted = FALSE;
186 $votekey = array_search($u_ID, $votes);
187 $sinkkey = array_search($u_ID, $sinks);
188 if ($votekey != '' | $sinkkey != '') {
189 $voted = TRUE;
190 }
191 return $voted;
192}
193
194//Checks if the visitor voted
195function GuestVoted($post_ID, $user_IPhash) {
196 global $wpdb;
197
198 //prevents SQL injection
199 $p_ID = $wpdb->escape($post_ID);
200 $u_ID = $wpdb->escape($user_IPhash);
201
202 //Create entry if not existant
203 SetPost($p_ID);
204
205 //Gets the votes
206 $votes_raw = $wpdb->get_var("SELECT guests FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
207 $sinks_raw = $wpdb->get_var("SELECT guestsinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
208
209 //Put it in array form
210 $votes = explode(",", $votes_raw);
211 $sinks = explode(",", $sinks_raw);
212
213 $voted = FALSE;
214 $votekey = array_search($u_ID, $votes);
215 $sinkkey = array_search($u_ID, $sinks);
216 if ($votekey != '' | $sinkkey != '') {
217 $voted = true;
218 }
219 return $voted;
220}
221
222//Checks the key to see if it is valid
223function CheckKey($key, $id) {
224 global $wpdb;
225 $userdata = $wpdb->get_results("SELECT display_name, user_email, user_url, user_registered FROM $wpdb->users WHERE ID = '".$id."'", ARRAY_N);
226 //$chhash = md5($userdata[0][0].$userdata[0][3]);
227 $chhash = md5($userdata[0][0].$userdata[0][3]);
228 if ($chhash == $key) {
229 return TRUE;
230 } else {
231 return FALSE;
232 }
233}
234
235//Saves the vote of a user to the database
236function Vote($post_ID, $user_ID, $type) {
237 global $wpdb;
238
239 //Prevents SQL injection
240 $p_ID = $wpdb->escape($post_ID);
241 $u_ID = $wpdb->escape($user_ID);
242
243 //Create entries if not existant
244 SetPost($p_ID);
245 SetUser($u_ID);
246
247 //Gets the votes
248 $votes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
249 $sinks_raw = $wpdb->get_var("SELECT usersinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
250 $uservotes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes_users WHERE user='".$u_ID."'");
251 $usersinks_raw = $wpdb->get_var("SELECT sinks FROM ".$wpdb->prefix."votes_users WHERE user='".$u_ID."'");
252
253 //Gets the votes in array form
254 $votes = explode(",", $votes_raw);
255 $sinks = explode(",", $sinks_raw);
256 $uservotes = explode(",", $uservotes_raw);
257 $usersinks = explode(",", $usersinks_raw);
258
259 //Check if user voted
260 if (!UserVoted($post_ID,$user_ID)) {
261 //user hasn't vote, so the script allows the user to vote
262
263 if ($type != 'sink') {
264 //Add vote to array
265 $user_var[0] = $u_ID;
266 $post_var[0] = $p_ID;
267 $votes_result = array_merge($votes,$user_var);
268 $votes_result_raw = implode(",",$votes_result);
269 $uservotes_result = array_merge($uservotes,$post_var);
270 $uservotes_result_raw = implode(",",$uservotes_result);
271 $sinks_result_raw = $sinks_raw;
272 $usersinks_result_raw = $usersinks_raw;
273 } else {
274 //Add sink to array
275 $user_var[0] = $u_ID;
276 $post_var[0] = $p_ID;
277 $sinks_result = array_merge($sinks,$user_var);
278 $sinks_result_raw = implode(",",$sinks_result);
279 $usersinks_result = array_merge($usersinks,$post_var);
280 $usersinks_result_raw = implode(",",$usersinks_result);
281 $votes_result_raw = $votes_raw;
282 $uservotes_result_raw = $votesinks_raw;
283 }
284
285 //Prevents SQL injection
286 $votes_result_sql = $wpdb->escape($votes_result_raw);
287 $sinks_result_sql = $wpdb->escape($sinks_result_raw);
288 $uservotes_result_sql = $wpdb->escape($uservotes_result_raw);
289 $usersinks_result_sql = $wpdb->escape($usersinks_result_raw);
290
291 //Update votes
292 $wpdb->query("UPDATE ".$wpdb->prefix."votes SET votes='".$votes_result_sql."' WHERE post='".$p_ID."'");
293 $wpdb->query("UPDATE ".$wpdb->prefix."votes SET usersinks='".$sinks_result_sql."' WHERE post='".$p_ID."'");
294 $wpdb->query("UPDATE ".$wpdb->prefix."votes_users SET votes='".$uservotes_result_sql."' WHERE user='".$u_ID."'");
295 $wpdb->query("UPDATE ".$wpdb->prefix."votes_users SET sinks='".$usersinks_result_sql."' WHERE user='".$u_ID."'");
296
297 $result = 'true';
298 } else {
299 //The user voted, thus the script will not update the votes in the article
300 $result = 'false';
301 }
302
303 return $result; //returns '' on failure, returns 'true' if votes were casted, returns 'false' if user already casted a vote
304}
305
306//Saves the vote of a guest to the database
307function GuestVote($post_ID, $type) {
308 global $wpdb;
309
310 //Guest's vote is stored permanently. May implement votes that will expire.
311 //Use user IP
312 //$iphash = md5($_SERVER['REMOTE_ADDR']);
313 //$iphash = time();
314 //Set a cookie if there isn't any. This is to reduce the problem of users using proxies and voting multiple times on the same stories.
315
316 //$cookielife = time() + 86400; //Set to expire in 24 hours
317 //setcookie("votingcookie", $iphash, $cookielife);
318
319 if(isset($_COOKIE['tevinevotes'])) {
320 $iphash = $_COOKIE['tevinevotes'];
321 } else {
322 $iphash = time();
323 $cookielife = time() + 86400; //Set to expire in 24 hours
324 //$cookielife = time() + 60; //Set to expire in 24 hours
325 setcookie("tevinevotes", $iphash, $cookielife);
326 }
327
328
329 //Prevents SQL injection
330 $u_ID = $wpdb->escape($iphash);
331 $p_ID = $wpdb->escape($post_ID);
332
333 //Create entries if not existant
334 SetPost($p_ID);
335
336 //Gets the info
337 $votes_raw = $wpdb->get_var("SELECT guests FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
338 $sinks_raw = $wpdb->get_var("SELECT guestsinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
339
340 //Gets the votes in array form
341 $votes = explode(",", $votes_raw);
342 $sinks = explode(",", $sinks_raw);
343
344 //Check if user voted
345 if (!GuestVoted($post_ID,$user_ID)) {
346 //if (!GuestVoted($post_ID,$user_ID) && time() > ($_COOKIE["tevinevotes"] + 60)) {
347 //user hasn't vote, so the script allows the user to vote
348
349 if ($type != 'sink') {
350 //Add vote to array
351 $user_var[0] = $u_ID;
352 $post_var[0] = $p_ID;
353 $votes_result = array_merge($votes,$user_var);
354 $votes_result_raw = implode(",",$votes_result);
355 $sinks_result_raw = $sinks_raw;
356 } else {
357 //Add sink to array
358 $user_var[0] = $u_ID;
359 $post_var[0] = $p_ID;
360 $sinks_result = array_merge($sinks,$user_var);
361 $sinks_result_raw = implode(",",$sinks_result);
362 $votes_result_raw = $votes_raw;
363 }
364
365 //Prevents SQL injection
366 $votes_result_sql = $wpdb->escape($votes_result_raw);
367 $sinks_result_sql = $wpdb->escape($sinks_result_raw);
368
369 //Update votes
370 $wpdb->query("UPDATE ".$wpdb->prefix."votes SET guests='".$votes_result_sql."' WHERE post='".$p_ID."'");
371 $wpdb->query("UPDATE ".$wpdb->prefix."votes SET guestsinks='".$sinks_result_sql."' WHERE post='".$p_ID."'");
372
373 $result = 'true';
374 } else {
375 //The user voted, thus the script will not update the votes in the article
376 $result = 'false';
377 }
378
379 return $result; //returns '' on failure, returns 'true' if votes were casted, returns 'false' if user already casted a vote
380}
381
382//Gets an array of posts with vote count
383//Identical to SortVotes() except that it is sorted by ID in descending order, and has more information on votes
384function GetVoteArray() {
385 global $wpdb;
386
387 $limit = get_option('voteiu_limit');
388
389 //Get posts logged, and get the latest posts voted [new posts have larger IDs
390 $posts = $wpdb->get_results("SELECT post FROM ".$wpdb->prefix."votes WHERE ID != '' ORDER BY ID DESC");
391
392 $postarray = array();
393 $votesarray = array();
394 $uservotes_a = array();
395 $usersinks_a = array();
396 $guestvotes_a = array();
397 $guestsinks_a = array();
398
399 /*
400 Limit, so as to reduce time taken for this script to run. If you want to raise the limit to maximum, use
401 $limit = count($posts);
402 */
403 if ($limit == '') {
404 $limit = 100;
405 }
406 $limit = 999;
407
408 //Ignore limit if post count is below limit
409 if ($limit > count($posts)) {
410 $limit = count($posts);
411 }
412
413
414 //foreach ($posts as &$post) {
415 //Support PHP4 by not using foreach
416 for ($counter = 0; $counter < $limit; $counter += 1) {
417 $post = $posts[$counter];
418 $p_ID = $post->post;
419
420 //Gets the votes
421 $votes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
422 $sinks_raw = $wpdb->get_var("SELECT usersinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
423 $guestvotes_raw = $wpdb->get_var("SELECT guests FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
424 $guestsinks_raw = $wpdb->get_var("SELECT guestsinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
425
426 //Put it in array form
427 $votes = explode(",", $votes_raw);
428 $sinks = explode(",", $sinks_raw);
429 $guestvotes = explode(",", $guestvotes_raw);
430 $guestsinks = explode(",", $guestsinks_raw);
431
432 $initial = get_option('voteiu_initialoffset');
433
434 //The mathematics
435 $votecount = count($votes) - count($sinks) + count($guestvotes) - count($guestsinks) + $initial;
436 array_push($postarray, array($p_ID));
437 array_push($votesarray, array($votecount));
438 array_push($uservotes_a, array(count($votes)-1)); //Apparently there is one extra item in the array
439 array_push($usersinks_a, array(count($sinks)-1));
440 array_push($guestvotes_a, array(count($guestvotes)-1));
441 array_push($guestsinks_a, array(count($guestsinks)-1));
442 }
443 $output = array($postarray, $votesarray, $uservotes_a, $usersinks_a, $guestvotes_a, $guestsinks_a);
444 return $output;
445
446}
447
448//Used in options page
449function DisplayPostList() {
450 $a = GetVoteArray();
451 $i = 0;
452
453//Begin table
454?>
455<table class="widefat post fixed" id="formtable" style="clear: both;" cellspacing="0">
456 <thead>
457 <tr>
458
459 <th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" name="multiselect[]" onclick="javascript:CheckUncheck()" /></th>
460 <th scope="col" id="title" class="manage-column column-title" style="">Post</th>
461<?php /*?> <th scope="col" id="author" class="manage-column column-author" style="">Author</th><?php */ ?>
462 <th scope="col" id="votes" class="manage-column column-categories" style="width: 40%">Votes</th>
463
464
465 </tr>
466 </thead>
467
468 <tfoot>
469 <tr>
470 <th scope="col" class="manage-column column-cb check-column" style=""><input type="checkbox" name="multiselect[]" onclick="javascript:CheckUncheck()" /></th>
471 <th scope="col" class="manage-column column-title" style="">Post</th>
472<?php /* ?> <th scope="col" class="manage-column column-author" style="">Author</th><?php */ ?>
473
474 <th scope="col" class="manage-column column-categories" style="">Votes</th>
475
476 </tr>
477 </tfoot>
478 <tbody>
479<?php
480
481
482 while ($i < count($a[0])) {
483 $postdat = get_post($a[0][$i][0]);
484 if (!empty($postdat)) {
485?>
486 <tr id='post-<?php echo $a[0][$i][0]; ?>' class='alternate author-other status-publish iedit' valign="top">
487 <th scope="row" class="check-column"><input type="checkbox" name="post[]" value="<?php echo $a[0][$i][0]; ?>" /></th>
488<td class="post-title column-title"><strong><a class="row-title" href="<?php echo $postdat->guid; ?>" title="<?php echo $postdat->post_title; ?>"><?php echo $postdat->post_title; ?></a></strong></td>
489<?php /* ?><td class="author column-author"><?php echo $postdat->post_author; ?></td><?php */ ?>
490<td class="categories column-categories"><?php echo $a[1][$i][0]; ?> (Users: <span style="color:#00CC00">+<?php echo $a[2][$i][0]; ?></span>/<span style="color:#CC0000">-<?php echo $a[3][$i][0]; ?></span>, Guests: <span style="color:#00CC00">+<?php echo $a[4][$i][0]; ?></span>/<span style="color:#CC0000">-<?php echo $a[5][$i][0]; ?></span><?php
491if(get_option('voteiu_initialoffset') != '0') {
492echo ', Offset: ';
493if (get_option('voteiu_initialoffset') > 0) {
494echo '<span style="color:#00CC00">+'.get_option('voteiu_initialoffset').'</span>';
495} else {
496echo '<span style="color:#CC0000">'.get_option('voteiu_initialoffset').'</span>';
497} } ?>)</td>
498<?php
499
500/*
501 echo $postdat->post_title;
502 echo ' - ';
503 echo $a[1][$i][0];
504 echo '<br />';
505*/
506
507?>
508</tr>
509<?php
510 }
511 $i++;
512 }
513
514
515//End table
516?>
517 </tbody>
518 </table>
519<?php
520}
521
522//Handles the deleting of votes, used to read the POST when the page is submitted
523function VoteBulkEdit() {
524//error_reporting(E_ALL);
525/*print_r($_POST);*/
526$buttonnumber = 0; //Determines which apply button was clicked on. 0 if no button was clicked.
527$action = 'none'; //Determines what should be done
528if (array_key_exists('doaction1', $_POST)) {
529$buttonnumber = 1;
530}
531if (array_key_exists('doaction2', $_POST)) {
532$buttonnumber = 2;
533}
534if ($buttonnumber != 0 && array_key_exists('action'.$buttonnumber, $_POST)) {
535 if ($_POST['action'.$buttonnumber] != -1) {
536 //Assigns action to be done
537 $action = $_POST['action'.$buttonnumber];
538 }
539}
540
541if (!array_key_exists('post', $_POST)) {
542$action = 'none'; //set action to none if there are no posts to modify
543}
544
545//Begin modifying votes
546if ($action != 'none' && $action != '') {
547ResetVote($_POST['post'], $action);
548}
549
550}
551
552function ResetVote($postids, $action) {
553/*
554Testing stuff...
555echo 'Posts: '.implode(', ',$_POST['post']);
556echo '<br />';
557echo "Action: ".$action;
558*/
559global $wpdb;
560//$wpdb->show_errors();
561
562switch ($action) {
563
564case 'none':
565//do nothing
566break;
567case 'delete':
568//reset all votes for the post
569$i = 0;
570while ($i < count($postids)) {
571$wpdb->query("UPDATE ".$wpdb->prefix."votes SET votes = '',guests = '',usersinks = '', guestsinks = '' WHERE `post`=".$postids[$i]." LIMIT 1 ;");
572$i++;
573}
574EditVoteSuccess();
575break;
576case 'deleteuser':
577//reset all votes for users
578$i = 0;
579while ($i < count($postids)) {
580$wpdb->query("UPDATE ".$wpdb->prefix."votes SET votes = '',usersinks = '' WHERE post=".$postids[$i]." LIMIT 1 ;");
581$i++;
582}
583EditVoteSuccess();
584break;
585case 'deleteguest':
586//reset all votes for guests
587$i = 0;
588while ($i < count($postids)) {
589$wpdb->query("UPDATE ".$wpdb->prefix."votes SET guests = '',guestsinks = '' WHERE post=".$postids[$i]." LIMIT 1 ;");
590$i++;
591}
592EditVoteSuccess();
593break;
594}
595
596
597
598
599
600}
601
602function EditVoteSuccess() {
603?><div id="message" class="updated fade"><p><strong>Votes edited</strong></p></div><?php
604}
605
606//Used to sort votes for widgets
607function SortVotes() {
608 global $wpdb;
609
610 $limit = get_option('voteiu_limit');
611
612 //Get posts logged, and get the latest posts voted [new posts have larger IDs
613 $posts = $wpdb->get_results("SELECT post FROM ".$wpdb->prefix."votes WHERE ID != '' ORDER BY ID DESC");
614
615 $postarray = array();
616 $votesarray = array();
617
618 /*
619 Limit, so as to reduce time taken for this script to run. If you want to raise the limit to maximum, use
620 $limit = count($posts);
621 */
622 if ($limit == '') {
623 $limit = 100;
624 }
625
626 //Ignore limit if post count is below limit
627 if ($limit > count($posts)) {
628 $limit = count($posts);
629 }
630
631
632 //foreach ($posts as &$post) {
633 //Support PHP4 by not using foreach
634 for ($counter = 0; $counter < $limit; $counter += 1) {
635 $post = $posts[$counter];
636 $p_ID = $post->post;
637
638 //Gets the votes
639 $votes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
640 $sinks_raw = $wpdb->get_var("SELECT usersinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
641 $guestvotes_raw = $wpdb->get_var("SELECT guests FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
642 $guestsinks_raw = $wpdb->get_var("SELECT guestsinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
643
644 //Put it in array form
645 $votes = explode(",", $votes_raw);
646 $sinks = explode(",", $sinks_raw);
647 $guestvotes = explode(",", $guestvotes_raw);
648 $guestsinks = explode(",", $guestsinks_raw);
649
650 $initial = get_option('voteiu_initialoffset');
651
652 //The mathematics
653 $votecount = count($votes) - count($sinks) + count($guestvotes) - count($guestsinks) + $initial;
654 array_push($postarray, array($p_ID));
655 array_push($votesarray, array($votecount));
656 }
657 array_multisort($votesarray, SORT_DESC, $postarray);
658 $output = array($postarray, $votesarray);
659 return $output;
660
661}
662
663//Displays the widget
664function MostVotedAllTime_Widget() {
665 $a = SortVotes();
666 //Before
667
668?>
669<div class="votewidget">
670<div class="title">Most Voted</div>
671<?php
672 $rows = 0;
673
674//Now does not include deleted posts
675$i = 0;
676while ($rows < get_option('voteiu_widgetcount')) {
677 if ($a[0][$i][0] != '') {
678 $postdat = get_post($a[0][$i][0]);
679 if (!empty($postdat)) {
680 $rows++;
681
682 if (round($rows / 2) == ($rows / 2)) {
683 echo '<div class="fore">';
684 } else {
685 echo '<div class="back">';
686 }
687 echo '<div class="votecount">'.$a[1][$i][0].' '.Pluralize($a[1][$i][0], 'votes', 'vote').' </div><div><a href="'.$postdat->guid.'" title="'.$postdat->post_title.'">'.$postdat->post_title.'</a></div>';
688 echo '</div>';
689 }
690 }
691 if ($i < count($a[0])) {
692 $i++;
693 } else {
694 break; //exit the loop
695 }
696}
697
698//End
699?>
700
701</div>
702<?php
703
704}
705
706//Displays the widget optimised for sidebar
707function MostVotedAllTime_SidebarWidget() {
708 $a = SortVotes();
709 //Before
710
711?>
712<div class="votewidget">
713<?php
714 $rows = 0;
715
716//Now does not include deleted posts
717$i = 0;
718while ($rows < get_option('voteiu_widgetcount')) {
719 if ($a[0][$i][0] != '') {
720 $postdat = get_post($a[0][$i][0]);
721 if (!empty($postdat)) {
722 $rows++;
723 if (round($rows / 2) == ($rows / 2)) {
724 echo '<div class="fore">';
725 } else {
726 echo '<div class="back">';
727 }
728 echo '<div class="votecount" style="width: 1em; color: #555555; font-weight: bold;">'.$a[1][$i][0].' </div><div><a href="'.$postdat->guid.'" title="'.$postdat->post_title.'">'.$postdat->post_title.'</a></div>';
729 echo '</div>';
730 }
731 }
732 if ($i < count($a[0])) {
733 $i++;
734 } else {
735 break; //exit the loop
736 }
737}
738
739//End
740?>
741
742</div>
743<?php
744
745}
746
747//For those particular with English
748function Pluralize($number, $plural, $singular) {
749 if ($number == 1) {
750 return $singular;
751 } else {
752 return $plural;
753 }
754}
755
756//Not used yet
757function IsExcluded($id) {
758 global $excludedid;
759 $clean = str_replace("\r", "", $excludedid);
760 $excluded = explode("\n", $clean);
761}
762
763?>