Completed
Push — 193-feature/delete-post-revisi... ( 4c008c...2cafa6 )
by Rajan
141:08 queued 137:21
created

include/posts/class-bulk-delete-posts.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Utility class for deleting posts.
4
 *
5
 * @author     Sudar
6
 *
7
 * @package    BulkDelete
8
 */
9
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
10
11
class Bulk_Delete_Posts {
12
	/**
13
	 * Render post status box.
14
	 */
15
	public static function render_delete_posts_by_status_box() {
16
		if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_POST_STATUS ) ) {
17
			/* translators: 1 Number of posts that are deleted. */
18
			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 );
19
20
			return;
21
		}
22
23
		$post_statuses = bd_get_post_statuses();
24
		$post_count    = wp_count_posts();
25
?>
26
		<h4><?php _e( 'Select the post statuses from which you want to delete posts', 'bulk-delete' ); ?></h4>
27
28
		<fieldset class="options">
29
		<table class="optiontable">
30
31
			<?php foreach ( $post_statuses as $post_status ) : ?>
32
				<tr>
33
					<td>
34
						<input name="smbd_post_status[]" id="smbd_<?php echo esc_attr( $post_status->name ); ?>"
35
							value="<?php echo esc_attr( $post_status->name ); ?>" type="checkbox">
36
37
						<label for="smbd_<?php echo esc_attr( $post_status->name ); ?>">
38
							<?php echo esc_html( $post_status->label ), ' '; ?>
39
							<?php if ( property_exists( $post_count, $post_status->name ) ) : ?>
40
								(<?php echo absint( $post_count->{ $post_status->name } ) . ' ', __( 'Posts', 'bulk-delete' ); ?>)
41
							<?php endif; ?>
42
						</label>
43
					</td>
44
				</tr>
45
			<?php endforeach; ?>
46
47
			<?php $sticky_post_count = count( get_option( 'sticky_posts' ) ); ?>
48
49
			<tr>
50
				<td>
51
					<input name="smbd_sticky" id="smbd_sticky" value="on" type="checkbox">
52
					<label for="smbd_sticky">
53
						<?php echo __( 'All Sticky Posts', 'bulk-delete' ), ' '; ?>
54
						(<?php echo absint( $sticky_post_count ), ' ', __( 'Posts', 'bulk-delete' ); ?>)
55
						<?php echo '<strong>', __( 'Note', 'bulk-delete' ), '</strong>: ', __( 'The date filter will not work for sticky posts', 'bulk-delete' ); ?>
56
					</label>
57
				</td>
58
			</tr>
59
60
		</table>
61
62
		<table class="optiontable">
63
			<?php bd_render_filtering_table_header(); ?>
64
			<?php bd_render_restrict_settings( 'post_status' ); ?>
65
			<?php bd_render_delete_settings( 'post_status' ); ?>
66
			<?php bd_render_limit_settings( 'post_status' ); ?>
67
			<?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' ); ?>
68
		</table>
69
70
		</fieldset>
71
<?php
72
		bd_render_submit_button( 'delete_posts_by_status' );
73
	}
74
75
	/**
76
	 * Delete posts by post status.
77
	 *
78
	 * @since 5.0
79
	 * @static
80
	 *
81
	 * Nonce verification is done in the hook that calls this function.
82
	 * phpcs:disable WordPress.CSRF.NonceVerification.NoNonceVerification
83
	 */
84
	public static function do_delete_posts_by_status() {
85
		$delete_options = array();
86
87
		$delete_options['restrict']     = bd_array_get_bool( $_POST, 'smbd_post_status_restrict', false );
88
		$delete_options['limit_to']     = absint( bd_array_get( $_POST, 'smbd_post_status_limit_to', 0 ) );
89
		$delete_options['force_delete'] = bd_array_get_bool( $_POST, 'smbd_post_status_force_delete', false );
90
91
		$delete_options['date_op'] = bd_array_get( $_POST, 'smbd_post_status_op' );
92
		$delete_options['days']    = absint( bd_array_get( $_POST, 'smbd_post_status_days' ) );
93
94
		$delete_options['post_status'] = array_map( 'sanitize_text_field', bd_array_get( $_POST, 'smbd_post_status', array() ) );
95
96
		$delete_options['delete-sticky-posts'] = bd_array_get_bool( $_POST, 'smbd_sticky', false );
97
98
		if ( bd_array_get_bool( $_POST, 'smbd_post_status_cron', false ) ) {
99
			$freq = sanitize_text_field( $_POST['smbd_post_status_cron_freq'] );
100
			$time = strtotime( $_POST['smbd_post_status_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 );
101
102
			if ( -1 === $freq ) {
103
				wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_POST_STATUS, array( $delete_options ) );
104
			} else {
105
				wp_schedule_event( $time, $freq, Bulk_Delete::CRON_HOOK_POST_STATUS, array( $delete_options ) );
106
			}
107
108
			$msg = __( 'Posts with the selected status are scheduled for deletion.', 'bulk-delete' ) . ' ';
109
110
			/* translators: 1 Url to view cron jobs */
111
			$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 );
112
		} else {
113
			$deleted_count = self::delete_posts_by_status( $delete_options );
114
115
			/* translators: 1 Number of posts deleted */
116
			$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 );
117
		}
118
119
		add_settings_error(
120
			Bulk_Delete::POSTS_PAGE_SLUG,
121
			'deleted-posts',
122
			$msg,
123
			'updated'
124
		);
125
	} // phpcs:enable
126
127
	/**
128
	 * Delete posts by post status - drafts, pending posts, scheduled posts etc.
129
	 *
130
	 * @since  5.0
131
	 * @static
132
	 *
133
	 * @param array $delete_options Options for deleting posts.
134
	 *
135
	 * @return int $posts_deleted  Number of posts that were deleted
136
	 */
137
	public static function delete_posts_by_status( $delete_options ) {
138
		$delete_options = bd_convert_old_options_for_delete_post_by_status( $delete_options );
0 ignored issues
show
The call to bd_convert_old_options_for_delete_post_by_status() has too few arguments starting with metabox. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
		$delete_options = /** @scrutinizer ignore-call */ bd_convert_old_options_for_delete_post_by_status( $delete_options );

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

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