PluginProbe
WP-Polls / 2.77.2
WP-Polls v2.77.2
3.0.2 3.0.1 trunk 1.00 2.02a 2.03 2.04 2.05 2.06 2.06a 2.06b 2.10 2.11 2.12 2.13 2.14 2.20 2.21 2.30 2.31 2.40 2.50 2.64 2.65 2.66 All 54 releases
wp-polls / wp-polls.php

wp-polls.php in WP-Polls 2.77.2, at wp-polls.php

2,023 lines 83.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: WP-Polls
4 Plugin URI: https://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.77.2
7 Author: Lester 'GaMerZ' Chan
8 Author URI: https://lesterchan.net
9 Text Domain: wp-polls
10 */
11
12
13 /*
14 Copyright 2023 Lester Chan (email : lesterchan@gmail.com)
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */
30
31 ### Version
32 define( 'WP_POLLS_VERSION', '2.77.2' );
33
34
35 ### Create Text Domain For Translations
36 add_action( 'plugins_loaded', 'polls_textdomain' );
37 function polls_textdomain() {
38 load_plugin_textdomain( 'wp-polls' );
39 }
40
41
42 ### Polls Table Name
43 global $wpdb;
44 $wpdb->pollsq = $wpdb->prefix.'pollsq';
45 $wpdb->pollsa = $wpdb->prefix.'pollsa';
46 $wpdb->pollsip = $wpdb->prefix.'pollsip';
47
48
49 ### Function: Poll Administration Menu
50 add_action( 'admin_menu', 'poll_menu' );
51 function poll_menu() {
52 add_menu_page( __( 'Polls', 'wp-polls' ), __( 'Polls', 'wp-polls' ), 'manage_polls', 'wp-polls/polls-manager.php', '', 'dashicons-chart-bar' );
53
54 add_submenu_page( 'wp-polls/polls-manager.php', __( 'Manage Polls', 'wp-polls'), __( 'Manage Polls', 'wp-polls' ), 'manage_polls', 'wp-polls/polls-manager.php' );
55 add_submenu_page( 'wp-polls/polls-manager.php', __( 'Add Poll', 'wp-polls'), __( 'Add Poll', 'wp-polls' ), 'manage_polls', 'wp-polls/polls-add.php' );
56 add_submenu_page( 'wp-polls/polls-manager.php', __( 'Poll Options', 'wp-polls'), __( 'Poll Options', 'wp-polls' ), 'manage_polls', 'wp-polls/polls-options.php' );
57 add_submenu_page( 'wp-polls/polls-manager.php', __( 'Poll Templates', 'wp-polls'), __( 'Poll Templates', 'wp-polls' ), 'manage_polls', 'wp-polls/polls-templates.php' );
58 }
59
60
61 ### Function: Get Poll
62 function get_poll($temp_poll_id = 0, $display = true) {
63 global $wpdb, $polls_loaded;
64 // Poll Result Link
65 if(isset($_GET['pollresult'])) {
66 $pollresult_id = (int) $_GET['pollresult'];
67 } else {
68 $pollresult_id = 0;
69 }
70 $temp_poll_id = (int) $temp_poll_id;
71 // Check Whether Poll Is Disabled
72 if((int) get_option('poll_currentpoll') === -1) {
73 if($display) {
74 echo removeslashes(get_option('poll_template_disable'));
75 return '';
76 }
77
78 return removeslashes(get_option('poll_template_disable'));
79 // Poll Is Enabled
80 } else {
81 do_action('wp_polls_get_poll');
82 // Hardcoded Poll ID Is Not Specified
83 switch($temp_poll_id) {
84 // Random Poll
85 case -2:
86 $poll_id = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq WHERE pollq_active = 1 ORDER BY RAND() LIMIT 1");
87 break;
88 // Latest Poll
89 case 0:
90 // Random Poll
91 if((int) get_option('poll_currentpoll') === -2) {
92 $random_poll_id = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq WHERE pollq_active = 1 ORDER BY RAND() LIMIT 1");
93 $poll_id = (int) $random_poll_id;
94 if($pollresult_id > 0) {
95 $poll_id = $pollresult_id;
96 } elseif((int) $_POST['poll_id'] > 0) {
97 $poll_id = (int) $_POST['poll_id'];
98 }
99 // Current Poll ID Is Not Specified
100 } elseif((int) get_option('poll_currentpoll') === 0) {
101 // Get Lastest Poll ID
102 $poll_id = (int) get_option('poll_latestpoll');
103 } else {
104 // Get Current Poll ID
105 $poll_id = (int) get_option('poll_currentpoll');
106 }
107 break;
108 // Take Poll ID From Arguments
109 default:
110 $poll_id = $temp_poll_id;
111 }
112 }
113
114 // Assign All Loaded Poll To $polls_loaded
115 if(empty($polls_loaded)) {
116 $polls_loaded = array();
117 }
118 if(!in_array($poll_id, $polls_loaded, true)) {
119 $polls_loaded[] = $poll_id;
120 }
121
122 // User Click on View Results Link
123 if($pollresult_id === $poll_id) {
124 if($display) {
125 echo display_pollresult($poll_id);
126 } else {
127 return display_pollresult($poll_id);
128 }
129 // Check Whether User Has Voted
130 } else {
131 $poll_active = $wpdb->get_var( $wpdb->prepare( "SELECT pollq_active FROM $wpdb->pollsq WHERE pollq_id = %d", $poll_id ) );
132 $poll_active = (int) $poll_active;
133 $check_voted = check_voted( $poll_id );
134 $poll_close = 0;
135 if( $poll_active === 0 ) {
136 $poll_close = (int) get_option( 'poll_close' );
137 }
138 if( $poll_close === 2 ) {
139 if( $display ) {
140 echo '';
141 } else {
142 return '';
143 }
144 }
145 if( $poll_close === 1 || (int) $check_voted > 0 || ( is_array( $check_voted ) && count( $check_voted ) > 0 ) ) {
146 if($display) {
147 echo display_pollresult($poll_id, $check_voted);
148 } else {
149 return display_pollresult($poll_id, $check_voted);
150 }
151 } elseif( $poll_close === 3 || ! check_allowtovote() ) {
152 $disable_poll_js = '<script type="text/javascript">jQuery("#polls_form_'.$poll_id.' :input").each(function (i){jQuery(this).attr("disabled","disabled")});</script>';
153 if($display) {
154 echo display_pollvote($poll_id).$disable_poll_js;
155 } else {
156 return display_pollvote($poll_id).$disable_poll_js;
157 }
158 } elseif( $poll_active === 1 ) {
159 if($display) {
160 echo display_pollvote($poll_id);
161 } else {
162 return display_pollvote($poll_id);
163 }
164 }
165 }
166 }
167
168
169 ### Function: Enqueue Polls JavaScripts/CSS
170 add_action('wp_enqueue_scripts', 'poll_scripts');
171 function poll_scripts() {
172 if(@file_exists(get_stylesheet_directory().'/polls-css.css')) {
173 wp_enqueue_style('wp-polls', get_stylesheet_directory_uri().'/polls-css.css', false, WP_POLLS_VERSION, 'all');
174 } else {
175 wp_enqueue_style('wp-polls', plugins_url('wp-polls/polls-css.css'), false, WP_POLLS_VERSION, 'all');
176 }
177 if( is_rtl() ) {
178 if(@file_exists(get_stylesheet_directory().'/polls-css-rtl.css')) {
179 wp_enqueue_style('wp-polls-rtl', get_stylesheet_directory_uri().'/polls-css-rtl.css', false, WP_POLLS_VERSION, 'all');
180 } else {
181 wp_enqueue_style('wp-polls-rtl', plugins_url('wp-polls/polls-css-rtl.css'), false, WP_POLLS_VERSION, 'all');
182 }
183 }
184 $pollbar = get_option( 'poll_bar' );
185 if( $pollbar['style'] === 'use_css' ) {
186 $pollbar_css = '.wp-polls .pollbar {'."\n";
187 $pollbar_css .= "\t".'margin: 1px;'."\n";
188 $pollbar_css .= "\t".'font-size: '.($pollbar['height']-2).'px;'."\n";
189 $pollbar_css .= "\t".'line-height: '.$pollbar['height'].'px;'."\n";
190 $pollbar_css .= "\t".'height: '.$pollbar['height'].'px;'."\n";
191 $pollbar_css .= "\t".'background: #'.$pollbar['background'].';'."\n";
192 $pollbar_css .= "\t".'border: 1px solid #'.$pollbar['border'].';'."\n";
193 $pollbar_css .= '}'."\n";
194 } else {
195 $pollbar_css = '.wp-polls .pollbar {'."\n";
196 $pollbar_css .= "\t".'margin: 1px;'."\n";
197 $pollbar_css .= "\t".'font-size: '.($pollbar['height']-2).'px;'."\n";
198 $pollbar_css .= "\t".'line-height: '.$pollbar['height'].'px;'."\n";
199 $pollbar_css .= "\t".'height: '.$pollbar['height'].'px;'."\n";
200 $pollbar_css .= "\t".'background-image: url(\''.plugins_url('wp-polls/images/'.$pollbar['style'].'/pollbg.gif').'\');'."\n";
201 $pollbar_css .= "\t".'border: 1px solid #'.$pollbar['border'].';'."\n";
202 $pollbar_css .= '}'."\n";
203 }
204 wp_add_inline_style( 'wp-polls', $pollbar_css );
205 $poll_ajax_style = get_option('poll_ajax_style');
206 wp_enqueue_script('wp-polls', plugins_url('wp-polls/polls-js.js'), array('jquery'), WP_POLLS_VERSION, true);
207 wp_localize_script('wp-polls', 'pollsL10n', array(
208 'ajax_url' => admin_url('admin-ajax.php'),
209 'text_wait' => __('Your last request is still being processed. Please wait a while ...', 'wp-polls'),
210 'text_valid' => __('Please choose a valid poll answer.', 'wp-polls'),
211 'text_multiple' => __('Maximum number of choices allowed: ', 'wp-polls'),
212 'show_loading' => (int) $poll_ajax_style['loading'],
213 'show_fading' => (int) $poll_ajax_style['fading']
214 ));
215 }
216
217
218 ### Function: Enqueue Polls Stylesheets/JavaScripts In WP-Admin
219 add_action('admin_enqueue_scripts', 'poll_scripts_admin');
220 function poll_scripts_admin($hook_suffix) {
221 $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');
222 if(in_array($hook_suffix, $poll_admin_pages, true)) {
223 wp_enqueue_style('wp-polls-admin', plugins_url('wp-polls/polls-admin-css.css'), false, WP_POLLS_VERSION, 'all');
224 wp_enqueue_script('wp-polls-admin', plugins_url('wp-polls/polls-admin-js.js'), array('jquery'), WP_POLLS_VERSION, true);
225 wp_localize_script('wp-polls-admin', 'pollsAdminL10n', array(
226 'admin_ajax_url' => admin_url('admin-ajax.php'),
227 'text_direction' => is_rtl() ? 'right' : 'left',
228 'text_delete_poll' => __('Delete Poll', 'wp-polls'),
229 'text_no_poll_logs' => __('No poll logs available.', 'wp-polls'),
230 'text_delete_all_logs' => __('Delete All Logs', 'wp-polls'),
231 'text_checkbox_delete_all_logs' => __('Please check the \\\'Yes\\\' checkbox if you want to delete all logs.', 'wp-polls'),
232 'text_delete_poll_logs' => __('Delete Logs For This Poll Only', 'wp-polls'),
233 'text_checkbox_delete_poll_logs' => __('Please check the \\\'Yes\\\' checkbox if you want to delete all logs for this poll ONLY.', 'wp-polls'),
234 'text_delete_poll_ans' => __('Delete Poll Answer', 'wp-polls'),
235 'text_open_poll' => __('Open Poll', 'wp-polls'),
236 'text_close_poll' => __('Close Poll', 'wp-polls'),
237 'text_answer' => __('Answer', 'wp-polls'),
238 'text_remove_poll_answer' => __('Remove', 'wp-polls')
239 ));
240 }
241 }
242
243
244 ### Function: Displays Polls Footer In WP-Admin
245 add_action('admin_footer-post-new.php', 'poll_footer_admin');
246 add_action('admin_footer-post.php', 'poll_footer_admin');
247 add_action('admin_footer-page-new.php', 'poll_footer_admin');
248 add_action('admin_footer-page.php', 'poll_footer_admin');
249 function poll_footer_admin() {
250 ?>
251 <script type="text/javascript">
252 QTags.addButton('ed_wp_polls', '<?php echo esc_js(__('Poll', 'wp-polls')); ?>', function() {
253 var poll_id = jQuery.trim(prompt('<?php echo esc_js(__('Enter Poll ID', 'wp-polls')); ?>'));
254 while(isNaN(poll_id)) {
255 poll_id = jQuery.trim(prompt("<?php echo esc_js(__('Error: Poll ID must be numeric', 'wp-polls')); ?>\n\n<?php echo esc_js(__('Please enter Poll ID again', 'wp-polls')); ?>"));
256 }
257 if (poll_id >= -1 && poll_id != null && poll_id != "") {
258 QTags.insertContent('[poll id="' + poll_id + '"]');
259 }
260 });
261 </script>
262 <?php
263 }
264
265 ### Function: Add Quick Tag For Poll In TinyMCE >= WordPress 2.5
266 add_action('init', 'poll_tinymce_addbuttons');
267 function poll_tinymce_addbuttons() {
268 if(!current_user_can('edit_posts') && ! current_user_can('edit_pages')) {
269 return;
270 }
271 if(get_user_option('rich_editing') === 'true') {
272 add_filter('mce_external_plugins', 'poll_tinymce_addplugin');
273 add_filter('mce_buttons', 'poll_tinymce_registerbutton');
274 add_filter('wp_mce_translation', 'poll_tinymce_translation');
275 }
276 }
277 function poll_tinymce_registerbutton($buttons) {
278 array_push($buttons, 'separator', 'polls');
279 return $buttons;
280 }
281 function poll_tinymce_addplugin($plugin_array) {
282 if(WP_DEBUG) {
283 $plugin_array['polls'] = plugins_url( 'wp-polls/tinymce/plugins/polls/plugin.js?v=' . WP_POLLS_VERSION );
284 } else {
285 $plugin_array['polls'] = plugins_url( 'wp-polls/tinymce/plugins/polls/plugin.min.js?v=' . WP_POLLS_VERSION );
286 }
287 return $plugin_array;
288 }
289 function poll_tinymce_translation($mce_translation) {
290 $mce_translation['Enter Poll ID'] = esc_js(__('Enter Poll ID', 'wp-polls'));
291 $mce_translation['Error: Poll ID must be numeric'] = esc_js(__('Error: Poll ID must be numeric', 'wp-polls'));
292 $mce_translation['Please enter Poll ID again'] = esc_js(__('Please enter Poll ID again', 'wp-polls'));
293 $mce_translation['Insert Poll'] = esc_js(__('Insert Poll', 'wp-polls'));
294 return $mce_translation;
295 }
296
297
298 ### Function: Check Who Is Allow To Vote
299 function check_allowtovote() {
300 global $user_ID;
301 $user_ID = (int) $user_ID;
302 $allow_to_vote = (int) get_option( 'poll_allowtovote' );
303 switch($allow_to_vote) {
304 // Guests Only
305 case 0:
306 if($user_ID > 0) {
307 return false;
308 }
309 return true;
310 break;
311 // Registered Users Only
312 case 1:
313 if($user_ID === 0) {
314 return false;
315 }
316 return true;
317 break;
318 // Registered Users And Guests
319 case 2:
320 default:
321 return true;
322 }
323 }
324
325
326 ### Funcrion: Check Voted By Cookie Or IP
327 function check_voted($poll_id) {
328 $poll_logging_method = (int) get_option( 'poll_logging_method' );
329 switch($poll_logging_method) {
330 // Do Not Log
331 case 0:
332 return 0;
333 break;
334 // Logged By Cookie
335 case 1:
336 return check_voted_cookie($poll_id);
337 break;
338 // Logged By IP
339 case 2:
340 return check_voted_ip($poll_id);
341 break;
342 // Logged By Cookie And IP
343 case 3:
344 $check_voted_cookie = check_voted_cookie($poll_id);
345 if(!empty($check_voted_cookie)) {
346 return $check_voted_cookie;
347 }
348 return check_voted_ip($poll_id);
349 break;
350 // Logged By Username
351 case 4:
352 return check_voted_username($poll_id);
353 break;
354 }
355 }
356
357
358 ### Function: Check Voted By Cookie
359 function check_voted_cookie( $poll_id ) {
360 $get_voted_aids = 0;
361 if ( ! empty( $_COOKIE[ 'voted_' . $poll_id ] ) ) {
362 $get_voted_aids = explode( ',', $_COOKIE[ 'voted_' . $poll_id ] );
363 $get_voted_aids = array_map( 'intval', array_map( 'sanitize_key', $get_voted_aids ) );
364 }
365 return $get_voted_aids;
366 }
367
368
369 ### Function: Check Voted By IP
370 function check_voted_ip( $poll_id ) {
371 global $wpdb;
372 $log_expiry = (int) get_option( 'poll_cookielog_expiry' );
373 $log_expiry_sql = '';
374 if( $log_expiry > 0 ) {
375 $log_expiry_sql = ' AND (' . current_time('timestamp') . '-(pollip_timestamp+0)) < ' . $log_expiry;
376 }
377 // Check IP From IP Logging Database
378 $get_voted_aids = $wpdb->get_col( $wpdb->prepare( "SELECT pollip_aid FROM $wpdb->pollsip WHERE pollip_qid = %d AND pollip_ip = %s", $poll_id, poll_get_ipaddress() ) . $log_expiry_sql );
379 if( $get_voted_aids ) {
380 return $get_voted_aids;
381 }
382
383 return 0;
384 }
385
386
387 ### Function: Check Voted By Username
388 function check_voted_username($poll_id) {
389 global $wpdb, $user_ID;
390 // Check IP If User Is Guest
391 if ( ! is_user_logged_in() ) {
392 return 1;
393 }
394 $pollsip_userid = (int) $user_ID;
395 $log_expiry = (int) get_option( 'poll_cookielog_expiry' );
396 $log_expiry_sql = '';
397 if( $log_expiry > 0 ) {
398 $log_expiry_sql = ' AND (' . current_time('timestamp') . '-(pollip_timestamp+0)) < ' . $log_expiry;
399 }
400 // Check User ID From IP Logging Database
401 $get_voted_aids = $wpdb->get_col( $wpdb->prepare( "SELECT pollip_aid FROM $wpdb->pollsip WHERE pollip_qid = %d AND pollip_userid = %d", $poll_id, $pollsip_userid ) . $log_expiry_sql );
402 if($get_voted_aids) {
403 return $get_voted_aids;
404 } else {
405 return 0;
406 }
407 }
408
409 add_filter( 'wp_polls_template_voteheader_markup', 'poll_template_vote_markup', 10, 3 );
410 add_filter( 'wp_polls_template_votebody_markup', 'poll_template_vote_markup', 10, 3 );
411 add_filter( 'wp_polls_template_votefooter_markup', 'poll_template_vote_markup', 10, 3) ;
412 add_filter( 'wp_polls_template_resultheader_markup', 'poll_template_vote_markup', 10, 3) ;
413 add_filter( 'wp_polls_template_resultbody_markup', 'poll_template_vote_markup', 10, 3) ;
414 add_filter( 'wp_polls_template_resultbody2_markup', 'poll_template_vote_markup', 10, 3) ;
415 add_filter( 'wp_polls_template_resultfooter_markup', 'poll_template_vote_markup', 10, 3) ;
416 add_filter( 'wp_polls_template_resultfooter2_markup', 'poll_template_vote_markup', 10, 3) ;
417
418 function poll_template_vote_markup( $template, $object, $variables ) {
419 return str_replace( array_keys( $variables ), array_values( $variables ), $template ) ;
420 }
421
422
423 ### Function: Display Voting Form
424 function display_pollvote($poll_id, $display_loading = true) {
425 do_action('wp_polls_display_pollvote');
426 global $wpdb;
427 // Temp Poll Result
428 $temp_pollvote = '';
429 // Get Poll Question Data
430 $poll_question = $wpdb->get_row( $wpdb->prepare( "SELECT pollq_id, pollq_question, pollq_totalvotes, pollq_timestamp, pollq_expiry, pollq_multiple, pollq_totalvoters FROM $wpdb->pollsq WHERE pollq_id = %d LIMIT 1", $poll_id ) );
431
432 // Poll Question Variables
433 $poll_question_text = wp_kses_post( removeslashes( $poll_question->pollq_question ) );
434 $poll_question_id = (int) $poll_question->pollq_id;
435 $poll_question_totalvotes = (int) $poll_question->pollq_totalvotes;
436 $poll_question_totalvoters = (int) $poll_question->pollq_totalvoters;
437 $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));
438 $poll_expiry = trim($poll_question->pollq_expiry);
439 if(empty($poll_expiry)) {
440 $poll_end_date = __('No Expiry', 'wp-polls');
441 } else {
442 $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));
443 }
444 $poll_multiple_ans = (int) $poll_question->pollq_multiple;
445
446 $template_question = removeslashes(get_option('poll_template_voteheader'));
447
448 $template_question_variables = array(
449 '%POLL_QUESTION%' => $poll_question_text,
450 '%POLL_ID%' => $poll_question_id,
451 '%POLL_TOTALVOTES%' => $poll_question_totalvotes,
452 '%POLL_TOTALVOTERS%' => $poll_question_totalvoters,
453 '%POLL_START_DATE%' => $poll_start_date,
454 '%POLL_END_DATE%' => $poll_end_date,
455 '%POLL_MULTIPLE_ANS_MAX%' => $poll_multiple_ans > 0 ? $poll_multiple_ans : 1
456 );
457 $template_question_variables = apply_filters( 'wp_polls_template_voteheader_variables', $template_question_variables );
458 $template_question = apply_filters( 'wp_polls_template_voteheader_markup', $template_question, $poll_question, $template_question_variables );
459
460
461 // Get Poll Answers Data
462 list($order_by, $sort_order) = _polls_get_ans_sort();
463 $poll_answers = $wpdb->get_results( $wpdb->prepare( "SELECT polla_aid, polla_qid, polla_answers, polla_votes FROM $wpdb->pollsa WHERE polla_qid = %d ORDER BY $order_by $sort_order", $poll_question_id ) );
464 // If There Is Poll Question With Answers
465 if($poll_question && $poll_answers) {
466 // Display Poll Voting Form
467 $temp_pollvote .= "<div id=\"polls-$poll_question_id\" class=\"wp-polls\">\n";
468 $temp_pollvote .= "\t<form id=\"polls_form_$poll_question_id\" class=\"wp-polls-form\" action=\"" . sanitize_text_field( $_SERVER['SCRIPT_NAME'] ) ."\" method=\"post\">\n";
469 $temp_pollvote .= "\t\t<p style=\"display: none;\"><input type=\"hidden\" id=\"poll_{$poll_question_id}_nonce\" name=\"wp-polls-nonce\" value=\"".wp_create_nonce('poll_'.$poll_question_id.'-nonce')."\" /></p>\n";
470 $temp_pollvote .= "\t\t<p style=\"display: none;\"><input type=\"hidden\" name=\"poll_id\" value=\"$poll_question_id\" /></p>\n";
471 if($poll_multiple_ans > 0) {
472 $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";
473 }
474 // Print Out Voting Form Header Template
475 $temp_pollvote .= "\t\t$template_question\n";
476 foreach ( $poll_answers as $poll_answer ) {
477 // Poll Answer Variables
478 $poll_answer_id = (int) $poll_answer->polla_aid;
479 $poll_answer_text = wp_kses_post( removeslashes( $poll_answer->polla_answers ) );
480 $poll_answer_votes = (int) $poll_answer->polla_votes;
481 $poll_answer_percentage = $poll_question_totalvotes > 0 ? round( ( $poll_answer_votes / $poll_question_totalvotes ) * 100 ) : 0;
482 $poll_multiple_answer_percentage = $poll_question_totalvoters > 0 ? round( ( $poll_answer_votes / $poll_question_totalvoters ) * 100 ) : 0;
483 $template_answer = removeslashes( get_option( 'poll_template_votebody' ) );
484
485 $template_answer_variables = array(
486 '%POLL_ID%' => $poll_question_id,
487 '%POLL_ANSWER_ID%' => $poll_answer_id,
488 '%POLL_ANSWER%' => $poll_answer_text,
489 '%POLL_ANSWER_VOTES%' => number_format_i18n( $poll_answer_votes ),
490 '%POLL_ANSWER_PERCENTAGE%' => $poll_answer_percentage,
491 '%POLL_MULTIPLE_ANSWER_PERCENTAGE%' => $poll_multiple_answer_percentage,
492 '%POLL_CHECKBOX_RADIO%' => $poll_multiple_ans > 0 ? 'checkbox' : 'radio',
493 );
494
495 $template_answer_variables = apply_filters( 'wp_polls_template_votebody_variables', $template_answer_variables );
496 $template_answer = apply_filters( 'wp_polls_template_votebody_markup', $template_answer, $poll_answer, $template_answer_variables );
497
498 // Print Out Voting Form Body Template
499 $temp_pollvote .= "\t\t$template_answer\n";
500 }
501 // Determine Poll Result URL
502 $poll_result_url = esc_url_raw( $_SERVER['REQUEST_URI'] );
503 $poll_result_url = preg_replace('/pollresult=(\d+)/i', 'pollresult='.$poll_question_id, $poll_result_url);
504 if(isset($_GET['pollresult']) && (int) $_GET['pollresult'] === 0) {
505 if(strpos($poll_result_url, '?') !== false) {
506 $poll_result_url = "$poll_result_url&amp;pollresult=$poll_question_id";
507 } else {
508 $poll_result_url = "$poll_result_url?pollresult=$poll_question_id";
509 }
510 }
511 // Voting Form Footer Variables
512 $template_footer = removeslashes(get_option('poll_template_votefooter'));
513
514 $template_footer_variables = array(
515 '%POLL_ID%' => $poll_question_id,
516 '%POLL_RESULT_URL%' => $poll_result_url,
517 '%POLL_START_DATE%' => $poll_start_date,
518 '%POLL_END_DATE%' => $poll_end_date,
519 '%POLL_MULTIPLE_ANS_MAX%' => $poll_multiple_ans > 0 ? $poll_multiple_ans : 1
520 );
521
522 $template_footer_variables = apply_filters( 'wp_polls_template_votefooter_variables', $template_footer_variables );
523 $template_footer = apply_filters( 'wp_polls_template_votefooter_markup', $template_footer, $poll_question, $template_footer_variables );
524
525 // Print Out Voting Form Footer Template
526 $temp_pollvote .= "\t\t$template_footer\n";
527 $temp_pollvote .= "\t</form>\n";
528 $temp_pollvote .= "</div>\n";
529 if($display_loading) {
530 $poll_ajax_style = get_option('poll_ajax_style');
531 if((int) $poll_ajax_style['loading'] === 1) {
532 $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";
533 }
534 }
535 } else {
536 $temp_pollvote .= removeslashes(get_option('poll_template_disable'));
537 }
538 // Return Poll Vote Template
539 return $temp_pollvote;
540 }
541
542
543 ### Function: Display Results Form
544 function display_pollresult( $poll_id, $user_voted = array(), $display_loading = true ) {
545 global $wpdb;
546 do_action( 'wp_polls_display_pollresult', $poll_id, $user_voted );
547 $poll_id = (int) $poll_id;
548 // User Voted
549 if( empty( $user_voted ) ) {
550 $user_voted = array();
551 }
552 if ( is_array( $user_voted ) ) {
553 $user_voted = array_map( 'intval', $user_voted );
554 } else {
555 $user_voted = array( (int) $user_voted );
556 }
557
558 // Temp Poll Result
559 $temp_pollresult = '';
560 // Most/Least Variables
561 $poll_most_answer = '';
562 $poll_most_votes = 0;
563 $poll_most_percentage = 0;
564 $poll_least_answer = '';
565 $poll_least_votes = 0;
566 $poll_least_percentage = 0;
567 // Get Poll Question Data
568 $poll_question = $wpdb->get_row( $wpdb->prepare( "SELECT pollq_id, pollq_question, pollq_totalvotes, pollq_active, pollq_timestamp, pollq_expiry, pollq_multiple, pollq_totalvoters FROM $wpdb->pollsq WHERE pollq_id = %d LIMIT 1", $poll_id ) );
569 // No poll could be loaded from the database
570 if ( ! $poll_question ) {
571 return removeslashes( get_option( 'poll_template_disable' ) );
572 }
573 // Poll Question Variables
574 $poll_question_text = wp_kses_post( removeslashes( $poll_question->pollq_question ) );
575 $poll_question_id = (int) $poll_question->pollq_id;
576 $poll_question_totalvotes = (int) $poll_question->pollq_totalvotes;
577 $poll_question_totalvoters = (int) $poll_question->pollq_totalvoters;
578 $poll_question_active = (int) $poll_question->pollq_active;
579 $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 ) );
580 $poll_expiry = trim( $poll_question->pollq_expiry );
581 if ( empty( $poll_expiry ) ) {
582 $poll_end_date = __( 'No Expiry', 'wp-polls' );
583 } else {
584 $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 ) );
585 }
586 $poll_multiple_ans = (int) $poll_question->pollq_multiple;
587 $template_question = removeslashes( get_option( 'poll_template_resultheader' ) );
588 $template_variables = array(
589 '%POLL_QUESTION%' => $poll_question_text,
590 '%POLL_ID%' => $poll_question_id,
591 '%POLL_TOTALVOTES%' => $poll_question_totalvotes,
592 '%POLL_TOTALVOTERS%' => $poll_question_totalvoters,
593 '%POLL_START_DATE%' => $poll_start_date,
594 '%POLL_END_DATE%' => $poll_end_date
595 );
596 if ( $poll_multiple_ans > 0 ) {
597 $template_variables['%POLL_MULTIPLE_ANS_MAX%'] = $poll_multiple_ans;
598 } else {
599 $template_variables['%POLL_MULTIPLE_ANS_MAX%'] = '1';
600 }
601
602 $template_variables = apply_filters('wp_polls_template_resultheader_variables', $template_variables );
603 $template_question = apply_filters('wp_polls_template_resultheader_markup', $template_question, $poll_question, $template_variables );
604
605 // Get Poll Answers Data
606 list( $order_by, $sort_order ) = _polls_get_ans_result_sort();
607 $poll_answers = $wpdb->get_results( $wpdb->prepare( "SELECT polla_aid, polla_answers, polla_votes FROM $wpdb->pollsa WHERE polla_qid = %d ORDER BY $order_by $sort_order", $poll_question_id ) );
608 // If There Is Poll Question With Answers
609 if ( $poll_question && $poll_answers ) {
610 // Store The Percentage Of The Poll
611 $poll_answer_percentage_array = array();
612 // Is The Poll Total Votes or Voters 0?
613 $poll_totalvotes_zero = $poll_question_totalvotes <= 0;
614 $poll_totalvoters_zero = $poll_question_totalvoters <= 0;
615 // Print Out Result Header Template
616 $temp_pollresult .= "<div id=\"polls-$poll_question_id\" class=\"wp-polls\">\n";
617 $temp_pollresult .= "\t\t$template_question\n";
618 foreach ( $poll_answers as $poll_answer ) {
619 // Poll Answer Variables
620 $poll_answer_id = (int) $poll_answer->polla_aid;
621 $poll_answer_text = wp_kses_post( removeslashes( $poll_answer->polla_answers ) );
622 $poll_answer_votes = (int) $poll_answer->polla_votes;
623 // Calculate Percentage And Image Bar Width
624 $poll_answer_percentage = 0;
625 $poll_multiple_answer_percentage = 0;
626 $poll_answer_imagewidth = 1;
627 if ( ! $poll_totalvotes_zero && ! $poll_totalvoters_zero && $poll_answer_votes > 0 ) {
628 $poll_answer_percentage = round( ( $poll_answer_votes / $poll_question_totalvotes ) * 100 );
629 $poll_multiple_answer_percentage = round( ( $poll_answer_votes / $poll_question_totalvoters ) * 100 );
630 $poll_answer_imagewidth = round( $poll_answer_percentage );
631 if ( $poll_answer_imagewidth === 100 ) {
632 $poll_answer_imagewidth = 99;
633 }
634 }
635 // Make Sure That Total Percentage Is 100% By Adding A Buffer To The Last Poll Answer
636 $round_percentage = apply_filters( 'wp_polls_round_percentage', false );
637 if ( $round_percentage && $poll_multiple_ans === 0 ) {
638 $poll_answer_percentage_array[] = $poll_answer_percentage;
639 if ( count( $poll_answer_percentage_array ) === count( $poll_answers ) ) {
640 $percentage_error_buffer = 100 - array_sum( $poll_answer_percentage_array );
641 $poll_answer_percentage += $percentage_error_buffer;
642 if ( $poll_answer_percentage < 0 ) {
643 $poll_answer_percentage = 0;
644 }
645 }
646 }
647
648 $template_variables = array(
649 '%POLL_ID%' => $poll_question_id,
650 '%POLL_ANSWER_ID%' => $poll_answer_id,
651 '%POLL_ANSWER%' => $poll_answer_text,
652 '%POLL_ANSWER_TEXT%' => htmlspecialchars( wp_strip_all_tags( $poll_answer_text ) ),
653 '%POLL_ANSWER_VOTES%' => number_format_i18n( $poll_answer_votes ),
654 '%POLL_ANSWER_PERCENTAGE%' => $poll_answer_percentage,
655 '%POLL_MULTIPLE_ANSWER_PERCENTAGE%' => $poll_multiple_answer_percentage,
656 '%POLL_ANSWER_IMAGEWIDTH%' => $poll_answer_imagewidth
657 );
658 $template_variables = apply_filters('wp_polls_template_resultbody_variables', $template_variables);
659
660 // Let User See What Options They Voted
661 if ( in_array( $poll_answer_id, $user_voted, true ) ) {
662 // Results Body Variables
663 $template_answer = removeslashes( get_option( 'poll_template_resultbody2' ) );
664 $template_answer = apply_filters('wp_polls_template_resultbody2_markup', $template_answer, $poll_answer, $template_variables);
665 } else {
666 // Results Body Variables
667 $template_answer = removeslashes (get_option( 'poll_template_resultbody' ) );
668 $template_answer = apply_filters('wp_polls_template_resultbody_markup', $template_answer, $poll_answer, $template_variables);
669 }
670
671 // Print Out Results Body Template
672 $temp_pollresult .= "\t\t$template_answer\n";
673
674 // Get Most Voted Data
675 if ( $poll_answer_votes > $poll_most_votes ) {
676 $poll_most_answer = $poll_answer_text;
677 $poll_most_votes = $poll_answer_votes;
678 $poll_most_percentage = $poll_answer_percentage;
679 }
680 // Get Least Voted Data
681 if ( $poll_least_votes === 0 ) {
682 $poll_least_votes = $poll_answer_votes;
683 }
684 if ( $poll_answer_votes <= $poll_least_votes ) {
685 $poll_least_answer = $poll_answer_text;
686 $poll_least_votes = $poll_answer_votes;
687 $poll_least_percentage = $poll_answer_percentage;
688 }
689 }
690 // Results Footer Variables
691 $template_variables = array(
692 '%POLL_START_DATE%' => $poll_start_date,
693 '%POLL_END_DATE%' => $poll_end_date,
694 '%POLL_ID%' => $poll_question_id,
695 '%POLL_TOTALVOTES%' => number_format_i18n( $poll_question_totalvotes ),
696 '%POLL_TOTALVOTERS%' => number_format_i18n( $poll_question_totalvoters ),
697 '%POLL_MOST_ANSWER%' => $poll_most_answer,
698 '%POLL_MOST_VOTES%' => number_format_i18n( $poll_most_votes ),
699 '%POLL_MOST_PERCENTAGE%' => $poll_most_percentage,
700 '%POLL_LEAST_ANSWER%' => $poll_least_answer,
701 '%POLL_LEAST_VOTES%' => number_format_i18n( $poll_least_votes ),
702 '%POLL_LEAST_PERCENTAGE%' => $poll_least_percentage
703 );
704 if ( $poll_multiple_ans > 0 ) {
705 $template_variables['%POLL_MULTIPLE_ANS_MAX%'] = $poll_multiple_ans;
706 } else {
707 $template_variables['%POLL_MULTIPLE_ANS_MAX%'] = '1';
708 }
709 $template_variables = apply_filters('wp_polls_template_resultfooter_variables', $template_variables );
710
711 if ( ! empty( $user_voted ) || $poll_question_active === 0 || ! check_allowtovote() ) {
712 $template_footer = removeslashes( get_option( 'poll_template_resultfooter' ) );
713 $template_footer = apply_filters('wp_polls_template_resultfooter_markup', $template_footer, $poll_question, $template_variables);
714 } else {
715 $template_footer = removeslashes( get_option( 'poll_template_resultfooter2' ) );
716 $template_footer = apply_filters('wp_polls_template_resultfooter2_markup', $template_footer, $poll_question, $template_variables);
717 }
718
719 // Print Out Results Footer Template
720 $temp_pollresult .= "\t\t$template_footer\n";
721 $temp_pollresult .= "\t\t<input type=\"hidden\" id=\"poll_{$poll_question_id}_nonce\" name=\"wp-polls-nonce\" value=\"".wp_create_nonce('poll_'.$poll_question_id.'-nonce')."\" />\n";
722 $temp_pollresult .= "</div>\n";
723 if ( $display_loading ) {
724 $poll_ajax_style = get_option( 'poll_ajax_style' );
725 if ( (int) $poll_ajax_style['loading'] === 1 ) {
726 $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";
727 }
728 }
729 } else {
730 $temp_pollresult .= removeslashes( get_option ('poll_template_disable' ) );
731 }
732 // Return Poll Result
733 return apply_filters( 'wp_polls_result_markup', $temp_pollresult );
734 }
735
736
737 ### Function: Get IP Address
738 function poll_get_raw_ipaddress() {
739 $ip = esc_attr( $_SERVER['REMOTE_ADDR'] );
740 $poll_options = get_option( 'poll_options' );
741 if ( ! empty( $poll_options ) && ! empty( $poll_options['ip_header'] ) && ! empty( $_SERVER[ $poll_options['ip_header'] ] ) ) {
742 $ip = esc_attr( $_SERVER[ $poll_options['ip_header'] ] );
743 }
744
745 return $ip;
746 }
747
748 function poll_get_ipaddress() {
749 return apply_filters( 'wp_polls_ipaddress', wp_hash( poll_get_raw_ipaddress() ) );
750 }
751
752 function poll_get_hostname() {
753 $ip = poll_get_raw_ipaddress();
754 $hostname = gethostbyaddr( $ip );
755 if ( $hostname === $ip ) {
756 $hostname = wp_privacy_anonymize_ip( $ip );
757 }
758
759 if ( false !== $hostname ) {
760 $hostname = substr( $hostname, strpos( $hostname, '.' ) + 1 );
761 }
762
763 return apply_filters( 'wp_polls_hostname', $hostname );
764 }
765
766 ### Function: Short Code For Inserting Polls Archive Into Page
767 add_shortcode('page_polls', 'poll_page_shortcode');
768 function poll_page_shortcode($atts) {
769 return polls_archive();
770 }
771
772
773 ### Function: Short Code For Inserting Polls Into Posts
774 add_shortcode( 'poll', 'poll_shortcode' );
775 function poll_shortcode( $atts ) {
776 $attributes = shortcode_atts( array( 'id' => 0, 'type' => 'vote' ), $atts );
777 if( ! is_feed() ) {
778 $id = (int) $attributes['id'];
779
780 // To maintain backward compatibility with [poll=1]. Props @tz-ua
781 if( ! $id && isset( $atts[0] ) ) {
782 $id = (int) trim( $atts[0], '="\'' );
783 }
784
785 if( $attributes['type'] === 'vote' ) {
786 return get_poll( $id, false );
787 } elseif( $attributes['type'] === 'result' ) {
788 return display_pollresult( $id );
789 }
790 } else {
791 return __( 'Note: There is a poll embedded within this post, please visit the site to participate in this post\'s poll.', 'wp-polls' );
792 }
793 }
794
795
796 ### Function: Get Poll Question Based On Poll ID
797 if(!function_exists('get_poll_question')) {
798 function get_poll_question($poll_id) {
799 global $wpdb;
800 $poll_id = (int) $poll_id;
801 $poll_question = $wpdb->get_var( $wpdb->prepare( "SELECT pollq_question FROM $wpdb->pollsq WHERE pollq_id = %d LIMIT 1", $poll_id ) );
802 return wp_kses_post( removeslashes( $poll_question ) );
803 }
804 }
805
806
807 ### Function: Get Poll Total Questions
808 if(!function_exists('get_pollquestions')) {
809 function get_pollquestions($display = true) {
810 global $wpdb;
811 $totalpollq = (int) $wpdb->get_var("SELECT COUNT(pollq_id) FROM $wpdb->pollsq");
812 if($display) {
813 echo $totalpollq;
814 } else {
815 return $totalpollq;
816 }
817 }
818 }
819
820
821 ### Function: Get Poll Total Answers
822 if(!function_exists('get_pollanswers')) {
823 function get_pollanswers($display = true) {
824 global $wpdb;
825 $totalpolla = (int) $wpdb->get_var("SELECT COUNT(polla_aid) FROM $wpdb->pollsa");
826 if($display) {
827 echo $totalpolla;
828 } else {
829 return $totalpolla;
830 }
831 }
832 }
833
834
835 ### Function: Get Poll Total Votes
836 if(!function_exists('get_pollvotes')) {
837 function get_pollvotes($display = true) {
838 global $wpdb;
839 $totalvotes = (int) $wpdb->get_var("SELECT SUM(pollq_totalvotes) FROM $wpdb->pollsq");
840 if($display) {
841 echo $totalvotes;
842 } else {
843 return $totalvotes;
844 }
845 }
846 }
847
848 ### Function: Get Poll Votes Based on Poll ID
849 if(!function_exists('get_pollvotes_by_id')) {
850 function get_pollvotes_by_id($poll_id, $display = true) {
851 global $wpdb;
852 $poll_id = (int) $poll_id;
853 $totalvotes = (int) $wpdb->get_var( $wpdb->prepare("SELECT pollq_totalvotes FROM $wpdb->pollsq WHERE pollq_id = %d LIMIT 1", $poll_id));
854 if($display) {
855 echo $totalvotes;
856 } else {
857 return $totalvotes;
858 }
859 }
860 }
861
862
863 ### Function: Get Poll Total Voters
864 if(!function_exists('get_pollvoters')) {
865 function get_pollvoters($display = true) {
866 global $wpdb;
867 $totalvoters = (int) $wpdb->get_var("SELECT SUM(pollq_totalvoters) FROM $wpdb->pollsq");
868 if($display) {
869 echo $totalvoters;
870 } else {
871 return $totalvoters;
872 }
873 }
874 }
875
876 ### Function: Get Poll Time Based on Poll ID and Date Format
877 if ( ! function_exists( 'get_polltime' ) ) {
878 function get_polltime( $poll_id, $date_format = 'd/m/Y', $display = true ) {
879 global $wpdb;
880 $poll_id = (int) $poll_id;
881 $timestamp = (int) $wpdb->get_var( $wpdb->prepare( "SELECT pollq_timestamp FROM $wpdb->pollsq WHERE pollq_id = %d LIMIT 1", $poll_id ) );
882 $formatted_date = date( $date_format, $timestamp );
883 if ( $display ) {
884 echo $formatted_date;
885 } else {
886 return $formatted_date;
887 }
888 }
889 }
890
891
892 ### Function: Check Voted To Get Voted Answer
893 function check_voted_multiple($poll_id, $polls_ips) {
894 if(!empty($_COOKIE["voted_$poll_id"])) {
895 return explode(',', $_COOKIE["voted_$poll_id"]);
896 } else {
897 if($polls_ips) {
898 return $polls_ips;
899 } else {
900 return array();
901 }
902 }
903 }
904
905
906 ### Function: Polls Archive Link
907 function polls_archive_link($page) {
908 $polls_archive_url = get_option('poll_archive_url');
909 if($page > 0) {
910 if(strpos($polls_archive_url, '?') !== false) {
911 $polls_archive_url = "$polls_archive_url&amp;poll_page=$page";
912 } else {
913 $polls_archive_url = "$polls_archive_url?poll_page=$page";
914 }
915 }
916 return $polls_archive_url;
917 }
918
919
920 ### Function: Displays Polls Archive Link
921 function display_polls_archive_link($display = true) {
922 $template_pollarchivelink = removeslashes(get_option('poll_template_pollarchivelink'));
923 $template_pollarchivelink = str_replace("%POLL_ARCHIVE_URL%", get_option('poll_archive_url'), $template_pollarchivelink);
924 if($display) {
925 echo $template_pollarchivelink;
926 } else{
927 return $template_pollarchivelink;
928 }
929 }
930
931
932 ### Function: Display Polls Archive
933 function polls_archive() {
934 do_action('wp_polls_polls_archive');
935 global $wpdb, $in_pollsarchive;
936 // Polls Variables
937 $in_pollsarchive = true;
938 $page = isset($_GET['poll_page']) ? (int) sanitize_key( $_GET['poll_page'] ) : 0;
939 $polls_questions = array();
940 $polls_answers = array();
941 $polls_ips = array();
942 $polls_perpage = (int) get_option('poll_archive_perpage');
943 $poll_questions_ids = '0';
944 $poll_voted = false;
945 $poll_voted_aid = 0;
946 $poll_id = 0;
947 $pollsarchive_output_archive = '';
948 $polls_type = (int) get_option('poll_archive_displaypoll');
949 $polls_type_sql = '';
950 // Determine What Type Of Polls To Show
951 switch($polls_type) {
952 case 1:
953 $polls_type_sql = 'pollq_active = 0';
954 break;
955 case 2:
956 $polls_type_sql = 'pollq_active = 1';
957 break;
958 case 3:
959 $polls_type_sql = 'pollq_active IN (0,1)';
960 break;
961 }
962 // Get Total Polls
963 $total_polls = $wpdb->get_var("SELECT COUNT(pollq_id) FROM $wpdb->pollsq WHERE $polls_type_sql AND pollq_active != -1");
964
965 // Calculate Paging
966 $numposts = $total_polls;
967 $perpage = $polls_perpage;
968 $max_page = ceil($numposts/$perpage);
969 if(empty($page) || $page == 0) {
970 $page = 1;
971 }
972 $offset = ($page-1) * $perpage;
973 $pages_to_show = 10;
974 $pages_to_show_minus_1 = $pages_to_show-1;
975 $half_page_start = floor($pages_to_show_minus_1/2);
976 $half_page_end = ceil($pages_to_show_minus_1/2);
977 $start_page = $page - $half_page_start;
978 if($start_page <= 0) {
979 $start_page = 1;
980 }
981 $end_page = $page + $half_page_end;
982 if(($end_page - $start_page) !== $pages_to_show_minus_1) {
983 $end_page = $start_page + $pages_to_show_minus_1;
984 }
985 if($end_page > $max_page) {
986 $start_page = $max_page - $pages_to_show_minus_1;
987 $end_page = $max_page;
988 }
989 if($start_page <= 0) {
990 $start_page = 1;
991 }
992 if(($offset + $perpage) > $numposts) {
993 $max_on_page = $numposts;
994 } else {
995 $max_on_page = ($offset + $perpage);
996 }
997 if (($offset + 1) > ($numposts)) {
998 $display_on_page = $numposts;
999 } else {
1000 $display_on_page = ($offset + 1);
1001 }
1002
1003 // Get Poll Questions
1004 $questions = $wpdb->get_results("SELECT * FROM $wpdb->pollsq WHERE $polls_type_sql ORDER BY pollq_id DESC LIMIT $offset, $polls_perpage");
1005 if($questions) {
1006 foreach($questions as $question) {
1007 $polls_questions[] = array( 'id' => (int) $question->pollq_id, 'question' => wp_kses_post( removeslashes( $question->pollq_question ) ), 'timestamp' => $question->pollq_timestamp, 'totalvotes' => (int) $question->pollq_totalvotes, 'start' => $question->pollq_timestamp, 'end' => trim( $question->pollq_expiry ), 'multiple' => (int) $question->pollq_multiple, 'totalvoters' => (int) $question->pollq_totalvoters );
1008 $poll_questions_ids .= (int) $question->pollq_id . ', ';
1009 }
1010 $poll_questions_ids = substr($poll_questions_ids, 0, -2);
1011 }
1012
1013 // Get Poll Answers
1014 list($order_by, $sort_order) = _polls_get_ans_result_sort();
1015 $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 $order_by $sort_order");
1016 if($answers) {
1017 foreach($answers as $answer) {
1018 $polls_answers[(int)$answer->polla_qid][] = array( 'aid' => (int)$answer->polla_aid, 'qid' => (int) $answer->polla_qid, 'answers' => wp_kses_post( removeslashes( $answer->polla_answers ) ), 'votes' => (int) $answer->polla_votes );
1019 }
1020 }
1021
1022 // Get Poll IPs
1023 $ips = $wpdb->get_results( "SELECT pollip_qid, pollip_aid FROM $wpdb->pollsip WHERE pollip_qid IN ($poll_questions_ids) AND pollip_ip = '" . poll_get_ipaddress() . "' ORDER BY pollip_qid ASC" );
1024 if($ips) {
1025 foreach($ips as $ip) {
1026 $polls_ips[(int) $ip->pollip_qid][] = (int) $ip->pollip_aid;
1027 }
1028 }
1029 // Poll Archives
1030 $pollsarchive_output_archive .= "<div class=\"wp-polls wp-polls-archive\">\n";
1031 foreach($polls_questions as $polls_question) {
1032 // Most/Least Variables
1033 $poll_most_answer = '';
1034 $poll_most_votes = 0;
1035 $poll_most_percentage = 0;
1036 $poll_least_answer = '';
1037 $poll_least_votes = 0;
1038 $poll_least_percentage = 0;
1039 // Is The Poll Total Votes 0?
1040 $poll_totalvotes_zero = $polls_question['totalvotes'] <= 0;
1041 $poll_totalvoters_zero = $polls_question['totalvoters'] <= 0;
1042 $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']));
1043 if(empty($polls_question['end'])) {
1044 $poll_end_date = __('No Expiry', 'wp-polls');
1045 } else {
1046 $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']));
1047 }
1048 // Archive Poll Header
1049 $template_archive_header = removeslashes(get_option('poll_template_pollarchiveheader'));
1050 // Poll Question Variables
1051 $template_question = removeslashes(get_option('poll_template_resultheader'));
1052 $template_question = str_replace("%POLL_QUESTION%", $polls_question['question'], $template_question);
1053 $template_question = str_replace("%POLL_ID%", $polls_question['id'], $template_question);
1054 $template_question = str_replace("%POLL_TOTALVOTES%", number_format_i18n($polls_question['totalvotes']), $template_question);
1055 $template_question = str_replace("%POLL_TOTALVOTERS%", number_format_i18n($polls_question['totalvoters']), $template_question);
1056 $template_question = str_replace("%POLL_START_DATE%", $poll_start_date, $template_question);
1057 $template_question = str_replace("%POLL_END_DATE%", $poll_end_date, $template_question);
1058 if($polls_question['multiple'] > 0) {
1059 $template_question = str_replace("%POLL_MULTIPLE_ANS_MAX%", $polls_question['multiple'], $template_question);
1060 } else {
1061 $template_question = str_replace("%POLL_MULTIPLE_ANS_MAX%", '1', $template_question);
1062 }
1063 // Print Out Result Header Template
1064 $pollsarchive_output_archive .= $template_archive_header;
1065 $pollsarchive_output_archive .= $template_question;
1066 // Store The Percentage Of The Poll
1067 $poll_answer_percentage_array = array();
1068 foreach($polls_answers[$polls_question['id']] as $polls_answer) {
1069 // Calculate Percentage And Image Bar Width
1070 $poll_answer_percentage = 0;
1071 $poll_multiple_answer_percentage = 0;
1072 $poll_answer_imagewidth = 1;
1073 if ( ! $poll_totalvotes_zero && ! $poll_totalvoters_zero && $polls_answer['votes'] > 0 ) {
1074 $poll_answer_percentage = round( ( $polls_answer['votes'] / $polls_question['totalvotes'] ) * 100 );
1075 $poll_multiple_answer_percentage = round( ( $polls_answer['votes'] / $polls_question['totalvoters'] ) * 100 );
1076 $poll_answer_imagewidth = round( $poll_answer_percentage * 0.9 );
1077 }
1078 // Make Sure That Total Percentage Is 100% By Adding A Buffer To The Last Poll Answer
1079 if($polls_question['multiple'] === 0) {
1080 $poll_answer_percentage_array[] = $poll_answer_percentage;
1081 if(count($poll_answer_percentage_array) === count($polls_answers[$polls_question['id']])) {
1082 $percentage_error_buffer = 100 - array_sum($poll_answer_percentage_array);
1083 $poll_answer_percentage += $percentage_error_buffer;
1084 if($poll_answer_percentage < 0) {
1085 $poll_answer_percentage = 0;
1086 }
1087 }
1088 }
1089 $polls_answer['answers'] = wp_kses_post( $polls_answer['answers'] );
1090 // Let User See What Options They Voted
1091 if (isset( $polls_ips[$polls_question['id']] ) && in_array( $polls_answer['aid'], check_voted_multiple( $polls_question['id'], $polls_ips[$polls_question['id']] ), true ) ) {
1092 $template_answer = removeslashes(get_option('poll_template_resultbody2'));
1093 } else {
1094 $template_answer = removeslashes(get_option('poll_template_resultbody'));
1095 }
1096
1097 $template_answer = str_replace( array(
1098 '%POLL_ID%',
1099 '%POLL_ANSWER_ID%',
1100 '%POLL_ANSWER%',
1101 '%POLL_ANSWER_TEXT%',
1102 '%POLL_ANSWER_VOTES%',
1103 '%POLL_ANSWER_PERCENTAGE%',
1104 '%POLL_MULTIPLE_ANSWER_PERCENTAGE%',
1105 '%POLL_ANSWER_IMAGEWIDTH%',
1106 ), array(
1107 $polls_question['id'],
1108 $polls_answer['aid'],
1109 $polls_answer['answers'],
1110 htmlspecialchars( wp_strip_all_tags( $polls_answer['answers'] ) ),
1111 number_format_i18n( $polls_answer['votes'] ),
1112 $poll_answer_percentage,
1113 $poll_multiple_answer_percentage,
1114 $poll_answer_imagewidth,
1115 ),
1116 $template_answer );
1117
1118 // Print Out Results Body Template
1119 $pollsarchive_output_archive .= $template_answer;
1120
1121 // Get Most Voted Data
1122 if($polls_answer['votes'] > $poll_most_votes) {
1123 $poll_most_answer = $polls_answer['answers'];
1124 $poll_most_votes = $polls_answer['votes'];
1125 $poll_most_percentage = $poll_answer_percentage;
1126 }
1127 // Get Least Voted Data
1128 if($poll_least_votes === 0) {
1129 $poll_least_votes = $polls_answer['votes'];
1130 }
1131 if($polls_answer['votes'] <= $poll_least_votes) {
1132 $poll_least_answer = $polls_answer['answers'];
1133 $poll_least_votes = $polls_answer['votes'];
1134 $poll_least_percentage = $poll_answer_percentage;
1135 }
1136 }
1137 // Results Footer Variables
1138 $template_footer = removeslashes(get_option('poll_template_resultfooter'));
1139 $template_footer = str_replace("%POLL_ID%", $polls_question['id'], $template_footer);
1140 $template_footer = str_replace("%POLL_START_DATE%", $poll_start_date, $template_footer);
1141 $template_footer = str_replace("%POLL_END_DATE%", $poll_end_date, $template_footer);
1142 $template_footer = str_replace("%POLL_TOTALVOTES%", number_format_i18n($polls_question['totalvotes']), $template_footer);
1143 $template_footer = str_replace("%POLL_TOTALVOTERS%", number_format_i18n($polls_question['totalvoters']), $template_footer);
1144 $template_footer = str_replace("%POLL_MOST_ANSWER%", $poll_most_answer, $template_footer);
1145 $template_footer = str_replace("%POLL_MOST_VOTES%", number_format_i18n($poll_most_votes), $template_footer);
1146 $template_footer = str_replace("%POLL_MOST_PERCENTAGE%", $poll_most_percentage, $template_footer);
1147 $template_footer = str_replace("%POLL_LEAST_ANSWER%", $poll_least_answer, $template_footer);
1148 $template_footer = str_replace("%POLL_LEAST_VOTES%", number_format_i18n($poll_least_votes), $template_footer);
1149 $template_footer = str_replace("%POLL_LEAST_PERCENTAGE%", $poll_least_percentage, $template_footer);
1150 if($polls_question['multiple'] > 0) {
1151 $template_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", $polls_question['multiple'], $template_footer);
1152 } else {
1153 $template_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", '1', $template_footer);
1154 }
1155 // Archive Poll Footer
1156 $template_archive_footer = removeslashes(get_option('poll_template_pollarchivefooter'));
1157 $template_archive_footer = str_replace("%POLL_START_DATE%", $poll_start_date, $template_archive_footer);
1158 $template_archive_footer = str_replace("%POLL_END_DATE%", $poll_end_date, $template_archive_footer);
1159 $template_archive_footer = str_replace("%POLL_TOTALVOTES%", number_format_i18n($polls_question['totalvotes']), $template_archive_footer);
1160 $template_archive_footer = str_replace("%POLL_TOTALVOTERS%", number_format_i18n($polls_question['totalvoters']), $template_archive_footer);
1161 $template_archive_footer = str_replace("%POLL_MOST_ANSWER%", $poll_most_answer, $template_archive_footer);
1162 $template_archive_footer = str_replace("%POLL_MOST_VOTES%", number_format_i18n($poll_most_votes), $template_archive_footer);
1163 $template_archive_footer = str_replace("%POLL_MOST_PERCENTAGE%", $poll_most_percentage, $template_archive_footer);
1164 $template_archive_footer = str_replace("%POLL_LEAST_ANSWER%", $poll_least_answer, $template_archive_footer);
1165 $template_archive_footer = str_replace("%POLL_LEAST_VOTES%", number_format_i18n($poll_least_votes), $template_archive_footer);
1166 $template_archive_footer = str_replace("%POLL_LEAST_PERCENTAGE%", $poll_least_percentage, $template_archive_footer);
1167 if($polls_question['multiple'] > 0) {
1168 $template_archive_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", $polls_question['multiple'], $template_archive_footer);
1169 } else {
1170 $template_archive_footer = str_replace("%POLL_MULTIPLE_ANS_MAX%", '1', $template_archive_footer);
1171 }
1172 // Print Out Results Footer Template
1173 $pollsarchive_output_archive .= $template_footer;
1174 // Print Out Archive Poll Footer Template
1175 $pollsarchive_output_archive .= $template_archive_footer;
1176 }
1177 $pollsarchive_output_archive .= "</div>\n";
1178
1179 // Polls Archive Paging
1180 if($max_page > 1) {
1181 $pollsarchive_output_archive .= removeslashes(get_option('poll_template_pollarchivepagingheader'));
1182 if(function_exists('wp_pagenavi')) {
1183 $pollsarchive_output_archive .= '<div class="wp-pagenavi">'."\n";
1184 } else {
1185 $pollsarchive_output_archive .= '<div class="wp-polls-paging">'."\n";
1186 }
1187 $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>';
1188 if ($start_page >= 2 && $pages_to_show < $max_page) {
1189 $pollsarchive_output_archive .= '<a href="'.polls_archive_link(1).'" title="'.__('&laquo; First', 'wp-polls').'">&#8201;'.__('&laquo; First', 'wp-polls').'&#8201;</a>';
1190 $pollsarchive_output_archive .= '<span class="extend">...</span>';
1191 }
1192 if($page > 1) {
1193 $pollsarchive_output_archive .= '<a href="'.polls_archive_link(($page-1)).'" title="'.__('&laquo;', 'wp-polls').'">&#8201;'.__('&laquo;', 'wp-polls').'&#8201;</a>';
1194 }
1195 for($i = $start_page; $i <= $end_page; $i++) {
1196 if($i === $page) {
1197 $pollsarchive_output_archive .= '<span class="current">&#8201;'.number_format_i18n($i).'&#8201;</span>';
1198 } else {
1199 $pollsarchive_output_archive .= '<a href="'.polls_archive_link($i).'" title="'.number_format_i18n($i).'">&#8201;'.number_format_i18n($i).'&#8201;</a>';
1200 }
1201 }
1202 if(empty($page) || ($page+1) <= $max_page) {
1203 $pollsarchive_output_archive .= '<a href="'.polls_archive_link(($page+1)).'" title="'.__('&raquo;', 'wp-polls').'">&#8201;'.__('&raquo;', 'wp-polls').'&#8201;</a>';
1204 }
1205 if ($end_page < $max_page) {
1206 $pollsarchive_output_archive .= '<span class="extend">...</span>';
1207 $pollsarchive_output_archive .= '<a href="'.polls_archive_link($max_page).'" title="'.__('Last &raquo;', 'wp-polls').'">&#8201;'.__('Last &raquo;', 'wp-polls').'&#8201;</a>';
1208 }
1209 $pollsarchive_output_archive .= '</div>';
1210 $pollsarchive_output_archive .= removeslashes(get_option('poll_template_pollarchivepagingfooter'));
1211 }
1212
1213 // Output Polls Archive Page
1214 return apply_filters( 'wp_polls_archive', $pollsarchive_output_archive );
1215 }
1216
1217
1218 // Edit Timestamp Options
1219 function poll_timestamp($poll_timestamp, $fieldname = 'pollq_timestamp', $display = 'block') {
1220 global $month;
1221 echo '<div id="'.$fieldname.'" style="display: '.$display.'">'."\n";
1222 $day = (int) gmdate('j', $poll_timestamp);
1223 echo '<select name="'.$fieldname.'_day" size="1">'."\n";
1224 for($i = 1; $i <=31; $i++) {
1225 if($day === $i) {
1226 echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
1227 } else {
1228 echo "<option value=\"$i\">$i</option>\n";
1229 }
1230 }
1231 echo '</select>&nbsp;&nbsp;'."\n";
1232 $month2 = (int) gmdate('n', $poll_timestamp);
1233 echo '<select name="'.$fieldname.'_month" size="1">'."\n";
1234 for($i = 1; $i <= 12; $i++) {
1235 if ($i < 10) {
1236 $ii = '0'.$i;
1237 } else {
1238 $ii = $i;
1239 }
1240 if($month2 === $i) {
1241 echo "<option value=\"$i\" selected=\"selected\">$month[$ii]</option>\n";
1242 } else {
1243 echo "<option value=\"$i\">$month[$ii]</option>\n";
1244 }
1245 }
1246 echo '</select>&nbsp;&nbsp;'."\n";
1247 $year = (int) gmdate('Y', $poll_timestamp);
1248 echo '<select name="'.$fieldname.'_year" size="1">'."\n";
1249 for($i = 2000; $i <= ($year+10); $i++) {
1250 if($year === $i) {
1251 echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
1252 } else {
1253 echo "<option value=\"$i\">$i</option>\n";
1254 }
1255 }
1256 echo '</select>&nbsp;@'."\n";
1257 echo '<span dir="ltr">'."\n";
1258 $hour = (int) gmdate('H', $poll_timestamp);
1259 echo '<select name="'.$fieldname.'_hour" size="1">'."\n";
1260 for($i = 0; $i < 24; $i++) {
1261 if($hour === $i) {
1262 echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
1263 } else {
1264 echo "<option value=\"$i\">$i</option>\n";
1265 }
1266 }
1267 echo '</select>&nbsp;:'."\n";
1268 $minute = (int) gmdate('i', $poll_timestamp);
1269 echo '<select name="'.$fieldname.'_minute" size="1">'."\n";
1270 for($i = 0; $i < 60; $i++) {
1271 if($minute === $i) {
1272 echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
1273 } else {
1274 echo "<option value=\"$i\">$i</option>\n";
1275 }
1276 }
1277
1278 echo '</select>&nbsp;:'."\n";
1279 $second = (int) gmdate('s', $poll_timestamp);
1280 echo '<select name="'.$fieldname.'_second" size="1">'."\n";
1281 for($i = 0; $i <= 60; $i++) {
1282 if($second === $i) {
1283 echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
1284 } else {
1285 echo "<option value=\"$i\">$i</option>\n";
1286 }
1287 }
1288 echo '</select>'."\n";
1289 echo '</span>'."\n";
1290 echo '</div>'."\n";
1291 }
1292
1293
1294 ### Function: Place Cron
1295 function cron_polls_place() {
1296 wp_clear_scheduled_hook('polls_cron');
1297 if (!wp_next_scheduled('polls_cron')) {
1298 wp_schedule_event(time(), 'hourly', 'polls_cron');
1299 }
1300 }
1301
1302 ### Funcion: Check All Polls Status To Check If It Expires
1303 add_action('polls_cron', 'cron_polls_status');
1304 function cron_polls_status() {
1305 global $wpdb;
1306 // Close Poll
1307 $close_polls = $wpdb->query("UPDATE $wpdb->pollsq SET pollq_active = 0 WHERE pollq_expiry < '".current_time('timestamp')."' AND pollq_expiry != 0 AND pollq_active != 0");
1308 // Open Future Polls
1309 $active_polls = $wpdb->query("UPDATE $wpdb->pollsq SET pollq_active = 1 WHERE pollq_timestamp <= '".current_time('timestamp')."' AND pollq_active = -1");
1310 // Update Latest Poll If Future Poll Is Opened
1311 if($active_polls) {
1312 $update_latestpoll = update_option('poll_latestpoll', polls_latest_id());
1313 }
1314 return;
1315 }
1316
1317
1318 ### Funcion: Get Latest Poll ID
1319 function polls_latest_id() {
1320 global $wpdb;
1321 $poll_id = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq WHERE pollq_active = 1 ORDER BY pollq_timestamp DESC LIMIT 1");
1322 return (int) $poll_id;
1323 }
1324
1325
1326 ### Check If In Poll Archive Page
1327 function in_pollarchive() {
1328 $poll_archive_url = get_option('poll_archive_url');
1329 $poll_archive_url_array = explode('/', $poll_archive_url);
1330 $poll_archive_url = $poll_archive_url_array[count($poll_archive_url_array)-1];
1331 if(empty($poll_archive_url)) {
1332 $poll_archive_url = $poll_archive_url_array[count($poll_archive_url_array)-2];
1333 }
1334 $current_url = esc_url_raw( $_SERVER['REQUEST_URI'] );
1335 if ( strpos( $current_url, strval( $poll_archive_url ) ) === false ) {
1336 return false;
1337 }
1338
1339 return true;
1340 }
1341
1342 function vote_poll_process( $poll_id, $poll_aid_array = [] ) {
1343 global $wpdb, $user_identity, $user_ID;
1344
1345 do_action( 'wp_polls_vote_poll' );
1346
1347 // Acquire lock
1348 $fp_lock = polls_acquire_lock( $poll_id );
1349 if ( $fp_lock === false ) {
1350 throw new InvalidArgumentException( sprintf( __( 'Unable to obtain lock for Poll ID #%s', 'wp-polls'), $poll_id ) );
1351 }
1352
1353 $polla_aids = $wpdb->get_col( $wpdb->prepare( "SELECT polla_aid FROM $wpdb->pollsa WHERE polla_qid = %d", $poll_id ) );
1354 $is_real = count( array_intersect( $poll_aid_array, $polla_aids ) ) === count( $poll_aid_array );
1355
1356 if( !$is_real ) {
1357 throw new InvalidArgumentException(sprintf(__('Invalid Answer to Poll ID #%s', 'wp-polls'), $poll_id));
1358 }
1359
1360 if (!check_allowtovote()) {
1361 throw new InvalidArgumentException(sprintf(__('User is not allowed to vote for Poll ID #%s', 'wp-polls'), $poll_id));
1362 }
1363
1364 if (empty($poll_aid_array)) {
1365 throw new InvalidArgumentException(sprintf(__('No answers given for Poll ID #%s', 'wp-polls'), $poll_id));
1366 }
1367
1368 if($poll_id === 0) {
1369 throw new InvalidArgumentException(sprintf(__('Invalid Poll ID. Poll ID #%s', 'wp-polls'), $poll_id));
1370 }
1371
1372 $is_poll_open = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->pollsq WHERE pollq_id = %d AND pollq_active = 1", $poll_id ) );
1373
1374 if ($is_poll_open === 0) {
1375 throw new InvalidArgumentException(sprintf(__( 'Poll ID #%s is closed', 'wp-polls' ), $poll_id ));
1376 }
1377
1378 $check_voted = check_voted($poll_id);
1379 if ( !empty( $check_voted ) ) {
1380 throw new InvalidArgumentException(sprintf(__('You Had Already Voted For This Poll. Poll ID #%s', 'wp-polls'), $poll_id));
1381 }
1382
1383 if (!empty($user_identity)) {
1384 $pollip_user = $user_identity;
1385 } elseif ( ! empty( $_COOKIE['comment_author_' . COOKIEHASH] ) ) {
1386 $pollip_user = $_COOKIE['comment_author_' . COOKIEHASH];
1387 } else {
1388 $pollip_user = __('Guest', 'wp-polls');
1389 }
1390
1391 $pollip_user = sanitize_text_field( $pollip_user );
1392 $pollip_userid = $user_ID;
1393 $pollip_ip = poll_get_ipaddress();
1394 $pollip_host = poll_get_hostname();
1395 $pollip_timestamp = current_time('timestamp');
1396 $poll_logging_method = (int) get_option('poll_logging_method');
1397
1398 // Only Create Cookie If User Choose Logging Method 1 Or 3
1399 if ( $poll_logging_method === 1 || $poll_logging_method === 3 ) {
1400 $cookie_expiry = (int) get_option('poll_cookielog_expiry');
1401 if ($cookie_expiry === 0) {
1402 $cookie_expiry = YEAR_IN_SECONDS;
1403 }
1404 setcookie( 'voted_' . $poll_id, implode(',', $poll_aid_array ), $pollip_timestamp + $cookie_expiry, apply_filters( 'wp_polls_cookiepath', SITECOOKIEPATH ) );
1405 }
1406
1407 $i = 0;
1408 foreach ($poll_aid_array as $polla_aid) {
1409 $update_polla_votes = $wpdb->query( "UPDATE $wpdb->pollsa SET polla_votes = (polla_votes + 1) WHERE polla_qid = $poll_id AND polla_aid = $polla_aid" );
1410 if (!$update_polla_votes) {
1411 unset($poll_aid_array[$i]);
1412 }
1413 $i++;
1414 }
1415
1416 $vote_q = $wpdb->query("UPDATE $wpdb->pollsq SET pollq_totalvotes = (pollq_totalvotes+" . count( $poll_aid_array ) . "), pollq_totalvoters = (pollq_totalvoters + 1) WHERE pollq_id = $poll_id AND pollq_active = 1");
1417 if (!$vote_q) {
1418 throw new InvalidArgumentException(sprintf(__('Unable To Update Poll Total Votes And Poll Total Voters. Poll ID #%s', 'wp-polls'), $poll_id));
1419 }
1420
1421 foreach ($poll_aid_array as $polla_aid) {
1422 // Log Ratings In DB If User Choose Logging Method 2, 3 or 4
1423 if ( $poll_logging_method > 1 ){
1424 $wpdb->insert(
1425 $wpdb->pollsip,
1426 array(
1427 'pollip_qid' => $poll_id,
1428 'pollip_aid' => $polla_aid,
1429 'pollip_ip' => $pollip_ip,
1430 'pollip_host' => $pollip_host,
1431 'pollip_timestamp' => $pollip_timestamp,
1432 'pollip_user' => $pollip_user,
1433 'pollip_userid' => $pollip_userid
1434 ),
1435 array(
1436 '%s',
1437 '%s',
1438 '%s',
1439 '%s',
1440 '%s',
1441 '%s',
1442 '%d'
1443 )
1444 );
1445 }
1446 }
1447
1448 // Release lock
1449 polls_release_lock( $fp_lock, $poll_id );
1450
1451 do_action( 'wp_polls_vote_poll_success' );
1452
1453 return display_pollresult($poll_id, $poll_aid_array, false);
1454 }
1455
1456
1457 ### Function: Vote Poll
1458 add_action('wp_ajax_polls', 'vote_poll');
1459 add_action('wp_ajax_nopriv_polls', 'vote_poll');
1460 function vote_poll() {
1461 global $wpdb, $user_identity, $user_ID;
1462
1463 if( isset( $_REQUEST['action'] ) && sanitize_key( $_REQUEST['action'] ) === 'polls') {
1464 // Load Headers
1465 polls_textdomain();
1466 header('Content-Type: text/html; charset='.get_option('blog_charset').'');
1467
1468 // Get Poll ID
1469 $poll_id = (isset($_REQUEST['poll_id']) ? (int) sanitize_key( $_REQUEST['poll_id'] ) : 0);
1470
1471 // Ensure Poll ID Is Valid
1472 if($poll_id === 0) {
1473 _e('Invalid Poll ID', 'wp-polls');
1474 exit();
1475 }
1476
1477 // Verify Referer
1478 if( ! check_ajax_referer( 'poll_'.$poll_id.'-nonce', 'poll_'.$poll_id.'_nonce', false ) ) {
1479 _e('Failed To Verify Referrer', 'wp-polls');
1480 exit();
1481 }
1482
1483 // Which View
1484 switch( sanitize_key( $_REQUEST['view'] ) ) {
1485 // Poll Vote
1486 case 'process':
1487 try {
1488 $poll_aid_array = array_unique( array_map('intval', array_map('sanitize_key', explode( ',', $_POST["poll_$poll_id"] ) ) ) );
1489 echo vote_poll_process($poll_id, $poll_aid_array);
1490 } catch (Exception $e) {
1491 echo $e->getMessage();
1492 }
1493 break;
1494 // Poll Result
1495 case 'result':
1496 echo display_pollresult($poll_id, 0, false);
1497 break;
1498 // Poll Booth Aka Poll Voting Form
1499 case 'booth':
1500 echo display_pollvote($poll_id, false);
1501 break;
1502 } // End switch($_REQUEST['view'])
1503 } // End if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'polls')
1504 exit();
1505 }
1506
1507
1508 ### Function: Manage Polls
1509 add_action('wp_ajax_polls-admin', 'manage_poll');
1510 function manage_poll() {
1511 global $wpdb;
1512 ### Form Processing
1513 if( isset( $_POST['action'] ) && sanitize_key( $_POST['action'] ) === 'polls-admin' ) {
1514 if( ! empty( $_POST['do'] ) ) {
1515 // Set Header
1516 header('Content-Type: text/html; charset='.get_option('blog_charset').'');
1517
1518 // Decide What To Do
1519 switch($_POST['do']) {
1520 // Delete Polls Logs
1521 case __('Delete All Logs', 'wp-polls'):
1522 check_ajax_referer('wp-polls_delete-polls-logs');
1523 if( sanitize_key( trim( $_POST['delete_logs_yes'] ) ) === 'yes') {
1524 $delete_logs = $wpdb->query("DELETE FROM $wpdb->pollsip");
1525 if($delete_logs) {
1526 echo '<p style="color: green;">'.__('All Polls Logs Have Been Deleted.', 'wp-polls').'</p>';
1527 } else {
1528 echo '<p style="color: red;">'.__('An Error Has Occurred While Deleting All Polls Logs.', 'wp-polls').'</p>';
1529 }
1530 }
1531 break;
1532 // Delete Poll Logs For Individual Poll
1533 case __('Delete Logs For This Poll Only', 'wp-polls'):
1534 check_ajax_referer('wp-polls_delete-poll-logs');
1535 $pollq_id = (int) sanitize_key( $_POST['pollq_id'] );
1536 $pollq_question = $wpdb->get_var( $wpdb->prepare( "SELECT pollq_question FROM $wpdb->pollsq WHERE pollq_id = %d", $pollq_id ) );
1537 if( sanitize_key( trim( $_POST['delete_logs_yes'] ) ) === 'yes') {
1538 $delete_logs = $wpdb->delete( $wpdb->pollsip, array( 'pollip_qid' => $pollq_id ), array( '%d' ) );
1539 if( $delete_logs ) {
1540 echo '<p style="color: green;">'.sprintf(__('All Logs For \'%s\' Has Been Deleted.', 'wp-polls'), wp_kses_post( removeslashes( $pollq_question ) ) ).'</p>';
1541 } else {
1542 echo '<p style="color: red;">'.sprintf(__('An Error Has Occurred While Deleting All Logs For \'%s\'', 'wp-polls'), wp_kses_post( removeslashes( $pollq_question ) ) ).'</p>';
1543 }
1544 }
1545 break;
1546 // Delete Poll's Answer
1547 case __('Delete Poll Answer', 'wp-polls'):
1548 check_ajax_referer('wp-polls_delete-poll-answer');
1549 $pollq_id = (int) sanitize_key( $_POST['pollq_id'] );
1550 $polla_aid = (int) sanitize_key( $_POST['polla_aid'] );
1551 $poll_answers = $wpdb->get_row( $wpdb->prepare( "SELECT polla_votes, polla_answers FROM $wpdb->pollsa WHERE polla_aid = %d AND polla_qid = %d", $polla_aid, $pollq_id ) );
1552 $polla_votes = (int) $poll_answers->polla_votes;
1553 $polla_answers = wp_kses_post( removeslashes( trim( $poll_answers->polla_answers ) ) );
1554 $delete_polla_answers = $wpdb->delete( $wpdb->pollsa, array( 'polla_aid' => $polla_aid, 'polla_qid' => $pollq_id ), array( '%d', '%d' ) );
1555 $delete_pollip = $wpdb->delete( $wpdb->pollsip, array( 'pollip_qid' => $pollq_id, 'pollip_aid' => $polla_aid ), array( '%d', '%d' ) );
1556 $update_pollq_totalvotes = $wpdb->query( "UPDATE $wpdb->pollsq SET pollq_totalvotes = (pollq_totalvotes - $polla_votes) WHERE pollq_id = $pollq_id" );
1557 if($delete_polla_answers) {
1558 echo '<p style="color: green;">'.sprintf(__('Poll Answer \'%s\' Deleted Successfully.', 'wp-polls'), $polla_answers).'</p>';
1559 } else {
1560 echo '<p style="color: red;">'.sprintf(__('Error In Deleting Poll Answer \'%s\'.', 'wp-polls'), $polla_answers).'</p>';
1561 }
1562 break;
1563 // Open Poll
1564 case __('Open Poll', 'wp-polls'):
1565 check_ajax_referer('wp-polls_open-poll');
1566 $pollq_id = (int) sanitize_key( $_POST['pollq_id'] );
1567 $pollq_question = $wpdb->get_var( $wpdb->prepare( "SELECT pollq_question FROM $wpdb->pollsq WHERE pollq_id = %d", $pollq_id ) );
1568 $open_poll = $wpdb->update(
1569 $wpdb->pollsq,
1570 array(
1571 'pollq_active' => 1
1572 ),
1573 array(
1574 'pollq_id' => $pollq_id
1575 ),
1576 array(
1577 '%d'
1578 ),
1579 array(
1580 '%d'
1581 )
1582 );
1583 if( $open_poll ) {
1584 echo '<p style="color: green;">'.sprintf(__('Poll \'%s\' Is Now Opened', 'wp-polls'), wp_kses_post( removeslashes( $pollq_question ) ) ).'</p>';
1585 } else {
1586 echo '<p style="color: red;">'.sprintf(__('Error Opening Poll \'%s\'', 'wp-polls'), wp_kses_post( removeslashes( $pollq_question ) ) ).'</p>';
1587 }
1588 break;
1589 // Close Poll
1590 case __('Close Poll', 'wp-polls'):
1591 check_ajax_referer('wp-polls_close-poll');
1592 $pollq_id = (int) sanitize_key( $_POST['pollq_id'] );
1593 $pollq_question = $wpdb->get_var( $wpdb->prepare( "SELECT pollq_question FROM $wpdb->pollsq WHERE pollq_id = %d", $pollq_id ) );
1594 $close_poll = $wpdb->update(
1595 $wpdb->pollsq,
1596 array(
1597 'pollq_active' => 0
1598 ),
1599 array(
1600 'pollq_id' => $pollq_id
1601 ),
1602 array(
1603 '%d'
1604 ),
1605 array(
1606 '%d'
1607 )
1608 );
1609 if( $close_poll ) {
1610 echo '<p style="color: green;">'.sprintf(__('Poll \'%s\' Is Now Closed', 'wp-polls'), wp_kses_post( removeslashes( $pollq_question ) ) ).'</p>';
1611 } else {
1612 echo '<p style="color: red;">'.sprintf(__('Error Closing Poll \'%s\'', 'wp-polls'), wp_kses_post( removeslashes( $pollq_question ) ) ).'</p>';
1613 }
1614 break;
1615 // Delete Poll
1616 case __('Delete Poll', 'wp-polls'):
1617 check_ajax_referer('wp-polls_delete-poll');
1618 $pollq_id = (int) sanitize_key( $_POST['pollq_id'] );
1619 $pollq_question = $wpdb->get_var( $wpdb->prepare( "SELECT pollq_question FROM $wpdb->pollsq WHERE pollq_id = %d", $pollq_id ) );
1620 $delete_poll_question = $wpdb->delete( $wpdb->pollsq, array( 'pollq_id' => $pollq_id ), array( '%d' ) );
1621 $delete_poll_answers = $wpdb->delete( $wpdb->pollsa, array( 'polla_qid' => $pollq_id ), array( '%d' ) );
1622 $delete_poll_ip = $wpdb->delete( $wpdb->pollsip, array( 'pollip_qid' => $pollq_id ), array( '%d' ) );
1623 $poll_option_lastestpoll = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'poll_latestpoll'");
1624 if(!$delete_poll_question) {
1625 echo '<p style="color: red;">'.sprintf(__('Error In Deleting Poll \'%s\' Question', 'wp-polls'), wp_kses_post( removeslashes( $pollq_question ) ) ).'</p>';
1626 }
1627 if(empty($text)) {
1628 echo '<p style="color: green;">'.sprintf(__('Poll \'%s\' Deleted Successfully', 'wp-polls'), wp_kses_post( removeslashes( $pollq_question ) ) ).'</p>';
1629 }
1630
1631 // Update Lastest Poll ID To Poll Options
1632 update_option( 'poll_latestpoll', polls_latest_id() );
1633 do_action( 'wp_polls_delete_poll', $pollq_id );
1634 break;
1635 }
1636 exit();
1637 }
1638 }
1639 }
1640
1641 function polls_acquire_lock( $poll_id ) {
1642 $fp = fopen( polls_lock_file( $poll_id ), 'w+' );
1643
1644 if ( ! flock( $fp, LOCK_EX | LOCK_NB ) ) {
1645 return false;
1646 }
1647
1648 ftruncate( $fp, 0 );
1649 fwrite( $fp, microtime( true ) );
1650
1651 return $fp;
1652 }
1653
1654 function polls_release_lock( $fp, $poll_id ) {
1655 if ( is_resource( $fp ) ) {
1656 fflush( $fp );
1657 flock( $fp, LOCK_UN );
1658 fclose( $fp );
1659 unlink( polls_lock_file( $poll_id ) );
1660
1661 return true;
1662 }
1663
1664 return false;
1665 }
1666
1667 function polls_lock_file( $poll_id ) {
1668 return apply_filters( 'wp_polls_lock_file', get_temp_dir() . '/wp-blog-' . get_current_blog_id() . '-wp-polls-' . $poll_id . '.lock', $poll_id );
1669 }
1670
1671 function _polls_get_ans_sort() {
1672 $order_by = get_option( 'poll_ans_sortby' );
1673 switch( $order_by ) {
1674 case 'polla_votes':
1675 case 'polla_aid':
1676 case 'polla_answers':
1677 case 'RAND()':
1678 break;
1679 default:
1680 $order_by = 'polla_aid';
1681 break;
1682 }
1683 $sort_order = get_option( 'poll_ans_sortorder' ) === 'desc' ? 'desc' : 'asc';
1684 return array( $order_by, $sort_order );
1685 }
1686
1687 function _polls_get_ans_result_sort() {
1688 $order_by = get_option( 'poll_ans_result_sortby' );
1689 switch( $order_by ) {
1690 case 'polla_votes':
1691 case 'polla_aid':
1692 case 'polla_answers':
1693 case 'RAND()':
1694 break;
1695 default:
1696 $order_by = 'polla_aid';
1697 break;
1698 }
1699 $sort_order = get_option( 'poll_ans_result_sortorder' ) === 'desc' ? 'desc' : 'asc';
1700 return array( $order_by, $sort_order );
1701 }
1702
1703
1704 ### Function: Plug Into WP-Stats
1705 add_action( 'plugins_loaded','polls_wp_stats' );
1706 function polls_wp_stats() {
1707 add_filter( 'wp_stats_page_admin_plugins', 'polls_page_admin_general_stats' );
1708 add_filter( 'wp_stats_page_plugins', 'polls_page_general_stats' );
1709 }
1710
1711
1712 ### Function: Add WP-Polls General Stats To WP-Stats Page Options
1713 function polls_page_admin_general_stats($content) {
1714 $stats_display = get_option('stats_display');
1715 if( (int) $stats_display['polls'] === 1) {
1716 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_polls" value="polls" checked="checked" />&nbsp;&nbsp;<label for="wpstats_polls">'.__('WP-Polls', 'wp-polls').'</label><br />'."\n";
1717 } else {
1718 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_polls" value="polls" />&nbsp;&nbsp;<label for="wpstats_polls">'.__('WP-Polls', 'wp-polls').'</label><br />'."\n";
1719 }
1720 return $content;
1721 }
1722
1723
1724 ### Function: Add WP-Polls General Stats To WP-Stats Page
1725 function polls_page_general_stats($content) {
1726 $stats_display = get_option('stats_display');
1727 if( (int) $stats_display['polls'] === 1) {
1728 $content .= '<p><strong>'.__('WP-Polls', 'wp-polls').'</strong></p>'."\n";
1729 $content .= '<ul>'."\n";
1730 $content .= '<li>'.sprintf(_n('<strong>%s</strong> poll was created.', '<strong>%s</strong> polls were created.', get_pollquestions(false), 'wp-polls'), number_format_i18n(get_pollquestions(false))).'</li>'."\n";
1731 $content .= '<li>'.sprintf(_n('<strong>%s</strong> polls\' answer was given.', '<strong>%s</strong> polls\' answers were given.', get_pollanswers(false), 'wp-polls'), number_format_i18n(get_pollanswers(false))).'</li>'."\n";
1732 $content .= '<li>'.sprintf(_n('<strong>%s</strong> vote was cast.', '<strong>%s</strong> votes were cast.', get_pollvotes(false), 'wp-polls'), number_format_i18n(get_pollvotes(false))).'</li>'."\n";
1733 $content .= '</ul>'."\n";
1734 }
1735 return $content;
1736 }
1737
1738
1739 ### Class: WP-Polls Widget
1740 class WP_Widget_Polls extends WP_Widget {
1741 // Constructor
1742 public function __construct() {
1743 $widget_ops = array('description' => __('WP-Polls polls', 'wp-polls'));
1744 parent::__construct('polls-widget', __('Polls', 'wp-polls'), $widget_ops);
1745 }
1746
1747 // Display Widget
1748 public function widget( $args, $instance ) {
1749 $title = apply_filters( 'widget_title', esc_attr( $instance['title'] ) );
1750 $poll_id = (int) $instance['poll_id'];
1751 $display_pollarchive = (int) $instance['display_pollarchive'];
1752 echo $args['before_widget'];
1753 if( ! empty( $title ) ) {
1754 echo $args['before_title'] . $title . $args['after_title'];
1755 }
1756 get_poll( $poll_id );
1757 if( $display_pollarchive ) {
1758 display_polls_archive_link();
1759 }
1760 echo $args['after_widget'];
1761 }
1762
1763 // When Widget Control Form Is Posted
1764 public function update($new_instance, $old_instance) {
1765 if (!isset($new_instance['submit'])) {
1766 return false;
1767 }
1768 $instance = $old_instance;
1769 $instance['title'] = strip_tags($new_instance['title']);
1770 $instance['poll_id'] = (int) $new_instance['poll_id'];
1771 $instance['display_pollarchive'] = (int) $new_instance['display_pollarchive'];
1772 return $instance;
1773 }
1774
1775 // DIsplay Widget Control Form
1776 public function form($instance) {
1777 global $wpdb;
1778 $instance = wp_parse_args((array) $instance, array('title' => __('Polls', 'wp-polls'), 'poll_id' => 0, 'display_pollarchive' => 1));
1779 $title = esc_attr($instance['title']);
1780 $poll_id = (int) $instance['poll_id'];
1781 $display_pollarchive = (int) $instance['display_pollarchive'];
1782 ?>
1783 <p>
1784 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wp-polls'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label>
1785 </p>
1786 <p>
1787 <label for="<?php echo $this->get_field_id('display_pollarchive'); ?>"><?php _e('Display Polls Archive Link Below Poll?', 'wp-polls'); ?>
1788 <select name="<?php echo $this->get_field_name('display_pollarchive'); ?>" id="<?php echo $this->get_field_id('display_pollarchive'); ?>" class="widefat">
1789 <option value="0"<?php selected(0, $display_pollarchive); ?>><?php _e('No', 'wp-polls'); ?></option>
1790 <option value="1"<?php selected(1, $display_pollarchive); ?>><?php _e('Yes', 'wp-polls'); ?></option>
1791 </select>
1792 </label>
1793 </p>
1794 <p>
1795 <label for="<?php echo $this->get_field_id('poll_id'); ?>"><?php _e('Poll To Display:', 'wp-polls'); ?>
1796 <select name="<?php echo $this->get_field_name('poll_id'); ?>" id="<?php echo $this->get_field_id('poll_id'); ?>" class="widefat">
1797 <option value="-1"<?php selected(-1, $poll_id); ?>><?php _e('Do NOT Display Poll (Disable)', 'wp-polls'); ?></option>
1798 <option value="-2"<?php selected(-2, $poll_id); ?>><?php _e('Display Random Poll', 'wp-polls'); ?></option>
1799 <option value="0"<?php selected(0, $poll_id); ?>><?php _e('Display Latest Poll', 'wp-polls'); ?></option>
1800 <optgroup>&nbsp;</optgroup>
1801 <?php
1802 $polls = $wpdb->get_results("SELECT pollq_id, pollq_question FROM $wpdb->pollsq ORDER BY pollq_id DESC");
1803 if($polls) {
1804 foreach($polls as $poll) {
1805 $pollq_question = wp_kses_post( removeslashes( $poll->pollq_question ) );
1806 $pollq_id = (int) $poll->pollq_id;
1807 if($pollq_id === $poll_id) {
1808 echo "<option value=\"$pollq_id\" selected=\"selected\">$pollq_question</option>\n";
1809 } else {
1810 echo "<option value=\"$pollq_id\">$pollq_question</option>\n";
1811 }
1812 }
1813 }
1814 ?>
1815 </select>
1816 </label>
1817 </p>
1818 <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
1819 <?php
1820 }
1821 }
1822
1823
1824 ### Function: Init WP-Polls Widget
1825 add_action('widgets_init', 'widget_polls_init');
1826 function widget_polls_init() {
1827 polls_textdomain();
1828 register_widget('WP_Widget_Polls');
1829 }
1830
1831 if( ! function_exists( 'removeslashes' ) ) {
1832 function removeslashes( $string ) {
1833 $string = implode( '', explode( '\\', $string ) );
1834 return stripslashes( trim( $string ) );
1835 }
1836 }
1837
1838 ### Function: Activate Plugin
1839 register_activation_hook( __FILE__, 'polls_activation' );
1840 function polls_activation( $network_wide ) {
1841 if ( is_multisite() && $network_wide ) {
1842 $ms_sites = wp_get_sites();
1843
1844 if( 0 < count( $ms_sites ) ) {
1845 foreach ( $ms_sites as $ms_site ) {
1846 switch_to_blog( $ms_site['blog_id'] );
1847 polls_activate();
1848 restore_current_blog();
1849 }
1850 }
1851 } else {
1852 polls_activate();
1853 }
1854 }
1855
1856 function polls_activate() {
1857 global $wpdb;
1858
1859 if(@is_file(ABSPATH.'/wp-admin/includes/upgrade.php')) {
1860 include_once(ABSPATH.'/wp-admin/includes/upgrade.php');
1861 } elseif(@is_file(ABSPATH.'/wp-admin/upgrade-functions.php')) {
1862 include_once(ABSPATH.'/wp-admin/upgrade-functions.php');
1863 } else {
1864 die('We have problem finding your \'/wp-admin/upgrade-functions.php\' and \'/wp-admin/includes/upgrade.php\'');
1865 }
1866
1867 // Create Poll Tables (3 Tables)
1868 $charset_collate = $wpdb->get_charset_collate();
1869
1870 $create_table = array();
1871 $create_table['pollsq'] = "CREATE TABLE $wpdb->pollsq (".
1872 "pollq_id int(10) NOT NULL auto_increment," .
1873 "pollq_question varchar(200) character set utf8 NOT NULL default ''," .
1874 "pollq_timestamp varchar(20) NOT NULL default ''," .
1875 "pollq_totalvotes int(10) NOT NULL default '0'," .
1876 "pollq_active tinyint(1) NOT NULL default '1'," .
1877 "pollq_expiry int(10) NOT NULL default '0'," .
1878 "pollq_multiple tinyint(3) NOT NULL default '0'," .
1879 "pollq_totalvoters int(10) NOT NULL default '0'," .
1880 "PRIMARY KEY (pollq_id)" .
1881 ") $charset_collate;";
1882 $create_table['pollsa'] = "CREATE TABLE $wpdb->pollsa (" .
1883 "polla_aid int(10) NOT NULL auto_increment," .
1884 "polla_qid int(10) NOT NULL default '0'," .
1885 "polla_answers varchar(200) character set utf8 NOT NULL default ''," .
1886 "polla_votes int(10) NOT NULL default '0'," .
1887 "PRIMARY KEY (polla_aid)" .
1888 ") $charset_collate;";
1889 $create_table['pollsip'] = "CREATE TABLE $wpdb->pollsip (" .
1890 "pollip_id int(10) NOT NULL auto_increment," .
1891 "pollip_qid int(10) NOT NULL default '0'," .
1892 "pollip_aid int(10) NOT NULL default '0'," .
1893 "pollip_ip varchar(100) NOT NULL default ''," .
1894 "pollip_host VARCHAR(200) NOT NULL default ''," .
1895 "pollip_timestamp int(10) NOT NULL default '0'," .
1896 "pollip_user tinytext NOT NULL," .
1897 "pollip_userid int(10) NOT NULL default '0'," .
1898 "PRIMARY KEY (pollip_id)," .
1899 "KEY pollip_ip (pollip_ip)," .
1900 "KEY pollip_qid (pollip_qid)," .
1901 "KEY pollip_ip_qid (pollip_ip, pollip_qid)" .
1902 ") $charset_collate;";
1903 dbDelta( $create_table['pollsq'] );
1904 dbDelta( $create_table['pollsa'] );
1905 dbDelta( $create_table['pollsip'] );
1906 // Check Whether It is Install Or Upgrade
1907 $first_poll = $wpdb->get_var( "SELECT pollq_id FROM $wpdb->pollsq LIMIT 1" );
1908 // If Install, Insert 1st Poll Question With 5 Poll Answers
1909 if ( empty( $first_poll ) ) {
1910 // Insert Poll Question (1 Record)
1911 $insert_pollq = $wpdb->insert( $wpdb->pollsq, array( 'pollq_question' => __( 'How Is My Site?', 'wp-polls' ), 'pollq_timestamp' => current_time( 'timestamp' ) ), array( '%s', '%s' ) );
1912 if ( $insert_pollq ) {
1913 // Insert Poll Answers (5 Records)
1914 $wpdb->insert( $wpdb->pollsa, array( 'polla_qid' => $insert_pollq, 'polla_answers' => __( 'Good', 'wp-polls' ) ), array( '%d', '%s' ) );
1915 $wpdb->insert( $wpdb->pollsa, array( 'polla_qid' => $insert_pollq, 'polla_answers' => __( 'Excellent', 'wp-polls' ) ), array( '%d', '%s' ) );
1916 $wpdb->insert( $wpdb->pollsa, array( 'polla_qid' => $insert_pollq, 'polla_answers' => __( 'Bad', 'wp-polls' ) ), array( '%d', '%s' ) );
1917 $wpdb->insert( $wpdb->pollsa, array( 'polla_qid' => $insert_pollq, 'polla_answers' => __( 'Can Be Improved', 'wp-polls' ) ), array( '%d', '%s' ) );
1918 $wpdb->insert( $wpdb->pollsa, array( 'polla_qid' => $insert_pollq, 'polla_answers' => __( 'No Comments', 'wp-polls' ) ), array( '%d', '%s' ) );
1919 }
1920 }
1921 // Add In Options (16 Records)
1922 add_option('poll_template_voteheader', '<p style="text-align: center;"><strong>%POLL_QUESTION%</strong></p>'.
1923 '<div id="polls-%POLL_ID%-ans" class="wp-polls-ans">'.
1924 '<ul class="wp-polls-ul">');
1925 add_option('poll_template_votebody', '<li><input type="%POLL_CHECKBOX_RADIO%" id="poll-answer-%POLL_ANSWER_ID%" name="poll_%POLL_ID%" value="%POLL_ANSWER_ID%" /> <label for="poll-answer-%POLL_ANSWER_ID%">%POLL_ANSWER%</label></li>');
1926 add_option('poll_template_votefooter', '</ul>'.
1927 '<p style="text-align: center;"><input type="button" name="vote" value=" '.__('Vote', 'wp-polls').' " class="Buttons" onclick="poll_vote(%POLL_ID%);" /></p>'.
1928 '<p style="text-align: center;"><a href="#ViewPollResults" onclick="poll_result(%POLL_ID%); return false;" title="'.__('View Results Of This Poll', 'wp-polls').'">'.__('View Results', 'wp-polls').'</a></p>'.
1929 '</div>');
1930 add_option('poll_template_resultheader', '<p style="text-align: center;"><strong>%POLL_QUESTION%</strong></p>'.
1931 '<div id="polls-%POLL_ID%-ans" class="wp-polls-ans">'.
1932 '<ul class="wp-polls-ul">');
1933 add_option('poll_template_resultbody', '<li>%POLL_ANSWER% <small>(%POLL_ANSWER_PERCENTAGE%%'.__(',', 'wp-polls').' %POLL_ANSWER_VOTES% '.__('Votes', 'wp-polls').')</small><div class="pollbar" style="width: %POLL_ANSWER_IMAGEWIDTH%%;" title="%POLL_ANSWER_TEXT% (%POLL_ANSWER_PERCENTAGE%% | %POLL_ANSWER_VOTES% '.__('Votes', 'wp-polls').')"></div></li>');
1934 add_option('poll_template_resultbody2', '<li><strong><i>%POLL_ANSWER% <small>(%POLL_ANSWER_PERCENTAGE%%'.__(',', 'wp-polls').' %POLL_ANSWER_VOTES% '.__('Votes', 'wp-polls').')</small></i></strong><div class="pollbar" style="width: %POLL_ANSWER_IMAGEWIDTH%%;" title="'.__('You Have Voted For This Choice', 'wp-polls').' - %POLL_ANSWER_TEXT% (%POLL_ANSWER_PERCENTAGE%% | %POLL_ANSWER_VOTES% '.__('Votes', 'wp-polls').')"></div></li>');
1935 add_option('poll_template_resultfooter', '</ul>'.
1936 '<p style="text-align: center;">'.__('Total Voters', 'wp-polls').': <strong>%POLL_TOTALVOTERS%</strong></p>'.
1937 '</div>');
1938 add_option('poll_template_resultfooter2', '</ul>'.
1939 '<p style="text-align: center;">'.__('Total Voters', 'wp-polls').': <strong>%POLL_TOTALVOTERS%</strong></p>'.
1940 '<p style="text-align: center;"><a href="#VotePoll" onclick="poll_booth(%POLL_ID%); return false;" title="'.__('Vote For This Poll', 'wp-polls').'">'.__('Vote', 'wp-polls').'</a></p>'.
1941 '</div>');
1942 add_option('poll_template_disable', __('Sorry, there are no polls available at the moment.', 'wp-polls'));
1943 add_option('poll_template_error', __('An error has occurred when processing your poll.', 'wp-polls'));
1944 add_option('poll_currentpoll', 0);
1945 add_option('poll_latestpoll', 1);
1946 add_option('poll_archive_perpage', 5);
1947 add_option('poll_ans_sortby', 'polla_aid');
1948 add_option('poll_ans_sortorder', 'asc');
1949 add_option('poll_ans_result_sortby', 'polla_votes');
1950 add_option('poll_ans_result_sortorder', 'desc');
1951 // Database Upgrade For WP-Polls 2.1
1952 add_option('poll_logging_method', '3');
1953 add_option('poll_allowtovote', '2');
1954 // Database Upgrade For WP-Polls 2.12
1955 add_option('poll_archive_url', site_url('pollsarchive'));
1956 // Database Upgrade For WP-Polls 2.13
1957 add_option('poll_bar', array('style' => 'default', 'background' => 'd8e1eb', 'border' => 'c8c8c8', 'height' => 8));
1958 // Database Upgrade For WP-Polls 2.14
1959 add_option('poll_close', 1);
1960 // Database Upgrade For WP-Polls 2.20
1961 add_option('poll_ajax_style', array('loading' => 1, 'fading' => 1));
1962 add_option('poll_template_pollarchivelink', '<ul>'.
1963 '<li><a href="%POLL_ARCHIVE_URL%">'.__('Polls Archive', 'wp-polls').'</a></li>'.
1964 '</ul>');
1965 add_option('poll_archive_displaypoll', 2);
1966 add_option('poll_template_pollarchiveheader', '');
1967 add_option('poll_template_pollarchivefooter', '<p>'.__('Start Date:', 'wp-polls').' %POLL_START_DATE%<br />'.__('End Date:', 'wp-polls').' %POLL_END_DATE%</p>');
1968
1969 $pollq_totalvoters = (int) $wpdb->get_var( "SELECT SUM(pollq_totalvoters) FROM $wpdb->pollsq" );
1970 if ( 0 === $pollq_totalvoters ) {
1971 $wpdb->query( "UPDATE $wpdb->pollsq SET pollq_totalvoters = pollq_totalvotes" );
1972 }
1973
1974 // Database Upgrade For WP-Polls 2.30
1975 add_option('poll_cookielog_expiry', 0);
1976 add_option('poll_template_pollarchivepagingheader', '');
1977 add_option('poll_template_pollarchivepagingfooter', '');
1978
1979 // Database Upgrade For WP-Polls 2.50
1980 delete_option('poll_archive_show');
1981
1982 // Database Upgrade for WP-Polls 2.76
1983 add_option( 'poll_options', array( 'ip_header' => '' ) );
1984
1985 // Index
1986 $index = $wpdb->get_results( "SHOW INDEX FROM $wpdb->pollsip;" );
1987 $key_name = array();
1988 if( count( $index ) > 0 ) {
1989 foreach( $index as $i ) {
1990 $key_name[]= $i->Key_name;
1991 }
1992 }
1993 if ( ! in_array( 'pollip_ip', $key_name, true ) ) {
1994 $wpdb->query( "ALTER TABLE $wpdb->pollsip ADD INDEX pollip_ip (pollip_ip);" );
1995 }
1996 if ( ! in_array( 'pollip_qid', $key_name, true ) ) {
1997 $wpdb->query( "ALTER TABLE $wpdb->pollsip ADD INDEX pollip_qid (pollip_qid);" );
1998 }
1999 if ( ! in_array( 'pollip_ip_qid_aid', $key_name, true ) ) {
2000 $wpdb->query( "ALTER TABLE $wpdb->pollsip ADD INDEX pollip_ip_qid_aid (pollip_ip, pollip_qid, pollip_aid);" );
2001 }
2002 // No longer needed index
2003 if ( in_array( 'pollip_ip_qid', $key_name, true ) ) {
2004 $wpdb->query( "ALTER TABLE $wpdb->pollsip DROP INDEX pollip_ip_qid;" );
2005 }
2006
2007 // Change column datatype for wp_pollsip
2008 $col_pollip_qid = $wpdb->get_row( "DESCRIBE $wpdb->pollsip pollip_qid" );
2009 if( 'varchar(10)' === $col_pollip_qid->Type ) {
2010 $wpdb->query( "ALTER TABLE $wpdb->pollsip MODIFY COLUMN pollip_qid int(10) NOT NULL default '0';" );
2011 $wpdb->query( "ALTER TABLE $wpdb->pollsip MODIFY COLUMN pollip_aid int(10) NOT NULL default '0';" );
2012 $wpdb->query( "ALTER TABLE $wpdb->pollsip MODIFY COLUMN pollip_timestamp int(10) NOT NULL default '0';" );
2013 $wpdb->query( "ALTER TABLE $wpdb->pollsq MODIFY COLUMN pollq_expiry int(10) NOT NULL default '0';" );
2014 }
2015
2016 // Set 'manage_polls' Capabilities To Administrator
2017 $role = get_role( 'administrator' );
2018 if( ! $role->has_cap( 'manage_polls' ) ) {
2019 $role->add_cap( 'manage_polls' );
2020 }
2021 cron_polls_place();
2022 }
2023