| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP-Polls |
| 4 |
Plugin URI: http://www.lesterchan.net/portfolio/programming.php |
| 5 |
Description: Adds A Poll Feature To WordPress |
| 6 |
Version: 2.11 |
| 7 |
Author: GaMerZ |
| 8 |
Author URI: http://www.lesterchan.net |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
/* Copyright 2006 Lester Chan (email : gamerz84@hotmail.com) |
| 13 |
|
| 14 |
This program is free software; you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License as published by |
| 16 |
the Free Software Foundation; either version 2 of the License, or |
| 17 |
(at your option) any later version. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
GNU General Public License for more details. |
| 23 |
|
| 24 |
You should have received a copy of the GNU General Public License |
| 25 |
along with this program; if not, write to the Free Software |
| 26 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 |
*/ |
| 28 |
|
| 29 |
|
| 30 |
### Polls Table Name |
| 31 |
$wpdb->pollsq = $table_prefix . 'pollsq'; |
| 32 |
$wpdb->pollsa = $table_prefix . 'pollsa'; |
| 33 |
$wpdb->pollsip = $table_prefix . 'pollsip'; |
| 34 |
|
| 35 |
|
| 36 |
### Function: Poll Administration Menu |
| 37 |
add_action('admin_menu', 'poll_menu'); |
| 38 |
function poll_menu() { |
| 39 |
if (function_exists('add_menu_page')) { |
| 40 |
add_menu_page(__('Polls'), __('Polls'), 'manage_polls', 'polls/polls-manager.php'); |
| 41 |
} |
| 42 |
if (function_exists('add_submenu_page')) { |
| 43 |
add_submenu_page('polls/polls-manager.php', __('Manage Polls'), __('Manage Polls'), 'manage_polls', 'polls/polls-manager.php'); |
| 44 |
add_submenu_page('polls/polls-manager.php', __('Poll Options'), __('Poll Options'), 'manage_polls', 'polls/polls-options.php'); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
### Function: Get Poll |
| 50 |
function get_poll($temp_poll_id = 0, $display = true) { |
| 51 |
global $wpdb; |
| 52 |
// Poll Result Link |
| 53 |
$pollresult_id = intval($_GET['pollresult']); |
| 54 |
// Check Whether Poll Is Disabled |
| 55 |
if(intval(get_settings('poll_currentpoll')) == -1) { |
| 56 |
if($display) { |
| 57 |
echo stripslashes(get_settings('poll_template_disable')); |
| 58 |
return; |
| 59 |
} else { |
| 60 |
return stripslashes(get_settings('poll_template_disable')); |
| 61 |
} |
| 62 |
// Poll Is Enabled |
| 63 |
} else { |
| 64 |
// Hardcoded Poll ID Is Not Specified |
| 65 |
if(intval($temp_poll_id) == 0) { |
| 66 |
// Random Poll |
| 67 |
if(intval(get_settings('poll_currentpoll')) == -2) { |
| 68 |
$random_poll_id = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq ORDER BY RAND() LIMIT 1"); |
| 69 |
$poll_id = intval($random_poll_id); |
| 70 |
if($pollresult_id > 0) { |
| 71 |
$poll_id = $pollresult_id; |
| 72 |
} elseif(intval($_POST['poll_id']) > 0) { |
| 73 |
$poll_id = intval($_POST['poll_id']); |
| 74 |
} |
| 75 |
// Current Poll ID Is Not Specified |
| 76 |
} elseif(intval(get_settings('poll_currentpoll')) == 0) { |
| 77 |
// Get Lastest Poll ID |
| 78 |
$poll_id = intval(get_settings('poll_latestpoll')); |
| 79 |
} else { |
| 80 |
// Get Current Poll ID |
| 81 |
$poll_id = intval(get_settings('poll_currentpoll')); |
| 82 |
} |
| 83 |
// Get Hardcoded Poll ID |
| 84 |
} else { |
| 85 |
$poll_id = intval($temp_poll_id); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// User Click on View Results Link |
| 90 |
if($pollresult_id == $poll_id) { |
| 91 |
if($display) { |
| 92 |
echo display_pollresult($poll_id); |
| 93 |
return; |
| 94 |
} else { |
| 95 |
return display_pollresult($poll_id); |
| 96 |
} |
| 97 |
// Check Whether User Has Voted |
| 98 |
} else { |
| 99 |
$poll_active = $wpdb->get_var("SELECT pollq_active FROM $wpdb->pollsq WHERE pollq_id = $poll_id"); |
| 100 |
$poll_active = intval($poll_active); |
| 101 |
$check_voted = check_voted($poll_id); |
| 102 |
if($check_voted > 0 || $poll_active == 0 || !check_allowtovote()) { |
| 103 |
if($display) { |
| 104 |
echo display_pollresult($poll_id, $check_voted); |
| 105 |
return; |
| 106 |
} else { |
| 107 |
return display_pollresult($poll_id, $check_voted); |
| 108 |
} |
| 109 |
} else { |
| 110 |
if($display) { |
| 111 |
echo display_pollvote($poll_id); |
| 112 |
return; |
| 113 |
} else { |
| 114 |
return display_pollvote($poll_id); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
### Function: Displays Polls Header |
| 122 |
add_action('wp_head', 'poll_header'); |
| 123 |
function poll_header() { |
| 124 |
echo '<script type="text/javascript">'."\n"; |
| 125 |
echo '/* Start Of Javascript Generated By WP-Polls 2.11 */'."\n"; |
| 126 |
echo '/* <![CDATA[ */'."\n"; |
| 127 |
echo "\t".'if(site_url != \''.get_settings('siteurl').'\') {'."\n"; |
| 128 |
echo "\t\t".'var site_url = \''.get_settings('siteurl').'\';'."\n"; |
| 129 |
echo "\t".'}'."\n"; |
| 130 |
echo '/* ]]> */'."\n"; |
| 131 |
echo '/* End Of Javascript Generated By WP-Polls 2.11 */'."\n"; |
| 132 |
echo '</script>'."\n"; |
| 133 |
echo '<script src="'.get_settings('siteurl').'/wp-includes/js/tw-sack.js" type="text/javascript"></script>'."\n"; |
| 134 |
echo '<script src="'.get_settings('siteurl').'/wp-content/plugins/polls/polls-js.js" type="text/javascript"></script>'."\n"; |
| 135 |
echo '<link rel="stylesheet" href="'.get_settings('siteurl').'/wp-content/plugins/polls/polls-css.css" type="text/css" media="screen" />'."\n"; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
### Function: Check Who Is Allow To Vote |
| 140 |
function check_allowtovote() { |
| 141 |
global $user_ID; |
| 142 |
$user_ID = intval($user_ID); |
| 143 |
$allow_to_vote = intval(get_settings('poll_allowtovote')); |
| 144 |
switch($allow_to_vote) { |
| 145 |
// Guests Only |
| 146 |
case 0: |
| 147 |
if($user_ID > 0) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
return true; |
| 151 |
break; |
| 152 |
// Registered Users Only |
| 153 |
case 1: |
| 154 |
if($user_ID == 0) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
return true; |
| 158 |
break; |
| 159 |
// Registered Users And Guests |
| 160 |
case 2: |
| 161 |
default: |
| 162 |
return true; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
### Funcrion: Check Voted By Cookie Or IP |
| 168 |
function check_voted($poll_id) { |
| 169 |
$check_voted_cookie = check_voted_cookie($poll_id); |
| 170 |
if($check_voted_cookie > 0) { |
| 171 |
return $check_voted_cookie; |
| 172 |
} else { |
| 173 |
$check_voted_ip = check_voted_ip($poll_id); |
| 174 |
if($check_voted_ip > 0) { |
| 175 |
return $check_voted_ip; |
| 176 |
} else { |
| 177 |
return 0; |
| 178 |
} |
| 179 |
} |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
### Function: Check Voted By Cookie |
| 185 |
function check_voted_cookie($poll_id) { |
| 186 |
// 0: False | > 0: True |
| 187 |
return intval($_COOKIE["voted_$poll_id"]); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
### Function: Check Voted By IP |
| 192 |
function check_voted_ip($poll_id) { |
| 193 |
global $wpdb; |
| 194 |
// Check IP From IP Logging Database |
| 195 |
$get_voted_aid = $wpdb->get_var("SELECT pollip_aid FROM $wpdb->pollsip WHERE pollip_qid = $poll_id AND pollip_ip = '".get_ipaddress()."'"); |
| 196 |
// 0: False | > 0: True |
| 197 |
return intval($get_voted_aid); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
### Function: Display Voting Form |
| 202 |
function display_pollvote($poll_id, $without_poll_title = false) { |
| 203 |
global $wpdb; |
| 204 |
// Temp Poll Result |
| 205 |
$temp_pollvote = ''; |
| 206 |
// Get Poll Question Data |
| 207 |
$poll_question = $wpdb->get_row("SELECT pollq_id, pollq_question, pollq_totalvotes FROM $wpdb->pollsq WHERE pollq_id = $poll_id LIMIT 1"); |
| 208 |
// Poll Question Variables |
| 209 |
$poll_question_text = stripslashes($poll_question->pollq_question); |
| 210 |
$poll_question_id = intval($poll_question->pollq_id); |
| 211 |
$poll_question_totalvotes = intval($poll_question->pollq_totalvotes); |
| 212 |
$template_question = stripslashes(get_settings('poll_template_voteheader')); |
| 213 |
$template_question = str_replace("%POLL_QUESTION%", $poll_question_text, $template_question); |
| 214 |
$template_question = str_replace("%POLL_ID%", $poll_question_id, $template_question); |
| 215 |
$template_question = str_replace("%POLL_TOTALVOTES%", $poll_question_totalvotes, $template_question); |
| 216 |
// Get Poll Answers Data |
| 217 |
$poll_answers = $wpdb->get_results("SELECT polla_aid, polla_answers, polla_votes FROM $wpdb->pollsa WHERE polla_qid = $poll_question_id ORDER BY ".get_settings('poll_ans_sortby').' '.get_settings('poll_ans_sortorder')); |
| 218 |
// If There Is Poll Question With Answers |
| 219 |
if($poll_question && $poll_answers) { |
| 220 |
// Display Poll Voting Form |
| 221 |
if(!$without_poll_title) { |
| 222 |
$temp_pollvote .= "<div id=\"polls-$poll_question_id\" class=\"wp-polls\">\n"; |
| 223 |
$temp_pollvote .= "\t<form id=\"polls_form_$poll_question_id\" action=\"$_SERVER[REQUEST_URI]\" method=\"post\">\n"; |
| 224 |
$temp_pollvote .= "\t\t<input type=\"hidden\" name=\"poll_id\" value=\"$poll_question_id\" />\n"; |
| 225 |
// Print Out Voting Form Header Template |
| 226 |
$temp_pollvote .= "\t\t$template_question\n"; |
| 227 |
} |
| 228 |
foreach($poll_answers as $poll_answer) { |
| 229 |
// Poll Answer Variables |
| 230 |
$poll_answer_id = intval($poll_answer->polla_aid); |
| 231 |
$poll_answer_text = stripslashes($poll_answer->polla_answers); |
| 232 |
$poll_answer_votes = intval($poll_answer->polla_votes); |
| 233 |
$template_answer = stripslashes(get_settings('poll_template_votebody')); |
| 234 |
$template_answer = str_replace("%POLL_ID%", $poll_question_id, $template_answer); |
| 235 |
$template_answer = str_replace("%POLL_ANSWER_ID%", $poll_answer_id, $template_answer); |
| 236 |
$template_answer = str_replace("%POLL_ANSWER%", $poll_answer_text, $template_answer); |
| 237 |
$template_answer = str_replace("%POLL_ANSWER_VOTES%", number_format($poll_answer_votes), $template_answer); |
| 238 |
// Print Out Voting Form Body Template |
| 239 |
$temp_pollvote .= "\t\t$template_answer\n"; |
| 240 |
} |
| 241 |
// Determine Poll Result URL |
| 242 |
$poll_result_url = $_SERVER['REQUEST_URI']; |
| 243 |
$poll_result_url = preg_replace('/pollresult=(\d+)/i', 'pollresult='.$poll_question_id, $poll_result_url); |
| 244 |
if(intval($_GET['pollresult']) == 0) { |
| 245 |
if(strpos($poll_result_url, '?') !== false) { |
| 246 |
$poll_result_url = "$poll_result_url&pollresult=$poll_question_id"; |
| 247 |
} else { |
| 248 |
$poll_result_url = "$poll_result_url?pollresult=$poll_question_id"; |
| 249 |
} |
| 250 |
} |
| 251 |
// Voting Form Footer Variables |
| 252 |
$template_footer = stripslashes(get_settings('poll_template_votefooter')); |
| 253 |
$template_footer = str_replace("%POLL_ID%", $poll_question_id, $template_footer); |
| 254 |
$template_footer = str_replace("%POLL_RESULT_URL%", $poll_result_url, $template_footer); |
| 255 |
// Print Out Voting Form Footer Template |
| 256 |
$temp_pollvote .= "\t\t$template_footer\n"; |
| 257 |
if(!$without_poll_title) { |
| 258 |
$temp_pollvote .= "\t</form>\n"; |
| 259 |
$temp_pollvote .= "</div>\n"; |
| 260 |
$temp_pollvote .= "<div id=\"polls-$poll_question_id-loading\" class=\"wp-polls-loading\"><img src=\"".get_settings('siteurl')."/wp-content/plugins/polls/images/loading.gif\" width=\"16\" height=\"16\" alt=\"".__('Loading')." ...\" title=\"".__('Loading')." ...\" /> ".__('Loading')." ...</div>\n"; |
| 261 |
} |
| 262 |
} else { |
| 263 |
$temp_pollvote .= stripslashes(get_settings('poll_template_disable')); |
| 264 |
} |
| 265 |
// Return Poll Vote Template |
| 266 |
return $temp_pollvote; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
### Function: Display Results Form |
| 271 |
function display_pollresult($poll_id, $user_voted = 0, $without_poll_title = false) { |
| 272 |
global $wpdb; |
| 273 |
// Temp Poll Result |
| 274 |
$temp_pollresult = ''; |
| 275 |
// Most/Least Variables |
| 276 |
$poll_most_answer = ''; |
| 277 |
$poll_most_votes = 0; |
| 278 |
$poll_most_percentage = 0; |
| 279 |
$poll_least_answer = ''; |
| 280 |
$poll_least_votes = 0; |
| 281 |
$poll_least_percentage = 0; |
| 282 |
// Get Poll Question Data |
| 283 |
$poll_question = $wpdb->get_row("SELECT pollq_id, pollq_question, pollq_totalvotes, pollq_active FROM $wpdb->pollsq WHERE pollq_id = $poll_id LIMIT 1"); |
| 284 |
// Poll Question Variables |
| 285 |
$poll_question_text = stripslashes($poll_question->pollq_question); |
| 286 |
$poll_question_id = intval($poll_question->pollq_id); |
| 287 |
$poll_question_totalvotes = intval($poll_question->pollq_totalvotes); |
| 288 |
$poll_question_active = intval($poll_question->pollq_active); |
| 289 |
$template_question = stripslashes(get_settings('poll_template_resultheader')); |
| 290 |
$template_question = str_replace("%POLL_QUESTION%", $poll_question_text, $template_question); |
| 291 |
$template_question = str_replace("%POLL_ID%", $poll_question_id, $template_question); |
| 292 |
$template_question = str_replace("%POLL_TOTALVOTES%", $poll_question_totalvotes, $template_question); |
| 293 |
// Get Poll Answers Data |
| 294 |
$poll_answers = $wpdb->get_results("SELECT polla_aid, polla_answers, polla_votes FROM $wpdb->pollsa WHERE polla_qid = $poll_question_id ORDER BY ".get_settings('poll_ans_result_sortby').' '.get_settings('poll_ans_result_sortorder')); |
| 295 |
// If There Is Poll Question With Answers |
| 296 |
if($poll_question && $poll_answers) { |
| 297 |
// Is The Poll Total Votes 0? |
| 298 |
$poll_totalvotes_zero = true; |
| 299 |
if($poll_question_totalvotes > 0) { |
| 300 |
$poll_totalvotes_zero = false; |
| 301 |
} |
| 302 |
// Print Out Result Header Template |
| 303 |
if(!$without_poll_title) { |
| 304 |
$temp_pollresult .= "<div id=\"polls-$poll_question_id\" class=\"wp-polls\">\n"; |
| 305 |
$temp_pollresult .= "\t\t$template_question\n"; |
| 306 |
} |
| 307 |
foreach($poll_answers as $poll_answer) { |
| 308 |
// Poll Answer Variables |
| 309 |
$poll_answer_id = intval($poll_answer->polla_aid); |
| 310 |
$poll_answer_text = stripslashes($poll_answer->polla_answers); |
| 311 |
$poll_answer_votes = intval($poll_answer->polla_votes); |
| 312 |
$poll_answer_text = stripslashes($poll_answer->polla_answers); |
| 313 |
$poll_answer_percentage = 0; |
| 314 |
$poll_answer_imagewidth = 0; |
| 315 |
// Calculate Percentage And Image Bar Width |
| 316 |
if(!$poll_totalvotes_zero) { |
| 317 |
if($poll_answer_votes > 0) { |
| 318 |
$poll_answer_percentage = round((($poll_answer_votes/$poll_question_totalvotes)*100)); |
| 319 |
$poll_answer_imagewidth = round($poll_answer_percentage); |
| 320 |
} else { |
| 321 |
$poll_answer_percentage = 0; |
| 322 |
$poll_answer_imagewidth = 1; |
| 323 |
} |
| 324 |
} else { |
| 325 |
$poll_answer_percentage = 0; |
| 326 |
$poll_answer_imagewidth = 1; |
| 327 |
} |
| 328 |
// Let User See What Options They Voted |
| 329 |
if($user_voted == $poll_answer_id) { |
| 330 |
// Results Body Variables |
| 331 |
$template_answer = stripslashes(get_settings('poll_template_resultbody2')); |
| 332 |
$template_answer = str_replace("%POLL_ANSWER_ID%", $poll_answer_id, $template_answer); |
| 333 |
$template_answer = str_replace("%POLL_ANSWER%", $poll_answer_text, $template_answer); |
| 334 |
$template_answer = str_replace("%POLL_ANSWER_VOTES%", number_format($poll_answer_votes), $template_answer); |
| 335 |
$template_answer = str_replace("%POLL_ANSWER_PERCENTAGE%", $poll_answer_percentage, $template_answer); |
| 336 |
$template_answer = str_replace("%POLL_ANSWER_IMAGEWIDTH%", $poll_answer_imagewidth, $template_answer); |
| 337 |
// Print Out Results Body Template |
| 338 |
$temp_pollresult .= "\t\t$template_answer\n"; |
| 339 |
} else { |
| 340 |
// Results Body Variables |
| 341 |
$template_answer = stripslashes(get_settings('poll_template_resultbody')); |
| 342 |
$template_answer = str_replace("%POLL_ANSWER_ID%", $poll_answer_id, $template_answer); |
| 343 |
$template_answer = str_replace("%POLL_ANSWER%", $poll_answer_text, $template_answer); |
| 344 |
$template_answer = str_replace("%POLL_ANSWER_VOTES%", number_format($poll_answer_votes), $template_answer); |
| 345 |
$template_answer = str_replace("%POLL_ANSWER_PERCENTAGE%", $poll_answer_percentage, $template_answer); |
| 346 |
$template_answer = str_replace("%POLL_ANSWER_IMAGEWIDTH%", $poll_answer_imagewidth, $template_answer); |
| 347 |
// Print Out Results Body Template |
| 348 |
$temp_pollresult .= "\t\t$template_answer\n"; |
| 349 |
} |
| 350 |
// Get Most Voted Data |
| 351 |
if($poll_answer_votes > $poll_most_votes) { |
| 352 |
$poll_most_answer = $poll_answer_text; |
| 353 |
$poll_most_votes = $poll_answer_votes; |
| 354 |
$poll_most_percentage = $poll_answer_percentage; |
| 355 |
} |
| 356 |
// Get Least Voted Data |
| 357 |
if($poll_least_votes == 0) { |
| 358 |
$poll_least_votes = $poll_answer_votes; |
| 359 |
} |
| 360 |
if($poll_answer_votes <= $poll_least_votes) { |
| 361 |
$poll_least_answer = $poll_answer_text; |
| 362 |
$poll_least_votes = $poll_answer_votes; |
| 363 |
$poll_least_percentage = $poll_answer_percentage; |
| 364 |
} |
| 365 |
} |
| 366 |
// Results Footer Variables |
| 367 |
if($user_voted > 0 || $poll_question_active == 0 || !check_allowtovote()) { |
| 368 |
$template_footer = stripslashes(get_settings('poll_template_resultfooter')); |
| 369 |
} else { |
| 370 |
$template_footer = stripslashes(get_settings('poll_template_resultfooter2')); |
| 371 |
} |
| 372 |
$template_footer = str_replace("%POLL_ID%", $poll_question_id, $template_footer); |
| 373 |
$template_footer = str_replace("%POLL_TOTALVOTES%", number_format($poll_question_totalvotes), $template_footer); |
| 374 |
$template_footer = str_replace("%POLL_MOST_ANSWER%", $poll_most_answer, $template_footer); |
| 375 |
$template_footer = str_replace("%POLL_MOST_VOTES%", number_format($poll_most_votes), $template_footer); |
| 376 |
$template_footer = str_replace("%POLL_MOST_PERCENTAGE%", $poll_most_percentage, $template_footer); |
| 377 |
$template_footer = str_replace("%POLL_LEAST_ANSWER%", $poll_least_answer, $template_footer); |
| 378 |
$template_footer = str_replace("%POLL_LEAST_VOTES%", number_format($poll_least_votes), $template_footer); |
| 379 |
$template_footer = str_replace("%POLL_LEAST_PERCENTAGE%", $poll_least_percentage, $template_footer); |
| 380 |
// Print Out Results Footer Template |
| 381 |
$temp_pollresult .= "\t\t$template_footer\n"; |
| 382 |
if(!$without_poll_title) { |
| 383 |
$temp_pollresult .= "</div>\n"; |
| 384 |
$temp_pollresult .= "<div id=\"polls-$poll_question_id-loading\" class=\"wp-polls-loading\"><img src=\"".get_settings('siteurl')."/wp-content/plugins/polls/images/loading.gif\" width=\"16\" height=\"16\" alt=\"".__('Loading')." ...\" title=\"".__('Loading')." ...\" /> ".__('Loading')." ...</div>\n"; |
| 385 |
} |
| 386 |
} else { |
| 387 |
$temp_pollresult .= stripslashes(get_settings('poll_template_disable')); |
| 388 |
} |
| 389 |
// Return Poll Result |
| 390 |
return $temp_pollresult; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
### Function: Vote Poll |
| 395 |
add_action('init', 'vote_poll'); |
| 396 |
function vote_poll() { |
| 397 |
global $wpdb, $user_identity; |
| 398 |
if(!empty($_POST['vote'])) { |
| 399 |
$poll_id = intval($_POST['poll_id']); |
| 400 |
$poll_aid = intval($_POST["poll_$poll_id"]); |
| 401 |
if($poll_id > 0 && $poll_aid > 0 && check_allowtovote()) { |
| 402 |
$check_voted = check_voted($poll_id); |
| 403 |
if($check_voted == 0) { |
| 404 |
if(!empty($user_identity)) { |
| 405 |
$pollip_user = addslashes($user_identity); |
| 406 |
} elseif(!empty($_COOKIE['comment_author_'.COOKIEHASH])) { |
| 407 |
$pollip_user = addslashes($_COOKIE['comment_author_'.COOKIEHASH]); |
| 408 |
} else { |
| 409 |
$pollip_user = 'Guest'; |
| 410 |
} |
| 411 |
$pollip_ip = get_ipaddress(); |
| 412 |
$pollip_host = gethostbyaddr($pollip_ip); |
| 413 |
$pollip_timestamp = current_time('timestamp'); |
| 414 |
$poll_logging_method = intval(get_settings('poll_logging_method')); |
| 415 |
switch($poll_logging_method) { |
| 416 |
// Logged By Cookie |
| 417 |
case 1: |
| 418 |
$vote_cookie = setcookie("voted_".$poll_id, $poll_aid, time() + 30000000, COOKIEPATH); |
| 419 |
break; |
| 420 |
// Logged By IP |
| 421 |
case 2: |
| 422 |
$vote_ip = $wpdb->query("INSERT INTO $wpdb->pollsip VALUES(0,$poll_id,$poll_aid,'$pollip_ip','$pollip_host','$pollip_timestamp','$pollip_user')"); |
| 423 |
break; |
| 424 |
// Logged By Cookie And IP |
| 425 |
case 3: |
| 426 |
$vote_cookie = setcookie("voted_".$poll_id, $poll_aid, time() + 30000000, COOKIEPATH); |
| 427 |
$vote_ip = $wpdb->query("INSERT INTO $wpdb->pollsip VALUES(0,$poll_id,$poll_aid,'$pollip_ip','$pollip_host','$pollip_timestamp','$pollip_user')"); |
| 428 |
break; |
| 429 |
} |
| 430 |
$vote_a = $wpdb->query("UPDATE $wpdb->pollsa SET polla_votes = (polla_votes+1) WHERE polla_qid = $poll_id AND polla_aid = $poll_aid"); |
| 431 |
if($vote_a) { |
| 432 |
$vote_q = $wpdb->query("UPDATE $wpdb->pollsq SET pollq_totalvotes = (pollq_totalvotes+1) WHERE pollq_id = $poll_id"); |
| 433 |
if($vote_q) { |
| 434 |
echo "<ul class=\"wp-polls-ul\">\n".display_pollresult($poll_id,$poll_aid, 1); |
| 435 |
exit(); |
| 436 |
} else { |
| 437 |
_e("Unable To Update Poll Total Votes. Poll ID #$poll_id."); |
| 438 |
exit(); |
| 439 |
} // End if($vote_q) |
| 440 |
} else { |
| 441 |
_e("Unable To Update Poll Answer Votes. Poll ID #$poll_id, Poll Answer ID #$poll_aid."); |
| 442 |
exit(); |
| 443 |
} // End if($vote_a) |
| 444 |
} else { |
| 445 |
_e("You Had Already Voted For This Poll. Poll ID #$poll_id."); |
| 446 |
exit(); |
| 447 |
}// End if($check_voted) |
| 448 |
} else { |
| 449 |
_e("Invalid Poll ID Or Poll Answer ID. Poll ID #$poll_id, Poll Answer ID #$poll_aid."); |
| 450 |
exit(); |
| 451 |
} // End if($poll_id > 0 && $poll_aid > 0) |
| 452 |
} elseif (intval($_GET['pollresult']) > 0) { |
| 453 |
$poll_id = intval($_GET['pollresult']); |
| 454 |
echo "<ul class=\"wp-polls-ul\">\n".display_pollresult($poll_id, 0, true); |
| 455 |
exit(); |
| 456 |
} elseif (intval($_GET['pollbooth']) > 0) { |
| 457 |
$poll_id = intval($_GET['pollbooth']); |
| 458 |
echo "<ul class=\"wp-polls-ul\">\n".display_pollvote($poll_id, true); |
| 459 |
exit(); |
| 460 |
} // End if(!empty($_POST['vote'])) |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
### Function: Get IP Address |
| 465 |
if(!function_exists('get_ipaddress')) { |
| 466 |
function get_ipaddress() { |
| 467 |
if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) { |
| 468 |
$ip_address = $_SERVER["REMOTE_ADDR"]; |
| 469 |
} else { |
| 470 |
$ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"]; |
| 471 |
} |
| 472 |
if(strpos($ip_address, ',') !== false) { |
| 473 |
$ip_address = explode(',', $ip_address); |
| 474 |
$ip_address = $ip_address[0]; |
| 475 |
} |
| 476 |
return $ip_address; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
### Function: Place Poll In Content (By: Robert Accettura Of http://robert.accettura.com/) |
| 482 |
add_filter('the_content', 'place_poll', '12'); |
| 483 |
function place_poll($content){ |
| 484 |
$content = preg_replace( "/\[poll=(\d+)\]/ise", "display_poll('\\1')", $content); |
| 485 |
return $content; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
### Function: Display The Poll In Content (By: Robert Accettura Of http://robert.accettura.com/) |
| 490 |
function display_poll($poll_id, $display_pollarchive = false){ |
| 491 |
if (function_exists('vote_poll')){ |
| 492 |
if($display_pollarchive) { |
| 493 |
return get_poll($poll_id, false)."\n".'<p><a href="'.get_settings('siteurl').'/wp-content/plugins/polls/wp-polls.php">Polls Archive</a></p>'; |
| 494 |
} else { |
| 495 |
return get_poll($poll_id, false); |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
### Function: Get Poll Total Questions |
| 502 |
if(!function_exists('get_pollquestions')) { |
| 503 |
function get_pollquestions() { |
| 504 |
global $wpdb; |
| 505 |
$totalpollq = $wpdb->get_var("SELECT COUNT(pollq_id) FROM $wpdb->pollsq"); |
| 506 |
echo $totalpollq; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
### Function: Get Poll Total Answers |
| 512 |
if(!function_exists('get_pollanswers')) { |
| 513 |
function get_pollanswers() { |
| 514 |
global $wpdb; |
| 515 |
$totalpolla = $wpdb->get_var("SELECT COUNT(polla_aid) FROM $wpdb->pollsa"); |
| 516 |
echo $totalpolla; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
### Function: Get Poll Total Votes |
| 522 |
if(!function_exists('get_pollvotes')) { |
| 523 |
function get_pollvotes() { |
| 524 |
global $wpdb; |
| 525 |
$totalpollip = $wpdb->get_var("SELECT COUNT(pollip_id) FROM $wpdb->pollsip"); |
| 526 |
echo $totalpollip; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
### Function: Create Poll Tables |
| 532 |
add_action('activate_polls/polls.php', 'create_poll_table'); |
| 533 |
function create_poll_table() { |
| 534 |
global $wpdb; |
| 535 |
include_once(ABSPATH.'/wp-admin/upgrade-functions.php'); |
| 536 |
// Create Poll Tables (3 Tables) |
| 537 |
$create_table = array(); |
| 538 |
$create_table['pollsq'] = "CREATE TABLE $wpdb->pollsq (". |
| 539 |
"pollq_id int(10) NOT NULL auto_increment,". |
| 540 |
"pollq_question varchar(200) NOT NULL default '',". |
| 541 |
"pollq_timestamp varchar(20) NOT NULL default '',". |
| 542 |
"pollq_totalvotes int(10) NOT NULL default '0',". |
| 543 |
"pollq_active tinyint(1) NOT NULL default '1',". |
| 544 |
"PRIMARY KEY (pollq_id))"; |
| 545 |
$create_table['pollsa'] = "CREATE TABLE $wpdb->pollsa (". |
| 546 |
"polla_aid int(10) NOT NULL auto_increment,". |
| 547 |
"polla_qid int(10) NOT NULL default '0',". |
| 548 |
"polla_answers varchar(200) NOT NULL default '',". |
| 549 |
"polla_votes int(10) NOT NULL default '0',". |
| 550 |
"PRIMARY KEY (polla_aid))"; |
| 551 |
$create_table['pollsip'] = "CREATE TABLE $wpdb->pollsip (". |
| 552 |
"pollip_id int(10) NOT NULL auto_increment,". |
| 553 |
"pollip_qid varchar(10) NOT NULL default '',". |
| 554 |
"pollip_aid varchar(10) NOT NULL default '',". |
| 555 |
"pollip_ip varchar(100) NOT NULL default '',". |
| 556 |
"pollip_host VARCHAR(200) NOT NULL default '',". |
| 557 |
"pollip_timestamp varchar(20) NOT NULL default '0000-00-00 00:00:00',". |
| 558 |
"pollip_user tinytext NOT NULL,". |
| 559 |
"PRIMARY KEY (pollip_id))"; |
| 560 |
maybe_create_table($wpdb->pollsq, $create_table['pollsq']); |
| 561 |
maybe_create_table($wpdb->pollsa, $create_table['pollsa']); |
| 562 |
maybe_create_table($wpdb->pollsip, $create_table['pollsip']); |
| 563 |
// Check Whether It is Install Or Upgrade |
| 564 |
$first_poll = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq LIMIT 1"); |
| 565 |
// If Install, Insert 1st Poll Question With 5 Poll Answers |
| 566 |
if(empty($first_poll)) { |
| 567 |
// Insert Poll Question (1 Record) |
| 568 |
$insert_pollq = $wpdb->query("INSERT INTO $wpdb->pollsq VALUES (1, 'How Is My Site?', '".current_time('timestamp')."', 0, 1);"); |
| 569 |
if($insert_pollq) { |
| 570 |
// Insert Poll Answers (5 Records) |
| 571 |
$wpdb->query("INSERT INTO $wpdb->pollsa VALUES (1, 1, 'Good', 0);"); |
| 572 |
$wpdb->query("INSERT INTO $wpdb->pollsa VALUES (2, 1, 'Excellent', 0);"); |
| 573 |
$wpdb->query("INSERT INTO $wpdb->pollsa VALUES (3, 1, 'Bad', 0);"); |
| 574 |
$wpdb->query("INSERT INTO $wpdb->pollsa VALUES (4, 1, 'Can Be Improved', 0);"); |
| 575 |
$wpdb->query("INSERT INTO $wpdb->pollsa VALUES (5, 1, 'No Comments', 0);"); |
| 576 |
} |
| 577 |
} |
| 578 |
// Add In Options (16 Records) |
| 579 |
add_option('poll_template_voteheader', '<p align="center"><b>%POLL_QUESTION%</b></p>'. |
| 580 |
'<div id="polls-%POLL_ID%-ans" class="wp-polls-ans">'. |
| 581 |
'<ul class="wp-polls-ul">', 'Template For Poll\'s Question'); |
| 582 |
add_option('poll_template_votebody', '<li><label for="poll-answer-%POLL_ANSWER_ID%"><input type="radio" id="poll-answer-%POLL_ANSWER_ID%" name="poll_%POLL_ID%" value="%POLL_ANSWER_ID%" /> %POLL_ANSWER%</label></li>', 'Template For Poll\'s Answers'); |
| 583 |
add_option('poll_template_votefooter', '</ul>'. |
| 584 |
'<p align="center"><input type="button" name="vote" value=" Vote " class="Buttons" onclick="poll_vote(%POLL_ID%);" /><br /><a href="#ViewPollResults" onclick="poll_result(%POLL_ID%); return false;" title="View Results Of This Poll">View Results</a></p>'. |
| 585 |
'</div>', 'Template For Poll\'s Voting Footer'); |
| 586 |
add_option('poll_template_resultheader', '<p align="center"><b>%POLL_QUESTION%</b></p>'. |
| 587 |
'<div id="polls-%POLL_ID%-ans" class="wp-polls-ans">'. |
| 588 |
'<ul class="wp-polls-ul">', 'Template For Poll Header'); |
| 589 |
add_option('poll_template_resultbody', '<li>%POLL_ANSWER% <small>(%POLL_ANSWER_PERCENTAGE%%)</small><br /><img src="'.get_settings('siteurl').'/wp-content/plugins/polls/images/pollstart.gif" height="10" width="2" alt="" /><img src="'.get_settings('siteurl').'/wp-content/plugins/polls/images/pollbar.gif" height="10" width="%POLL_ANSWER_IMAGEWIDTH%" alt="%POLL_ANSWER% -> %POLL_ANSWER_PERCENTAGE%% (%POLL_ANSWER_VOTES% Votes)" title="%POLL_ANSWER% -> %POLL_ANSWER_PERCENTAGE%% (%POLL_ANSWER_VOTES% Votes)" /><img src="'.get_settings('siteurl').'/wp-content/plugins/polls/images/pollend.gif" height="10" width="2" alt="" /></li>', 'Template For Poll Results'); |
| 590 |
add_option('poll_template_resultbody2', '<li><b><i>%POLL_ANSWER% <small>(%POLL_ANSWER_PERCENTAGE%%)</small></i></b><br /><img src="'.get_settings('siteurl').'/wp-content/plugins/polls/images/pollstart.gif" height="10" width="2" alt="" /><img src="'.get_settings('siteurl').'/wp-content/plugins/polls/images/pollbar.gif" height="10" width="%POLL_ANSWER_IMAGEWIDTH%" alt="You Have Voted For This Choice - %POLL_ANSWER% -> %POLL_ANSWER_PERCENTAGE%% (%POLL_ANSWER_VOTES% Votes)" title="You Have Voted For This Choice - %POLL_ANSWER% -> %POLL_ANSWER_PERCENTAGE%% (%POLL_ANSWER_VOTES% Votes)" /><img src="'.get_settings('siteurl').'/wp-content/plugins/polls/images/pollend.gif" height="10" width="2" alt="" /></li>', 'Template For Poll Results (User Voted)'); |
| 591 |
add_option('poll_template_resultfooter', '</ul>'. |
| 592 |
'<p align="center">Total Votes: <b>%POLL_TOTALVOTES%</b></p>'. |
| 593 |
'</div>', 'Template For Poll Result Footer'); |
| 594 |
add_option('poll_template_resultfooter2', '</ul>'. |
| 595 |
'<p align="center">Total Votes: <b>%POLL_TOTALVOTES%</b><br /><a href="#VotePoll" onclick="poll_booth(%POLL_ID%); return false;" title="Vote For This Poll">Vote</a></p>'. |
| 596 |
'</div>', 'Template For Poll Result Footer'); |
| 597 |
add_option('poll_template_disable', 'Sorry, there are no polls available at the moment.', 'Template For Poll When It Is Disabled'); |
| 598 |
add_option('poll_template_error', 'An error has occurred when processing your poll.', 'Template For Poll When An Error Has Occured'); |
| 599 |
add_option('poll_currentpoll', 0, 'Current Displayed Poll'); |
| 600 |
add_option('poll_latestpoll', 1, 'The Lastest Poll'); |
| 601 |
add_option('poll_archive_perpage', 5, 'Number Of Polls To Display Per Page On The Poll\'s Archive', 'no'); |
| 602 |
add_option('poll_ans_sortby', 'polla_aid', 'Sorting Of Poll\'s Answers'); |
| 603 |
add_option('poll_ans_sortorder', 'asc', 'Sort Order Of Poll\'s Answers'); |
| 604 |
add_option('poll_ans_result_sortby', 'polla_votes', 'Sorting Of Poll\'s Answers Result'); |
| 605 |
add_option('poll_ans_result_sortorder', 'desc', 'Sorting Order Of Poll\'s Answers Result'); |
| 606 |
// Database Upgrade For WP-Polls 2.1 |
| 607 |
add_option('poll_logging_method', '3', 'Logging Method Of User Poll\'s Answer'); |
| 608 |
add_option('poll_allowtovote', '2', 'Who Is Allowed To Vote'); |
| 609 |
maybe_add_column($wpdb->pollsq, 'pollq_active', "ALTER TABLE $wpdb->pollsq ADD pollq_active TINYINT( 1 ) NOT NULL DEFAULT '1';"); |
| 610 |
// Set 'manage_polls' Capabilities To Administrator |
| 611 |
$role = get_role('administrator'); |
| 612 |
if(!$role->has_cap('manage_polls')) { |
| 613 |
$role->add_cap('manage_polls'); |
| 614 |
} |
| 615 |
} |
| 616 |
?> |