1
|
|
|
<?php |
2
|
|
|
namespace BulkWP\BulkDelete\Core\Posts\Metabox; |
3
|
|
|
|
4
|
|
|
use BulkWP\BulkDelete\Core\Posts\PostsMetabox; |
5
|
|
|
|
6
|
1 |
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Delete Posts by Category Metabox. |
10
|
|
|
* |
11
|
|
|
* @since 6.0.0 |
12
|
|
|
*/ |
13
|
|
|
class DeletePostsByCategoryMetabox extends PostsMetabox { |
14
|
|
|
protected function initialize() { |
15
|
|
|
$this->item_type = 'posts'; |
16
|
|
|
$this->field_slug = 'cats'; |
17
|
|
|
$this->meta_box_slug = 'bd_by_category'; |
18
|
|
|
$this->action = 'delete_posts_by_category'; |
19
|
|
|
$this->cron_hook = 'do-bulk-delete-cat'; |
20
|
|
|
$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-category/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sc'; |
21
|
|
|
$this->messages = array( |
22
|
|
|
'box_label' => __( 'By Post Category', 'bulk-delete' ), |
23
|
|
|
'scheduled' => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ), |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function render() { |
28
|
|
|
?> |
29
|
|
|
<!-- Category Start--> |
30
|
|
|
<h4><?php _e( 'Select the post type from which you want to delete posts by category', 'bulk-delete' ); ?></h4> |
31
|
|
|
<fieldset class="options"> |
32
|
|
|
<table class="optiontable"> |
33
|
|
|
<?php bd_render_post_type_dropdown( 'cats' ); ?> |
34
|
|
|
</table> |
35
|
|
|
|
36
|
|
|
<h4><?php _e( 'Select the categories from which you wan to delete posts', 'bulk-delete' ); ?></h4> |
37
|
|
|
<p><?php _e( 'Note: The post count below for each category is the total number of posts in that category, irrespective of post type', 'bulk-delete' ); ?>.</p> |
38
|
|
|
<?php |
39
|
|
|
$bd_select2_ajax_limit_categories = apply_filters( 'bd_select2_ajax_limit_categories', 50 ); |
40
|
|
|
|
41
|
|
|
$categories = get_categories( array( |
42
|
|
|
'hide_empty' => false, |
43
|
|
|
'number' => $bd_select2_ajax_limit_categories, |
44
|
|
|
) |
45
|
|
|
); |
46
|
|
|
?> |
47
|
|
|
<table class="form-table"> |
48
|
|
|
<tr> |
49
|
|
|
<td scope="row"> |
50
|
|
|
<?php if( count($categories) >= $bd_select2_ajax_limit_categories ){?> |
51
|
|
|
<select class="select2Ajax" name="smbd_cats[]" data-taxonomy="category" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"> |
52
|
|
|
<option value="all" selected="selected"><?php _e( 'All Categories', 'bulk-delete' ); ?></option> |
53
|
|
|
</select> |
54
|
|
|
<?php }else{?> |
55
|
|
|
<select class="select2" name="smbd_cats[]" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"> |
56
|
|
|
<option value="all" selected="selected"><?php _e( 'All Categories', 'bulk-delete' ); ?></option> |
57
|
|
|
<?php foreach ( $categories as $category ) { ?> |
58
|
|
|
<option value="<?php echo $category->cat_ID; ?>"><?php echo $category->cat_name, ' (', $category->count, ' ', __( 'Posts', 'bulk-delete' ), ')'; ?></option> |
59
|
|
|
<?php } ?> |
60
|
|
|
</select> |
61
|
|
|
<?php }?> |
62
|
|
|
</td> |
63
|
|
|
</tr> |
64
|
|
|
</table> |
65
|
|
|
|
66
|
|
|
<table class="optiontable"> |
67
|
|
|
<?php |
68
|
|
|
$this->render_filtering_table_header(); |
69
|
|
|
$this->render_restrict_settings(); |
70
|
|
|
$this->render_delete_settings(); |
71
|
|
|
//$this->render_private_post_settings(); |
|
|
|
|
72
|
|
|
$this->render_limit_settings(); |
73
|
|
|
$this->render_cron_settings(); |
74
|
|
|
?> |
75
|
|
|
</table> |
76
|
|
|
|
77
|
|
|
</fieldset> |
78
|
|
|
<?php |
79
|
|
|
$this->render_submit_button( 'delete_posts_by_category' ); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
83
|
|
|
$options['post_type'] = bd_array_get( $_POST, 'smbd_cats_post_type', 'post' ); |
84
|
|
|
$options['selected_cats'] = bd_array_get( $_POST, 'smbd_cats' ); |
85
|
|
|
$options['private'] = bd_array_get_bool( $_POST, 'smbd_cats_private', false ); |
86
|
|
|
|
87
|
|
|
return $options; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function delete( $delete_options ) { |
91
|
|
|
if ( array_key_exists( 'tags_op', $delete_options ) ) { |
92
|
|
|
$delete_options['date_op'] = $delete_options['tags_op']; |
93
|
|
|
$delete_options['days'] = $delete_options['tags_days']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$delete_options = apply_filters( 'bd_delete_options', $delete_options ); |
97
|
|
|
|
98
|
|
|
$options = array(); |
99
|
|
|
$selected_tags = $delete_options['selected_tags']; |
100
|
|
|
if ( in_array( 'all', $selected_tags ) ) { |
101
|
|
|
$options['tag__not__in'] = array(0); |
102
|
|
|
} else { |
103
|
|
|
$options['tag__in'] = $selected_tags; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$options = bd_build_query_options( $delete_options, $options ); |
107
|
|
|
$post_ids = bd_query( $options ); |
108
|
|
|
foreach ( $post_ids as $post_id ) { |
109
|
|
|
wp_delete_post( $post_id, $delete_options['force_delete'] ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$posts_deleted += count( $post_ids ); |
|
|
|
|
113
|
|
|
|
114
|
|
|
return $posts_deleted; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function get_success_message( $items_deleted ) { |
118
|
|
|
/* translators: 1 Number of posts deleted */ |
119
|
|
|
return _n( 'Deleted %d post with the selected post category', 'Deleted %d posts with the selected post category', $items_deleted, 'bulk-delete' ); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.