root/wp-polls/trunk/wp-polls.php

Revision 52946, 66.3 kB (checked in by GamerZ, 4 days ago)

Remove nested dirname() call

Line 
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.31
7 Author: Lester 'GaMerZ' Chan
8 Author URI: http://lesterchan.net
9 */
10
11
12 /* 
13     Copyright 2008  Lester Chan  (email : lesterchan@gmail.com)
14
15     This program is free software; you can redistribute it and/or modify
16     it under the terms of the GNU General Public License as published by
17     the Free Software Foundation; either version 2 of the License, or
18     (at your option) any later version.
19
20     This program is distributed in the hope that it will be useful,
21     but WITHOUT ANY WARRANTY; without even the implied warranty of
22     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     GNU General Public License for more details.
24
25     You should have received a copy of the GNU General Public License
26     along with this program; if not, write to the Free Software
27     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 */
29
30
31 ### Load WP-Config File If This File Is Called Directly
32 if (!function_exists('add_action')) {
33     $wp_root = '../../..';
34     if (file_exists($wp_root.'/wp-load.php')) {
35         require_once($wp_root.'/wp-load.php');
36     } else {
37         require_once($wp_root.'/wp-config.php');
38     }
39 }
40
41
42 ### Use WordPress 2.6 Constants
43 if (!defined('WP_CONTENT_DIR')) {
44     define( 'WP_CONTENT_DIR', ABSPATH.'wp-content');
45 }
46 if (!defined('WP_CONTENT_URL')) {
47     define('WP_CONTENT_URL', get_option('siteurl').'/wp-content');
48 }
49 if (!defined('WP_PLUGIN_DIR')) {
50     define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');
51 }
52 if (!defined('WP_PLUGIN_URL')) {
53     define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
54 }
55
56
57 ### Create Text Domain For Translations
58 add_action('init', 'polls_textdomain');
59 function polls_textdomain() {
60     if (!function_exists('wp_print_styles')) {
61         load_plugin_textdomain('wp-polls', 'wp-content/plugins/wp-polls');
62     } else {
63         load_plugin_textdomain('wp-polls', false, 'wp-polls');
64     }
65 }
66
67
68 ### Polls Table Name
69 global $wpdb;
70 $wpdb->pollsq = $wpdb->prefix.'pollsq';
71 $wpdb->pollsa = $wpdb->prefix.'pollsa';
72 $wpdb->pollsip     = $wpdb->prefix.'pollsip';
73
74
75 ### Function: Poll Administration Menu
76 add_action('admin_menu', 'poll_menu');
77 function poll_menu() {
78     if (function_exists('add_menu_page')) {
79         add_menu_page(__('Polls', 'wp-polls'), __('Polls', 'wp-polls'), 'manage_polls', 'wp-polls/polls-manager.php');
80     }
81     if (function_exists('add_submenu_page')) {
82         add_submenu_page('wp-polls/polls-manager.php', __('Manage Polls', 'wp-polls'), __('Manage Polls', 'wp-polls'), 'manage_polls', 'wp-polls/polls-manager.php');
83         add_submenu_page('wp-polls/polls-manager.php', __('Add Poll', 'wp-polls'), __('Add Poll', 'wp-polls'), 'manage_polls', 'wp-polls/polls-add.php');       
84         add_submenu_page('wp-polls/polls-manager.php', __('Poll Options', 'wp-polls'), __('Poll Options', 'wp-polls'), 'manage_polls', 'wp-polls/polls-options.php');
85         add_submenu_page('wp-polls/polls-manager.php', __('Poll Templates', 'wp-polls'), __('Poll Templates', 'wp-polls'), 'manage_polls', 'wp-polls/polls-templates.php');
86         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');
87     }
88 }
89
90
91 ### Function: Get Poll
92 function get_poll($temp_poll_id = 0, $display = true) {
93     global $wpdb, $polls_loaded;
94     // Poll Result Link
95     $pollresult_id = intval($_GET['pollresult']);
96     // Check Whether Poll Is Disabled
97     if(intval(get_option('poll_currentpoll')) == -1) {
98         if($display) {
99             echo stripslashes(get_option('poll_template_disable'));
100             return;
101         } else {
102             return stripslashes(get_option('poll_template_disable'));
103         }       
104     // Poll Is Enabled
105     } else {
106         // Hardcoded Poll ID Is Not Specified
107         if(intval($temp_poll_id) == 0) {
108             // Random Poll
109             if(intval(get_option('poll_currentpoll')) == -2) {
110                 $random_poll_id = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq WHERE pollq_active = 1 ORDER BY RAND() LIMIT 1");
111                 $poll_id = intval($random_poll_id);
112                 if($pollresult_id > 0) {
113                     $poll_id = $pollresult_id;
114                 } elseif(intval($_POST['poll_id']) > 0) {
115                     $poll_id = intval($_POST['poll_id']);
116                 }
117             // Current Poll ID Is Not Specified
118             } elseif(intval(get_option('poll_currentpoll')) == 0) {
119                 // Get Lastest Poll ID
120                 $poll_id = intval(get_option('poll_latestpoll'));
121             } else {
122                 // Get Current Poll ID
123                 $poll_id = intval(get_option('poll_currentpoll'));
124             }
125         // Get Hardcoded Poll ID
126         } else {
127             $poll_id = intval($temp_poll_id);
128         }
129     }
130     
131     // Assign All Loaded Poll To $polls_loaded
132     if(empty($polls_loaded)) {
133         $polls_loaded = array();
134     }
135     if(!in_array($poll_id, $polls_loaded)) {
136         $polls_loaded[] = $poll_id;
137     }
138
139     // User Click on View Results Link
140     if($pollresult_id == $poll_id) {
141         if($display) {
142             echo display_pollresult($poll_id);
143             return;
144         } else {
145             return display_pollresult($poll_id);
146         }
147     // Check Whether User Has Voted
148     } else {
149         $poll_active = $wpdb->get_var("SELECT pollq_active FROM $wpdb->pollsq WHERE pollq_id = $poll_id");
150         $poll_active = intval($poll_active);
151         $check_voted = check_voted($poll_id);
152         if($poll_active == 0) {
153             $poll_close = intval(get_option('poll_close'));
154         } else {
155             $poll_close = 0;
156         }
157         if($check_voted > 0 || ($poll_active == 0 && $poll_close == 1)) {
158             if($display) {
159                 echo display_pollresult($poll_id, $check_voted);
160                 return;
161             } else {
162                 return display_pollresult($poll_id, $check_voted);
163             }
164         } elseif(!check_allowtovote() || ($poll_active == 0 && $poll_close == 3)) {
165             $disable_poll_js = '<script type="text/javascript">poll_disable_voting('.$poll_id.');</script>';
166             if($display) {
167                 echo display_pollvote($poll_id).$disable_poll_js;
168                 return;
169             } else {
170                 return display_pollvote($poll_id).$disable_poll_js;
171             }           
172         } elseif($poll_active == 1) {
173             if($display) {
174                 echo display_pollvote($poll_id);
175                 return;
176             } else {
177                 return display_pollvote($poll_id);
178             }
179         }
180     }   
181 }
182
183
184 ### Function: Displays Polls Header
185 add_action('wp_head', 'poll_header');
186 function poll_header() {   
187     $poll_ajax_style = get_option('poll_ajax_style');
188     $pollbar = get_option('poll_bar');
189     wp_register_script('wp-polls', WP_PLUGIN_URL.'/wp-polls/polls-js-packed.js', false, '2.31');
190     echo "\n".'<!-- Start Of Script Generated By WP-Polls 2.31 -->'."\n";
191     echo '<script type="text/javascript">'."\n";
192     echo '/* <![CDATA[ */'."\n";
193     echo "\t".'var polls_ajax_url = \''.WP_PLUGIN_URL.'/wp-polls/wp-polls.php'."';\n";
194     echo "\t".'var polls_text_wait = \''.js_escape(__('Your last request is still being processed. Please wait a while ...', 'wp-polls')).'\';'."\n";
195     echo "\t".'var polls_text_valid = \''.js_escape(__('Please choose a valid poll answer.', 'wp-polls')).'\';'."\n";
196     echo "\t".'var polls_text_multiple = \''.js_escape(__('Maximum number of choices allowed:', 'wp-polls')).'\';'."\n";
197     echo "\t".'var poll_show_loading = '.intval($poll_ajax_style['loading']).';'."\n";
198     echo "\t".'var poll_show_fading = '.intval($poll_ajax_style['fading']).';'."\n";
199     echo '/* ]]> */'."\n";
200     echo '</script>'."\n";
201     wp_print_scripts(array('sack', 'wp-polls'));
202     if(@file_exists(TEMPLATEPATH.'/polls-css.css')) {
203         echo '<link rel="stylesheet" href="'.get_stylesheet_directory_uri().'/polls-css.css" type="text/css" media="screen" />'."\n";   
204     } else {
205         echo '<link rel="stylesheet" href="'.WP_PLUGIN_URL.'/wp-polls/polls-css.css" type="text/css" media="screen" />'."\n";   
206     }
207     echo '<style type="text/css">'."\n";   
208     if($pollbar['style'] == 'use_css') {
209         echo '.wp-polls .pollbar {'."\n";
210         echo "\t".'margin: 1px;'."\n";
211         echo "\t".'font-size: '.($pollbar['height']-2).'px;'."\n";
212         echo "\t".'line-height: '.$pollbar['height'].'px;'."\n";
213         echo "\t".'height: '.$pollbar['height'].'px;'."\n";
214         echo "\t".'background: #'.$pollbar['background'].';'."\n";
215         echo "\t".'border: 1px solid #'.$pollbar['border'].';'."\n";
216         echo '}'."\n";
217     } else {
218         echo '.wp-polls .pollbar {'."\n";
219         echo "\t".'margin: 1px;'."\n";
220         echo "\t".'font-size: '.($pollbar['height']-2).'px;'."\n";
221         echo "\t".'line-height: '.$pollbar['height'].'px;'."\n";
222         echo "\t".'height: '.$pollbar['height'].'px;'."\n";
223         echo "\t".'background-image: url(\''.WP_PLUGIN_URL.'/wp-polls/images/'.$pollbar['style'].'/pollbg.gif\');'."\n";
224         echo "\t".'border: 1px solid #'.$pollbar['border'].';'."\n";
225         echo '}'."\n";
226     }
227     echo '</style>'."\n";
228     echo '<!-- End Of Script Generated By WP-Polls 2.31 -->'."\n";
229 }
230
231
232 ### Function: Displays Polls Header In WP-Admin
233 add_action('admin_head', 'poll_header_admin');
234 function poll_header_admin() {
235     wp_register_script('wp-polls-admin', WP_PLUGIN_URL.'/wp-polls/polls-admin-js-packed.js', false, '2.31');
236     echo "\n".'<!-- Start Of Script Generated By WP-Polls 2.31 -->'."\n";
237     echo '<script type="text/javascript">'."\n";
238     echo '/* <![CDATA[ */'."\n";
239     echo "\t".'var polls_admin_ajax_url = \''.WP_PLUGIN_URL.'/wp-polls/polls-admin-ajax.php'."';\n";
240     echo "\t".'var polls_admin_text_delete_poll = \''.js_escape(__('Delete Poll', 'wp-polls')).'\';'."\n";
241     echo "\t".'var polls_admin_text_no_poll_logs = \''.js_escape(__('No poll logs available.', 'wp-polls')).'\';'."\n";
242     echo "\t".'var polls_admin_text_delete_all_logs = \''.js_escape(__('Delete All Logs', 'wp-polls')).'\';'."\n";
243     echo "\t".'var polls_admin_text_checkbox_delete_all_logs = \''.js_escape(__('Please check the \\\'Yes\\\' checkbox if you want to delete all logs.', 'wp-polls')).'\';'."\n";
244     echo "\t".'var polls_admin_text_delete_poll_logs = \''.js_escape(__('Delete Logs For This Poll Only', 'wp-polls')).'\';'."\n";
245     echo "\t".'var polls_admin_text_checkbox_delete_poll_logs = \''.js_escape(__('Please check the \\\'Yes\\\' checkbox if you want to delete all logs for this poll ONLY.', 'wp-polls')).'\';'."\n";
246     echo "\t".'var polls_admin_text_delete_poll_ans = \''.js_escape(__('Delete Poll Answer', 'wp-polls')).'\';'."\n";
247     echo "\t".'var polls_admin_text_open_poll = \''.js_escape(__('Open Poll', 'wp-polls')).'\';'."\n";
248     echo "\t".'var polls_admin_text_close_poll = \''.js_escape(__('Close Poll', 'wp-polls')).'\';'."\n";
249     echo "\t".'var polls_admin_text_enter_poll_id = \''.js_escape(__('Enter Poll ID', 'wp-polls')).'\';'."\n";
250     echo "\t".'var polls_admin_text_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";
251     echo '/* ]]> */'."\n";
252     echo '</script>'."\n";
253     wp_print_scripts(array('sack', 'wp-polls-admin'));
254     echo '<link rel="stylesheet" href="'.WP_PLUGIN_URL.'/wp-polls/polls-css.css" type="text/css" media="screen" />'."\n";
255     echo '<!-- End Of Script Generated By WP-Polls 2.31 -->'."\n";
256 }
257
258
259 ### Function: Displays Polls Footer In WP-Admin
260 add_action('admin_footer', 'poll_footer_admin');
261 function poll_footer_admin() {
262     // Javascript Code Courtesy Of WP-AddQuicktag (http://bueltge.de/wp-addquicktags-de-plugin/120/)
263     echo '<script type="text/javascript">'."\n";
264     echo "\t".'if(document.getElementById("ed_toolbar")){'."\n";
265     echo "\t\t".'qt_toolbar = document.getElementById("ed_toolbar");'."\n";
266     echo "\t\t".'edButtons[edButtons.length] = new edButton("ed_poll","'.__('Poll', 'wp-polls').'", "", "","");'."\n";
267     echo "\t\t".'var qt_button = qt_toolbar.lastChild;'."\n";
268     echo "\t\t".'while (qt_button.nodeType != 1){'."\n";
269     echo "\t\t\t".'qt_button = qt_button.previousSibling;'."\n";
270     echo "\t\t".'}'."\n";
271     echo "\t\t".'qt_button = qt_button.cloneNode(true);'."\n";
272     echo "\t\t".'qt_button.value = "'.__('Poll', 'wp-polls').'";'."\n";
273     echo "\t\t".'qt_button.title = "'.__('Insert Poll', 'wp-polls').'";'."\n";
274     echo "\t\t".'qt_button.onclick = function () { insertPoll(\'code\', edCanvas);}'."\n";
275     echo "\t\t".'qt_button.id = "ed_poll";'."\n";
276     echo "\t\t".'qt_toolbar.appendChild(qt_button);'."\n";
277     echo "\t".'}'."\n";
278     echo '</script>'."\n";
279 }
280
281
282 ### Function: Add Quick Tag For Poll In TinyMCE >= WordPress 2.5
283 add_action('init', 'poll_tinymce_addbuttons');
284 function poll_tinymce_addbuttons() {
285     if(!current_user_can('edit_posts') && ! current_user_can('edit_pages')) {
286         return;
287     }
288     if(get_user_option('rich_editing') == 'true') {
289         add_filter("mce_external_plugins", "poll_tinymce_addplugin");
290         add_filter('mce_buttons', 'poll_tinymce_registerbutton');
291     }
292 }
293 function poll_tinymce_registerbutton($buttons) {
294     array_push($buttons, 'separator', 'polls');
295     return $buttons;
296 }
297 function poll_tinymce_addplugin($plugin_array) {
298     $plugin_array['polls'] = WP_PLUGIN_URL.'/wp-polls/tinymce/plugins/polls/editor_plugin.js';
299     return $plugin_array;
300 }
301
302
303 ### Function: Check Who Is Allow To Vote
304 function check_allowtovote() {
305     global $user_ID;
306     $user_ID = intval($user_ID);
307     $allow_to_vote = intval(get_option('poll_allowtovote'));
308     switch($allow_to_vote) {
309         // Guests Only
310         case 0:
311             if($user_ID > 0) {
312                 return false;
313             }
314             return true;
315             break;
316         // Registered Users Only
317         case 1:
318             if($user_ID == 0) {
319                 return false;
320             }
321             return true;
322             break;
323         // Registered Users And Guests
324         case 2:
325         default:
326             return true;
327     }
328 }
329
330
331 ### Funcrion: Check Voted By Cookie Or IP
332 function check_voted($poll_id) {
333     $poll_logging_method = intval(get_option('poll_logging_method'));
334     switch($poll_logging_method) {
335         // Do Not Log
336         case 0:
337             return 0;
338             break;
339         // Logged By Cookie
340         case 1:
341             return check_voted_cookie($poll_id);
342             break;
343         // Logged By IP
344         case 2:
345             return check_voted_ip($poll_id);
346             break;
347         // Logged By Cookie And IP
348         case 3:
349             $check_voted_cookie = check_voted_cookie($poll_id);
350             if(!empty($check_voted_cookie)) {
351                 return $check_voted_cookie;
352             } else {
353                 return check_voted_ip($poll_id);
354             }
355             break;
356         // Logged By Username
357         case 4:
358             return check_voted_username($poll_id);
359             break;
360     }
361 }
362
363
364 ### Function: Check Voted By Cookie
365 function check_voted_cookie($poll_id) {
366     if(!empty($_COOKIE["voted_$poll_id"])) {
367         $get_voted_aids = explode(',', $_COOKIE["voted_$poll_id"]);
368     } else {
369         $get_voted_aids = 0;
370     }
371     return $get_voted_aids;
372 }
373
374
375 ### Function: Check Voted By IP
376 function check_voted_ip($poll_id) {
377     global $wpdb;
378     $log_expiry = intval(get_option('poll_cookielog_expiry'));
379     $log_expiry_sql = '';
380     if($log_expiry > 0) {
381         $log_expiry_sql = 'AND ('.current_time('timestamp').'-(pollip_timestamp+0)) < '.$log_expiry;
382     }
383     // Check IP From IP Logging Database
384     $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");
385     if($get_voted_aids) {
386         return $get_voted_aids;
387     } else {
388         return 0;
389     }
390 }
391
392
393 ### Function: Check Voted By Username
394 function check_voted_username($poll_id) {
395     global $wpdb, $user_ID;
396     // Check IP If User Is Guest
397     if ($user_ID == 0) {
398         return check_voted_ip($poll_id);
399     }
400     $pollsip_userid = intval($user_ID);
401     $log_expiry = intval(get_option('poll_cookielog_expiry'));
402     $log_expiry_sql = '';
403     if($log_expiry > 0) {
404         $log_expiry_sql = 'AND ('.current_time('timestamp').'-(pollip_timestamp+0)) < '.$log_expiry;
405     }
406     // Check User ID From IP Logging Database
407     $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");
408     if($get_voted_aids) {
409         return $get_voted_aids;
410     } else {
411         return 0;
412     }
413 }
414
415
416 ### Function: Display Voting Form
417 function display_pollvote($poll_id, $without_poll_title = false) {
418     global $wpdb;
419     // Temp Poll Result
420     $temp_pollvote = '';
421     // Get Poll Question Data
422     $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");
423     // Poll Question Variables
424     $poll_question_text = stripslashes($poll_question->pollq_question);
425     $poll_question_id = intval($poll_question->pollq_id);
426     $poll_question_totalvotes = intval($poll_question->pollq_totalvotes);
427     $poll_question_totalvoters = intval($poll_question->pollq_totalvoters);
428     $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));
429     $poll_expiry = trim($poll_question->pollq_expiry);
430     if(empty($poll_expiry)) {
431         $poll_end_date  = __('No Expiry', 'wp-polls');
432     } else {
433         $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));
434     }
435     $poll_multiple_ans = intval($poll_question->pollq_multiple);
436     $template_question = stripslashes(get_option('poll_template_voteheader'));
437     $template_question = str_replace("%POLL_QUESTION%", $poll_question_text, $template_question);
438     $template_question = str_replace("%POLL_ID%", $poll_question_id, $template_question);
439     $template_question = str_replace("%POLL_TOTALVOTES%", $poll_question_totalvotes, $template_question);
440     $template_question = str_replace("%POLL_TOTALVOTERS%", $poll_question_totalvoters, $template_question);
441     $template_question = str_replace("%POLL_START_DATE%", $poll_start_date, $template_question);
442     $template_question = str_replace("%POLL_END_DATE%", $poll_end_date, $template_question);
443     if($poll_multiple_ans > 0) {
444         $template_question = str_replace("%POLL_MULTIPLE_ANS_MAX%", $poll_multiple_ans, $template_question);
445     } else {
446         $template_question = str_replace(