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