Completed
Push — dev/5.6 ( 7e7a19...64b458 )
by Sudar
01:55
created

Bulk_Delete_Posts::delete_posts_by_status()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 6
nop 1
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * Utility class for deleting posts.
4
 *
5
 * @author     Sudar
6
 *
7
 * @package    BulkDelete
8
 */
9
10
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
11
12
class Bulk_Delete_Posts {
13
	/**
14
	 * Render post status box.
15
	 */
16
	public static function render_delete_posts_by_status_box() {
17
		if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_POST_STATUS ) ) {
18
			/* translators: 1 Number of posts that are deleted. */
19
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . Bulk_Delete::POSTS_PAGE_SLUG );
20
21
			return;
22
		}
23
24
		$post_statuses = bd_get_post_statuses();
25
		$post_count    = wp_count_posts();
26
?>
27
		<h4><?php _e( 'Select the post statuses from which you want to delete posts', 'bulk-delete' ); ?></h4>
28
29
		<fieldset class="options">
30
		<table class="optiontable">
31
32
			<?php foreach ( $post_statuses as $post_status ) : ?>
33
				<tr>
34
					<td>
35
						<input name="smbd_post_status[]" id="smbd_<?php echo esc_attr( $post_status->name ); ?>"
36
							value="<?php echo esc_attr( $post_status->name ); ?>" type="checkbox">
37
38
						<label for="smbd_<?php echo esc_attr( $post_status->name ); ?>">
39
							<?php echo esc_html( $post_status->label ), ' '; ?>
40
							<?php if ( property_exists( $post_count, $post_status->name ) ) : ?>
41
								(<?php echo absint( $post_count->{ $post_status->name } ) . ' ', __( 'Posts', 'bulk-delete' ); ?>)
42
							<?php endif; ?>
43
						</label>
44
					</td>
45
				</tr>
46
			<?php endforeach; ?>
47
48
			<?php $sticky_post_count = count( get_option( 'sticky_posts' ) ); ?>
49
50
			<tr>
51
				<td>
52
					<input name="smbd_sticky" id="smbd_sticky" value="on" type="checkbox">
53
					<label for="smbd_sticky">
54
						<?php echo __( 'All Sticky Posts', 'bulk-delete' ), ' '; ?>
55
						(<?php echo absint( $sticky_post_count ), ' ', __( 'Posts', 'bulk-delete' ); ?>)
56
						<?php echo '<strong>', __( 'Note', 'bulk-delete' ), '</strong>: ', __( 'The date filter will not work for sticky posts', 'bulk-delete' ); ?>
57
					</label>
58
				</td>
59
			</tr>
60
61
		</table>
62
63
		<table class="optiontable">
64
			<?php bd_render_filtering_table_header(); ?>
65
			<?php bd_render_restrict_settings( 'post_status' ); ?>
66
			<?php bd_render_delete_settings( 'post_status' ); ?>
67
			<?php bd_render_limit_settings( 'post_status' ); ?>
68
			<?php bd_render_cron_settings( 'post_status', 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-status/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sps' ); ?>
69
		</table>
70
71
		</fieldset>
72
<?php
73
		bd_render_submit_button( 'delete_posts_by_status' );
74
	}
75
76
	/**
77
	 * Delete posts by post status.
78
	 *
79
	 * @since 5.0
80
	 * @static
81
	 *
82
	 * Nonce verification is done in the hook that calls this function.
83
	 * phpcs:disable WordPress.CSRF.NonceVerification.NoNonceVerification
84
	 */
85
	public static function do_delete_posts_by_status() {
86
		$delete_options = array();
87
88
		$delete_options['restrict']     = bd_array_get_bool( $_POST, 'smbd_post_status_restrict', false );
89
		$delete_options['limit_to']     = absint( bd_array_get( $_POST, 'smbd_post_status_limit_to', 0 ) );
90
		$delete_options['force_delete'] = bd_array_get_bool( $_POST, 'smbd_post_status_force_delete', false );
91
92
		$delete_options['date_op'] = bd_array_get( $_POST, 'smbd_post_status_op' );
93
		$delete_options['days']    = absint( bd_array_get( $_POST, 'smbd_post_status_days' ) );
94
95
		$delete_options['post_status'] = array_map( 'sanitize_text_field', bd_array_get( $_POST, 'smbd_post_status', array() ) );
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
97
		$delete_options['delete-sticky-posts'] = bd_array_get_bool( $_POST, 'smbd_sticky', false );
98
99
		if ( bd_array_get_bool( $_POST, 'smbd_post_status_cron', false ) ) {
100
			$freq = sanitize_text_field( $_POST['smbd_post_status_cron_freq'] );
101
			$time = strtotime( $_POST['smbd_post_status_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 );
102
103
			if ( -1 === $freq ) {
104
				wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_POST_STATUS, array( $delete_options ) );
105
			} else {
106
				wp_schedule_event( $time, $freq, Bulk_Delete::CRON_HOOK_POST_STATUS, array( $delete_options ) );
107
			}
108
109
			$msg = __( 'Posts with the selected status are scheduled for deletion.', 'bulk-delete' ) . ' ';
110
111
			/* translators: 1 Url to view cron jobs */
112
			$msg .= sprintf( __( 'See the full list of <a href = "%s">scheduled tasks</a>', 'bulk-delete' ), get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG );
113
		} else {
114
			$deleted_count = self::delete_posts_by_status( $delete_options );
115
116
			/* translators: 1 Number of posts deleted */
117
			$msg = sprintf( _n( 'Deleted %d post with the selected post status', 'Deleted %d posts with the selected post status', $deleted_count, 'bulk-delete' ), $deleted_count );
118
		}
119
120
		add_settings_error(
121
			Bulk_Delete::POSTS_PAGE_SLUG,
122
			'deleted-posts',
123
			$msg,
124
			'updated'
125
		);
126
	} // phpcs:enable
127
128
	/**
129
	 * Delete posts by post status - drafts, pending posts, scheduled posts etc.
130
	 *
131
	 * @since  5.0
132
	 * @static
133
	 *
134
	 * @param array $delete_options Options for deleting posts.
135
	 *
136
	 * @return int $posts_deleted  Number of posts that were deleted
137
	 */
138
	public static function delete_posts_by_status( $delete_options ) {
139
		$delete_options = bd_convert_old_options_for_delete_post_by_status( $delete_options );
140
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
141
142
		$posts_deleted = 0;
143
144
		if ( $delete_options['delete-sticky-posts'] ) {
145
			$posts_deleted += self::delete_sticky_posts( $delete_options['force_delete'] );
146
		}
147
148
		if ( empty( $delete_options['post_status'] ) ) {
149
			return $posts_deleted;
150
		}
151
152
		$options = array(
153
			'post_status'  => $delete_options['post_status'],
154
			'post__not_in' => get_option( 'sticky_posts' ),
155
		);
156
157
		$options = bd_build_query_options( $delete_options, $options );
158
159
		$post_ids = bd_query( $options );
160
		foreach ( $post_ids as $post_id ) {
161
			wp_delete_post( $post_id, $delete_options['force_delete'] );
162
		}
163
164
		$posts_deleted += count( $post_ids );
165
166
		return $posts_deleted;
167
	}
168
169
	/**
170
	 * Delete all sticky posts.
171
	 *
172
	 * @since 5.6.0
173
	 *
174
	 * @param bool $force_delete Whether to force delete the posts.
175
	 *
176
	 * @return int Number of posts deleted.
177
	 */
178
	public static function delete_sticky_posts( $force_delete ) {
179
		$sticky_post_ids = get_option( 'sticky_posts' );
180
181
		foreach ( $sticky_post_ids as $sticky_post_id ) {
182
			wp_delete_post( $sticky_post_id, $force_delete );
183
		}
184
185
		return count( $sticky_post_ids );
186
	}
187
188
	/**
189
	 * Render Delete posts by category box.
190
	 */
191
	public static function render_delete_posts_by_category_box() {
192
		if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_CATEGORY ) ) {
193
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . Bulk_Delete::POSTS_PAGE_SLUG );
194
195
			return;
196
		}
197
?>
198
        <!-- Category Start-->
199
        <h4><?php _e( 'Select the post type from which you want to delete posts by category', 'bulk-delete' ); ?></h4>
200
        <fieldset class="options">
201
        <table class="optiontable">
202
			<?php bd_render_post_type_dropdown( 'cats' ); ?>
203
        </table>
204
205
        <h4><?php _e( 'Select the categories from which you wan to delete posts', 'bulk-delete' ); ?></h4>
206
        <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>
207
<?php
208
		$categories = get_categories( array(
209
				'hide_empty' => false,
210
			)
211
		);
212
?>
213
        <table class="form-table">
214
            <tr>
215
                <td scope="row">
216
				<select class="select2" name="smbd_cats[]" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>">
217
					<option value="all"><?php _e( 'All Categories', 'bulk-delete' ); ?></option>
218
		<?php foreach ( $categories as $category ) { ?>
219
			<option value="<?php echo $category->cat_ID; ?>"><?php echo $category->cat_name, ' (', $category->count, ' ', __( 'Posts', 'bulk-delete' ), ')'; ?></option>
220
		<?php } ?>
221
					</select>
222
                </td>
223
            </tr>
224
        </table>
225
226
        <table class="optiontable">
227
			<?php bd_render_filtering_table_header(); ?>
228
			<?php bd_render_restrict_settings( 'cats' ); ?>
229
			<?php bd_render_delete_settings( 'cats' ); ?>
230
			<?php bd_render_private_post_settings( 'cats' ); ?>
231
			<?php bd_render_limit_settings( 'cats' ); ?>
232
			<?php bd_render_cron_settings( 'cats', 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-category/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sc' ); ?>
233
        </table>
234
235
        </fieldset>
236
<?php
237
		bd_render_submit_button( 'delete_posts_by_category' );
238
	}
239
240
	/**
241
	 * Process delete posts by category.
242
	 *
243
	 * @since 5.0
244
	 * @static
245
	 */
246
	public static function do_delete_posts_by_category() {
247
		$delete_options = array();
248
249
		$delete_options['post_type']     = array_get( $_POST, 'smbd_cats_post_type', 'post' );
250
		$delete_options['selected_cats'] = array_get( $_POST, 'smbd_cats' );
251
		$delete_options['restrict']      = array_get_bool( $_POST, 'smbd_cats_restrict', false );
252
		$delete_options['private']       = array_get_bool( $_POST, 'smbd_cats_private', false );
253
		$delete_options['limit_to']      = absint( array_get( $_POST, 'smbd_cats_limit_to', 0 ) );
254
		$delete_options['force_delete']  = array_get_bool( $_POST, 'smbd_cats_force_delete', false );
255
256
		$delete_options['date_op']       = array_get( $_POST, 'smbd_cats_op' );
257
		$delete_options['days']          = absint( array_get( $_POST, 'smbd_cats_days' ) );
258
259
		if ( array_get_bool( $_POST, 'smbd_cats_cron', false ) ) {
260
			$freq = $_POST['smbd_cats_cron_freq'];
261
			$time = strtotime( $_POST['smbd_cats_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 );
262
263
			if ( $freq == -1 ) {
264
				wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_CATEGORY, array( $delete_options ) );
265
			} else {
266
				wp_schedule_event( $time, $freq , Bulk_Delete::CRON_HOOK_CATEGORY, array( $delete_options ) );
267
			}
268
269
			$msg = __( 'Posts from the selected categories are scheduled for deletion.', 'bulk-delete' ) . ' ' .
270
				sprintf( __( 'See the full list of <a href = "%s">scheduled tasks</a>' , 'bulk-delete' ), get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG );
271
		} else {
272
			$deleted_count = self::delete_posts_by_category( $delete_options );
273
			$msg           = sprintf( _n( 'Deleted %d post from the selected categories', 'Deleted %d posts from the selected categories' , $deleted_count, 'bulk-delete' ), $deleted_count );
274
		}
275
276
		add_settings_error(
277
			Bulk_Delete::POSTS_PAGE_SLUG,
278
			'deleted-posts',
279
			$msg,
280
			'updated'
281
		);
282
	}
283
284
	/**
285
	 * Delete posts by category.
286
	 *
287
	 * @since 5.0
288
	 * @static
289
	 *
290
	 * @param array $delete_options Options for deleting posts
291
	 *
292
	 * @return int $posts_deleted  Number of posts that were deleted
293
	 */
294
	public static function delete_posts_by_category( $delete_options ) {
295
		// Backward compatibility code. Will be removed in Bulk Delete v6.0
296
		$delete_options['post_type'] = array_get( $delete_options, 'post_type', 'post' );
297
298
		if ( array_key_exists( 'cats_op', $delete_options ) ) {
299
			$delete_options['date_op'] = $delete_options['cats_op'];
300
			$delete_options['days']    = $delete_options['cats_days'];
301
		}
302
303
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
304
305
		$options       = array();
306
		$selected_cats = $delete_options['selected_cats'];
307
		if ( in_array( 'all', $selected_cats ) ) {
308
			$options['category__not__in'] = array(0);
309
		} else {
310
			$options['category__in'] = $selected_cats;
311
		}
312
313
		$options  = bd_build_query_options( $delete_options, $options );
314
		$post_ids = bd_query( $options );
315
316
		foreach ( $post_ids as $post_id ) {
317
			// $force delete parameter to custom post types doesn't work
318
			if ( $delete_options['force_delete'] ) {
319
				wp_delete_post( $post_id, true );
320
			} else {
321
				wp_trash_post( $post_id );
322
			}
323
		}
324
325
		return count( $post_ids );
326
	}
327
328
	/**
329
	 * Render delete posts by tag box.
330
	 */
331
	public static function render_delete_posts_by_tag_box() {
332
		if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_TAG ) ) {
333
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . Bulk_Delete::POSTS_PAGE_SLUG );
334
335
			return;
336
		}
337
338
		$tags = get_tags();
339
		if ( count( $tags ) > 0 ) {
340
?>
341
            <h4><?php _e( 'Select the tags from which you want to delete posts', 'bulk-delete' ) ?></h4>
342
343
            <!-- Tags start-->
344
            <fieldset class="options">
345
            <table class="form-table">
346
                <tr>
347
				<td scope="row" colspan="2">
348
					<select class="select2" name="smbd_tags[]" multiple data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>">
349
					<option value="all"><?php _e( 'All Tags', 'bulk-delete' ); ?></option>
350
				<?php foreach ( $tags as $tag ) { ?>
351
					<option value="<?php echo absint( $tag->term_id ); ?>"><?php echo $tag->name, ' (', $tag->count, ' ', __( 'Posts', 'bulk-delete' ), ')'; ?></option>
352
				<?php } ?>
353
					</select>
354
				</td>
355
                </tr>
356
			</table>
357
358
            <table class="optiontable">
359
				<?php bd_render_filtering_table_header(); ?>
360
				<?php bd_render_restrict_settings( 'tags' ); ?>
361
				<?php bd_render_delete_settings( 'tags' ); ?>
362
				<?php bd_render_private_post_settings( 'tags' ); ?>
363
				<?php bd_render_limit_settings( 'tags' ); ?>
364
				<?php bd_render_cron_settings( 'tags', 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-tag/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-st' ); ?>
365
            </table>
366
            </fieldset>
367
<?php
368
			bd_render_submit_button( 'delete_posts_by_tag' );
369
		} else {
370
?>
371
            <h4><?php _e( "You don't have any posts assigned to tags in this blog.", 'bulk-delete' ) ?></h4>
372
<?php
373
		}
374
	}
375
376
	/**
377
	 * Process Delete Posts by tag request.
378
	 *
379
	 * @static
380
	 *
381
	 * @since 5.0
382
	 */
383
	public static function do_delete_posts_by_tag() {
384
		$delete_options                  = array();
385
		$delete_options['selected_tags'] = array_get( $_POST, 'smbd_tags' );
386
		$delete_options['restrict']      = array_get_bool( $_POST, 'smbd_tags_restrict', false );
387
		$delete_options['private']       = array_get( $_POST, 'smbd_tags_private' );
388
		$delete_options['limit_to']      = absint( array_get( $_POST, 'smbd_tags_limit_to', 0 ) );
389
		$delete_options['force_delete']  = array_get_bool( $_POST, 'smbd_tags_force_delete', false );
390
391
		$delete_options['date_op']       = array_get( $_POST, 'smbd_tags_op' );
392
		$delete_options['days']          = absint( array_get( $_POST, 'smbd_tags_days' ) );
393
394
		if ( array_get( $_POST, 'smbd_tags_cron', 'false' ) == 'true' ) {
395
			$freq = $_POST['smbd_tags_cron_freq'];
396
			$time = strtotime( $_POST['smbd_tags_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 );
397
398
			if ( $freq == -1 ) {
399
				wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_TAG, array( $delete_options ) );
400
			} else {
401
				wp_schedule_event( $time, $freq, Bulk_Delete::CRON_HOOK_TAG, array( $delete_options ) );
402
			}
403
			$msg = __( 'Posts from the selected tags are scheduled for deletion.', 'bulk-delete' ) . ' ' .
404
				sprintf( __( 'See the full list of <a href = "%s">scheduled tasks</a>' , 'bulk-delete' ), get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG );
405
		} else {
406
			$deleted_count = self::delete_posts_by_tag( $delete_options );
407
			$msg           = sprintf( _n( 'Deleted %d post from the selected tags', 'Deleted %d posts from the selected tags' , $deleted_count, 'bulk-delete' ), $deleted_count );
408
		}
409
410
		add_settings_error(
411
			Bulk_Delete::POSTS_PAGE_SLUG,
412
			'deleted-posts',
413
			$msg,
414
			'updated'
415
		);
416
	}
417
418
	/**
419
	 * Delete posts by tag.
420
	 *
421
	 * @since 5.0
422
	 * @static
423
	 *
424
	 * @param array $delete_options Options for deleting posts
425
	 *
426
	 * @return int $posts_deleted  Number of posts that were deleted
427
	 */
428
	public static function delete_posts_by_tag( $delete_options ) {
429
		// Backward compatibility code. Will be removed in Bulk Delete v6.0
430
		if ( array_key_exists( 'tags_op', $delete_options ) ) {
431
			$delete_options['date_op'] = $delete_options['tags_op'];
432
			$delete_options['days']    = $delete_options['tags_days'];
433
		}
434
435
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
436
437
		$options       = array();
438
		$selected_tags = $delete_options['selected_tags'];
439
		if ( in_array( 'all', $selected_tags ) ) {
440
			$options['tag__not__in'] = array(0);
441
		} else {
442
			$options['tag__in'] = $selected_tags;
443
		}
444
445
		$options  = bd_build_query_options( $delete_options, $options );
446
		$post_ids = bd_query( $options );
447
		foreach ( $post_ids as $post_id ) {
448
			wp_delete_post( $post_id, $delete_options['force_delete'] );
449
		}
450
451
		return count( $post_ids );
452
	}
453
454
	/**
455
	 * Render delete by custom taxonomy box.
456
	 */
457
	public static function render_delete_posts_by_taxonomy_box() {
458
		if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_TAX ) ) {
459
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . Bulk_Delete::POSTS_PAGE_SLUG );
460
461
			return;
462
		}
463
464
		$taxs =  get_taxonomies( array(
465
				'public'   => true,
466
				'_builtin' => false,
467
			), 'objects'
468
		);
469
470
		$terms_array = array();
471
		if ( count( $taxs ) > 0 ) {
472
			foreach ( $taxs as $tax ) {
473
				$terms = get_terms( $tax->name );
474
				if ( count( $terms ) > 0 ) {
475
					$terms_array[$tax->name] = $terms;
476
				}
477
			}
478
		}
479
480
		if ( count( $terms_array ) > 0 ) {
481
?>
482
        <!-- Custom tax Start-->
483
        <h4><?php _e( 'Select the post type from which you want to delete posts by custom taxonomy', 'bulk-delete' ); ?></h4>
484
485
        <fieldset class="options">
486
            <table class="optiontable">
487
				<?php bd_render_post_type_dropdown( 'tax' ); ?>
488
            </table>
489
490
            <h4><?php _e( 'Select the taxonomies from which you want to delete posts', 'bulk-delete' ) ?></h4>
491
492
            <table class="optiontable">
493
<?php
494
			foreach ( $terms_array as $tax => $terms ) {
495
?>
496
                <tr>
497
                    <td scope="row" >
498
                        <input name="smbd_taxs" value="<?php echo $tax; ?>" type="radio" class="custom-tax">
499
                    </td>
500
                    <td>
501
                        <label for="smbd_taxs"><?php echo $taxs[$tax]->labels->name; ?> </label>
502
                    </td>
503
                </tr>
504
<?php
505
			}
506
?>
507
            </table>
508
509
            <h4><?php _e( 'The selected taxonomy has the following terms. Select the terms from which you want to delete posts', 'bulk-delete' ) ?></h4>
510
            <p><?php _e( 'Note: The post count below for each term is the total number of posts in that term, irrespective of post type', 'bulk-delete' ); ?>.</p>
511
<?php
512
			foreach ( $terms_array as $tax => $terms ) {
513
?>
514
            <table class="optiontable terms_<?php echo $tax;?> terms">
515
<?php
516
				foreach ( $terms as $term ) {
517
?>
518
                    <tr>
519
                        <td scope="row" >
520
                            <input name="smbd_tax_terms[]" value="<?php echo $term->slug; ?>" type="checkbox" class="terms">
521
                        </td>
522
                        <td>
523
                            <label for="smbd_tax_terms"><?php echo $term->name; ?> (<?php echo $term->count . ' '; _e( 'Posts', 'bulk-delete' ); ?>)</label>
524
                        </td>
525
                    </tr>
526
<?php
527
				}
528
?>
529
            </table>
530
<?php
531
			}
532
?>
533
            <table class="optiontable">
534
				<?php bd_render_filtering_table_header(); ?>
535
				<?php bd_render_restrict_settings( 'taxs' ); ?>
536
				<?php bd_render_delete_settings( 'taxs' ); ?>
537
				<?php bd_render_private_post_settings( 'taxs' ); ?>
538
				<?php bd_render_limit_settings( 'taxs' ); ?>
539
				<?php bd_render_cron_settings( 'taxs', 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-taxonomy/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-stx' ); ?>
540
            </table>
541
542
            </fieldset>
543
<?php
544
			bd_render_submit_button( 'delete_posts_by_taxonomy' );
545
		} else {
546
?>
547
            <h4><?php _e( "This WordPress installation doesn't have any non-empty custom taxonomies defined", 'bulk-delete' ) ?></h4>
548
<?php
549
		}
550
	}
551
552
	/**
553
	 * Process Delete posts by Taxonomy Request.
554
	 *
555
	 * @static
556
	 *
557
	 * @since 5.0
558
	 */
559
	public static function do_delete_posts_by_taxonomy() {
560
		$delete_options                       = array();
561
		$delete_options['post_type']          = array_get( $_POST, 'smbd_tax_post_type', 'post' );
562
		$delete_options['selected_taxs']      = array_get( $_POST, 'smbd_taxs' );
563
		$delete_options['selected_tax_terms'] = array_get( $_POST, 'smbd_tax_terms' );
564
		$delete_options['restrict']           = array_get_bool( $_POST, 'smbd_taxs_restrict', false );
565
		$delete_options['private']            = array_get_bool( $_POST, 'smbd_taxs_private' );
566
		$delete_options['limit_to']           = absint( array_get( $_POST, 'smbd_taxs_limit_to', 0 ) );
567
		$delete_options['force_delete']       = array_get_bool( $_POST, 'smbd_taxs_force_delete', false );
568
569
		$delete_options['date_op']            = array_get( $_POST, 'smbd_taxs_op' );
570
		$delete_options['days']               = absint( array_get( $_POST, 'smbd_taxs_days' ) );
571
572
		if ( array_get( $_POST, 'smbd_taxs_cron', 'false' ) == 'true' ) {
573
			$freq = $_POST['smbd_taxs_cron_freq'];
574
			$time = strtotime( $_POST['smbd_taxs_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 );
575
576
			if ( $freq == -1 ) {
577
				wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_TAXONOMY, array( $delete_options ) );
578
			} else {
579
				wp_schedule_event( $time, $freq, Bulk_Delete::CRON_HOOK_TAXONOMY, array( $delete_options ) );
580
			}
581
			$msg = __( 'Posts from the selected custom taxonomies are scheduled for deletion.', 'bulk-delete' ) . ' ' .
582
				sprintf( __( 'See the full list of <a href = "%s">scheduled tasks</a>' , 'bulk-delete' ), get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG );
583
		} else {
584
			$deleted_count = self::delete_posts_by_taxonomy( $delete_options );
585
			$msg           = sprintf( _n( 'Deleted %d post from the selected custom taxonomies', 'Deleted %d posts from the selected custom taxonomies' , $deleted_count, 'bulk-delete' ), $deleted_count );
586
		}
587
588
		add_settings_error(
589
			Bulk_Delete::POSTS_PAGE_SLUG,
590
			'deleted-posts',
591
			$msg,
592
			'updated'
593
		);
594
	}
595
596
	/**
597
	 * Delete posts by custom taxonomy.
598
	 *
599
	 * @since 5.0
600
	 * @static
601
	 *
602
	 * @param array $delete_options Options for deleting posts
603
	 *
604
	 * @return int $posts_deleted  Number of posts that were deleted
605
	 */
606
	public static function delete_posts_by_taxonomy( $delete_options ) {
607
		// For compatibility reasons set default post type to 'post'
608
		$post_type = array_get( $delete_options, 'post_type', 'post' );
609
610
		if ( array_key_exists( 'taxs_op', $delete_options ) ) {
611
			$delete_options['date_op'] = $delete_options['taxs_op'];
612
			$delete_options['days']    = $delete_options['taxs_days'];
613
		}
614
615
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
616
617
		$selected_taxs      = $delete_options['selected_taxs'];
618
		$selected_tax_terms = $delete_options['selected_tax_terms'];
619
620
		$options = array(
621
			'post_status' => 'publish',
622
			'post_type'   => $post_type,
623
			'tax_query'   => array(
624
				array(
625
					'taxonomy' => $selected_taxs,
626
					'terms'    => $selected_tax_terms,
627
					'field'    => 'slug',
628
				),
629
			),
630
		);
631
632
		$options  = bd_build_query_options( $delete_options, $options );
633
		$post_ids = bd_query( $options );
634
		foreach ( $post_ids as $post_id ) {
635
			// $force delete parameter to custom post types doesn't work
636
			if ( $delete_options['force_delete'] ) {
637
				wp_delete_post( $post_id, true );
638
			} else {
639
				wp_trash_post( $post_id );
640
			}
641
		}
642
643
		return count( $post_ids );
644
	}
645
646
	/**
647
	 * Render delete by custom post type box.
648
	 *
649
	 * @static
650
	 */
651
	public static function render_delete_posts_by_post_type_box() {
652
		if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_POST_TYPE ) ) {
653
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . Bulk_Delete::POSTS_PAGE_SLUG );
654
655
			return;
656
		}
657
658
		$types_array = array();
659
660
		$types = get_post_types( array(
661
				'_builtin' => false,
662
			), 'names'
663
		);
664
665
		if ( count( $types ) > 0 ) {
666
			foreach ( $types as $type ) {
667
				$post_count = wp_count_posts( $type );
668
				if ( $post_count->publish > 0 ) {
669
					$types_array["$type-publish"] = $post_count->publish;
670
				}
671
				if ( $post_count->future > 0 ) {
672
					$types_array["$type-future"] = $post_count->future;
673
				}
674
				if ( $post_count->pending > 0 ) {
675
					$types_array["$type-pending"] = $post_count->pending;
676
				}
677
				if ( $post_count->draft > 0 ) {
678
					$types_array["$type-draft"] = $post_count->draft;
679
				}
680
				if ( $post_count->private > 0 ) {
681
					$types_array["$type-private"] = $post_count->private;
682
				}
683
			}
684
		}
685
686
		if ( count( $types_array ) > 0 ) {
687
?>
688
            <!-- Custom post type Start-->
689
            <h4><?php _e( 'Select the custom post types from which you want to delete posts', 'bulk-delete' ) ?></h4>
690
691
            <fieldset class="options">
692
            <table class="optiontable">
693
<?php
694
			foreach ( $types_array as $type => $count ) {
695
?>
696
                <tr>
697
                    <td scope="row" >
698
                        <input name="smbd_types[]" value="<?php echo $type; ?>" type="checkbox">
699
                    </td>
700
                    <td>
701
						<label for="smbd_types"><?php echo BD_Util::display_post_type_status( $type ), ' (', $count, ')'; ?></label>
702
                    </td>
703
                </tr>
704
<?php
705
			}
706
707
			bd_render_filtering_table_header();
708
			bd_render_restrict_settings( 'types' );
709
			bd_render_delete_settings( 'types' );
710
			bd_render_limit_settings( 'types' );
711
			bd_render_cron_settings( 'types', 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-post-type/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-spt' );
712
?>
713
            </table>
714
            </fieldset>
715
<?php
716
			bd_render_submit_button( 'delete_posts_by_post_type' );
717
		} else {
718
            printf( '<h4>%s</h4>', __( "This WordPress installation doesn't have any non-empty custom post types", 'bulk-delete' ) );
719
		}
720
	}
721
722
	/**
723
	 * Process request to delete posts by post type.
724
	 *
725
	 * @static
726
	 *
727
	 * @since 5.0
728
	 */
729
	public static function do_delete_posts_by_post_type() {
730
		$delete_options                   = array();
731
732
		$delete_options['selected_types'] = array_get( $_POST, 'smbd_types' );
733
		$delete_options['restrict']       = array_get_bool( $_POST, 'smbd_types_restrict', false );
734
		$delete_options['limit_to']       = absint( array_get( $_POST, 'smbd_types_limit_to', 0 ) );
735
		$delete_options['force_delete']   = array_get_bool( $_POST, 'smbd_types_force_delete', false );
736
737
		$delete_options['date_op']        = array_get( $_POST, 'smbd_types_op' );
738
		$delete_options['days']           = absint( array_get( $_POST, 'smbd_types_days' ) );
739
740
		if ( array_get( $_POST, 'smbd_types_cron', 'false' ) == 'true' ) {
741
			$freq = $_POST['smbd_types_cron_freq'];
742
			$time = strtotime( $_POST['smbd_types_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 );
743
744
			if ( $freq == -1 ) {
745
				wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_POST_TYPE, array( $delete_options ) );
746
			} else {
747
				wp_schedule_event( $time, $freq, Bulk_Delete::CRON_HOOK_POST_TYPE, array( $delete_options ) );
748
			}
749
750
			$msg = __( 'Posts from the selected custom post type are scheduled for deletion.', 'bulk-delete' ) . ' ' .
751
				sprintf( __( 'See the full list of <a href = "%s">scheduled tasks</a>' , 'bulk-delete' ), get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG );
752
		} else {
753
			$deleted_count = self::delete_posts_by_post_type( $delete_options );
754
			$msg           = sprintf( _n( 'Deleted %d post from the selected custom post type', 'Deleted %d posts from the selected custom post type' , $deleted_count, 'bulk-delete' ), $deleted_count );
755
		}
756
757
		add_settings_error(
758
			Bulk_Delete::POSTS_PAGE_SLUG,
759
			'deleted-posts',
760
			$msg,
761
			'updated'
762
		);
763
	}
764
765
	/**
766
	 * Delete posts by custom post type.
767
	 *
768
	 * @static
769
	 *
770
	 * @since  5.0
771
	 *
772
	 * @param array $delete_options Options for deleting posts
773
	 *
774
	 * @return int $posts_deleted  Number of posts that were deleted
775
	 */
776
	public static function delete_posts_by_post_type( $delete_options ) {
777
		// Backward compatibility code. Will be removed in Bulk Delete v6.0
778
		if ( array_key_exists( 'types_op', $delete_options ) ) {
779
			$delete_options['date_op'] = $delete_options['types_op'];
780
			$delete_options['days']    = $delete_options['types_days'];
781
		}
782
783
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
784
785
		$count          = 0;
786
		$selected_types = $delete_options['selected_types'];
787
788
		foreach ( $selected_types as $selected_type ) {
789
			$type_status = BD_Util::split_post_type_status( $selected_type );
790
791
			$type        = $type_status['type'];
792
			$status      = $type_status['status'];
793
794
			$options = array(
795
				'post_status' => $status,
796
				'post_type'   => $type,
797
			);
798
799
			$options  = bd_build_query_options( $delete_options, $options );
800
			$post_ids = bd_query( $options );
801
			foreach ( $post_ids as $post_id ) {
802
				// $force delete parameter to custom post types doesn't work
803
				if ( $delete_options['force_delete'] ) {
804
					wp_delete_post( $post_id, true );
805
				} else {
806
					wp_trash_post( $post_id );
807
				}
808
			}
809
810
			$count += count( $post_ids );
811
		}
812
813
		return $count;
814
	}
815
816
	/**
817
	 * Render delete by url box.
818
	 *
819
	 * @static
820
	 */
821
	public static function render_delete_posts_by_url_box() {
822
		if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_URL ) ) {
823
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . Bulk_Delete::POSTS_PAGE_SLUG );
824
825
			return;
826
		}
827
?>
828
        <!-- URLs start-->
829
        <h4><?php _e( 'Delete posts and pages that have the following Permalink', 'bulk-delete' ); ?></h4>
830
831
        <fieldset class="options">
832
        <table class="optiontable">
833
            <tr>
834
                <td scope="row" colspan="2">
835
                    <label for="smdb_specific_pages"><?php _e( 'Enter one post url (not post ids) per line', 'bulk-delete' ); ?></label>
836
                    <br>
837
                    <textarea style="width: 450px; height: 80px;" id="smdb_specific_pages_urls" name="smdb_specific_pages_urls" rows="5" columns="80"></textarea>
838
                </td>
839
            </tr>
840
841
			<?php bd_render_filtering_table_header(); ?>
842
			<?php bd_render_delete_settings( 'specific' ); ?>
843
844
        </table>
845
        </fieldset>
846
<?php
847
		bd_render_submit_button( 'delete_posts_by_url' );
848
	}
849
850
	/**
851
	 * Delete posts by url.
852
	 *
853
	 * @static
854
	 *
855
	 * @since 5.0
856
	 */
857
	public static function do_delete_posts_by_url() {
858
		$force_delete = array_get_bool( $_POST, 'smbd_specific_force_delete', false );
859
860
		$urls = preg_split( '/\r\n|\r|\n/', array_get( $_POST, 'smdb_specific_pages_urls' ) );
861
		foreach ( $urls as $url ) {
862
			$checkedurl = $url;
863
			if ( substr( $checkedurl , 0, 1 ) == '/' ) {
864
				$checkedurl = get_site_url() . $checkedurl ;
865
			}
866
			$postid = url_to_postid( $checkedurl );
867
			wp_delete_post( $postid, $force_delete );
868
		}
869
870
		$deleted_count = count( $urls );
871
		$msg           = sprintf( _n( 'Deleted %d post with the specified urls', 'Deleted %d posts with the specified urls' , $deleted_count, 'bulk-delete' ), $deleted_count );
872
873
		add_settings_error(
874
			Bulk_Delete::POSTS_PAGE_SLUG,
875
			'deleted-posts',
876
			$msg,
877
			'updated'
878
		);
879
	}
880
881
	/**
882
	 * Render delete by post revisions box.
883
	 *
884
	 * @static
885
	 */
886
	public static function render_posts_by_revision_box() {
887
		global $wpdb;
888
889
		if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_POST_REVISION ) ) {
890
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . Bulk_Delete::POSTS_PAGE_SLUG );
891
892
			return;
893
		}
894
895
		$revisions = $wpdb->get_var( "select count(*) from $wpdb->posts where post_type = 'revision'" );
896
?>
897
        <!-- Post Revisions start-->
898
        <h4><?php _e( 'Select the posts which you want to delete', 'bulk-delete' ); ?></h4>
899
900
        <fieldset class="options">
901
        <table class="optiontable">
902
            <tr>
903
                <td>
904
                    <input name="smbd_revisions" id ="smbd_revisions" value="revisions" type="checkbox">
905
                    <label for="smbd_revisions"><?php _e( 'All Revisions', 'bulk-delete' ); ?> (<?php echo $revisions . ' '; _e( 'Revisions', 'bulk-delete' ); ?>)</label>
906
                </td>
907
            </tr>
908
909
        </table>
910
        </fieldset>
911
<?php
912
		bd_render_submit_button( 'delete_posts_by_revision' );
913
	}
914
915
	/**
916
	 * Process delete revisions request.
917
	 *
918
	 * @static
919
	 *
920
	 * @since 5.0
921
	 */
922
	public static function do_delete_posts_by_revision() {
923
		$delete_options = array( 'revisions' => array_get( $_POST, 'smbd_revisions' ) );
924
925
		$deleted_count = self::delete_posts_by_revision( $delete_options );
926
927
		$msg = sprintf( _n( 'Deleted %d post revision', 'Deleted %d post revisions' , $deleted_count, 'bulk-delete' ), $deleted_count );
928
929
		add_settings_error(
930
			Bulk_Delete::POSTS_PAGE_SLUG,
931
			'deleted-posts',
932
			$msg,
933
			'updated'
934
		);
935
	}
936
937
	/**
938
	 * Delete all post revisions.
939
	 *
940
	 * @since 5.0
941
	 * @static
942
	 *
943
	 * @param array $delete_options
944
	 *
945
	 * @return int The number of posts that were deleted
946
	 */
947
	public static function delete_posts_by_revision( $delete_options ) {
948
		global $wpdb;
949
950
		// Revisions
951
		if ( 'revisions' == $delete_options['revisions'] ) {
952
			$revisions = $wpdb->get_results( "select ID from $wpdb->posts where post_type = 'revision'" );
953
954
			foreach ( $revisions as $revision ) {
955
				wp_delete_post( $revision->ID );
956
			}
957
958
			return count( $revisions );
959
		}
960
961
		return 0;
962
	}
963
964
	/**
965
	 * Filter JS Array and add validation hooks.
966
	 *
967
	 * @since 5.4
968
	 * @static
969
	 *
970
	 * @param array $js_array JavaScript Array
971
	 *
972
	 * @return array Modified JavaScript Array
973
	 */
974
	public static function filter_js_array( $js_array ) {
975
		$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' );
976
		$js_array['msg']['selectPostOption']   = __( 'Please select posts from at least one option', 'bulk-delete' );
977
978
		$js_array['validators']['delete_posts_by_category'] = 'validateSelect2';
979
		$js_array['error_msg']['delete_posts_by_category']  = 'selectCategory';
980
		$js_array['msg']['selectCategory']                  = __( 'Please select at least one category', 'bulk-delete' );
981
982
		$js_array['validators']['delete_posts_by_tag']     = 'validateSelect2';
983
		$js_array['error_msg']['delete_posts_by_category'] = 'selectTag';
984
		$js_array['msg']['selectTag']                      = __( 'Please select at least one tag', 'bulk-delete' );
985
986
		$js_array['validators']['delete_posts_by_url'] = 'validateUrl';
987
		$js_array['error_msg']['delete_posts_by_url']  = 'enterUrl';
988
		$js_array['msg']['enterUrl']                   = __( 'Please enter at least one post url', 'bulk-delete' );
989
990
		$js_array['dt_iterators'][] = '_cats';
991
		$js_array['dt_iterators'][] = '_tags';
992
		$js_array['dt_iterators'][] = '_taxs';
993
		$js_array['dt_iterators'][] = '_types';
994
		$js_array['dt_iterators'][] = '_post_status';
995
996
		return $js_array;
997
	}
998
999
	/**
1000
	 * Process delete cron job request.
1001
	 * This should ideally go in a separate class. But I was
1002
	 * lazy to create a separate class for a single function.
1003
	 *
1004
	 * @since 5.0
1005
	 * @static
1006
	 */
1007
	public static function do_delete_cron() {
1008
		$cron_id    = absint( $_GET['cron_id'] );
1009
		$cron_items = BD_Util::get_cron_schedules();
1010
		wp_unschedule_event( $cron_items[$cron_id]['timestamp'], $cron_items[$cron_id]['type'], $cron_items[$cron_id]['args'] );
1011
1012
		$msg = __( 'The selected scheduled job was successfully deleted ', 'bulk-delete' );
1013
1014
		add_settings_error(
1015
			Bulk_Delete::CRON_PAGE_SLUG,
1016
			'deleted-cron',
1017
			$msg,
1018
			'updated'
1019
		);
1020
	}
1021
}
1022
1023
// hooks
1024
add_action( 'bd_delete_posts_by_status'    , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_status' ) );
1025
add_action( 'bd_delete_posts_by_category'  , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_category' ) );
1026
add_action( 'bd_delete_posts_by_tag'       , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_tag' ) );
1027
add_action( 'bd_delete_posts_by_taxonomy'  , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_taxonomy' ) );
1028
add_action( 'bd_delete_posts_by_post_type' , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_post_type' ) );
1029
add_action( 'bd_delete_posts_by_url'       , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_url' ) );
1030
add_action( 'bd_delete_posts_by_revision'  , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_revision' ) );
1031
1032
add_action( 'bd_delete_cron'               , array( 'Bulk_Delete_Posts', 'do_delete_cron' ) );
1033
add_filter( 'bd_javascript_array'          , array( 'Bulk_Delete_Posts', 'filter_js_array' ) );
1034
?>
1035