PluginProbe
Bulk Delete / 0.7
Bulk Delete v0.7
6.12 trunk 0.2 0.3 0.4 0.6 0.7 0.8 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.2.1 2.2.2 3.0 3.1 All 64 releases
bulk-delete / bulk-delete.php

bulk-delete.php in Bulk Delete 0.7, at bulk-delete.php

592 lines 24.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Bulk Delete
4 Plugin Script: bulk-delete.php
5 Plugin URI: http://sudarmuthu.com/wordpress/bulk-delete
6 Description: Bulk delete posts from selected categories or tags. Use it with caution.
7 Version: 0.7
8 License: GPL
9 Author: Sudar
10 Author URI: http://sudarmuthu.com/
11 Text Domain: bulk-delete
12
13 === RELEASE NOTES ===
14 2009-02-02 - v0.1 - first version
15 2009-02-03 - v0.2 - Second release - Fixed issues with pagging
16 2009-04-05 - v0.3 - Third release - Prevented drafts from deleted when only posts are selected
17 2009-07-05 - v0.4 - Fourth release - Added option to delete by date.
18 2009-07-21 - v0.5 - Fifth release - Added option to delete all pending posts.
19 2009-07-22 - v0.6 - Sixth release - Added option to delete all scheduled posts.
20 2010-02-21 - v0.7 - Added an option to delete posts directly or send them to trash and support for translation.
21
22 /* Copyright 2009 Sudar Muthu (email : sudar@sudarmuthu.com)
23
24 This program is free software; you can redistribute it and/or modify
25 it under the terms of the GNU General Public License, version 2, as
26 published by the Free Software Foundation.
27
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, write to the Free Software
35 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 */
37
38 /**
39 * Request Handler
40 */
41
42 if (!function_exists('smbd_request_handler')) {
43 function smbd_request_handler() {
44 global $wpdb;
45
46 if (isset($_POST['smbd_action'])) {
47
48 $wp_query = new WP_Query;
49 check_admin_referer( 'bulk-delete-posts');
50
51 switch($_POST['smbd_action']) {
52
53 case "bulk-delete-cats":
54 // delete by cats
55 $selected_cats = $_POST['smbd_cats'];
56 if ($_POST['smbd_cats_restrict'] == "true") {
57
58 add_option('cats_op', $_POST['smbd_cats_op']);
59 add_option('cats_days', $_POST['smbd_cats_days']);
60
61 add_filter ('posts_where', 'cats_by_days');
62 }
63 $options = array('category__in'=>$selected_cats,'post_status'=>'publish', 'post_type'=>'post');
64
65 $limit_to = absint($_POST['smbd_cats_limits_to']);
66
67 if ($limit_to > 0) {
68 $options['showposts'] = $limit_to;
69 } else {
70 $options['nopaging'] = 'true';
71 }
72
73 $force_delete = $_POST['smbd_cats_force_delete'];
74
75 if ($force_delete == 'true') {
76 $force_delete = true;
77 } else {
78 $force_delete = false;
79 }
80
81 $posts = $wp_query->query($options);
82 foreach ($posts as $post) {
83 wp_delete_post($post->ID, $force_delete);
84 }
85
86 break;
87
88 case "bulk-delete-tags":
89 // delete by tags
90 $selected_tags = $_POST['smbd_tags'];
91 if ($_POST['smbd_tags_restrict'] == "true") {
92
93 add_option('tags_op', $_POST['smbd_tags_op']);
94 add_option('tags_days', $_POST['smbd_tags_days']);
95
96 add_filter ('posts_where', 'tags_by_days');
97 }
98 $options = array('tag__in'=>$selected_tags,'post_status'=>'publish', 'post_type'=>'post');
99
100 $limit_to = absint($_POST['smbd_tags_limits_to']);
101
102 if ($limit_to > 0) {
103 $options['showposts'] = $limit_to;
104 } else {
105 $options['nopaging'] = 'true';
106 }
107
108 $force_delete = $_POST['smbd_tags_force_delete'];
109
110 if ($force_delete == 'true') {
111 $force_delete = true;
112 } else {
113 $force_delete = false;
114 }
115
116 $posts = $wp_query->query($options);
117
118 foreach ($posts as $post) {
119 wp_delete_post($post->ID, $force_delete);
120 }
121
122 break;
123
124 case "bulk-delete-special":
125 $options = array();
126
127 $limit_to = absint($_POST['smbd_cats_limits_to']);
128
129 if ($limit_to > 0) {
130 $options['showposts'] = $limit_to;
131 } else {
132 $options['nopaging'] = 'true';
133 }
134
135 $force_delete = $_POST['smbd_special_force_delete'];
136 if ($force_delete == 'true') {
137 $force_delete = true;
138 } else {
139 $force_delete = false;
140 }
141
142 // Drafts
143 if ("drafs" == $_POST['smbd_drafs']) {
144 $options['post_status'] = 'draft';
145 $drafts = $wp_query->query($options);
146
147 foreach ($drafts as $draft) {
148 wp_delete_post($draft->ID, $force_delete);
149 }
150 }
151
152 // Revisions
153 if ("revisions" == $_POST['smbd_revisions']) {
154 $revisions = $wpdb->get_results($wpdb->prepare("select * from $wpdb->posts where post_type = 'revision'"));
155
156 foreach ($revisions as $revision) {
157 wp_delete_post($revision->ID, $force_delete);
158 }
159 }
160
161 // Pending Posts
162 if ("pending" == $_POST['smbd_pending']) {
163 $pendings = $wpdb->get_results($wpdb->prepare("select * from $wpdb->posts where post_status = 'pending'"));
164
165 foreach ($pendings as $pending) {
166 wp_delete_post($pending->ID, $force_delete);
167 }
168 }
169
170 // Future Posts
171 if ("future" == $_POST['smbd_future']) {
172 $futures = $wpdb->get_results($wpdb->prepare("select * from $wpdb->posts where post_status = 'future'"));
173
174 foreach ($futures as $future) {
175 wp_delete_post($future->ID, $force_delete);
176 }
177 }
178
179 // Pages
180 if ("pages" == $_POST['smbd_pages']) {
181 $options['post_type'] = 'page';
182 $pages = $wp_query->query($options);
183
184 foreach ($pages as $page) {
185 wp_delete_post($page->ID, $force_delete);
186 }
187 }
188 break;
189 }
190
191 // hook the admin notices action
192 add_action( 'admin_notices', 'smbd_deleted_notice', 9 );
193 }
194 }
195 }
196
197 /**
198 * Show deleted notice messages
199 */
200 function smbd_deleted_notice() {
201 echo "<div class = 'updated'><p>" . __("All the selected posts have been sucessfully deleted.", 'bulk-delete') . "</p></div>";
202 }
203
204 /**
205 * Show the Admin page
206 */
207 if (!function_exists('smbd_displayOptions')) {
208 function smbd_displayOptions() {
209 global $wpdb;
210 ?>
211 <div class="updated fade" style="background:#ff0;text-align:center;color: red;"><p><strong><?php _e("WARNING: Posts deleted once cannot be retrieved back. Use with caution.", 'bulk-delete'); ?></strong></p></div>
212 <div class="wrap">
213 <?php screen_icon(); ?>
214 <h2>Bulk Delete</h2>
215
216 <h3><?php _e("Select the posts which you want to delete", 'bulk-delete'); ?></h3>
217
218 <form name="smbd_form" id = "smbd_misc_form"
219 action="<?php echo get_bloginfo("wpurl"); ?>/wp-admin/options-general.php?page=bulk-delete.php" method="post"
220 onsubmit="return bd_validateForm(this);">
221
222 <?php
223 $wp_query = new WP_Query;
224 $drafts = $wp_query->query(array('post_status'=>'draft'));
225 $revisions = $wpdb->get_results($wpdb->prepare("select * from $wpdb->posts where post_type = 'revision'"));
226 $pending = $wpdb->get_results($wpdb->prepare("select * from $wpdb->posts where post_status = 'pending'"));
227 $future = $wpdb->get_results($wpdb->prepare("select * from $wpdb->posts where post_status = 'future'"));
228 $pages = $wp_query->query(array('post_type'=>'page'));
229 ?>
230 <fieldset class="options">
231 <table class="optiontable">
232 <tr>
233 <td scope="row" >
234 <input name="smbd_drafs" id ="smbd_drafs" value = "drafs" type = "checkbox" />
235 <label for="smbd_drafs"><?php echo _e("All Drafts", 'bulk-delete'); ?> (<?php echo count($drafts) . " "; _e("Drafts", 'bulk-delete'); ?>)</label>
236 </td>
237 </tr>
238 <tr>
239 <td>
240 <input name="smbd_revisions" id ="smbd_revisions" value = "revisions" type = "checkbox" />
241 <label for="smbd_revisions"><?php echo _e("All Revisions", 'bulk-delete'); ?> (<?php echo count($revisions) . " "; _e("Revisons", 'bulk-delete'); ?>)</label>
242 </td>
243 </tr>
244 <tr>
245 <td>
246 <input name="smbd_pending" id ="smbd_pending" value = "pending" type = "checkbox" />
247 <label for="smbd_pending"><?php echo _e("All Pending posts", 'bulk-delete'); ?> (<?php echo count($pending) . " "; _e("Posts", 'bulk-delete'); ?>)</label>
248 </td>
249 </tr>
250 <tr>
251 <td>
252 <input name="smbd_future" id ="smbd_future" value = "future" type = "checkbox" />
253 <label for="smbd_future"><?php echo _e("All scheduled posts", 'bulk-delete'); ?> (<?php echo count($future) . " "; _e("Posts", 'bulk-delete'); ?>)</label>
254 </td>
255 </tr>
256 <tr>
257 <td>
258 <input name="smbd_pages" value = "pages" type = "checkbox" />
259 <label for="smbd_pages"><?php echo _e("All Pages", 'bulk-delete'); ?> (<?php echo count($pages) . " "; _e("Pages", 'bulk-delete'); ?>)</label>
260 </td>
261 </tr>
262
263 <tr>
264 <td scope="row">
265 <input name="smbd_special_force_delete" value = "false" type = "radio" checked="checked" /> <?php _e('Move to Trash', 'bulk-delete'); ?>
266 <input name="smbd_special_force_delete" value = "true" type = "radio" /> <?php _e('Delete permanently', 'bulk-delete'); ?>
267 </td>
268 </tr>
269
270 <tr>
271 <td scope="row">
272 <input name="smbd_special_limit" id="smbd_special_limit" value = "true" type = "checkbox" onclick="toggle_limit_restrict('special');" />
273 <?php _e("Only delete first ", 'bulk-delete');?>
274 <input type ="textbox" name="smbd_special_limit_to" id="smbd_special_limit_to" disabled value ="0" maxlength="4" size="4" /><?php _e("posts.", 'bulk-delete');?>
275 <?php _e("Use this option if there are more than 1000 posts and the script timesout.", 'bulk-delete') ?>
276 </td>
277 </tr>
278
279 </table>
280 </fieldset>
281 <p class="submit">
282 <input type="submit" name="submit" value="<?php _e("Bulk Delete ", 'bulk-delete') ?>&raquo;">
283 </p>
284
285 <?php wp_nonce_field('bulk-delete-posts'); ?>
286
287 <input type="hidden" name="smbd_action" value="bulk-delete-special" />
288 </form>
289
290 <h3><?php _e("By Category", 'bulk-delete'); ?></h3>
291 <h4><?php _e("Select the categories whose post you want to delete", 'bulk-delete'); ?></h4>
292
293 <form name="smbd_form" id = "smbd_cat_form"
294 action="<?php echo get_bloginfo("wpurl"); ?>/wp-admin/options-general.php?page=bulk-delete.php" method="post"
295 onsubmit="return bd_validateForm(this);">
296
297 <fieldset class="options">
298 <table class="optiontable">
299 <?php
300 $categories = get_categories(array('hide_empty' => false));
301 foreach ($categories as $category) {
302 ?>
303 <tr>
304 <td scope="row" >
305 <input name="smbd_cats[]" value = "<?php echo $category->cat_ID; ?>" type = "checkbox" />
306 </td>
307 <td>
308 <label for="smbd_cats"><?php echo $category->cat_name; ?> (<?php echo $category->count . " "; _e("Posts", 'bulk-delete'); ?>)</label>
309 </td>
310 </tr>
311 <?php
312 }
313 ?>
314 <tr>
315 <td scope="row" >
316 <input name="smbd_cats_all" id ="smbd_cats_all" value = "-1" type = "checkbox" onclick="bd_checkAll(document.getElementById('smbd_cat_form'));" />
317 </td>
318 <td>
319 <label for="smbd_cats_all"><?php _e("All Categories", 'bulk-delete') ?></label>
320 </td>
321 </tr>
322 <tr>
323 <td colspan="2"></td>
324 </tr>
325
326 <tr>
327 <td scope="row">
328 <input name="smbd_cats_restrict" id="smbd_cats_restrict" value = "true" type = "checkbox" onclick="toggle_date_restrict('cats');" />
329 </td>
330 <td>
331 <?php _e("Only restrict to posts which are ", 'bulk-delete');?>
332 <select name="smbd_cats_op" id="smbd_cats_op" disabled>
333 <option value ="<"><?php _e("older than", 'bulk-delete');?></option>
334 <option value =">"><?php _e("posted within last", 'bulk-delete');?></option>
335 </select>
336 <input type ="textbox" name="smbd_cats_days" id="smbd_cats_days" disabled value ="0" maxlength="4" size="4" /><?php _e("days", 'bulk-delete');?>
337 </td>
338 </tr>
339
340 <tr>
341 <td scope="row" colspan="2">
342 <input name="smbd_cats_force_delete" value = "false" type = "radio" checked="checked" /> <?php _e('Move to Trash', 'bulk-delete'); ?>
343 <input name="smbd_cats_force_delete" value = "true" type = "radio" /> <?php _e('Delete permanently', 'bulk-delete'); ?>
344 </td>
345 </tr>
346
347 <tr>
348 <td scope="row">
349 <input name="smbd_cats_limit" id="smbd_cats_limit" value = "true" type = "checkbox" onclick="toggle_limit_restrict('cats');" />
350 </td>
351 <td>
352 <?php _e("Only delete first ", 'bulk-delete');?>
353 <input type ="textbox" name="smbd_cats_limit_to" id="smbd_cats_limit_to" disabled value ="0" maxlength="4" size="4" /><?php _e("posts.", 'bulk-delete');?>
354 <?php _e("Use this option if there are more than 1000 posts and the script timesout.", 'bulk-delete') ?>
355 </td>
356 </tr>
357
358 </table>
359 </fieldset>
360 <p class="submit">
361 <input type="submit" name="submit" value="<?php _e("Bulk Delete ", 'bulk-delete') ?>&raquo;">
362 </p>
363
364 <?php wp_nonce_field('bulk-delete-posts'); ?>
365
366 <input type="hidden" name="smbd_action" value="bulk-delete-cats" />
367 </form>
368 <?php
369 $tags = get_tags();
370 if (count($tags) > 0) {
371 ?>
372 <h3><?php _e("By Tags", 'bulk-delete'); ?></h3>
373 <h4><?php _e("Select the tags whose post you want to delete", 'bulk-delete') ?></h4>
374
375 <form name="smbd_form" id = "smbd_tag_form"
376 action="<?php echo get_bloginfo("wpurl"); ?>/wp-admin/options-general.php?page=bulk-delete.php" method="post"
377 onsubmit="return bd_validateForm(this);">
378
379 <fieldset class="options">
380 <table class="optiontable">
381 <?php
382 foreach ($tags as $tag) {
383 ?>
384 <tr>
385 <td scope="row" >
386 <input name="smbd_tags[]" value = "<?php echo $tag->term_id; ?>" type = "checkbox" />
387 </td>
388 <td>
389 <label for="smbd_tags"><?php echo $tag->name; ?> (<?php echo $tag->count . " "; _e("Posts", 'bulk-delete'); ?>)</label>
390 </td>
391 </tr>
392 <?php
393 }
394 ?>
395 <tr>
396 <td scope="row" >
397 <input name="smbd_tags_all" id ="smbd_tags_all" value = "-1" type = "checkbox" onclick="bd_checkAll(document.getElementById('smbd_tag_form'));" />
398 </td>
399 <td>
400 <label for="smbd_tags_all"><?php _e("All Tags", 'bulk-delete') ?></label>
401 </td>
402 </tr>
403
404 <tr>
405 <td colspan="2"></td>
406 </tr>
407
408 <tr>
409 <td scope="row">
410 <input name="smbd_tags_restrict" id ="smbd_tags_restrict" value = "true" type = "checkbox" onclick="toggle_date_restrict('tags');" />
411 </td>
412 <td>
413 <?php _e("Only restrict to posts which are ", 'bulk-delete');?>
414 <select name="smbd_tags_op" id="smbd_tags_op" disabled>
415 <option value ="<"><?php _e("older than", 'bulk-delete');?></option>
416 <option value =">"><?php _e("posted within last", 'bulk-delete');?></option>
417 </select>
418 <input type ="textbox" name="smbd_tags_days" id ="smbd_tags_days" value ="0" maxlength="4" size="4" disabled /><?php _e("days", 'bulk-delete');?>
419 </td>
420 </tr>
421
422 <tr>
423 <td scope="row" colspan="2">
424 <input name="smbd_tags_force_delete" value = "false" type = "radio" checked="checked" /> <?php _e('Move to Trash', 'bulk-delete'); ?>
425 <input name="smbd_tags_force_delete" value = "true" type = "radio" /> <?php _e('Delete permanently', 'bulk-delete'); ?>
426 </td>
427 </tr>
428
429 <tr>
430 <td scope="row">
431 <input name="smbd_tags_limit" id="smbd_tags_limit" value = "true" type = "checkbox" onclick="toggle_limit_restrict('tags');" />
432 </td>
433 <td>
434 <?php _e("Only delete first ", 'bulk-delete');?>
435 <input type ="textbox" name="smbd_tags_limit_to" id="smbd_tags_limit_to" disabled value ="0" maxlength="4" size="4" /><?php _e("posts.", 'bulk-delete');?>
436 <?php _e("Use this option if there are more than 1000 posts and the script timesout.", 'bulk-delete') ?>
437 </td>
438 </tr>
439
440 </table>
441 </fieldset>
442 <p class="submit">
443 <input type="submit" name="submit" value="<?php _e("Bulk Delete ", 'bulk-delete') ?>&raquo;">
444 </p>
445
446 <?php wp_nonce_field('bulk-delete-posts'); ?>
447
448 <input type="hidden" name="smbd_action" value="bulk-delete-tags" />
449 </form>
450 <?php
451 }
452 ?>
453 <p><em><?php _e("If you are looking to move posts in bulk, instead of deleting then try out my ", 'bulk-delete'); ?> <a href = "http://sudarmuthu.com/wordpress/bulk-move"><?php _e("Bulk Move Plugin", 'bulk-delete');?></a>.</em></p>
454 </div>
455 <?php
456
457 // Display credits in Footer
458 add_action( 'in_admin_footer', 'smbd_admin_footer' );
459 }
460 }
461
462 /**
463 * Print JavaScript
464 */
465 function smbd_print_scripts() {
466 ?>
467 <script type="text/javascript">
468
469 /**
470 * Check All Checkboxes
471 */
472 function bd_checkAll(form) {
473 for (i = 0, n = form.elements.length; i < n; i++) {
474 if(form.elements[i].type == "checkbox" && !(form.elements[i].getAttribute('onclick',2))) {
475 if(form.elements[i].checked == true)
476 form.elements[i].checked = false;
477 else
478 form.elements[i].checked = true;
479 }
480 }
481 }
482
483 function toggle_date_restrict(el) {
484 if (jQuery("#smbd_" + el + "_restrict").is(":checked")) {
485 jQuery("#smbd_" + el + "_op").removeAttr('disabled');
486 jQuery("#smbd_" + el + "_days").removeAttr('disabled');
487 } else {
488 jQuery("#smbd_" + el + "_op").attr('disabled', 'true');
489 jQuery("#smbd_" + el + "_days").attr('disabled', 'true');
490 }
491 }
492
493 function toggle_limit_restrict(el) {
494 if (jQuery("#smbd_" + el + "_limit").is(":checked")) {
495 jQuery("#smbd_" + el + "_limit_to").removeAttr('disabled');
496 } else {
497 jQuery("#smbd_" + el + "_limit_to").attr('disabled', 'true');
498 }
499 }
500 /**
501 * Validate Form
502 */
503 function bd_validateForm(form) {
504 var valid = false;
505 for (i = 0, n = form.elements.length; i < n; i++) {
506 if(form.elements[i].type == "checkbox" && !(form.elements[i].getAttribute('onclick',2))) {
507 if(form.elements[i].checked == true) {
508 valid = true;
509 break;
510 }
511 }
512 }
513
514 if (valid) {
515 return confirm("<?php _e('Are you sure you want to delete all the selected posts', 'bulk-delete'); ?>");
516 } else {
517 alert ("<?php _e('Please select atleast one', 'bulk-delete'); ?>");
518 return false;
519 }
520 }
521 </script>
522 <?php
523 }
524
525 /**
526 * function to filter posts by days
527 * @param <type> $where
528 * @return <type>
529 */
530 function cats_by_days ($where = '') {
531 $cats_op = get_option('cats_op');
532 $cats_days = get_option('cats_days');
533 remove_filter('posts_where', 'cats_by_days');
534
535 $where .= " AND post_date $cats_op '" . date('y-m-d', strtotime("-$cats_days days")) . "'";
536 return $where;
537 }
538
539 /**
540 * function to filter posts by days
541 * @param <type> $where
542 * @return <type>
543 */
544 function tags_by_days ($where = '') {
545 $tags_op = get_option('tags_op');
546 $tags_days = get_option('tags_days');
547
548 remove_filter('posts_where', 'tags_by_days');
549 $where .= " AND post_date $tags_op '" . date('y-m-d', strtotime("-$tags_days days")) . "'";
550 return $where;
551 }
552
553 /**
554 * Add navigation menu
555 */
556 if(!function_exists('smbd_add_menu')) {
557 function smbd_add_menu() {
558 //Add a submenu to Manage
559 $page = add_options_page("Bulk Delete", "Bulk Delete", 8, basename(__FILE__), "smbd_displayOptions");
560 add_action('admin_print_scripts-' . $page, 'smbd_print_scripts');
561 }
562 }
563
564 /**
565 * Adds the settings link in the Plugin page. Based on http://striderweb.com/nerdaphernalia/2008/06/wp-use-action-links/
566 * @staticvar <type> $this_plugin
567 * @param <type> $links
568 * @param <type> $file
569 */
570 function smbd_filter_plugin_actions($links, $file) {
571 static $this_plugin;
572 if( ! $this_plugin ) $this_plugin = plugin_basename(__FILE__);
573
574 if( $file == $this_plugin ) {
575 $settings_link = '<a href="options-general.php?page=bulk-delete.php">' . _('Manage') . '</a>';
576 array_unshift( $links, $settings_link ); // before other links
577 }
578 return $links;
579 }
580
581 /**
582 * Adds Footer links. Based on http://striderweb.com/nerdaphernalia/2008/06/give-your-wordpress-plugin-credit/
583 */
584 function smbd_admin_footer() {
585 $plugin_data = get_plugin_data( __FILE__ );
586 printf('%1$s ' . __("plugin", 'bulk-delete') .' | ' . __("Version", 'bulk-delete') . ' %2$s | '. __('by', 'bulk-delete') . ' %3$s<br />', $plugin_data['Title'], $plugin_data['Version'], $plugin_data['Author']);
587 }
588
589 add_filter( 'plugin_action_links', 'smbd_filter_plugin_actions', 10, 2 );
590 add_action('admin_menu', 'smbd_add_menu');
591 add_action('init', 'smbd_request_handler');
592 ?>