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 Status Metabox. |
10
|
|
|
* |
11
|
|
|
* @since 6.0.0 |
12
|
|
|
*/ |
13
|
|
|
class DeletePostsByStatusMetabox extends PostsMetabox { |
14
|
|
|
protected function initialize() { |
15
|
|
|
$this->item_type = 'posts'; |
16
|
|
|
$this->field_slug = 'post_status'; |
17
|
|
|
$this->meta_box_slug = 'bd_posts_by_status'; |
18
|
|
|
$this->action = 'delete_posts_by_status'; |
19
|
|
|
$this->cron_hook = 'do-bulk-delete-post-by-status'; |
20
|
|
|
$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-status/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sps'; |
21
|
|
|
$this->messages = array( |
22
|
|
|
'box_label' => __( 'By Post Status', 'bulk-delete' ), |
23
|
|
|
'scheduled' => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ), |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function render() { |
28
|
|
|
$post_statuses = bd_get_post_statuses(); |
29
|
|
|
$post_count = wp_count_posts(); |
30
|
|
|
?> |
31
|
|
|
<h4><?php _e( 'Select the post statuses from which you want to delete posts', 'bulk-delete' ); ?></h4> |
32
|
|
|
|
33
|
|
|
<fieldset class="options"> |
34
|
|
|
<table class="optiontable"> |
35
|
|
|
|
36
|
|
|
<?php foreach ( $post_statuses as $post_status ) : ?> |
37
|
|
|
<tr> |
38
|
|
|
<td> |
39
|
|
|
<input name="smbd_post_status[]" id="smbd_<?php echo esc_attr( $post_status->name ); ?>" |
40
|
|
|
value="<?php echo esc_attr( $post_status->name ); ?>" type="checkbox"> |
41
|
|
|
|
42
|
|
|
<label for="smbd_<?php echo esc_attr( $post_status->name ); ?>"> |
43
|
|
|
<?php echo esc_html( $post_status->label ), ' '; ?> |
44
|
|
|
<?php if ( property_exists( $post_count, $post_status->name ) ) : ?> |
45
|
|
|
(<?php echo absint( $post_count->{ $post_status->name } ) . ' ', __( 'Posts', 'bulk-delete' ); ?>) |
46
|
|
|
<?php endif; ?> |
47
|
|
|
</label> |
48
|
|
|
</td> |
49
|
|
|
</tr> |
50
|
|
|
<?php endforeach; ?> |
51
|
|
|
|
52
|
|
|
<?php $sticky_post_count = count( get_option( 'sticky_posts' ) ); ?> |
|
|
|
|
53
|
|
|
|
54
|
|
|
<tr> |
55
|
|
|
<td> |
56
|
|
|
<input name="smbd_sticky" id="smbd_sticky" value="on" type="checkbox"> |
57
|
|
|
<label for="smbd_sticky"> |
58
|
|
|
<?php echo __( 'All Sticky Posts', 'bulk-delete' ), ' '; ?> |
59
|
|
|
(<?php echo absint( $sticky_post_count ), ' ', __( 'Posts', 'bulk-delete' ); ?>) |
60
|
|
|
<?php echo '<strong>', __( 'Note', 'bulk-delete' ), '</strong>: ', __( 'The date filter will not work for sticky posts', 'bulk-delete' ); ?> |
61
|
|
|
</label> |
62
|
|
|
</td> |
63
|
|
|
</tr> |
64
|
|
|
|
65
|
|
|
</table> |
66
|
|
|
|
67
|
|
|
<table class="optiontable"> |
68
|
|
|
<?php |
69
|
|
|
$this->render_filtering_table_header(); |
70
|
|
|
$this->render_restrict_settings(); |
71
|
|
|
$this->render_delete_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_status' ); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
83
|
|
|
$options['post_status'] = array_map( 'sanitize_text_field', bd_array_get( $request, 'smbd_post_status', array() ) ); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$options['delete-sticky-posts'] = bd_array_get_bool( $request, 'smbd_sticky', false ); |
86
|
|
|
|
87
|
|
|
return $options; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function delete( $delete_options ) { |
91
|
|
|
$delete_options = bd_convert_old_options_for_delete_post_by_status( $delete_options ); |
92
|
|
|
$delete_options = apply_filters( 'bd_delete_options', $delete_options ); |
93
|
|
|
|
94
|
|
|
$posts_deleted = 0; |
95
|
|
|
|
96
|
|
|
if ( $delete_options['delete-sticky-posts'] ) { |
97
|
|
|
$posts_deleted += self::delete_sticky_posts( $delete_options['force_delete'] ); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ( empty( $delete_options['post_status'] ) ) { |
101
|
|
|
return $posts_deleted; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$options = array( |
105
|
|
|
'post_status' => $delete_options['post_status'], |
106
|
|
|
'post__not_in' => get_option( 'sticky_posts' ), |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$options = bd_build_query_options( $delete_options, $options ); |
110
|
|
|
|
111
|
|
|
$post_ids = bd_query( $options ); |
112
|
|
|
foreach ( $post_ids as $post_id ) { |
113
|
|
|
wp_delete_post( $post_id, $delete_options['force_delete'] ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$posts_deleted += count( $post_ids ); |
117
|
|
|
|
118
|
|
|
return $posts_deleted; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function get_success_message( $items_deleted ) { |
122
|
|
|
/* translators: 1 Number of pages deleted */ |
123
|
|
|
return _n( 'Deleted %d post with the selected post status', 'Deleted %d posts with the selected post status', $items_deleted, 'bulk-delete' ); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|