root/worst-offenders/trunk/functions.php

Revision 44532, 8.7 kB (checked in by ear1grey, 2 months ago)

Cleaned up message describing what is about to be deleted.

Line 
1 <?php
2
3 add_action('init', 'wo3_init');
4 add_action('admin_menu', 'wo3_list_page');
5 add_action('admin_menu', 'wo3_config_page');
6
7 function wo3_init() {
8     wo3_ensureDefaults();
9     do_action('wo3_litmus_init');
10 }
11
12 function wo3_ensureDefaults() {
13     global $keys;
14
15     $n = "worst_offenders_";
16     $keys['ui_vis'] = $n."ui_visibility_threshold";
17     $keys['ui_preselect'] = $n."ui_preselect_threshold";
18     $keys['ui_list_size'] = $n."ui_size_threshold";
19     $keys['user_ban_list'] = $n."current_user_ban";
20     $keys['user_ban_list_max_size'] = $n."user_ban_list_max_size";
21     $keys['user_ban_list_size'] = $n."current_user_ban_count";
22     $keys['debug'] = $n."debug";
23     $keys['domain_cache'] = $n."domain_cache";
24     $keys['counter'] = $n."counter";
25     $keys['counter_offset'] = $n."counter_offset";
26
27     $ui_vis = get_option( $keys['ui_vis'] );
28     $ui_preselect = get_option( $keys['ui_preselect'] );
29     $ui_list_size = get_option( $keys['ui_list_size'] );
30     $ban_list = get_option( $keys['ban_list'] );
31     $user_ban_list = get_option( $keys['user_ban_list'] );
32     $ban_list_size = get_option( $keys['ban_list_size'] );
33     $user_ban_list_size = get_option( $keys['user_ban_list_size'] );
34     $user_ban_list_max_size = get_option( $keys['user_ban_list_max_size'] );
35     $counter = get_option( $keys['counter'] );
36     $counter_offset = get_option( $keys['$counter_offset'] );
37
38     if ($ui_vis=="") update_option( $keys['ui_vis'], 2);
39     if ($ui_preselect=="") update_option( $keys['ui_preselect'], 4);
40     if ($ui_list_size=="") update_option($keys['ui_list_size'], 20);
41     // if (!is_array($ban_list)) wo3_reset_user_ban( );
42     if ($user_ban_list_max_size=="") update_option($keys['user_ban_list_max_size'], 800);
43     if ($counter=="") update_option( $keys['counter'], 0);
44     
45     // if there's no offset, then set it to the current akismet spam count
46     if ($counter_offset=="") {
47         $count = get_option( 'akismet_spam_count' );
48         $count = ($count == "" ? 0 : $count);
49         update_option( $keys['counter_offset'], $count);
50     }
51     
52 }
53
54 //add_action('akismet_tabs', 'wo3_akismet_tab');
55
56 function wo3_akismet_tab() {   
57     global $wo3_title, $keys;
58     $active = $_GET['ctype'] === "worstoffenders";
59     $count = get_current_count();
60     $extra = ($active ? ' class="active"' : '');
61
62     // draw the tab always
63     echo "<li $extra><a href='edit-comments.php?page=akismet-admin&amp;ctype=worstoffenders'>Worst ($count)</a></li>";
64     // but draw the content only when selected
65     if ($active) {
66         ?>
67         <ul class="commentlist" style="list-style:none;margin:0;padding:0;">
68             <li>
69                 <p>Use Worst Offenders to remove <?php echo $count; ?> of spam comments immediately.</p>
70                 <p><a href='edit-comments.php?page=<?php echo $wo3_title; ?>'>Worst Offenders</a> has so far removed <?php    _e(get_option( $keys['counter'] )); ?> messages.</p>
71             </li>
72         </ul>
73         <?php
74     }
75 }
76
77 function wo3_config_page() {
78     global $wpdb,    $wo3_title;
79     if ( function_exists('add_submenu_page') )
80         add_submenu_page('plugins.php',    __($wo3_title.'    Config'),    __($wo3_title.'    Config'),    1, __($wo3_title), 'wo3_conf');
81 }
82
83 function wo3_conf()    {
84     if ( isset($_POST['submit']) ) {
85         echo '<div class="wrap">';
86         do_action( "wo3_add_index" );
87         echo '<p>OK</p></div>';
88     }
89 ?><title></title>
90
91
92 <div class="wrap">
93 <h2><?php    _e($wo3_title.'    Configuration'); ?></h2>
94 <form    action=""    method="post"    id="wo-conf" >
95 <h3><label for="key"><?php _e('Indexes'); ?></label></h3>
96 <!-- <p><input    id="key" name="key"    type="text"    size="15"    maxlength="12" value="<?php    echo get_option('wo-thing'); ?>"/> </p> -->
97 <p><input class="button" type="submit" name="submit" value="<?php    _e('Add / Update Indexes &raquo;');    ?>"    /></p>
98 </form>
99
100
101 </div>
102 <?php
103 }
104
105
106
107
108 function wo3_list_page() {
109     global $wpdb,    $wo3_title,    $submenu;
110     $count = get_current_count();
111
112     wp_enqueue_script('jquery');
113
114     wo3_check_user_input();
115     
116     if ( isset(    $submenu['edit-comments.php']    )    )
117         add_submenu_page('edit-comments.php',    __($wo3_title),    __($wo3_title . "(" . $count . ")"),    1, __($wo3_title), 'wo3_list'    );
118     elseif ( function_exists('add_management_page')    )
119         add_management_page(__($wo3_title),    __($wo3_title."x"),    1, __($wo3_title), 'wo3_list');
120 }
121
122 function css() { ?>
123     <style type="text/css">
124     .wo3_tabs    {
125         list-style:    none;
126         margin:    0;
127         padding: 0;
128         clear: both;
129         border-bottom: 1px solid #ccc;
130         height:    31px;
131         margin-bottom: 20px;
132         background:    #ddd;
133         border-top:    1px    solid    #bdbdbd;
134     }
135     .wo3_tabs    li {
136         float: left;
137         margin:    5px    0    0    20px;
138     }
139     .wo3_tabs    a    {
140         display: block;
141         padding: 4px .5em    3px;
142         border-bottom: none;
143         color: #036;
144     }
145     .wo3_tabs    .active    a    {
146         background:    #fff;
147         border:    1px    solid    #ccc;
148         border-bottom: none;
149         color: #000;
150         font-weight: bold;
151         padding-bottom:    4px;
152     }
153
154     .wo3_count {
155         color: #c30;
156     }
157
158     span.spam{
159     position:relative;
160     z-index:24;
161     background-color:#fff;
162     color:#000;
163     text-decoration:none;
164     }
165
166     span.spam:hover{
167         z-index:25;
168         background-color:#EEE;
169         }
170
171     span.spam span{
172         display: none;
173         }
174
175     span.spam:hover span{
176     display:block;
177     position:absolute;
178     top:1em;
179     left:1em;
180     width:20em;
181     border:1px solid #666;
182     background-color:#FFF;
183     color:#333;
184     margin:0em;
185     padding:1ex 2ex;
186     }
187
188     </style>
189
190     <script type="text/javascript">
191     //<![CDATA[
192         function wibble(id) {
193           var elem = document.getElementById(id);
194             while ( elem.hasChildNodes() ) {
195                     elem.removeChild(elem.firstChild);
196             }
197             var para = document.createElement('p');
198             var txt = document.createTextNode('wibble');
199             para.appendChild(txt);
200             elem.appendChild(para);
201         }
202        
203         $j=jQuery.noConflict();
204
205         $j(document).ready(function(){
206             $('.spam').css('opacity', '0.5');
207         });
208
209        
210     //]]>
211 </script>
212
213 <?php
214 }
215
216 function reset_current_count() {
217     global $keys, $wo3_title;
218     wp_cache_delete("current_count", $wo3_title);
219     wp_cache_delete("current_ids", $wo3_title);
220 }
221
222 function get_current_count() {
223     global $wo3_title;
224     $result = wp_cache_get("current_count", $wo3_title);
225     if ($result === false) {
226         do_action('wo3_prep');
227         $result = wp_cache_get("current_count", $wo3_title);
228     }
229     return $result;
230 }
231
232
233 function wo3_list()    {
234     global $wpdb,    $comment,    $wo3_title, $wo3_feedback;
235     css();
236     ?>
237     <div class='wrap'>
238     <h2><?php    _e($wo3_title);    ?></h2>
239     <?php
240     if (is_array($wo3_feedback)) {
241         foreach($wo3_feedback as $feedback) {
242             echo $feedback;
243         }
244         $wo3_feedback = array();
245     }
246     wo3_tabs();
247     echo("</div>");
248 }
249
250
251 function do_delete($comma_separated_id_list) {
252     global $wpdb, $keys;
253     $info["banned"] = wo3_prepare_ban_ip_addresses($comma_separated_id_list);
254     $query = "delete from wp_comments where comment_ID in ($comma_separated_id_list)";
255     $result = $wpdb->query($query);
256     $info["deleted"] = $result;
257     return $info;
258 }
259
260
261 function wo3_prepare_ban_ip_addresses($comma_separated_id_list) {
262     global $wpdb, $keys, $wo3_feedback;
263     $query = "select distinct comment_author_IP from wp_comments where comment_ID in ($comma_separated_id_list)";
264     $results = $wpdb->get_results($query);
265     $ban=0;
266     foreach($results as $result) {
267         do_action("ban_ip_address", $result->comment_author_IP);
268         $ban++;
269     }
270     return $ban;   
271 }
272
273
274 function wo3_tabs()    {
275     global $wo3_title;
276     ?>
277
278 <ul    class="wo3_tabs">
279     <?php
280     do_action( "wo3_tabs" );
281     ?>
282 </ul>
283
284 <script type="text/javascript">
285     function toggleCheck(me) {
286         var new_value = me.checked;
287         for(i=0; i<me.form.length;i++){
288           if(me.form[i].type == 'checkbox') {
289                 me.form[i].checked = new_value;
290             }
291         }
292     }
293 </script>
294
295 <form    method="post"    action="">
296     <input type="submit" class="button delete" name="submit" value="<?php    _e('Delete selected'); ?>" />
297     <?php    do_action( "wo3_content" );    ?>
298     <input type="hidden" name="act"    value="delete" />
299     <input type="submit" class="button delete" name="submit" value="<?php    _e('Delete selected'); ?>" />
300 </form>
301 <?php
302 }
303
304 function wo3_check_user_input()    {
305     global $keys, $wo3_feedback;
306     if ($_POST['act']    == 'delete') {
307         if (!empty($_POST['worst'])) {
308             $deletionList    =    implode(",", $_POST['worst']);
309             $info = do_delete($deletionList);
310             if ($info["deleted"] === false)    {
311                 $wo3_feedback[] = "<div class='updated'>DB    Error    -    Failed to    delete - Boo!     Perhaps the DB    log    can    help!</div>";
312             }    else {
313                 $wo3_feedback[] = "<div class='updated'>Congratulations!  You just ignored <strong>".$info["deleted"]."</strong> servings of spam, from <strong>".$info["banned"]."</strong> worst offenders.</div>";
314                 update_option( $keys['counter'], $deleted + get_option( $keys['counter'] ) );
315                 reset_current_count();
316             }
317         } else {
318             $wo3_feedback[] = "<div class='updated'>Nothing selected for deletion.</div>";
319         }
320     }
321
322 }
323
324
325 function wo3_dashboard() {
326     global $keys, $wo3_title;
327     $count = get_current_count();
328     $link = "edit-comments.php?page=$wo3_title";
329         
330     if ($count > 0) {
331         $msg = "Worst Offenders has identified <a href='$link'>$count spam messages that you can delete immediately</a>.";
332     } else {
333         $msg = "The bad guys got squished.  No <a href='$link'>Worst Offenders</a> in your spam queue!";
334     }
335     echo "<p class='right-now'>$msg</p>\n";
336
337 }
338
339 add_action('rightnow_end', 'wo3_dashboard');
340
Note: See TracBrowser for help on using the browser.