PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/justjs/wp-content/plugins/wp-polls/wp-polls.php

https://github.com/jkreska/test1
PHP | 1597 lines | 1322 code | 96 blank | 179 comment | 283 complexity | 16cd076de2b62224b46d89e807eb7738 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. Plugin Name: WP-Polls
  4. Plugin URI: http://lesterchan.net/portfolio/programming/php/
  5. Description: Adds an AJAX poll system to your WordPress blog. You can easily include a poll into your WordPress's blog post/page. WP-Polls is extremely customizable via templates and css styles and there are tons of options for you to choose to ensure that WP-Polls runs the way you wanted. It now supports multiple selection of answers.
  6. Version: 2.50
  7. Author: Lester 'GaMerZ' Chan
  8. Author URI: http://lesterchan.net
  9. */
  10. /*
  11. Copyright 2009 Lester Chan (email : lesterchan@gmail.com)
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. ### Load WP-Config File If This File Is Called Directly
  25. if (!function_exists('add_action')) {
  26. $wp_root = '../../..';
  27. if (file_exists($wp_root.'/wp-load.php')) {
  28. require_once($wp_root.'/wp-load.php');
  29. } else {
  30. require_once($wp_root.'/wp-config.php');
  31. }
  32. }
  33. ### Create Text Domain For Translations
  34. add_action('init', 'polls_textdomain');
  35. function polls_textdomain() {
  36. load_plugin_textdomain('wp-polls', false, 'wp-polls');
  37. }
  38. ### Polls Table Name
  39. global $wpdb;
  40. $wpdb->pollsq = $wpdb->prefix.'pollsq';
  41. $wpdb->pollsa = $wpdb->prefix.'pollsa';
  42. $wpdb->pollsip = $wpdb->prefix.'pollsip';
  43. ### Function: Poll Administration Menu
  44. add_action('admin_menu', 'poll_menu');
  45. function poll_menu() {
  46. if (function_exists('add_menu_page')) {
  47. add_menu_page(__('Polls', 'wp-polls'), __('Polls', 'wp-polls'), 'manage_polls', 'wp-polls/polls-manager.php', '', plugins_url('wp-polls/images/poll.png'));
  48. }
  49. if (function_exists('add_submenu_page')) {
  50. add_submenu_page('wp-polls/polls-manager.php', __('Manage Polls', 'wp-polls'), __('Manage Polls', 'wp-polls'), 'manage_polls', 'wp-polls/polls-manager.php');
  51. add_submenu_page('wp-polls/polls-manager.php', __('Add Poll', 'wp-polls'), __('Add Poll', 'wp-polls'), 'manage_polls', 'wp-polls/polls-add.php');
  52. add_submenu_page('wp-polls/polls-manager.php', __('Poll Options', 'wp-polls'), __('Poll Options', 'wp-polls'), 'manage_polls', 'wp-polls/polls-options.php');
  53. add_submenu_page('wp-polls/polls-manager.php', __('Poll Templates', 'wp-polls'), __('Poll Templates', 'wp-polls'), 'manage_polls', 'wp-polls/polls-templates.php');
  54. add_submenu_page('wp-polls/polls-manager.php', __('Uninstall WP-Polls', 'wp-polls'), __('Uninstall WP-Polls', 'wp-polls'), 'manage_polls', 'wp-polls/polls-uninstall.php');
  55. }
  56. }
  57. ### Function: Get Poll
  58. function get_poll($temp_poll_id = 0, $display = true) {
  59. global $wpdb, $polls_loaded;
  60. // Poll Result Link
  61. $pollresult_id = intval($_GET['pollresult']);
  62. $temp_poll_id = intval($temp_poll_id);
  63. // Check Whether Poll Is Disabled
  64. if(intval(get_option('poll_currentpoll')) == -1) {
  65. if($display) {
  66. echo stripslashes(get_option('poll_template_disable'));
  67. return;
  68. } else {
  69. return stripslashes(get_option('poll_template_disable'));
  70. }
  71. // Poll Is Enabled
  72. } else {
  73. // Hardcoded Poll ID Is Not Specified
  74. switch($temp_poll_id) {
  75. // Random Poll
  76. case -2:
  77. $poll_id = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq WHERE pollq_active = 1 ORDER BY RAND() LIMIT 1");
  78. break;
  79. // Latest Poll
  80. case 0:
  81. // Random Poll
  82. if(intval(get_option('poll_currentpoll')) == -2) {
  83. $random_poll_id = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq WHERE pollq_active = 1 ORDER BY RAND() LIMIT 1");
  84. $poll_id = intval($random_poll_id);
  85. if($pollresult_id > 0) {
  86. $poll_id = $pollresult_id;
  87. } elseif(intval($_POST['poll_id']) > 0) {
  88. $poll_id = intval($_POST['poll_id']);
  89. }
  90. // Current Poll ID Is Not Specified
  91. } elseif(intval(get_option('poll_currentpoll')) == 0) {
  92. // Get Lastest Poll ID
  93. $poll_id = intval(get_option('poll_latestpoll'));
  94. } else {
  95. // Get Current Poll ID
  96. $poll_id = intval(get_option('poll_currentpoll'));
  97. }
  98. break;
  99. // Take Poll ID From Arguments
  100. default:
  101. $poll_id = $temp_poll_id;
  102. }
  103. }
  104. // Assign All Loaded Poll To $polls_loaded
  105. if(empty($polls_loaded)) {
  106. $polls_loaded = array();
  107. }
  108. if(!in_array($poll_id, $polls_loaded)) {
  109. $polls_loaded[] = $poll_id;
  110. }
  111. // User Click on View Results Link
  112. if($pollresult_id == $poll_id) {
  113. if($display) {
  114. echo display_pollresult($poll_id);
  115. return;
  116. } else {
  117. return display_pollresult($poll_id);
  118. }
  119. // Check Whether User Has Voted
  120. } else {
  121. $poll_active = $wpdb->get_var("SELECT pollq_active FROM $wpdb->pollsq WHERE pollq_id = $poll_id");
  122. $poll_active = intval($poll_active);
  123. $check_voted = check_voted($poll_id);
  124. if($poll_active == 0) {
  125. $poll_close = intval(get_option('poll_close'));
  126. } else {
  127. $poll_close = 0;
  128. }
  129. if($check_voted > 0 || ($poll_active == 0 && $poll_close == 1)) {
  130. if($display) {
  131. echo display_pollresult($poll_id, $check_voted);
  132. return;
  133. } else {
  134. return display_pollresult($poll_id, $check_voted);
  135. }
  136. } elseif(!check_allowtovote() || ($poll_active == 0 && $poll_close == 3)) {
  137. $disable_poll_js = '<script type="text/javascript">jQuery("#polls_form_'.$poll_id.' :input").each(function (i){jQuery(this).esc_attr("disabled","disabled")});</script>';
  138. if($display) {
  139. echo display_pollvote($poll_id).$disable_poll_js;
  140. return;
  141. } else {
  142. return display_pollvote($poll_id).$disable_poll_js;
  143. }
  144. } elseif($poll_active == 1) {
  145. if($display) {
  146. echo display_pollvote($poll_id);
  147. return;
  148. } else {
  149. return display_pollvote($poll_id);
  150. }
  151. }
  152. }
  153. }
  154. ### Function: Print Polls Stylesheets That Are Dynamic And jQuery At The Top
  155. add_action('wp_head', 'poll_head_scripts');
  156. function poll_head_scripts() {
  157. $pollbar = get_option('poll_bar');
  158. echo '<style type="text/css">'."\n";
  159. if($pollbar['style'] == 'use_css') {
  160. echo '.wp-polls .pollbar {'."\n";
  161. echo "\t".'margin: 1px;'."\n";
  162. echo "\t".'font-size: '.($pollbar['height']-2).'px;'."\n";
  163. echo "\t".'line-height: '.$pollbar['height'].'px;'."\n";
  164. echo "\t".'height: '.$pollbar['height'].'px;'."\n";
  165. echo "\t".'background: #'.$pollbar['background'].';'."\n";
  166. echo "\t".'border: 1px solid #'.$pollbar['border'].';'."\n";
  167. echo '}'."\n";
  168. } else {
  169. echo '.wp-polls .pollbar {'."\n";
  170. echo "\t".'margin: 1px;'."\n";
  171. echo "\t".'font-size: '.($pollbar['height']-2).'px;'."\n";
  172. echo "\t".'line-height: '.$pollbar['height'].'px;'."\n";
  173. echo "\t".'height: '.$pollbar['height'].'px;'."\n";
  174. echo "\t".'background-image: url(\''.plugins_url('wp-polls/images/'.$pollbar['style'].'/pollbg.gif').'\');'."\n";
  175. echo "\t".'border: 1px solid #'.$pollbar['border'].';'."\n";
  176. echo '}'."\n";
  177. }
  178. echo '</style>'."\n";
  179. wp_print_scripts('jquery');
  180. }
  181. ### Function: Enqueue Polls JavaScripts/CSS
  182. add_action('wp_enqueue_scripts', 'poll_scripts');
  183. function poll_scripts() {
  184. global $text_direction;
  185. if(@file_exists(TEMPLATEPATH.'/polls-css.css')) {
  186. wp_enqueue_style('wp-polls', get_stylesheet_directory_uri().'/polls-css.css', false, '2.50', 'all');
  187. } else {
  188. wp_enqueue_style('wp-polls', plugins_url('wp-polls/polls-css.css'), false, '2.50', 'all');
  189. }
  190. if('rtl' == $text_direction) {
  191. if(@file_exists(TEMPLATEPATH.'/polls-css-rtl.css')) {
  192. wp_enqueue_style('wp-polls-rtl', get_stylesheet_directory_uri().'/polls-css-rtl.css', false, '2.50', 'all');
  193. } else {
  194. wp_enqueue_style('wp-polls-rtl', plugins_url('wp-polls/polls-css-rtl.css'), false, '2.50', 'all');
  195. }
  196. }
  197. $poll_ajax_style = get_option('poll_ajax_style');
  198. $pollbar = get_option('poll_bar');
  199. wp_enqueue_script('wp-polls', plugins_url('wp-polls/polls-js.js'), array('jquery'), '2.50', true);
  200. wp_localize_script('wp-polls', 'pollsL10n', array(
  201. 'ajax_url' => plugins_url('wp-polls/wp-polls.php'),
  202. 'text_wait' => __('Your last request is still being processed. Please wait a while ...', 'wp-polls'),
  203. 'text_valid' => __('Please choose a valid poll answer.', 'wp-polls'),
  204. 'text_multiple' => __('Maximum number of choices allowed: ', 'wp-polls'),
  205. 'show_loading' => intval($poll_ajax_style['loading']),
  206. 'show_fading' => intval($poll_ajax_style['fading'])
  207. ));
  208. }
  209. ### Function: Enqueue Polls Stylesheets/JavaScripts In WP-Admin
  210. add_action('admin_enqueue_scripts', 'poll_scripts_admin');
  211. function poll_scripts_admin($hook_suffix) {
  212. global $text_direction;
  213. $poll_admin_pages = array('wp-polls/polls-manager.php', 'wp-polls/polls-add.php', 'wp-polls/polls-options.php', 'wp-polls/polls-templates.php', 'wp-polls/polls-uninstall.php');
  214. if(in_array($hook_suffix, $poll_admin_pages)) {
  215. wp_enqueue_style('wp-polls-admin', plugins_url('wp-polls/polls-admin-css.css'), false, '2.50', 'all');
  216. wp_enqueue_script('wp-polls-admin', plugins_url('wp-polls/polls-admin-js.js'), array('jquery'), '2.50', true);
  217. wp_localize_script('wp-polls-admin', 'pollsAdminL10n', array(
  218. 'admin_ajax_url' => plugins_url('wp-polls/polls-admin-ajax.php'),
  219. 'text_direction' => ('rtl' == $text_direction) ? 'left' : 'right',
  220. 'text_delete_poll' => __('Delete Poll', 'wp-polls'),
  221. 'text_no_poll_logs' => __('No poll logs available.', 'wp-polls'),
  222. 'text_delete_all_logs' => __('Delete All Logs', 'wp-polls'),
  223. 'text_checkbox_delete_all_logs' => __('Please check the \\\'Yes\\\' checkbox if you want to delete all logs.', 'wp-polls'),
  224. 'text_delete_poll_logs' => __('Delete Logs For This Poll Only', 'wp-polls'),
  225. 'text_checkbox_delete_poll_logs' => __('Please check the \\\'Yes\\\' checkbox if you want to delete all logs for this poll ONLY.', 'wp-polls'),
  226. 'text_delete_poll_ans' => __('Delete Poll Answer', 'wp-polls'),
  227. 'text_open_poll' => __('Open Poll', 'wp-polls'),
  228. 'text_close_poll' => __('Close Poll', 'wp-polls'),
  229. 'text_answer' => __('Answer', 'wp-polls'),
  230. 'text_remove_poll_answer' => __('Remove', 'wp-polls')
  231. ));
  232. }
  233. }
  234. ### Function: Displays Polls Footer In WP-Admin
  235. add_action('admin_footer-post-new.php', 'poll_footer_admin');
  236. add_action('admin_footer-post.php', 'poll_footer_admin');
  237. add_action('admin_footer-page-new.php', 'poll_footer_admin');
  238. add_action('admin_footer-page.php', 'poll_footer_admin');
  239. function poll_footer_admin() {
  240. // Javascript Code Courtesy Of WP-AddQuicktag (http://bueltge.de/wp-addquicktags-de-plugin/120/)
  241. echo '<script type="text/javascript">'."\n";
  242. echo '/* <![CDATA[ */'."\n";
  243. echo "\t".'var pollsEdL10n = {'."\n";
  244. echo "\t\t".'enter_poll_id: "'.js_escape(__('Enter Poll ID', 'wp-polls')).'",'."\n";
  245. echo "\t\t".'enter_poll_id_again: "'.js_escape(__('Error: Poll ID must be numeric', 'wp-polls')).'\n\n'.js_escape(__('Please enter Poll ID again', 'wp-polls')).'",'."\n";
  246. echo "\t\t".'poll: "'.js_escape(__('Poll', 'wp-polls')).'",'."\n";
  247. echo "\t\t".'insert_poll: "'.js_escape(__('Insert_Poll', 'wp-polls')).'",'."\n";
  248. echo "\t".'};'."\n";
  249. echo "\t".'function insertPoll(where, myField) {'."\n";
  250. echo "\t\t".'var poll_id = jQuery.trim(prompt(pollsEdL10n.enter_poll_id));'."\n";
  251. echo "\t\t".'while(isNaN(poll_id)) {'."\n";
  252. echo "\t\t\t".'poll_id = jQuery.trim(prompt(pollsEdL10n.enter_poll_id_again));'."\n";
  253. echo "\t\t".'}'."\n";
  254. echo "\t\t".'if (poll_id >= -1 && poll_id != null && poll_id != "") {'."\n";
  255. echo "\t\t\t".'if(where == \'code\') {'."\n";
  256. echo "\t\t\t\t".'edInsertContent(myField, \'[poll id="\' + poll_id + \'"]\');'."\n";
  257. echo "\t\t\t".'} else {'."\n";
  258. echo "\t\t\t\t".'return \'[poll id="\' + poll_id + \'"]\';'."\n";
  259. echo "\t\t\t".'}'."\n";
  260. echo "\t\t".'}'."\n";
  261. echo "\t".'}'."\n";
  262. echo "\t".'if(document.getElementById("ed_toolbar")){'."\n";
  263. echo "\t\t".'qt_toolbar = document.getElementById("ed_toolbar");'."\n";
  264. echo "\t\t".'edButtons[edButtons.length] = new edButton("ed_poll",pollsEdL10n.poll, "", "","");'."\n";
  265. echo "\t\t".'var qt_button = qt_toolbar.lastChild;'."\n";
  266. echo "\t\t".'while (qt_button.nodeType != 1){'."\n";
  267. echo "\t\t\t".'qt_button = qt_button.previousSibling;'."\n";
  268. echo "\t\t".'}'."\n";
  269. echo "\t\t".'qt_button = qt_button.cloneNode(true);'."\n";
  270. echo "\t\t".'qt_button.value = pollsEdL10n.poll;'."\n";
  271. echo "\t\t".'qt_button.title = pollsEdL10n.insert_poll;'."\n";
  272. echo "\t\t".'qt_button.onclick = function () { insertPoll(\'code\', edCanvas);}'."\n";
  273. echo "\t\t".'qt_button.id = "ed_poll";'."\n";
  274. echo "\t\t".'qt_toolbar.appendChild(qt_button);'."\n";
  275. echo "\t".'}'."\n";
  276. echo '/* ]]> */'."\n";
  277. echo '</script>'."\n";
  278. }
  279. ### Function: Add Favourite Actions >= WordPress 2.7
  280. add_filter('favorite_actions', 'poll_favorite_actions');
  281. function poll_favorite_actions($favorite_actions) {
  282. $favorite_actions['admin.php?page=wp-polls/polls-add.php'] = array(__('Add Poll', 'wp-polls'), 'manage_polls');
  283. return $favorite_actions;
  284. }
  285. ### Function: Add Quick Tag For Poll In TinyMCE >= WordPress 2.5
  286. add_action('init', 'poll_tinymce_addbuttons');
  287. function poll_tinymce_addbuttons() {
  288. if(!current_user_can('edit_posts') && ! current_user_can('edit_pages')) {
  289. return;
  290. }
  291. if(get_user_option('rich_editing') == 'true') {
  292. add_filter("mce_external_plugins", "poll_tinymce_addplugin");
  293. add_filter('mce_buttons', 'poll_tinymce_registerbutton');
  294. }
  295. }
  296. function poll_tinymce_registerbutton($buttons) {
  297. array_push($buttons, 'separator', 'polls');
  298. return $buttons;
  299. }
  300. function poll_tinymce_addplugin($plugin_array) {
  301. $plugin_array['polls'] = plugins_url('wp-polls/tinymce/plugins/polls/editor_plugin.js');
  302. return $plugin_array;
  303. }
  304. ### Function: Check Who Is Allow To Vote
  305. function check_allowtovote() {
  306. global $user_ID;
  307. $user_ID = intval($user_ID);
  308. $allow_to_vote = intval(get_option('poll_allowtovote'));
  309. switch($allow_to_vote) {
  310. // Guests Only
  311. case 0:
  312. if($user_ID > 0) {
  313. return false;
  314. }
  315. return true;
  316. break;
  317. // Registered Users Only
  318. case 1:
  319. if($user_ID == 0) {
  320. return false;
  321. }
  322. return true;
  323. break;
  324. // Registered Users And Guests
  325. case 2:
  326. default:
  327. return true;
  328. }
  329. }
  330. ### Funcrion: Check Voted By Cookie Or IP
  331. function check_voted($poll_id) {
  332. $poll_logging_method = intval(get_option('poll_logging_method'));
  333. switch($poll_logging_method) {
  334. // Do Not Log
  335. case 0:
  336. return 0;
  337. break;
  338. // Logged By Cookie
  339. case 1:
  340. return check_voted_cookie($poll_id);
  341. break;
  342. // Logged By IP
  343. case 2:
  344. return check_voted_ip($poll_id);
  345. break;
  346. // Logged By Cookie And IP
  347. case 3:
  348. $check_voted_cookie = check_voted_cookie($poll_id);
  349. if(!empty($check_voted_cookie)) {
  350. return $check_voted_cookie;
  351. } else {
  352. return check_voted_ip($poll_id);
  353. }
  354. break;
  355. // Logged By Username
  356. case 4:
  357. return check_voted_username($poll_id);
  358. break;
  359. }
  360. }
  361. ### Function: Check Voted By Cookie
  362. function check_voted_cookie($poll_id) {
  363. if(!empty($_COOKIE["voted_$poll_id"])) {
  364. $get_voted_aids = explode(',', $_COOKIE["voted_$poll_id"]);
  365. } else {
  366. $get_voted_aids = 0;
  367. }
  368. return $get_voted_aids;
  369. }
  370. ### Function: Check Voted By IP
  371. function check_voted_ip($poll_id) {
  372. global $wpdb;
  373. $log_expiry = intval(get_option('poll_cookielog_expiry'));
  374. $log_expiry_sql = '';
  375. if($log_expiry > 0) {
  376. $log_expiry_sql = 'AND ('.current_time('timestamp').'-(pollip_timestamp+0)) < '.$log_expiry;
  377. }
  378. // Check IP From IP Logging Database
  379. $get_voted_aids = $wpdb->get_col("SELECT pollip_aid FROM $wpdb->pollsip WHERE pollip_qid = $poll_id AND pollip_ip = '".get_ipaddress()."' $log_expiry_sql");
  380. if($get_voted_aids) {
  381. return $get_voted_aids;
  382. } else {
  383. return 0;
  384. }
  385. }
  386. ### Function: Check Voted By Username
  387. function check_voted_username($poll_id) {
  388. global $wpdb, $user_ID;
  389. // Check IP If User Is Guest
  390. if (!is_user_logged_in()) {
  391. return 1;
  392. }
  393. $pollsip_userid = intval($user_ID);
  394. $log_expiry = intval(get_option('poll_cookielog_expiry'));
  395. $log_expiry_sql = '';
  396. if($log_expiry > 0) {
  397. $log_expiry_sql = 'AND ('.current_time('timestamp').'-(pollip_timestamp+0)) < '.$log_expiry;
  398. }
  399. // Check User ID From IP Logging Database
  400. $get_voted_aids = $wpdb->get_col("SELECT pollip_aid FROM $wpdb->pollsip WHERE pollip_qid = $poll_id AND pollip_userid = $pollsip_userid $log_expiry_sql");
  401. if($get_voted_aids) {
  402. return $get_voted_aids;
  403. } else {
  404. return 0;
  405. }
  406. }
  407. ### Function: Display Voting Form
  408. function display_pollvote($poll_id, $display_loading = true) {
  409. global $wpdb;
  410. // Temp Poll Result
  411. $temp_pollvote = '';
  412. // Get Poll Question Data
  413. $poll_question = $wpdb->get_row("SELECT pollq_id, pollq_question, pollq_totalvotes, pollq_timestamp, pollq_expiry, pollq_multiple, pollq_totalvoters FROM $wpdb->pollsq WHERE pollq_id = $poll_id LIMIT 1");
  414. // Poll Question Variables
  415. $poll_question_text = stripslashes($poll_question->pollq_question);
  416. $poll_question_id = intval($poll_question->pollq_id);
  417. $poll_question_totalvotes = intval($poll_question->pollq_totalvotes);
  418. $poll_question_totalvoters = intval($poll_question->pollq_totalvoters);
  419. $poll_start_date = mysql2date(sprintf(__('%s @ %s', 'wp-polls'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $poll_question->pollq_timestamp));
  420. $poll_expiry = trim($poll_question->pollq_expiry);
  421. if(empty($poll_expiry)) {
  422. $poll_end_date = __('No Expiry', 'wp-polls');
  423. } else {
  424. $poll_end_date = mysql2date(sprintf(__('%s @ %s', 'wp-polls'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $poll_expiry));
  425. }
  426. $poll_multiple_ans = intval($poll_question->pollq_multiple);
  427. $template_question = stripslashes(get_option('poll_template_voteheader'));
  428. $template_question = str_replace("%POLL_QUESTION%", $poll_question_text, $template_question);
  429. $template_question = str_replace("%POLL_ID%", $poll_question_id, $template_question);
  430. $template_question = str_replace("%POLL_TOTALVOTES%", $poll_question_totalvotes, $template_question);
  431. $template_question = str_replace("%POLL_TOTALVOTERS%", $poll_question_totalvoters, $template_question);
  432. $template_question = str_replace("%POLL_START_DATE%", $poll_start_date, $template_question);
  433. $template_question = str_replace("%POLL_END_DATE%", $poll_end_date, $template_question);
  434. if($poll_multiple_ans > 0) {
  435. $template_question = str_replace("%POLL_MULTIPLE_ANS_MAX%", $poll_multiple_ans, $template_question);
  436. } else {
  437. $template_question = str_replace("%POLL_MULTIPLE_ANS_MAX%", '1', $template_question);
  438. }
  439. // Get Poll Answers Data
  440. $poll_answers = $wpdb->get_results("SELECT polla_aid, polla_answers, polla_votes FROM $wpdb->pollsa WHERE polla_qid = $poll_question_id ORDER BY ".get_option('poll_ans_sortby').' '.get_option('poll_ans_sortorder'));
  441. // If There Is Poll Question With Answers
  442. if($poll_question && $poll_answers) {
  443. // Display Poll Voting Form
  444. $temp_pollvote .= "<div id=\"polls-$poll_question_id\" class=\"wp-polls\">\n";
  445. $temp_pollvote .= "\t<form id=\"polls_form_$poll_question_id\" class=\"wp-polls-form\" action=\"".htmlspecialchars($_SERVER['REQUEST_URI'])."\" method=\"post\">\n";
  446. $temp_pollvote .= "\t\t<p style=\"display: none;\"><input type=\"hidden\" name=\"poll_id\" value=\"$poll_question_id\" /></p>\n";
  447. if($poll_multiple_ans > 0) {
  448. $temp_pollvote .= "\t\t<p style=\"display: none;\"><input type=\"hidden\" id=\"poll_multiple_ans_$poll_question_id\" name=\"poll_multiple_ans_$poll_question_id\" value=\"$poll_multiple_ans\" /></p>\n";
  449. }
  450. // Print Out Voting Form Header Template
  451. $temp_pollvote .= "\t\t$template_question\n";
  452. foreach($poll_answers as $poll_answer) {
  453. // Poll Answer Variables
  454. $poll_answer_id = intval($poll_answer->polla_aid);
  455. $poll_answer_text = stripslashes($poll_answer->polla_answers);
  456. $poll_answer_votes = intval($poll_answer->polla_votes);
  457. $template_answer = stripslashes(get_option('poll_template_votebody'));
  458. $template_answer = str_replace("%POLL_ID%", $poll_question_id, $template_answer);
  459. $template_answer = str_replace("%POLL_ANSWER_ID%", $poll_answer_id, $template_answer);
  460. $template_answer = str_replace("%POLL_ANSWER%", $poll_answer_text, $template_answer);
  461. $template_answer = str_replace("%POLL_ANSWER_VOTES%", number_format_i18n($poll_answer_votes), $template_answer);
  462. if($poll_multiple_ans > 0) {
  463. $template_answer = str_replace("%POLL_CHECKBOX_RADIO%", 'checkbox', $template_answer);
  464. } else {
  465. $template_answer = str_replace("%POLL_CHECKBOX_RADIO%", 'radio', $template_answer);
  466. }
  467. // Print Out Voting Form Body Template
  468. $temp_pollvote .= "\t\t$template_answer\n";
  469. }
  470. // Determine Poll Result URL
  471. $poll_result_url = $_SERVER['REQUEST_URI'];
  472. $poll_result_url = preg_replace('/pollresult=(\d+)/i', 'pollresult='.$poll_question_id, $poll_result_url);
  473. if(intval($_GET['pollresult']) == 0) {
  474. if(strpos($poll_result_url, '?') !== false) {
  475. $poll_result_url = "$poll_result_url&amp;pollresult=$poll_question_id";
  476. } else {
  477. $poll_result_url = "$poll_result_url?pollresult=$poll_question_id";
  478. }
  479. }
  480. // Voting Form Footer Variables
  481. $template_footer = stripslashes(get_option('poll_template_votefooter'));
  482. $template_footer = str_replace("%POLL_ID%", $poll_question_id, $template_footer);
  483. $template_footer = str_replace("%POLL_RESULT_URL%", $poll_result_url, $template_footer);
  484. $template_footer = str_replace("%POLL_START_DATE%", $poll_start_date, $template_footer);
  485. $template_footer = str_replace("%POLL_END_DATE%", $poll_end_date, $template_footer);
  486. if($poll_multiple_ans > 0) {
  487. $template_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", $poll_multiple_ans, $template_footer);
  488. } else {
  489. $template_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", '1', $template_footer);
  490. }
  491. // Print Out Voting Form Footer Template
  492. $temp_pollvote .= "\t\t$template_footer\n";
  493. $temp_pollvote .= "\t</form>\n";
  494. $temp_pollvote .= "</div>\n";
  495. if($display_loading) {
  496. $poll_ajax_style = get_option('poll_ajax_style');
  497. if(intval($poll_ajax_style['loading']) == 1) {
  498. $temp_pollvote .= "<div id=\"polls-$poll_question_id-loading\" class=\"wp-polls-loading\"><img src=\"".plugins_url('wp-polls/images/loading.gif')."\" width=\"16\" height=\"16\" alt=\"".__('Loading', 'wp-polls')." ...\" title=\"".__('Loading', 'wp-polls')." ...\" class=\"wp-polls-image\" />&nbsp;".__('Loading', 'wp-polls')." ...</div>\n";
  499. }
  500. }
  501. } else {
  502. $temp_pollvote .= stripslashes(get_option('poll_template_disable'));
  503. }
  504. // Return Poll Vote Template
  505. return $temp_pollvote;
  506. }
  507. ### Function: Display Results Form
  508. function display_pollresult($poll_id, $user_voted = '', $display_loading = true) {
  509. global $wpdb;
  510. // User Voted
  511. if(!is_array($user_voted)) {
  512. $user_voted = array();
  513. }
  514. // Temp Poll Result
  515. $temp_pollresult = '';
  516. // Most/Least Variables
  517. $poll_most_answer = '';
  518. $poll_most_votes = 0;
  519. $poll_most_percentage = 0;
  520. $poll_least_answer = '';
  521. $poll_least_votes = 0;
  522. $poll_least_percentage = 0;
  523. // Get Poll Question Data
  524. $poll_question = $wpdb->get_row("SELECT pollq_id, pollq_question, pollq_totalvotes, pollq_active, pollq_timestamp, pollq_expiry, pollq_multiple, pollq_totalvoters FROM $wpdb->pollsq WHERE pollq_id = $poll_id LIMIT 1");
  525. // Poll Question Variables
  526. $poll_question_text = stripslashes($poll_question->pollq_question);
  527. $poll_question_id = intval($poll_question->pollq_id);
  528. $poll_question_totalvotes = intval($poll_question->pollq_totalvotes);
  529. $poll_question_totalvoters = intval($poll_question->pollq_totalvoters);
  530. $poll_question_active = intval($poll_question->pollq_active);
  531. $poll_start_date = mysql2date(sprintf(__('%s @ %s', 'wp-polls'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $poll_question->pollq_timestamp));
  532. $poll_expiry = trim($poll_question->pollq_expiry);
  533. if(empty($poll_expiry)) {
  534. $poll_end_date = __('No Expiry', 'wp-polls');
  535. } else {
  536. $poll_end_date = mysql2date(sprintf(__('%s @ %s', 'wp-polls'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $poll_expiry));
  537. }
  538. $poll_multiple_ans = intval($poll_question->pollq_multiple);
  539. $template_question = stripslashes(get_option('poll_template_resultheader'));
  540. $template_question = str_replace("%POLL_QUESTION%", $poll_question_text, $template_question);
  541. $template_question = str_replace("%POLL_ID%", $poll_question_id, $template_question);
  542. $template_question = str_replace("%POLL_TOTALVOTES%", $poll_question_totalvotes, $template_question);
  543. $template_question = str_replace("%POLL_TOTALVOTERS%", $poll_question_totalvoters, $template_question);
  544. $template_question = str_replace("%POLL_START_DATE%", $poll_start_date, $template_question);
  545. $template_question = str_replace("%POLL_END_DATE%", $poll_end_date, $template_question);
  546. if($poll_multiple_ans > 0) {
  547. $template_question = str_replace("%POLL_MULTIPLE_ANS_MAX%", $poll_multiple_ans, $template_question);
  548. } else {
  549. $template_question = str_replace("%POLL_MULTIPLE_ANS_MAX%", '1', $template_question);
  550. }
  551. // Get Poll Answers Data
  552. $poll_answers = $wpdb->get_results("SELECT polla_aid, polla_answers, polla_votes FROM $wpdb->pollsa WHERE polla_qid = $poll_question_id ORDER BY ".get_option('poll_ans_result_sortby').' '.get_option('poll_ans_result_sortorder'));
  553. // If There Is Poll Question With Answers
  554. if($poll_question && $poll_answers) {
  555. // Store The Percentage Of The Poll
  556. $poll_answer_percentage_array = array();
  557. // Is The Poll Total Votes 0?
  558. $poll_totalvotes_zero = true;
  559. if($poll_question_totalvotes > 0) {
  560. $poll_totalvotes_zero = false;
  561. }
  562. // Print Out Result Header Template
  563. $temp_pollresult .= "<div id=\"polls-$poll_question_id\" class=\"wp-polls\">\n";
  564. $temp_pollresult .= "\t\t$template_question\n";
  565. foreach($poll_answers as $poll_answer) {
  566. // Poll Answer Variables
  567. $poll_answer_id = intval($poll_answer->polla_aid);
  568. $poll_answer_text = stripslashes($poll_answer->polla_answers);
  569. $poll_answer_votes = intval($poll_answer->polla_votes);
  570. $poll_answer_percentage = 0;
  571. $poll_answer_imagewidth = 0;
  572. // Calculate Percentage And Image Bar Width
  573. if(!$poll_totalvotes_zero) {
  574. if($poll_answer_votes > 0) {
  575. $poll_answer_percentage = round((($poll_answer_votes/$poll_question_totalvoters)*100));
  576. $poll_answer_imagewidth = round($poll_answer_percentage);
  577. if($poll_answer_imagewidth == 100) {
  578. $poll_answer_imagewidth = 99;
  579. }
  580. } else {
  581. $poll_answer_percentage = 0;
  582. $poll_answer_imagewidth = 1;
  583. }
  584. } else {
  585. $poll_answer_percentage = 0;
  586. $poll_answer_imagewidth = 1;
  587. }
  588. // Make Sure That Total Percentage Is 100% By Adding A Buffer To The Last Poll Answer
  589. if($poll_multiple_ans == 0) {
  590. $poll_answer_percentage_array[] = $poll_answer_percentage;
  591. if(sizeof($poll_answer_percentage_array) == sizeof($poll_answers)) {
  592. $percentage_error_buffer = 100 - array_sum($poll_answer_percentage_array);
  593. $poll_answer_percentage = $poll_answer_percentage + $percentage_error_buffer;
  594. }
  595. }
  596. // Let User See What Options They Voted
  597. if(in_array($poll_answer_id, $user_voted)) {
  598. // Results Body Variables
  599. $template_answer = stripslashes(get_option('poll_template_resultbody2'));
  600. $template_answer = str_replace("%POLL_ANSWER_ID%", $poll_answer_id, $template_answer);
  601. $template_answer = str_replace("%POLL_ANSWER%", $poll_answer_text, $template_answer);
  602. $template_answer = str_replace("%POLL_ANSWER_TEXT%", htmlspecialchars(strip_tags($poll_answer_text)), $template_answer);
  603. $template_answer = str_replace("%POLL_ANSWER_VOTES%", number_format_i18n($poll_answer_votes), $template_answer);
  604. $template_answer = str_replace("%POLL_ANSWER_PERCENTAGE%", $poll_answer_percentage, $template_answer);
  605. $template_answer = str_replace("%POLL_ANSWER_IMAGEWIDTH%", $poll_answer_imagewidth, $template_answer);
  606. // Print Out Results Body Template
  607. $temp_pollresult .= "\t\t$template_answer\n";
  608. } else {
  609. // Results Body Variables
  610. $template_answer = stripslashes(get_option('poll_template_resultbody'));
  611. $template_answer = str_replace("%POLL_ANSWER_ID%", $poll_answer_id, $template_answer);
  612. $template_answer = str_replace("%POLL_ANSWER%", $poll_answer_text, $template_answer);
  613. $template_answer = str_replace("%POLL_ANSWER_TEXT%", htmlspecialchars(strip_tags($poll_answer_text)), $template_answer);
  614. $template_answer = str_replace("%POLL_ANSWER_VOTES%", number_format_i18n($poll_answer_votes), $template_answer);
  615. $template_answer = str_replace("%POLL_ANSWER_PERCENTAGE%", $poll_answer_percentage, $template_answer);
  616. $template_answer = str_replace("%POLL_ANSWER_IMAGEWIDTH%", $poll_answer_imagewidth, $template_answer);
  617. // Print Out Results Body Template
  618. $temp_pollresult .= "\t\t$template_answer\n";
  619. }
  620. // Get Most Voted Data
  621. if($poll_answer_votes > $poll_most_votes) {
  622. $poll_most_answer = $poll_answer_text;
  623. $poll_most_votes = $poll_answer_votes;
  624. $poll_most_percentage = $poll_answer_percentage;
  625. }
  626. // Get Least Voted Data
  627. if($poll_least_votes == 0) {
  628. $poll_least_votes = $poll_answer_votes;
  629. }
  630. if($poll_answer_votes <= $poll_least_votes) {
  631. $poll_least_answer = $poll_answer_text;
  632. $poll_least_votes = $poll_answer_votes;
  633. $poll_least_percentage = $poll_answer_percentage;
  634. }
  635. }
  636. // Results Footer Variables
  637. if(!empty($user_voted) || $poll_question_active == 0 || !check_allowtovote()) {
  638. $template_footer = stripslashes(get_option('poll_template_resultfooter'));
  639. } else {
  640. $template_footer = stripslashes(get_option('poll_template_resultfooter2'));
  641. }
  642. $template_footer = str_replace("%POLL_START_DATE%", $poll_start_date, $template_footer);
  643. $template_footer = str_replace("%POLL_END_DATE%", $poll_end_date, $template_footer);
  644. $template_footer = str_replace("%POLL_ID%", $poll_question_id, $template_footer);
  645. $template_footer = str_replace("%POLL_TOTALVOTES%", number_format_i18n($poll_question_totalvotes), $template_footer);
  646. $template_footer = str_replace("%POLL_TOTALVOTERS%", number_format_i18n($poll_question_totalvoters), $template_footer);
  647. $template_footer = str_replace("%POLL_MOST_ANSWER%", $poll_most_answer, $template_footer);
  648. $template_footer = str_replace("%POLL_MOST_VOTES%", number_format_i18n($poll_most_votes), $template_footer);
  649. $template_footer = str_replace("%POLL_MOST_PERCENTAGE%", $poll_most_percentage, $template_footer);
  650. $template_footer = str_replace("%POLL_LEAST_ANSWER%", $poll_least_answer, $template_footer);
  651. $template_footer = str_replace("%POLL_LEAST_VOTES%", number_format_i18n($poll_least_votes), $template_footer);
  652. $template_footer = str_replace("%POLL_LEAST_PERCENTAGE%", $poll_least_percentage, $template_footer);
  653. if($poll_multiple_ans > 0) {
  654. $template_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", $poll_multiple_ans, $template_footer);
  655. } else {
  656. $template_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", '1', $template_footer);
  657. }
  658. // Print Out Results Footer Template
  659. $temp_pollresult .= "\t\t$template_footer\n";
  660. $temp_pollresult .= "</div>\n";
  661. if($display_loading) {
  662. $poll_ajax_style = get_option('poll_ajax_style');
  663. if(intval($poll_ajax_style['loading']) == 1) {
  664. $temp_pollresult .= "<div id=\"polls-$poll_question_id-loading\" class=\"wp-polls-loading\"><img src=\"".plugins_url('wp-polls/images/loading.gif')."\" width=\"16\" height=\"16\" alt=\"".__('Loading', 'wp-polls')." ...\" title=\"".__('Loading', 'wp-polls')." ...\" class=\"wp-polls-image\" />&nbsp;".__('Loading', 'wp-polls')." ...</div>\n";
  665. }
  666. }
  667. } else {
  668. $temp_pollresult .= stripslashes(get_option('poll_template_disable'));
  669. }
  670. // Return Poll Result
  671. return $temp_pollresult;
  672. }
  673. ### Function: Get IP Address
  674. if(!function_exists('get_ipaddress')) {
  675. function get_ipaddress() {
  676. if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
  677. $ip_address = $_SERVER["REMOTE_ADDR"];
  678. } else {
  679. $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
  680. }
  681. if(strpos($ip_address, ',') !== false) {
  682. $ip_address = explode(',', $ip_address);
  683. $ip_address = $ip_address[0];
  684. }
  685. return $ip_address;
  686. }
  687. }
  688. ### Function: Short Code For Inserting Polls Archive Into Page
  689. add_shortcode('page_polls', 'poll_page_shortcode');
  690. function poll_page_shortcode($atts) {
  691. return polls_archive();
  692. }
  693. ### Function: Short Code For Inserting Polls Into Posts
  694. add_shortcode('poll', 'poll_shortcode');
  695. function poll_shortcode($atts) {
  696. extract(shortcode_atts(array('id' => 0, 'type' => 'vote'), $atts));
  697. if(!is_feed()) {
  698. if($type == 'vote') {
  699. return get_poll($id, false);
  700. } elseif($type == 'result') {
  701. return display_pollresult($id);
  702. }
  703. } else {
  704. return __('Note: There is a poll embedded within this post, please visit the site to participate in this post\'s poll.', 'wp-polls');
  705. }
  706. }
  707. ### Function: Get Poll Total Questions
  708. if(!function_exists('get_pollquestions')) {
  709. function get_pollquestions($display = true) {
  710. global $wpdb;
  711. $totalpollq = intval($wpdb->get_var("SELECT COUNT(pollq_id) FROM $wpdb->pollsq"));
  712. if($display) {
  713. echo $totalpollq;
  714. } else {
  715. return $totalpollq;
  716. }
  717. }
  718. }
  719. ### Function: Get Poll Total Answers
  720. if(!function_exists('get_pollanswers')) {
  721. function get_pollanswers($display = true) {
  722. global $wpdb;
  723. $totalpolla = intval($wpdb->get_var("SELECT COUNT(polla_aid) FROM $wpdb->pollsa"));
  724. if($display) {
  725. echo $totalpolla;
  726. } else {
  727. return $totalpolla;
  728. }
  729. }
  730. }
  731. ### Function: Get Poll Total Votes
  732. if(!function_exists('get_pollvotes')) {
  733. function get_pollvotes($display = true) {
  734. global $wpdb;
  735. $totalvotes = intval($wpdb->get_var("SELECT SUM(pollq_totalvotes) FROM $wpdb->pollsq"));
  736. if($display) {
  737. echo $totalvotes;
  738. } else {
  739. return $totalvotes;
  740. }
  741. }
  742. }
  743. ### Function: Get Poll Total Voters
  744. if(!function_exists('get_pollvoters')) {
  745. function get_pollvoters($display = true) {
  746. global $wpdb;
  747. $totalvoters = intval($wpdb->get_var("SELECT SUM(pollq_totalvoters) FROM $wpdb->pollsq"));
  748. if($display) {
  749. echo $totalvoters;
  750. } else {
  751. return $totalvoters;
  752. }
  753. }
  754. }
  755. ### Function: Check Voted To Get Voted Answer
  756. function check_voted_multiple($poll_id, $polls_ips) {
  757. if(!empty($_COOKIE["voted_$poll_id"])) {
  758. return explode(',', $_COOKIE["voted_$poll_id"]);
  759. } else {
  760. if($polls_ips) {
  761. return $polls_ips;
  762. } else {
  763. return array();
  764. }
  765. }
  766. }
  767. ### Function: Polls Archive Link
  768. function polls_archive_link($page) {
  769. $polls_archive_url = get_option('poll_archive_url');
  770. if($page > 0) {
  771. if(strpos($polls_archive_url, '?') !== false) {
  772. $polls_archive_url = "$polls_archive_url&amp;poll_page=$page";
  773. } else {
  774. $polls_archive_url = "$polls_archive_url?poll_page=$page";
  775. }
  776. }
  777. return $polls_archive_url;
  778. }
  779. ### Function: Displays Polls Archive Link
  780. function display_polls_archive_link($display = true) {
  781. $template_pollarchivelink = stripslashes(get_option('poll_template_pollarchivelink'));
  782. $template_pollarchivelink = str_replace("%POLL_ARCHIVE_URL%", get_option('poll_archive_url'), $template_pollarchivelink);
  783. if($display) {
  784. echo $template_pollarchivelink;
  785. } else{
  786. return $template_pollarchivelink;
  787. }
  788. }
  789. ### Function: Display Polls Archive
  790. function polls_archive() {
  791. global $wpdb, $in_pollsarchive;
  792. // Polls Variables
  793. $in_pollsarchive = true;
  794. $page = intval($_GET['poll_page']);
  795. $polls_questions = array();
  796. $polls_answers = array();
  797. $polls_ip = array();
  798. $polls_perpage = intval(get_option('poll_archive_perpage'));
  799. $poll_questions_ids = '0';
  800. $poll_voted = false;
  801. $poll_voted_aid = 0;
  802. $poll_id = 0;
  803. $pollsarchive_output_archive = '';
  804. $polls_type = intval(get_option('poll_archive_displaypoll'));
  805. $polls_type_sql = '';
  806. // Determine What Type Of Polls To Show
  807. switch($polls_type) {
  808. case 1:
  809. $polls_type_sql = 'pollq_active = 0';
  810. break;
  811. case 2:
  812. $polls_type_sql = 'pollq_active = 1';
  813. break;
  814. case 3:
  815. $polls_type_sql = 'pollq_active IN (0,1)';
  816. break;
  817. }
  818. // Get Total Polls
  819. $total_polls = $wpdb->get_var("SELECT COUNT(pollq_id) FROM $wpdb->pollsq WHERE $polls_type_sql AND pollq_active != -1");
  820. // Calculate Paging
  821. $numposts = $total_polls;
  822. $perpage = $polls_perpage;
  823. $max_page = ceil($numposts/$perpage);
  824. if(empty($page) || $page == 0) {
  825. $page = 1;
  826. }
  827. $offset = ($page-1) * $perpage;
  828. $pages_to_show = 10;
  829. $pages_to_show_minus_1 = $pages_to_show-1;
  830. $half_page_start = floor($pages_to_show_minus_1/2);
  831. $half_page_end = ceil($pages_to_show_minus_1/2);
  832. $start_page = $page - $half_page_start;
  833. if($start_page <= 0) {
  834. $start_page = 1;
  835. }
  836. $end_page = $page + $half_page_end;
  837. if(($end_page - $start_page) != $pages_to_show_minus_1) {
  838. $end_page = $start_page + $pages_to_show_minus_1;
  839. }
  840. if($end_page > $max_page) {
  841. $start_page = $max_page - $pages_to_show_minus_1;
  842. $end_page = $max_page;
  843. }
  844. if($start_page <= 0) {
  845. $start_page = 1;
  846. }
  847. if(($offset + $perpage) > $numposts) {
  848. $max_on_page = $numposts;
  849. } else {
  850. $max_on_page = ($offset + $perpage);
  851. }
  852. if (($offset + 1) > ($numposts)) {
  853. $display_on_page = $numposts;
  854. } else {
  855. $display_on_page = ($offset + 1);
  856. }
  857. // Get Poll Questions
  858. $questions = $wpdb->get_results("SELECT * FROM $wpdb->pollsq WHERE $polls_type_sql ORDER BY pollq_id DESC LIMIT $offset, $polls_perpage");
  859. if($questions) {
  860. foreach($questions as $question) {
  861. $polls_questions[] = array('id' => intval($question->pollq_id), 'question' => stripslashes($question->pollq_question), 'timestamp' => $question->pollq_timestamp, 'totalvotes' => intval($question->pollq_totalvotes), 'start' => $question->pollq_timestamp, 'end' => trim($question->pollq_expiry), 'multiple' => intval($question->pollq_multiple), 'totalvoters' => intval($question->pollq_totalvoters));
  862. $poll_questions_ids .= intval($question->pollq_id).', ';
  863. }
  864. $poll_questions_ids = substr($poll_questions_ids, 0, -2);
  865. }
  866. // Get Poll Answers
  867. $answers = $wpdb->get_results("SELECT polla_aid, polla_qid, polla_answers, polla_votes FROM $wpdb->pollsa WHERE polla_qid IN ($poll_questions_ids) ORDER BY ".get_option('poll_ans_result_sortby').' '.get_option('poll_ans_result_sortorder'));
  868. if($answers) {
  869. foreach($answers as $answer) {
  870. $polls_answers[intval($answer->polla_qid)][] = array('aid' => intval($answer->polla_aid), 'qid' => intval($answer->polla_qid), 'answers' => stripslashes($answer->polla_answers), 'votes' => intval($answer->polla_votes));
  871. }
  872. }
  873. // Get Poll IPs
  874. $ips = $wpdb->get_results("SELECT pollip_qid, pollip_aid FROM $wpdb->pollsip WHERE pollip_qid IN ($poll_questions_ids) AND pollip_ip = '".get_ipaddress()."' ORDER BY pollip_qid ASC");
  875. if($ips) {
  876. foreach($ips as $ip) {
  877. $polls_ips[intval($ip->pollip_qid)][] = intval($ip->pollip_aid);
  878. }
  879. }
  880. // Poll Archives
  881. $pollsarchive_output_archive .= "<div class=\"wp-polls wp-polls-archive\">\n";
  882. foreach($polls_questions as $polls_question) {
  883. // Most/Least Variables
  884. $poll_most_answer = '';
  885. $poll_most_votes = 0;
  886. $poll_most_percentage = 0;
  887. $poll_least_answer = '';
  888. $poll_least_votes = 0;
  889. $poll_least_percentage = 0;
  890. // Is The Poll Total Votes 0?
  891. $poll_totalvotes_zero = true;
  892. if($polls_question['totalvotes'] > 0) {
  893. $poll_totalvotes_zero = false;
  894. }
  895. $poll_start_date = mysql2date(sprintf(__('%s @ %s', 'wp-polls'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $polls_question['start']));
  896. if(empty($polls_question['end'])) {
  897. $poll_end_date = __('No Expiry', 'wp-polls');
  898. } else {
  899. $poll_end_date = mysql2date(sprintf(__('%s @ %s', 'wp-polls'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $polls_question['end']));
  900. }
  901. // Archive Poll Header
  902. $template_archive_header = stripslashes(get_option('poll_template_pollarchiveheader'));
  903. // Poll Question Variables
  904. $template_question = stripslashes(get_option('poll_template_resultheader'));
  905. $template_question = str_replace("%POLL_QUESTION%", $polls_question['question'], $template_question);
  906. $template_question = str_replace("%POLL_ID%", $polls_question['id'], $template_question);
  907. $template_question = str_replace("%POLL_TOTALVOTES%", number_format_i18n($polls_question['totalvotes']), $template_question);
  908. $template_question = str_replace("%POLL_TOTALVOTERS%", number_format_i18n($polls_question['totalvoters']), $template_question);
  909. $template_question = str_replace("%POLL_START_DATE%", $poll_start_date, $template_question);
  910. $template_question = str_replace("%POLL_END_DATE%", $poll_end_date, $template_question);
  911. if($polls_question['multiple'] > 0) {
  912. $template_question = str_replace("%POLL_MULTIPLE_ANS_MAX%", $polls_question['multiple'], $template_question);
  913. } else {
  914. $template_question = str_replace("%POLL_MULTIPLE_ANS_MAX%", '1', $template_question);
  915. }
  916. // Print Out Result Header Template
  917. $pollsarchive_output_archive .= $template_archive_header;
  918. $pollsarchive_output_archive .= $template_question;
  919. // Store The Percentage Of The Poll
  920. $poll_answer_percentage_array = array();
  921. foreach($polls_answers[$polls_question['id']] as $polls_answer) {
  922. // Calculate Percentage And Image Bar Width
  923. if(!$poll_totalvotes_zero) {
  924. if($polls_answer['votes'] > 0) {
  925. $poll_answer_percentage = round((($polls_answer['votes']/$polls_question['totalvoters'])*100));
  926. $poll_answer_imagewidth = round($poll_answer_percentage*0.9);
  927. } else {
  928. $poll_answer_percentage = 0;
  929. $poll_answer_imagewidth = 1;
  930. }
  931. } else {
  932. $poll_answer_percentage = 0;
  933. $poll_answer_imagewidth = 1;
  934. }
  935. // Make Sure That Total Percentage Is 100% By Adding A Buffer To The Last Poll Answer
  936. if($polls_question['multiple'] == 0) {
  937. $poll_answer_percentage_array[] = $poll_answer_percentage;
  938. if(sizeof($poll_answer_percentage_array) == sizeof($polls_answers[$polls_question['id']])) {
  939. $percentage_error_buffer = 100 - array_sum($poll_answer_percentage_array);
  940. $poll_answer_percentage = $poll_answer_percentage + $percentage_error_buffer;
  941. }
  942. }
  943. // Let User See What Options They Voted
  944. if(in_array($polls_answer['aid'], check_voted_multiple($polls_question['id'], $polls_ips[$polls_question['id']]))) {
  945. // Results Body Variables
  946. $template_answer = stripslashes(get_option('poll_template_resultbody2'));
  947. $template_answer = str_replace("%POLL_ANSWER_ID%", $polls_answer['aid'], $template_answer);
  948. $template_answer = str_replace("%POLL_ANSWER%", $polls_answer['answers'], $template_answer);
  949. $template_answer = str_replace("%POLL_ANSWER_TEXT%", htmlspecialchars(strip_tags($polls_answer['answers'])), $template_answer);
  950. $template_answer = str_replace("%POLL_ANSWER_VOTES%", number_format_i18n($polls_answer['votes']), $template_answer);
  951. $template_answer = str_replace("%POLL_ANSWER_PERCENTAGE%", $poll_answer_percentage, $template_answer);
  952. $template_answer = str_replace("%POLL_ANSWER_IMAGEWIDTH%", $poll_answer_imagewidth, $template_answer);
  953. // Print Out Results Body Template
  954. $pollsarchive_output_archive .= $template_answer;
  955. } else {
  956. // Results Body Variables
  957. $template_answer = stripslashes(get_option('poll_template_resultbody'));
  958. $template_answer = str_replace("%POLL_ANSWER_ID%", $polls_answer['aid'], $template_answer);
  959. $template_answer = str_replace("%POLL_ANSWER%", $polls_answer['answers'], $template_answer);
  960. $template_answer = str_replace("%POLL_ANSWER_TEXT%", htmlspecialchars(strip_tags($polls_answer['answers'])), $template_answer);
  961. $template_answer = str_replace("%POLL_ANSWER_VOTES%", number_format_i18n($polls_answer['votes']), $template_answer);
  962. $template_answer = str_replace("%POLL_ANSWER_PERCENTAGE%", $poll_answer_percentage, $template_answer);
  963. $template_answer = str_replace("%POLL_ANSWER_IMAGEWIDTH%", $poll_answer_imagewidth, $template_answer);
  964. // Print Out Results Body Template
  965. $pollsarchive_output_archive .= $template_answer;
  966. }
  967. // Get Most Voted Data
  968. if($polls_answer['votes'] > $poll_most_votes) {
  969. $poll_most_answer = $polls_answer['answers'];
  970. $poll_most_votes = $polls_answer['votes'];
  971. $poll_most_percentage = $poll_answer_percentage;
  972. }
  973. // Get Least Voted Data
  974. if($poll_least_votes == 0) {
  975. $poll_least_votes = $polls_answer['votes'];
  976. }
  977. if($polls_answer['votes'] <= $poll_least_votes) {
  978. $poll_least_answer = $polls_answer['answers'];
  979. $poll_least_votes = $polls_answer['votes'];
  980. $poll_least_percentage = $poll_answer_percentage;
  981. }
  982. }
  983. // Results Footer Variables
  984. $template_footer = stripslashes(get_option('poll_template_resultfooter'));
  985. $template_footer = str_replace("%POLL_START_DATE%", $poll_start_date, $template_footer);
  986. $template_footer = str_replace("%POLL_END_DATE%", $poll_end_date, $template_footer);
  987. $template_footer = str_replace("%POLL_TOTALVOTES%", number_format_i18n($polls_question['totalvotes']), $template_footer);
  988. $template_footer = str_replace("%POLL_TOTALVOTERS%", number_format_i18n($polls_question['totalvoters']), $template_footer);
  989. $template_footer = str_replace("%POLL_MOST_ANSWER%", $poll_most_answer, $template_footer);
  990. $template_footer = str_replace("%POLL_MOST_VOTES%", number_format_i18n($poll_most_votes), $template_footer);
  991. $template_footer = str_replace("%POLL_MOST_PERCENTAGE%", $poll_most_percentage, $template_footer);
  992. $template_footer = str_replace("%POLL_LEAST_ANSWER%", $poll_least_answer, $template_footer);
  993. $template_footer = str_replace("%POLL_LEAST_VOTES%", number_format_i18n($poll_least_votes), $template_footer);
  994. $template_footer = str_replace("%POLL_LEAST_PERCENTAGE%", $poll_least_percentage, $template_footer);
  995. if($polls_question['multiple'] > 0) {
  996. $template_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", $polls_question['multiple'], $template_footer);
  997. } else {
  998. $template_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", '1', $template_footer);
  999. }
  1000. // Archive Poll Footer
  1001. $template_archive_footer = stripslashes(get_option('poll_template_pollarchivefooter'));
  1002. $template_archive_footer = str_replace("%POLL_START_DATE%", $poll_start_date, $template_archive_footer);
  1003. $template_archive_footer = str_replace("%POLL_END_DATE%", $poll_end_date, $template_archive_footer);
  1004. $template_archive_footer = str_replace("%POLL_TOTALVOTES%", number_format_i18n($polls_question['totalvotes']), $template_archive_footer);
  1005. $template_archive_footer = str_replace("%POLL_TOTALVOTERS%", number_format_i18n($polls_question['totalvoters']), $template_archive_footer);
  1006. $template_archive_footer = str_replace("%POLL_MOST_ANSWER%", $poll_most_answer, $template_archive_footer);
  1007. $template_archive_footer = str_replace("%POLL_MOST_VOTES%", number_format_i18n($poll_most_votes), $template_archive_footer);
  1008. $template_archive_footer = str_replace("%POLL_MOST_PERCENTAGE%", $poll_most_percentage, $template_archive_footer);
  1009. $template_archive_footer = str_replace("%POLL_LEAST_ANSWER%", $poll_least_answer, $template_archive_footer);
  1010. $template_archive_footer = str_replace("%POLL_LEAST_VOTES%", number_format_i18n($poll_least_votes), $template_archive_footer);
  1011. $template_archive_footer = str_replace("%POLL_LEAST_PERCENTAGE%", $poll_least_percentage, $template_archive_footer);
  1012. if($polls_question['multiple'] > 0) {
  1013. $template_archive_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", $polls_question['multiple'], $template_archive_footer);
  1014. } else {
  1015. $template_archive_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", '1', $template_archive_footer);
  1016. }
  1017. // Print Out Results Footer Template
  1018. $pollsarchive_output_archive .= $template_footer;
  1019. // Print Out Archive Poll Footer Template
  1020. $pollsarchive_output_archive .= $template_archive_footer;
  1021. }
  1022. $pollsarchive_output_archive .= "</div>\n";
  1023. // Polls Archive Paging
  1024. if($max_page > 1) {
  1025. $pollsarchive_output_archive .= stripslashes(get_option('poll_template_pollarchivepagingheader'));
  1026. if(function_exists('wp_pagenavi')) {
  1027. $pollsarchive_output_archive .= '<div class="wp-pagenavi">'."\n";
  1028. } else {
  1029. $pollsarchive_output_archive .= '<div class="wp-polls-paging">'."\n";
  1030. }
  1031. $pollsarchive_output_archive .= '<span class="pages">&#8201;'.sprintf(__('Page %s of %s', 'wp-polls'), number_format_i18n($page), number_format_i18n($max_page)).'&#8201;</span>';
  1032. if ($start_page >= 2 && $pages_to_show < $max_page) {
  1033. $pollsarchive_output_archive .= '<a href="'.polls_archive_link(1).'" title="'.__('&laquo; First', 'wp-polls').'">&#8201;'.__('&laquo; First', 'wp-polls').'&#8201;</a>';
  1034. $pollsarchive_output_archive .= '<span class="extend">...</span>';
  1035. }
  1036. if($page > 1) {
  1037. $pollsarchive_output_archive .= '<a href="'.polls_archive_link(($page-1)).'" title="'.__('&laquo;', 'wp-polls').'">&#8201;'.__('&laquo;', 'wp-polls').'&#8201;</a>';
  1038. }
  1039. for($i = $start_page; $i <= $end_page; $i++) {
  1040. if($i == $page) {
  1041. $pollsarchive_output_archive .= '<span class="current">&#8201;'.number_f

Large files files are truncated, but you can click here to view the full file