Passed
Push — 217-feature/delete-posts-by-po... ( d7bccc...aea384 )
by Sudar
04:38
created

DeletePostsByStatusMetabox   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 53.19%

Importance

Changes 0
Metric Value
dl 0
loc 111
ccs 25
cts 47
cp 0.5319
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 53 3
A initialize() 0 10 1
A get_success_message() 0 3 1
A convert_user_input_to_options() 0 6 1
B delete() 0 29 4
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Posts\Metabox;
4
5
use BulkWP\BulkDelete\Core\Posts\PostsMetabox;
6
7 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Status Metabox.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByStatusMetabox extends PostsMetabox {
15 11
	protected function initialize() {
16 11
		$this->item_type     = 'posts';
17 11
		$this->field_slug    = 'post_status';
18 11
		$this->meta_box_slug = 'bd_posts_by_status';
19 11
		$this->action        = 'delete_posts_by_status';
20 11
		$this->cron_hook     = 'do-bulk-delete-post-by-status';
21 11
		$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-status/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sps';
22 11
		$this->messages      = array(
23 11
			'box_label' => __( 'By Post Status', 'bulk-delete' ),
24 11
			'scheduled' => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
25
		);
26 11
	}
27
28
	public function render() {
29
		$post_statuses = $this->get_post_statuses();
30
		$post_count    = wp_count_posts();
31
		?>
32
		<h4><?php _e( 'Select the post statuses from which you want to delete posts', 'bulk-delete' ); ?></h4>
33
34
		<fieldset class="options">
35
		<table class="optiontable">
36
37
			<?php foreach ( $post_statuses as $post_status ) : ?>
38
				<tr>
39
					<td>
40
						<input name="smbd_post_status[]" id="smbd_<?php echo esc_attr( $post_status->name ); ?>"
41
							value="<?php echo esc_attr( $post_status->name ); ?>" type="checkbox">
42
43
						<label for="smbd_<?php echo esc_attr( $post_status->name ); ?>">
44
							<?php echo esc_html( $post_status->label ), ' '; ?>
45
							<?php if ( property_exists( $post_count, $post_status->name ) ) : ?>
46
								(<?php echo absint( $post_count->{ $post_status->name } ) . ' ', __( 'Posts', 'bulk-delete' ); ?>)
47
							<?php endif; ?>
48
						</label>
49
					</td>
50
				</tr>
51
			<?php endforeach; ?>
52
53
			<?php $sticky_post_count = count( get_option( 'sticky_posts' ) ); ?>
0 ignored issues
show
Bug introduced by
It seems like get_option('sticky_posts') can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

53
			<?php $sticky_post_count = count( /** @scrutinizer ignore-type */ get_option( 'sticky_posts' ) ); ?>
Loading history...
54
55
			<tr>
56
				<td>
57
					<input name="smbd_sticky" id="smbd_sticky" value="on" type="checkbox">
58
					<label for="smbd_sticky">
59
						<?php echo __( 'All Sticky Posts', 'bulk-delete' ), ' '; ?>
60
						(<?php echo absint( $sticky_post_count ), ' ', __( 'Posts', 'bulk-delete' ); ?>)
61
						<?php echo '<strong>', __( 'Note', 'bulk-delete' ), '</strong>: ', __( 'The date filter will not work for sticky posts', 'bulk-delete' ); ?>
62
					</label>
63
				</td>
64
			</tr>
65
66
		</table>
67
68
		<table class="optiontable">
69
			<?php
70
			$this->render_filtering_table_header();
71
			$this->render_restrict_settings();
72
			$this->render_delete_settings();
73
			$this->render_limit_settings();
74
			$this->render_cron_settings();
75
			?>
76
		</table>
77
78
		</fieldset>
79
<?php
80
		$this->render_submit_button();
81
	}
82
83
	protected function convert_user_input_to_options( $request, $options ) {
84
		$options['post_status'] = array_map( 'sanitize_text_field', bd_array_get( $request, 'smbd_post_status', array() ) );
0 ignored issues
show
Bug introduced by
array() of type array is incompatible with the type string expected by parameter $default of bd_array_get(). ( Ignorable by Annotation )

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

84
		$options['post_status'] = array_map( 'sanitize_text_field', bd_array_get( $request, 'smbd_post_status', /** @scrutinizer ignore-type */ array() ) );
Loading history...
85
86
		$options['delete-sticky-posts'] = bd_array_get_bool( $request, 'smbd_sticky', false );
87
88
		return $options;
89
	}
90
91 11
	public function delete( $delete_options ) {
92 11
		$delete_options = bd_convert_old_options_for_delete_post_by_status( $delete_options );
93 11
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
94
95 11
		$posts_deleted = 0;
96
97 11
		if ( isset( $delete_options['delete-sticky-posts'] ) ) {
98
			$posts_deleted += $this->delete_sticky_posts( $delete_options['force_delete'] );
99
		}
100
101 11
		if ( empty( $delete_options['post_status'] ) ) {
102
			return $posts_deleted;
103
		}
104
105
		$options = array(
106 11
			'post_status'  => $delete_options['post_status'],
107 11
			'post__not_in' => get_option( 'sticky_posts' ),
108
		);
109
110 11
		$options = bd_build_query_options( $delete_options, $options );
111
112 11
		$post_ids = bd_query( $options );
113 11
		foreach ( $post_ids as $post_id ) {
114 11
			wp_delete_post( $post_id, $delete_options['force_delete'] );
115
		}
116
117 11
		$posts_deleted += count( $post_ids );
118
119 11
		return $posts_deleted;
120
	}
121
122
	protected function get_success_message( $items_deleted ) {
123
		/* translators: 1 Number of pages deleted */
124
		return _n( 'Deleted %d post with the selected post status', 'Deleted %d posts with the selected post status', $items_deleted, 'bulk-delete' );
125
	}
126
}
127