Changeset 1309

Show
Ignore:
Timestamp:
08/27/08 07:25:01 (3 months ago)
Author:
_ck_
Message:

0.5.5 serious ajax bug with data loss fixed, admin functions externalized

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • bbpress-polls/trunk/bb-polls.php

    r952 r1309  
    66Author: _ck_ 
    77Author URI: http://bbShowcase.org 
    8 Version: 0.5.4 
     8Version: 0.5.5 
    99 
    1010License: CC-GNU-GPL http://creativecommons.org/licenses/GPL/2.0/ 
     
    2525 
    2626add_action( 'bb_send_headers', 'bb_polls_initialize');  // bb_init 
    27 add_action( 'bb_admin-header.php','bb_polls_process_post'); 
    28 add_action( 'bb_admin_menu_generator', 'bb_polls_add_admin_page' ); 
    29  
    30 function bb_polls_add_admin_page() {bb_admin_add_submenu(__('bbPress Polls'), 'administrate', 'bb_polls_admin_page');} 
    31  
    32 function bb_polls_initialize() { 
     27 
     28function bb_polls_initialize() {         
    3329        global $bb_polls, $bb_polls_type, $bb_polls_label; 
    34         if (!isset($bb_polls)) {$bb_polls = bb_get_option('bb_polls'); 
    35                 if (!$bb_polls) { 
    36  
    37         $bb_polls['minimum_view_level']="read";   // who can view polls = read / participate / moderate / administrate  (watchout for typos) 
    38         $bb_polls['minimum_vote_level']="participate";   // who can vote on polls = participate / moderate / administrate  (watchout for typos) 
    39         $bb_polls['minimum_add_level']="participate";   // who can add polls = participate / moderate / administrate  (watchout for typos) 
    40         $bb_polls['minimum_edit_level']="administrate";   // who can edit polls = participate / moderate / administrate  (watchout for typos) 
    41         $bb_polls['minimum_delete_level']="administrate";   // who can edit polls = participate / moderate / administrate  (watchout for typos) 
    42  
    43         $bb_polls['only_topic_author_can_add']=true;   // false=anyone can add a poll to any topic /  true=only the topic starter (admin can always add) 
    44         $bb_polls['show_poll_on_which_pages']="both";    // show poll only on pages = first / last / both / all 
    45                  
    46         $bb_polls['add_within_hours']=3;   // how many hours later can a poll be added  (for users/moderator - admin can always add) 
    47         $bb_polls['edit_within_hours']=12;   // how many hours later can poll be edited (for users/moderator - admin can always edit)            
    48  
    49         $bb_polls['close_with_topic']=true;   // if topic is closed, is poll closed?                                            // doesn't work yet 
    50         $bb_polls['close_after_days']=365;   // if not closed with topic, close after how many days?                            // doesn't work yet 
    51  
    52         $bb_polls['max_options']=9;     // default number of poll answer slots offered  
    53         $bb_polls['max_length']=100;     // how long can the poll question & answers be 
    54         $bb_polls['options_sort']=false;         // true=show options by most votes, false=show options in original order 
    55          
    56         $bb_polls['use_ajax']=true;             // true = enables ajax-ish behaviours, still works without javascript  / false = typical page refreshes 
    57         $bb_polls['test_mode']=false;   // if set to "true" allows multiple votes per person for testing purposes only 
    58  
    59         $bb_polls['style']= 
    60         "#bb_polls {list-style: none; width:400px; line-height:120%; margin:5px 0; padding:5px; border:1px solid #ADADAD;  font-size:90%; color:#000; background:#eee; } 
    61         #bb_polls .submit {cursor: pointer; cursor: hand; text-align:center; padding:2px 5px;} 
    62         #bb_polls .nowrap {white-space:nowrap;} 
    63         #bb_polls p {margin:15px 0;padding:0;} 
    64         #bb_polls .poll_question, #bb_polls .poll_footer {font-weight:bold; text-align:center; color:#2E6E15;} 
    65         #bb_polls .poll_label {font-weight:bold; margin:1em 0 1em 1em;}                                                          
    66         #bb_polls .poll_option {margin:2px 0 -2px 0; text-align:center;font-weight:bold; font-size:9px; line-height:5px; padding:2px 1px;  border:1px solid #303030; color:#fff; } 
    67         #bb_polls .poll_option1 {background:red;} 
    68         #bb_polls .poll_option2 {background:green;} 
    69         #bb_polls .poll_option3 {background:blue;} 
    70         #bb_polls .poll_option4 {background:orange;} 
    71         #bb_polls .poll_option5 {background:purple;} 
    72         #bb_polls .poll_option6 {background:pink;} 
    73         #bb_polls .poll_option7 {background:yellow;} 
    74         #bb_polls .poll_option8 {background:navy;} 
    75         #bb_polls .poll_option9 {background:grey;} 
    76         ";                       
    77                                          
    78         $bb_polls['poll_question']=__("Would you like to add a poll to this topic for members to vote on?"); 
    79         $bb_polls['poll_instructions']=__("You may add a poll question with options for members to vote from."); 
    80         $bb_polls['label_single']=__("you can vote on <u>ONE</u> choice"); 
    81         $bb_polls['label_multiple']=__("you can vote on <u>MULTIPLE</u> choices"); 
    82         $bb_polls['label_poll_text']=__("poll");    // default "poll" = text to show if on topic title if it has a poll (delete text to disable) // you can even use HTML/CSS 
    83         $bb_polls['label_votes_text']=__("votes");  // default "votes" = text to show for votes 
    84         $bb_polls['label_vote_text']=__("Vote");  // default "VOTE" = text to show for VOTE button       
    85         $bb_polls['label_save_text']=__("Save");  // default "SAVE" = text to show for SAVE button 
    86         $bb_polls['label_cancel_text']=__("Cancel");  // default "CANCEL" = text to show for CANCEL button 
    87         $bb_polls['label_edit_text']=__("Edit");  // default "EDIT" = text to show for Edit button 
    88         $bb_polls['label_delete_text']=__("Delete");  // default "DELETE" = text to show for Delete button 
    89         $bb_polls['label_option_text']=__("option");  // default "option" = text to show for options 
    90         $bb_polls['label_question_text']=__("poll question"); 
    91         $bb_polls['label_results_text']=__("show poll results"); 
    92         $bb_polls['label_now_text']=__("vote now");      
    93         $bb_polls['label_nocheck_text']=__("You haven't selected anything!");    
    94         $bb_polls['label_warning_text']=__("This cannot be undone. Are you sure to delete?"); 
    95  
    96                 }}                                               
    97         // if (BB_IS_ADMIN) {           // doesn't exist until 1040 :-( 
    98          
    99         $bb_polls_type['minimum_view_level']="read,participate,moderate,administrate";  
    100         $bb_polls_type['minimum_vote_level']="participate,moderate,administrate"; 
    101         $bb_polls_type['minimum_add_level']="participate,moderate,administrate";   
    102         $bb_polls_type['minimum_edit_level']="participate,moderate,administrate"; 
    103         $bb_polls_type['minimum_delete_level']="participate,moderate,administrate"; 
    104          
    105         $bb_polls_type['only_topic_author_can_add']="binary"; 
    106         $bb_polls_type['show_poll_on_which_pages']="first,last,both,all";        
    107                  
    108         $bb_polls_type['add_within_hours']="1,2,6,12,24,48,72,999999"; 
    109         $bb_polls_type['edit_within_hours']="1,2,6,12,24,48,72,999999"; 
    110  
    111         $bb_polls_type['close_with_topic']="binary"; 
    112         $bb_polls_type['close_after_days']="1,2,7,30,365"; 
    113  
    114         $bb_polls_type['max_options']="3,5,9,15,20"; 
    115         $bb_polls_type['max_length']="50,100,200"; 
    116         $bb_polls_type['options_sort']="binary"; 
    117          
    118         $bb_polls_type['use_ajax']="binary"; 
    119         $bb_polls_type['test_mode']="binary"; 
    120  
    121         $bb_polls_type['style']="textarea"; 
    122                                                  
    123         $bb_polls_type['poll_question']="text"; 
    124         $bb_polls_type['poll_instructions']="text"; 
    125         $bb_polls_type['label_single']="text"; 
    126         $bb_polls_type['label_multiple']="text"; 
    127         $bb_polls_type['label_poll_text']="text"; 
    128         $bb_polls_type['label_votes_text']="text"; 
    129         $bb_polls_type['label_vote_text']="text"; 
    130         $bb_polls_type['label_save_text']="text"; 
    131         $bb_polls_type['label_cancel_text']="text"; 
    132         $bb_polls_type['label_edit_text']="text"; 
    133         $bb_polls_type['label_delete_text']="text"; 
    134         $bb_polls_type['label_option_text']="text"; 
    135         $bb_polls_type['label_question_text']="text"; 
    136         $bb_polls_type['label_results_text']="text"; 
    137         $bb_polls_type['label_now_text']="text"; 
    138         $bb_polls_type['label_nocheck_text']="text"; 
    139         $bb_polls_type['label_warning_text']="text"; 
    140          
    141          
    142         $bb_polls_label['minimum_view_level']=__("At what level can users SEE polls?"); 
    143         $bb_polls_label['minimum_vote_level']=__("At what level can users VOTE on polls?"); 
    144         $bb_polls_label['minimum_add_level']=__("At what level can users ADD a poll?"); 
    145         $bb_polls_label['minimum_edit_level']=__("At what level can users EDIT a poll?"); 
    146         $bb_polls_label['minimum_delete_level']=__("At what level can users DELETE a poll?"); 
    147  
    148         $bb_polls_label['only_topic_author_can_add']=__("Only the topic starter can add a poll?"); 
    149         $bb_polls_label['show_poll_on_which_pages']=__("Show poll only on which topic pages?"); 
    150                  
    151         $bb_polls_label['add_within_hours']=__("How many hours later can a poll be ADDED?"); 
    152         $bb_polls_label['edit_within_hours']=__("How many hours later can a poll be EDITED?"); 
    153  
    154         $bb_polls_label['close_with_topic']=__("Should polls close when a topic is closed?"); 
    155         $bb_polls_label['close_after_days']=__("If not closed with topic, after how many days?"); 
    156  
    157         $bb_polls_label['max_options']=__("How many poll question slots should be offered?"); 
    158         $bb_polls_label['max_length']=__("How many characters can the poll questions be?"); 
    159         $bb_polls_label['options_sort']=__("Sort results by number of votes?"); 
    160          
    161         $bb_polls_label['use_ajax']=__("Use AJAX-like actions if javascript enabled?"); 
    162         $bb_polls_label['test_mode']=__("Enable TEST MODE (multiple votes per person)?"); 
    163  
    164         $bb_polls_label['style']=__("Custom CSS style for polls:"); 
    165  
    166         $bb_polls_label['poll_question']=__("Question to ask to start poll:"); 
    167         $bb_polls_label['poll_instructions']=__("Instructions to add poll:"); 
    168         $bb_polls_label['label_single']=__("Label for single vote selections:"); 
    169         $bb_polls_label['label_multiple']=__("Label for multiple vote selections:"); 
    170         $bb_polls_label['label_poll_text']=__("Text to show if a topic title has a poll:"); 
    171         $bb_polls_label['label_votes_text']=__("Text to show for votes:"); 
    172         $bb_polls_label['label_vote_text']=__("Text to show for VOTE button:"); 
    173         $bb_polls_label['label_save_text']=__("Text to show for SAVE button:"); 
    174         $bb_polls_label['label_cancel_text']=__("Text to show for CANCEL button:"); 
    175         $bb_polls_label['label_edit_text']=__("Text to show for EDIT button:"); 
    176         $bb_polls_label['label_delete_text']=__("Text to show for DELETE button:"); 
    177         $bb_polls_label['label_option_text']=__("Text to show for each option:"); 
    178         $bb_polls_label['label_question_text']=__("Text to show for question label:"); 
    179         $bb_polls_label['label_results_text']=__("Text to show for results label:"); 
    180         $bb_polls_label['label_now_text']=__("Text to show for VOTE NOW label:"); 
    181         $bb_polls_label['label_nocheck_text']=__("No selection warning:"); 
    182         $bb_polls_label['label_warning_text']=__("Delete warning:"); 
    183  
    184         // }     
    185  
     30        if (!isset($bb_polls)) { 
     31                $bb_polls = bb_get_option('bb_polls'); 
     32                if (empty($bb_polls)) { 
     33                        require_once("bb-polls-admin.php"); 
     34                        bb_polls_strings(); 
     35                } 
     36        } 
    18637        bb_polls_add_header();  // add_action('bb_send_headers', 'bb_polls_add_header');         
    18738        if (is_topic()) {               // this eventually may have to be added on an admin page 
     
    251102function bb_polls_check_cache($topic_id) { 
    252103global $bb_polls,$topic,$poll_options;   
    253 if (!$topic_id || $topic_id != $topic->topic_id) {if (!isset($topic)) {bb_repermalink();} $topic_id=get_topic_id(); $topic = get_topic($topic_id);  $poll_options=(isset($topic->poll_options)==true ? $topic->poll_options : null); } 
     104if (!$topic_id || $topic_id != $topic->topic_id) {if (!isset($topic)) {bb_repermalink();} $topic_id=get_topic_id(); $topic = get_topic($topic_id);} 
     105if (isset($topic->poll_options)) {if (!isset($poll_options)) {$poll_options=$topic->poll_options;}} else {$poll_options=NULL;} 
    254106if (isset($poll_options) && !is_array($poll_options)) {$poll_options=unserialize(substr($poll_options,2));}   // trick bb_press to keep poll data unserialized 
    255107return $topic_id; 
     
    321173$output.= '<p class="poll_footer">'.intval($test).' '.$bb_polls['label_votes_text'].'</p>'; 
    322174if (isset($_GET['show_poll_results']) || (bb_get_current_user_info( 'id' ) && !bb_polls_has_voted(bb_get_current_user_info( 'id' ),$topic_id) )) { 
    323 $output.= '<p class="poll_footer">( <a onClick="if (window.bb_polls_insert_ajax) {bb_polls_show_poll_vote_form_ajax();return false;}" href="'.remove_query_arg(array('start_new_poll','edit_poll','delete_poll','show_poll_vote_form_ajax','show_poll_setup_form_ajax','bb_polls_cache')).'">'.$bb_polls['label_now_text'].'</a> )</p>'; 
     175$output.= '<p class="poll_footer">( <a onClick="if (window.bb_polls_insert_ajax) {bb_polls_show_poll_vote_form_ajax();return false;}" href="'.remove_query_arg(array('start_new_poll','show_poll_results','edit_poll','delete_poll','show_poll_vote_form_ajax','show_poll_setup_form_ajax','bb_polls_cache')).'">'.$bb_polls['label_now_text'].'</a> )</p>'; 
    324176} 
    325177$output.=bb_polls_edit_link(); 
     
    427279                bb_polls_save_poll_setup(0);                             
    428280                // header("HTTP/1.1 307 Temporary redirect"); 
    429                 wp_redirect($GLOBALS["HTTP_SERVER_VARS"]["REQUEST_URI"]);     // I *really* don't like this technique but it's the only way to clear post data? 
     281                wp_redirect($_SERVER["REQUEST_URI"]); // I *really* don't like this technique but it's the only way to clear post data? 
    430282                // exit();  // not sure why but this makes it fail? 
    431283        }        
     
    433285                bb_polls_add_vote(bb_get_current_user_info( 'id' ),'');          
    434286                // header("HTTP/1.1 307 Temporary redirect"); 
    435                 wp_redirect($GLOBALS["HTTP_SERVER_VARS"]["REQUEST_URI"]);     // I *really* don't like this technique but it's the only way to clear post data? 
     287                wp_redirect($_SERVER["REQUEST_URI"]); // I *really* don't like this technique but it's the only way to clear post data? 
    436288                // exit();  // not sure why but this makes it fail? 
    437289        } 
     
    546398}  
    547399 
    548 function bb_polls_admin_page() { 
    549         global $bb_polls, $bb_polls_type, $bb_polls_label;                       
    550         ?> 
    551                 <div style="text-align:right;margin-bottom:-1.5em;">                     
    552                         [ <a href="<?php echo add_query_arg('bb_polls_reset','1',remove_query_arg('bb_polls_recount')); ?>">Reset All Settings To Defaults</a> ]                         
    553                 </div> 
    554                  
    555                 <h2>bbPress Polls</h2> 
    556                  
    557                 <form method="post" name="bb_polls_form" id="bb_polls_form" action="<?php echo remove_query_arg(array('bb_polls_reset','bb_polls_recount')); ?>"> 
    558                 <input type="hidden" name="bb_polls" value="1" /> 
    559                         <table class="widefat"> 
    560                                 <thead> 
    561                                         <tr> <th width="33%">Option</th>        <th>Setting</th> </tr> 
    562                                 </thead> 
    563                                 <tbody> 
    564                                         <?php 
    565                                          
    566                                         foreach(array_keys( $bb_polls_type) as $key) { 
    567                                          
    568                                         // if ($key=="style") {echo "<div id='bb_polls_rollup' style='display:none;'>";} 
    569                                          
    570                                         $bb_polls[$key]=stripslashes_deep($bb_polls[$key]);                                      
    571                                         $colspan= (substr($bb_polls_type[$key],0,strpos($bb_polls_type[$key].",",","))=="array") ? "2" : "1"; 
    572                                                 ?> 
    573                                                 <tr <?php alt_class('recount'); ?>> 
    574                                                         <td nowrap colspan=<?php echo $colspan; ?>> 
    575                                                         <label for="bb_polls_<?php echo $key; ?>"> 
    576                                                         <b><?php  if ($bb_polls_label[$key])  {echo $bb_polls_label[$key];} else {echo ucwords(str_replace("_"," ",$key));} ?></b> 
    577                                                         </label> 
    578                                                         <?php 
    579                                                         if ($colspan<2) {echo "</td><td>";} else {echo "<br />";} 
    580                                                         switch (substr($bb_polls_type[$key],0,strpos($bb_polls_type[$key].",",","))) : 
    581                                                         case 'binary' : 
    582                                                                 ?><input type="radio" name="<?php echo $key;  ?>" value="1" <?php echo ($bb_polls[$key]==true ? 'checked="checked"' : ''); ?> />Yes                                                                     &nbsp;  
    583                                                                      <input type="radio" name="<?php echo $key;  ?>" value="0" <?php echo ($bb_polls[$key]==false ? 'checked="checked"' : ''); ?> />No <?php 
    584                                                         break; 
    585                                                         case 'numeric' : 
    586                                                                 ?><input type="text" maxlength=3 name="<?php echo $key;  ?>" value="<?php echo $bb_polls[$key]; ?>" /> <?php  
    587                                                         break; 
    588                                                         case 'textarea' :                                                                
    589                                                                 ?><textarea rows="9" style="width:98%" name="<?php echo $key;  ?>"><?php echo $bb_polls[$key]; ?></textarea><?php                                                        
    590                                                         break; 
    591                                                         default :  // type "input" and everything else we forgot 
    592                                                                 $values=explode(",",$bb_polls_type[$key]); 
    593                                                                 if (count($values)>2) { 
    594                                                                 echo '<select name="'.$key.'">'; 
    595                                                                 foreach ($values as $value) {echo '<option '; echo ($bb_polls[$key]== $value ? 'selected' : ''); echo '>'.$value.'</option>'; } 
    596                                                                 echo '</select>'; 
    597                                                                 } else {                                                                                                                 
    598                                                                 ?><input type="text" style="width:98%" name="<?php echo $key;  ?>" value="<?php echo $bb_polls[$key]; ?>" /> <?php  
    599                                                                 } 
    600                                                         endswitch;                                                       
    601                                                         ?> 
    602                                                         </td> 
    603                                                 </tr> 
    604                                                 <?php 
    605                                         }  
    606                                         // echo "</div>"; 
    607                                         ?> 
    608                                 </tbody> 
    609                         </table> 
    610                         <p class="submit"><input type="submit" name="submit" value="Save bbPress Polls Settings" /></p> 
    611                  
    612                 </form> 
    613                 <?php 
    614 
    615  
    616 function bb_polls_process_post() { 
    617 global $bb_polls; 
    618         if (bb_current_user_can('administrate')) { 
    619                 if (isset($_REQUEST['bb_polls_reset'])) { 
    620                         unset($bb_polls);                
    621                         bb_delete_option('bb_polls'); 
    622                         bb_polls_initialize();                   
    623                         bb_update_option('bb_polls',$bb_polls); 
    624                         bb_admin_notice('<b>bbPress Polls: '.__('All Settings Reset To Defaults.').'</b>');     // , 'error'                     
    625                         wp_redirect(remove_query_arg(array('bb_polls_reset'))); // bug workaround, page doesn't show reset settings 
    626                 }                
    627                 elseif (isset($_POST['submit']) && isset($_POST['bb_polls'])) { 
    628                                                          
    629                         foreach(array_keys( $bb_polls) as $key) { 
    630                                 if (isset($_POST[$key])) {$bb_polls[$key]=$_POST[$key];} 
    631                         } 
    632                  
    633                         bb_update_option('bb_polls',$bb_polls); 
    634                         bb_admin_notice('<b>bbPress Polls: '.__('All Settings Saved.').'</b>'); 
    635                         // unset($GLOBALS['bb_polls']); $bb_polls = bb_get_option('bb_polls'); 
    636                 } 
    637         } 
     400if ((defined('BB_IS_ADMIN') && BB_IS_ADMIN) || strpos($_SERVER['REQUEST_URI'],"/bb-admin/")!==false) { // "stub" only load functions if in admin  
     401        function bb_polls_add_admin_page() {bb_admin_add_submenu(__('Polls'), 'administrate', 'bb_polls_admin');} 
     402        add_action( 'bb_admin_menu_generator', 'bb_polls_add_admin_page' ); 
     403        if (isset($_GET['plugin']) && $_GET['plugin']=="bb_polls_admin") {require_once("bb-polls-admin.php");} // load entire core only when needed 
    638404} 
    639405 
  • bbpress-polls/trunk/readme.txt

    r949 r1309  
    5656* 0.5.3 2008-March-14 javascript payload is now kept as small as possible  
    5757* 0.5.4 2008-March-14 new poll form (and edit form) remotely loaded when in "ajax" mode 
     58* 0.5.5 2008-August-27 serious ajax bug with data loss fixed, admin functions externalized 
    5859         
    5960== To Do ==