1
|
|
|
<?php |
2
|
|
|
namespace BulkWP\BulkDelete\Core\Posts; |
3
|
|
|
|
4
|
|
|
use BulkWP\BulkDelete\Core\Base\BaseModule; |
5
|
|
|
|
6
|
1 |
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Module for deleting posts. |
10
|
|
|
* |
11
|
|
|
* @since 6.0.0 |
12
|
|
|
*/ |
13
|
|
|
abstract class PostsModule extends BaseModule { |
14
|
|
|
/** |
15
|
|
|
* Build query params for WP_Query by using delete options. |
16
|
|
|
* |
17
|
|
|
* Return an empty query array to short-circuit deletion. |
18
|
|
|
* |
19
|
|
|
* @param array $options Delete options. |
20
|
|
|
* |
21
|
|
|
* @return array Query. |
22
|
|
|
*/ |
23
|
|
|
abstract protected function build_query( $options ); |
24
|
|
|
|
25
|
|
|
protected $item_type = 'posts'; |
26
|
|
|
|
27
|
|
|
public function filter_js_array( $js_array ) { |
28
|
|
|
$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' ); |
29
|
|
|
$js_array['msg']['selectPostOption'] = __( 'Please select posts from at least one option', 'bulk-delete' ); |
30
|
|
|
|
31
|
|
|
$js_array['validators']['delete_posts_by_category'] = 'validateSelect2'; |
32
|
|
|
$js_array['error_msg']['delete_posts_by_category'] = 'selectCategory'; |
33
|
|
|
$js_array['msg']['selectCategory'] = __( 'Please select at least one category', 'bulk-delete' ); |
34
|
|
|
|
35
|
|
|
$js_array['validators']['delete_posts_by_tag'] = 'validateSelect2'; |
36
|
|
|
$js_array['error_msg']['delete_posts_by_category'] = 'selectTag'; |
37
|
|
|
$js_array['msg']['selectTag'] = __( 'Please select at least one tag', 'bulk-delete' ); |
38
|
|
|
|
39
|
|
|
$js_array['validators']['delete_posts_by_url'] = 'validateUrl'; |
40
|
|
|
$js_array['error_msg']['delete_posts_by_url'] = 'enterUrl'; |
41
|
|
|
$js_array['msg']['enterUrl'] = __( 'Please enter at least one post url', 'bulk-delete' ); |
42
|
|
|
|
43
|
|
|
$js_array['dt_iterators'][] = '_cats'; |
44
|
|
|
$js_array['dt_iterators'][] = '_tags'; |
45
|
|
|
$js_array['dt_iterators'][] = '_taxs'; |
46
|
|
|
$js_array['dt_iterators'][] = '_post_status'; |
47
|
|
|
|
48
|
|
|
return $js_array; |
49
|
|
|
} |
50
|
|
|
|
51
|
44 |
|
public function delete( $options ) { |
52
|
|
|
/** |
53
|
|
|
* Filter delete options before deleting posts. |
54
|
|
|
* |
55
|
|
|
* @since 6.0.0 Added `Modules` parameter. |
56
|
|
|
* |
57
|
|
|
* @param array $options Delete options. |
58
|
|
|
* @param \BulkWP\BulkDelete\Core\Base\BaseModule Modules that is triggering deletion. |
59
|
|
|
*/ |
60
|
44 |
|
$options = apply_filters( 'bd_delete_options', $options, $this ); |
61
|
|
|
|
62
|
44 |
|
$query = $this->build_query( $options ); |
63
|
|
|
|
64
|
44 |
|
if ( empty( $query ) ) { |
65
|
|
|
// Short circuit deletion, if nothing needs to be deleted. |
66
|
|
|
return 0; |
67
|
|
|
} |
68
|
|
|
|
69
|
44 |
|
return $this->delete_posts_from_query( $query, $options ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Build the query using query params and then Delete posts. |
74
|
|
|
* |
75
|
|
|
* @param array $query Params for WP Query. |
76
|
|
|
* @param array $options Delete Options. |
77
|
|
|
* |
78
|
|
|
* @return int Number of posts deleted. |
79
|
|
|
*/ |
80
|
44 |
|
protected function delete_posts_from_query( $query, $options ) { |
81
|
44 |
|
$query = bd_build_query_options( $options, $query ); |
82
|
44 |
|
$post_ids = bd_query( $query ); |
83
|
|
|
|
84
|
44 |
|
return $this->delete_posts_by_id( $post_ids, $options['force_delete'] ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Render the "private post" setting fields. |
89
|
|
|
*/ |
90
|
|
|
protected function render_private_post_settings() { |
91
|
|
|
bd_render_private_post_settings( $this->field_slug ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Render Post type dropdown. |
96
|
|
|
*/ |
97
|
|
|
protected function render_post_type_dropdown() { |
98
|
|
|
bd_render_post_type_dropdown( $this->field_slug ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Render Category dropdown. |
103
|
|
|
*/ |
104
|
|
|
protected function render_category_dropdown() { |
105
|
|
|
$categories = $this->get_categories(); |
106
|
|
|
?> |
107
|
|
|
|
108
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>" |
109
|
|
|
class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>" |
110
|
|
|
data-taxonomy="category" multiple> |
111
|
|
|
|
112
|
|
|
<option value="all"> |
113
|
|
|
<?php _e( 'All Categories', 'bulk-delete' ); ?> |
114
|
|
|
</option> |
115
|
|
|
|
116
|
|
|
<?php foreach ( $categories as $category ) : ?> |
117
|
|
|
<option value="<?php echo absint( $category->cat_ID ); ?>"> |
118
|
|
|
<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?> |
119
|
|
|
</option> |
120
|
|
|
<?php endforeach; ?> |
121
|
|
|
|
122
|
|
|
</select> |
123
|
|
|
<?php |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Render Tags dropdown. |
128
|
|
|
*/ |
129
|
|
|
protected function render_tags_dropdown() { |
130
|
|
|
$tags = $this->get_tags(); |
131
|
|
|
?> |
132
|
|
|
|
133
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>" |
134
|
|
|
class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>" |
135
|
|
|
data-taxonomy="post_tag" multiple> |
136
|
|
|
|
137
|
|
|
<option value="all"> |
138
|
|
|
<?php _e( 'All Tags', 'bulk-delete' ); ?> |
139
|
|
|
</option> |
140
|
|
|
|
141
|
|
|
<?php foreach ( $tags as $tag ) : ?> |
142
|
|
|
<option value="<?php echo absint( $tag->term_id ); ?>"> |
143
|
|
|
<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?> |
144
|
|
|
</option> |
145
|
|
|
<?php endforeach; ?> |
146
|
|
|
</select> |
147
|
|
|
<?php |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Render Sticky Posts dropdown. |
152
|
|
|
*/ |
153
|
|
|
protected function render_sticky_post_dropdown() { |
154
|
|
|
$posts = $this->get_sticky_posts(); |
155
|
|
|
?> |
156
|
|
|
<table class="optiontable"> |
157
|
|
|
<tr> |
158
|
|
|
<td scope="row"> |
159
|
|
|
<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="All"> |
160
|
|
|
<label>All</label> |
161
|
|
|
</td> |
162
|
|
|
</tr> |
163
|
|
|
<?php foreach ( $posts as $post ) : |
164
|
|
|
$user = get_userdata( $post->post_author ); |
165
|
|
|
?> |
166
|
|
|
<tr> |
167
|
|
|
<td scope="row"> |
168
|
|
|
<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>"> |
169
|
|
|
<label><?php echo esc_html( $post->post_title . ' Published by ' . $user->display_name . ' on ' . $post->post_date ); ?></label> |
170
|
|
|
</td> |
171
|
|
|
</tr> |
172
|
|
|
<?php endforeach; ?> |
173
|
|
|
</table> |
174
|
|
|
<?php |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the list of sticky posts. |
179
|
|
|
* |
180
|
|
|
* @return array List of sticky posts. |
181
|
|
|
*/ |
182
|
|
|
protected function get_sticky_posts(){ |
183
|
|
|
$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) ); |
184
|
|
|
|
185
|
|
|
return $posts; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the list of categories. |
190
|
|
|
* |
191
|
|
|
* @return array List of categories. |
192
|
|
|
*/ |
193
|
|
|
protected function get_categories() { |
194
|
|
|
$enhanced_select_threshold = $this->get_enhanced_select_threshold(); |
195
|
|
|
|
196
|
|
|
$categories = get_categories( |
197
|
|
|
array( |
198
|
|
|
'hide_empty' => false, |
199
|
|
|
'number' => $enhanced_select_threshold, |
200
|
|
|
) |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
return $categories; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Are tags present in this WordPress installation? |
208
|
|
|
* |
209
|
|
|
* Only one tag is retrieved to check if tags are present for performance reasons. |
210
|
|
|
* |
211
|
|
|
* @return bool True if tags are present, False otherwise. |
212
|
|
|
*/ |
213
|
|
|
protected function are_tags_present() { |
214
|
|
|
$tags = $this->get_tags( 1 ); |
215
|
|
|
|
216
|
|
|
return ( count( $tags ) > 0 ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Are sticky post present in this WordPress? |
221
|
|
|
* |
222
|
|
|
* Only one post is retrieved to check if stick post are present for performance reasons. |
223
|
|
|
* |
224
|
|
|
* @return bool True if posts are present, False otherwise. |
225
|
|
|
*/ |
226
|
|
|
protected function are_sticky_post_present() { |
227
|
|
|
$sticky_post_ids = get_option( 'sticky_posts' ); |
228
|
|
|
if( is_array($sticky_post_ids) ){ |
229
|
|
|
return ( count( $sticky_post_ids ) > 0 ); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get the list of tags. |
235
|
|
|
* |
236
|
|
|
* @param int $max_count The maximum number of tags to be returned (Optional). Default 0. |
237
|
|
|
* If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned. |
238
|
|
|
* |
239
|
|
|
* @return array List of tags. |
240
|
|
|
*/ |
241
|
|
|
protected function get_tags( $max_count = 0 ) { |
242
|
|
|
if ( absint( $max_count ) === 0 ) { |
243
|
|
|
$max_count = $this->get_enhanced_select_threshold(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$tags = get_tags( |
247
|
|
|
array( |
248
|
|
|
'hide_empty' => false, |
249
|
|
|
'number' => $max_count, |
250
|
|
|
) |
251
|
|
|
); |
252
|
|
|
|
253
|
|
|
return $tags; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Delete sticky posts. |
258
|
|
|
* |
259
|
|
|
* @param bool $force_delete Whether to bypass trash and force deletion. |
260
|
|
|
* |
261
|
|
|
* @return int Number of posts deleted. |
262
|
|
|
*/ |
263
|
|
|
protected function delete_sticky_posts( $force_delete ) { |
264
|
|
|
$sticky_post_ids = get_option( 'sticky_posts' ); |
265
|
|
|
|
266
|
|
|
if ( ! is_array( $sticky_post_ids ) ) { |
267
|
|
|
return 0; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return $this->delete_posts_by_id( $sticky_post_ids, $force_delete ); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Delete posts by ids. |
275
|
|
|
* |
276
|
|
|
* @param int[] $post_ids List of post ids to delete. |
277
|
|
|
* @param bool $force_delete True to force delete posts, False otherwise. |
278
|
|
|
* |
279
|
|
|
* @return int Number of posts deleted. |
280
|
|
|
*/ |
281
|
44 |
|
protected function delete_posts_by_id( $post_ids, $force_delete ) { |
282
|
44 |
|
foreach ( $post_ids as $post_id ) { |
283
|
|
|
// `$force_delete` parameter to `wp_delete_post` won't work for custom post types. |
284
|
|
|
// See https://core.trac.wordpress.org/ticket/43672 |
285
|
44 |
|
if ( $force_delete ) { |
286
|
12 |
|
wp_delete_post( $post_id, true ); |
287
|
|
|
} else { |
288
|
44 |
|
wp_trash_post( $post_id ); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
44 |
|
return count( $post_ids ); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|