· 8 years ago · Jul 10, 2018, 12:12 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 if ($chhash == $key) {
228 return TRUE;
229 } else {
230 return FALSE;
231 }
232}
233
234//Saves the vote of a user to the database
235function Vote($post_ID, $user_ID, $type) {
236 global $wpdb;
237
238 //Prevents SQL injection
239 $p_ID = $wpdb->escape($post_ID);
240 $u_ID = $wpdb->escape($user_ID);
241
242 //Create entries if not existant
243 SetPost($p_ID);
244 SetUser($u_ID);
245
246 //Gets the votes
247 $votes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
248 $sinks_raw = $wpdb->get_var("SELECT usersinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
249 $uservotes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes_users WHERE user='".$u_ID."'");
250 $usersinks_raw = $wpdb->get_var("SELECT sinks FROM ".$wpdb->prefix."votes_users WHERE user='".$u_ID."'");
251
252 //Gets the votes in array form
253 $votes = explode(",", $votes_raw);
254 $sinks = explode(",", $sinks_raw);
255 $uservotes = explode(",", $uservotes_raw);
256 $usersinks = explode(",", $usersinks_raw);
257
258 //Check if user voted
259 if (!UserVoted($post_ID,$user_ID)) {
260 //user hasn't vote, so the script allows the user to vote
261
262 if ($type != 'sink') {
263 //Add vote to array
264 $user_var[0] = $u_ID;
265 $post_var[0] = $p_ID;
266 $votes_result = array_merge($votes,$user_var);
267 $votes_result_raw = implode(",",$votes_result);
268 $uservotes_result = array_merge($uservotes,$post_var);
269 $uservotes_result_raw = implode(",",$uservotes_result);
270 $sinks_result_raw = $sinks_raw;
271 $usersinks_result_raw = $usersinks_raw;
272 } else {
273 //Add sink to array
274 $user_var[0] = $u_ID;
275 $post_var[0] = $p_ID;
276 $sinks_result = array_merge($sinks,$user_var);
277 $sinks_result_raw = implode(",",$sinks_result);
278 $usersinks_result = array_merge($usersinks,$post_var);
279 $usersinks_result_raw = implode(",",$usersinks_result);
280 $votes_result_raw = $votes_raw;
281 $uservotes_result_raw = $votesinks_raw;
282 }
283
284 //Prevents SQL injection
285 $votes_result_sql = $wpdb->escape($votes_result_raw);
286 $sinks_result_sql = $wpdb->escape($sinks_result_raw);
287 $uservotes_result_sql = $wpdb->escape($uservotes_result_raw);
288 $usersinks_result_sql = $wpdb->escape($usersinks_result_raw);
289
290 //Update votes
291 $wpdb->query("UPDATE ".$wpdb->prefix."votes SET votes='".$votes_result_sql."' WHERE post='".$p_ID."'");
292 $wpdb->query("UPDATE ".$wpdb->prefix."votes SET usersinks='".$sinks_result_sql."' WHERE post='".$p_ID."'");
293 $wpdb->query("UPDATE ".$wpdb->prefix."votes_users SET votes='".$uservotes_result_sql."' WHERE user='".$u_ID."'");
294 $wpdb->query("UPDATE ".$wpdb->prefix."votes_users SET sinks='".$usersinks_result_sql."' WHERE user='".$u_ID."'");
295
296 $result = 'true';
297 } else {
298 //The user voted, thus the script will not update the votes in the article
299 $result = 'false';
300 }
301
302 return $result; //returns '' on failure, returns 'true' if votes were casted, returns 'false' if user already casted a vote
303}
304
305//Saves the vote of a guest to the database
306function GuestVote($post_ID, $type) {
307 global $wpdb;
308
309 //Guest's vote is stored permanently. May implement votes that will expire.
310 //Use user IP
311 //$iphash = md5($_SERVER['REMOTE_ADDR']);
312
313 //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.
314
315 if(isset($_COOKIE['tevinevotes'])) {
316 $iphash = $_COOKIE['tevinevotes'];
317 } else {
318 $iphash = time();
319 $cookielife = time() + 1000; //Set to expire in 24 hours
320 setcookie('tevinevotes', $iphash, $cookielife);
321 }
322
323
324 //Prevents SQL injection
325 $u_ID = $wpdb->escape($iphash);
326 $p_ID = $wpdb->escape($post_ID);
327
328 //Create entries if not existant
329 SetPost($p_ID);
330
331 //Gets the info
332 $votes_raw = $wpdb->get_var("SELECT guests FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
333 $sinks_raw = $wpdb->get_var("SELECT guestsinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
334
335 //Gets the votes in array form
336 $votes = explode(",", $votes_raw);
337 $sinks = explode(",", $sinks_raw);
338
339 //Check if user voted
340 if (!GuestVoted($post_ID,$user_ID)) {
341 //user hasn't vote, so the script allows the user to vote
342
343 if ($type != 'sink') {
344 //Add vote to array
345 $user_var[0] = $u_ID;
346 $post_var[0] = $p_ID;
347 $votes_result = array_merge($votes,$user_var);
348 $votes_result_raw = implode(",",$votes_result);
349 $sinks_result_raw = $sinks_raw;
350 } else {
351 //Add sink to array
352 $user_var[0] = $u_ID;
353 $post_var[0] = $p_ID;
354 $sinks_result = array_merge($sinks,$user_var);
355 $sinks_result_raw = implode(",",$sinks_result);
356 $votes_result_raw = $votes_raw;
357 }
358
359 //Prevents SQL injection
360 $votes_result_sql = $wpdb->escape($votes_result_raw);
361 $sinks_result_sql = $wpdb->escape($sinks_result_raw);
362
363 //Update votes
364 $wpdb->query("UPDATE ".$wpdb->prefix."votes SET guests='".$votes_result_sql."' WHERE post='".$p_ID."'");
365 $wpdb->query("UPDATE ".$wpdb->prefix."votes SET guestsinks='".$sinks_result_sql."' WHERE post='".$p_ID."'");
366
367 $result = 'true';
368 } else {
369 //The user voted, thus the script will not update the votes in the article
370 $result = 'false';
371 }
372
373 return $result; //returns '' on failure, returns 'true' if votes were casted, returns 'false' if user already casted a vote
374}
375
376//Gets an array of posts with vote count
377//Identical to SortVotes() except that it is sorted by ID in descending order, and has more information on votes
378function GetVoteArray() {
379 global $wpdb;
380
381 $limit = get_option('voteiu_limit');
382
383 //Get posts logged, and get the latest posts voted [new posts have larger IDs
384 $posts = $wpdb->get_results("SELECT post FROM ".$wpdb->prefix."votes WHERE ID != '' ORDER BY ID DESC");
385
386 $postarray = array();
387 $votesarray = array();
388 $uservotes_a = array();
389 $usersinks_a = array();
390 $guestvotes_a = array();
391 $guestsinks_a = array();
392
393 /*
394 Limit, so as to reduce time taken for this script to run. If you want to raise the limit to maximum, use
395 $limit = count($posts);
396 */
397 if ($limit == '') {
398 $limit = 100;
399 }
400 $limit = 999;
401
402 //Ignore limit if post count is below limit
403 if ($limit > count($posts)) {
404 $limit = count($posts);
405 }
406
407
408 //foreach ($posts as &$post) {
409 //Support PHP4 by not using foreach
410 for ($counter = 0; $counter < $limit; $counter += 1) {
411 $post = $posts[$counter];
412 $p_ID = $post->post;
413
414 //Gets the votes
415 $votes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
416 $sinks_raw = $wpdb->get_var("SELECT usersinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
417 $guestvotes_raw = $wpdb->get_var("SELECT guests FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
418 $guestsinks_raw = $wpdb->get_var("SELECT guestsinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
419
420 //Put it in array form
421 $votes = explode(",", $votes_raw);
422 $sinks = explode(",", $sinks_raw);
423 $guestvotes = explode(",", $guestvotes_raw);
424 $guestsinks = explode(",", $guestsinks_raw);
425
426 $initial = get_option('voteiu_initialoffset');
427
428 //The mathematics
429 $votecount = count($votes) - count($sinks) + count($guestvotes) - count($guestsinks) + $initial;
430 array_push($postarray, array($p_ID));
431 array_push($votesarray, array($votecount));
432 array_push($uservotes_a, array(count($votes)-1)); //Apparently there is one extra item in the array
433 array_push($usersinks_a, array(count($sinks)-1));
434 array_push($guestvotes_a, array(count($guestvotes)-1));
435 array_push($guestsinks_a, array(count($guestsinks)-1));
436 }
437 $output = array($postarray, $votesarray, $uservotes_a, $usersinks_a, $guestvotes_a, $guestsinks_a);
438 return $output;
439
440}
441
442//Used in options page
443function DisplayPostList() {
444 $a = GetVoteArray();
445 $i = 0;
446
447//Begin table
448?>
449<table class="widefat post fixed" id="formtable" style="clear: both;" cellspacing="0">
450 <thead>
451 <tr>
452
453 <th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" name="multiselect[]" onclick="javascript:CheckUncheck()" /></th>
454 <th scope="col" id="title" class="manage-column column-title" style="">Post</th>
455<?php /*?> <th scope="col" id="author" class="manage-column column-author" style="">Author</th><?php */ ?>
456 <th scope="col" id="votes" class="manage-column column-categories" style="width: 40%">Votes</th>
457
458
459 </tr>
460 </thead>
461
462 <tfoot>
463 <tr>
464 <th scope="col" class="manage-column column-cb check-column" style=""><input type="checkbox" name="multiselect[]" onclick="javascript:CheckUncheck()" /></th>
465 <th scope="col" class="manage-column column-title" style="">Post</th>
466<?php /* ?> <th scope="col" class="manage-column column-author" style="">Author</th><?php */ ?>
467
468 <th scope="col" class="manage-column column-categories" style="">Votes</th>
469
470 </tr>
471 </tfoot>
472 <tbody>
473<?php
474
475
476 while ($i < count($a[0])) {
477 $postdat = get_post($a[0][$i][0]);
478 if (!empty($postdat)) {
479?>
480 <tr id='post-<?php echo $a[0][$i][0]; ?>' class='alternate author-other status-publish iedit' valign="top">
481 <th scope="row" class="check-column"><input type="checkbox" name="post[]" value="<?php echo $a[0][$i][0]; ?>" /></th>
482<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>
483<?php /* ?><td class="author column-author"><?php echo $postdat->post_author; ?></td><?php */ ?>
484<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
485if(get_option('voteiu_initialoffset') != '0') {
486echo ', Offset: ';
487if (get_option('voteiu_initialoffset') > 0) {
488echo '<span style="color:#00CC00">+'.get_option('voteiu_initialoffset').'</span>';
489} else {
490echo '<span style="color:#CC0000">'.get_option('voteiu_initialoffset').'</span>';
491} } ?>)</td>
492<?php
493
494/*
495 echo $postdat->post_title;
496 echo ' - ';
497 echo $a[1][$i][0];
498 echo '<br />';
499*/
500
501?>
502</tr>
503<?php
504 }
505 $i++;
506 }
507
508
509//End table
510?>
511 </tbody>
512 </table>
513<?php
514}
515
516//Handles the deleting of votes, used to read the POST when the page is submitted
517function VoteBulkEdit() {
518//error_reporting(E_ALL);
519/*print_r($_POST);*/
520$buttonnumber = 0; //Determines which apply button was clicked on. 0 if no button was clicked.
521$action = 'none'; //Determines what should be done
522if (array_key_exists('doaction1', $_POST)) {
523$buttonnumber = 1;
524}
525if (array_key_exists('doaction2', $_POST)) {
526$buttonnumber = 2;
527}
528if ($buttonnumber != 0 && array_key_exists('action'.$buttonnumber, $_POST)) {
529 if ($_POST['action'.$buttonnumber] != -1) {
530 //Assigns action to be done
531 $action = $_POST['action'.$buttonnumber];
532 }
533}
534
535if (!array_key_exists('post', $_POST)) {
536$action = 'none'; //set action to none if there are no posts to modify
537}
538
539//Begin modifying votes
540if ($action != 'none' && $action != '') {
541ResetVote($_POST['post'], $action);
542}
543
544}
545
546function ResetVote($postids, $action) {
547/*
548Testing stuff...
549echo 'Posts: '.implode(', ',$_POST['post']);
550echo '<br />';
551echo "Action: ".$action;
552*/
553global $wpdb;
554//$wpdb->show_errors();
555
556switch ($action) {
557
558case 'none':
559//do nothing
560break;
561case 'delete':
562//reset all votes for the post
563$i = 0;
564while ($i < count($postids)) {
565$wpdb->query("UPDATE ".$wpdb->prefix."votes SET votes = '',guests = '',usersinks = '', guestsinks = '' WHERE `post`=".$postids[$i]." LIMIT 1 ;");
566$i++;
567}
568EditVoteSuccess();
569break;
570case 'deleteuser':
571//reset all votes for users
572$i = 0;
573while ($i < count($postids)) {
574$wpdb->query("UPDATE ".$wpdb->prefix."votes SET votes = '',usersinks = '' WHERE post=".$postids[$i]." LIMIT 1 ;");
575$i++;
576}
577EditVoteSuccess();
578break;
579case 'deleteguest':
580//reset all votes for guests
581$i = 0;
582while ($i < count($postids)) {
583$wpdb->query("UPDATE ".$wpdb->prefix."votes SET guests = '',guestsinks = '' WHERE post=".$postids[$i]." LIMIT 1 ;");
584$i++;
585}
586EditVoteSuccess();
587break;
588}
589
590
591
592
593
594}
595
596function EditVoteSuccess() {
597?><div id="message" class="updated fade"><p><strong>Votes edited</strong></p></div><?php
598}
599
600//Used to sort votes for widgets
601function SortVotes() {
602 global $wpdb;
603
604 $limit = get_option('voteiu_limit');
605
606 //Get posts logged, and get the latest posts voted [new posts have larger IDs
607 $posts = $wpdb->get_results("SELECT post FROM ".$wpdb->prefix."votes WHERE ID != '' ORDER BY ID DESC");
608
609 $postarray = array();
610 $votesarray = array();
611
612 /*
613 Limit, so as to reduce time taken for this script to run. If you want to raise the limit to maximum, use
614 $limit = count($posts);
615 */
616 if ($limit == '') {
617 $limit = 100;
618 }
619
620 //Ignore limit if post count is below limit
621 if ($limit > count($posts)) {
622 $limit = count($posts);
623 }
624
625
626 //foreach ($posts as &$post) {
627 //Support PHP4 by not using foreach
628 for ($counter = 0; $counter < $limit; $counter += 1) {
629 $post = $posts[$counter];
630 $p_ID = $post->post;
631
632 //Gets the votes
633 $votes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
634 $sinks_raw = $wpdb->get_var("SELECT usersinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
635 $guestvotes_raw = $wpdb->get_var("SELECT guests FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
636 $guestsinks_raw = $wpdb->get_var("SELECT guestsinks FROM ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
637
638 //Put it in array form
639 $votes = explode(",", $votes_raw);
640 $sinks = explode(",", $sinks_raw);
641 $guestvotes = explode(",", $guestvotes_raw);
642 $guestsinks = explode(",", $guestsinks_raw);
643
644 $initial = get_option('voteiu_initialoffset');
645
646 //The mathematics
647 $votecount = count($votes) - count($sinks) + count($guestvotes) - count($guestsinks) + $initial;
648 array_push($postarray, array($p_ID));
649 array_push($votesarray, array($votecount));
650 }
651 array_multisort($votesarray, SORT_DESC, $postarray);
652 $output = array($postarray, $votesarray);
653 return $output;
654
655}
656
657//Displays the widget
658function MostVotedAllTime_Widget() {
659 $a = SortVotes();
660 //Before
661
662?>
663<div class="votewidget">
664<div class="title">Most Voted</div>
665<?php
666 $rows = 0;
667
668//Now does not include deleted posts
669$i = 0;
670while ($rows < get_option('voteiu_widgetcount')) {
671 if ($a[0][$i][0] != '') {
672 $postdat = get_post($a[0][$i][0]);
673 if (!empty($postdat)) {
674 $rows++;
675
676 if (round($rows / 2) == ($rows / 2)) {
677 echo '<div class="fore">';
678 } else {
679 echo '<div class="back">';
680 }
681 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>';
682 echo '</div>';
683 }
684 }
685 if ($i < count($a[0])) {
686 $i++;
687 } else {
688 break; //exit the loop
689 }
690}
691
692//End
693?>
694
695</div>
696<?php
697
698}
699
700//Displays the widget optimised for sidebar
701function MostVotedAllTime_SidebarWidget() {
702 $a = SortVotes();
703 //Before
704
705?>
706<div class="votewidget">
707<?php
708 $rows = 0;
709
710//Now does not include deleted posts
711$i = 0;
712while ($rows < get_option('voteiu_widgetcount')) {
713 if ($a[0][$i][0] != '') {
714 $postdat = get_post($a[0][$i][0]);
715 if (!empty($postdat)) {
716 $rows++;
717 if (round($rows / 2) == ($rows / 2)) {
718 echo '<div class="fore">';
719 } else {
720 echo '<div class="back">';
721 }
722 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>';
723 echo '</div>';
724 }
725 }
726 if ($i < count($a[0])) {
727 $i++;
728 } else {
729 break; //exit the loop
730 }
731}
732
733//End
734?>
735
736</div>
737<?php
738
739}
740
741//For those particular with English
742function Pluralize($number, $plural, $singular) {
743 if ($number == 1) {
744 return $singular;
745 } else {
746 return $plural;
747 }
748}
749
750//Not used yet
751function IsExcluded($id) {
752 global $excludedid;
753 $clean = str_replace("\r", "", $excludedid);
754 $excluded = explode("\n", $clean);
755}
756
757?>