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