Completed
Push — master ( 2a5bdf...b12cef )
by Sudar
08:15 queued 04:50
created

Bulk_Delete_Posts   F

Complexity

Total Complexity 83

Size/Duplication

Total Lines 1007
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1007
rs 1.2189
c 0
b 0
f 0
wmc 83

23 Methods

Rating   Name   Duplication   Size   Complexity  
A do_delete_posts_by_url() 0 21 3
B do_delete_posts_by_post_type() 0 33 3
A do_delete_cron() 0 12 1
B do_delete_posts_by_tag() 0 32 3
A render_delete_posts_by_status_box() 0 58 4
B delete_posts_by_taxonomy() 0 38 4
B render_delete_posts_by_tag_box() 0 41 4
B delete_posts_by_status() 0 29 4
A do_delete_posts_by_revision() 0 12 1
B render_posts_by_revision_box() 0 27 2
B render_delete_posts_by_url_box() 0 27 2
B delete_posts_by_category() 0 32 5
B do_delete_posts_by_taxonomy() 0 34 3
A render_delete_posts_by_category_box() 0 47 3
A filter_js_array() 0 23 1
B do_delete_posts_by_status() 0 40 3
B delete_posts_by_post_type() 0 38 5
B do_delete_posts_by_category() 0 35 3
C render_delete_posts_by_taxonomy_box() 0 91 9
A delete_posts_by_revision() 0 15 3
A delete_sticky_posts() 0 8 2
C render_delete_posts_by_post_type_box() 0 68 11
B delete_posts_by_tag() 0 24 4

How to fix   Complexity   

Complex Class

Complex classes like Bulk_Delete_Posts often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Bulk_Delete_Posts, and based on these observations, apply Extract Interface, too.

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