Passed
Push — 217-feature/delete-posts-by-po... ( 18f55b...407d9c )
by Rajan
06:19
created

DeletePostsByStatusMetabox::delete_sticky_posts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace BulkWP\BulkDelete\Core\Posts\Metabox;
3
4
use BulkWP\BulkDelete\Core\Posts\PostsMetabox;
5
6 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
7
8
/**
9
 * Delete Posts by Status Metabox.
10
 *
11
 * @since 6.0.0
12
 */
13
class DeletePostsByStatusMetabox extends PostsMetabox {
14
	protected function initialize() {
15
		$this->item_type     = 'posts';
16
		$this->field_slug    = 'post_status';
17
		$this->meta_box_slug = 'bd_posts_by_status';
18
		$this->action        = 'delete_posts_by_status';
19
		$this->cron_hook     = 'do-bulk-delete-post-by-status';
20
		$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';
21
		$this->messages      = array(
22
			'box_label' => __( 'By Post Status', 'bulk-delete' ),
23
			'scheduled' => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
24
		);
25
	}
26
27
	public function render() {
28
		$post_statuses = bd_get_post_statuses();
29
		$post_count    = wp_count_posts();
30
		?>
31
		<h4><?php _e( 'Select the post statuses from which you want to delete posts', 'bulk-delete' ); ?></h4>
32
33
		<fieldset class="options">
34
		<table class="optiontable">
35
36
			<?php foreach ( $post_statuses as $post_status ) : ?>
37
				<tr>
38
					<td>
39
						<input name="smbd_post_status[]" id="smbd_<?php echo esc_attr( $post_status->name ); ?>"
40
							value="<?php echo esc_attr( $post_status->name ); ?>" type="checkbox">
41
42
						<label for="smbd_<?php echo esc_attr( $post_status->name ); ?>">
43
							<?php echo esc_html( $post_status->label ), ' '; ?>
44
							<?php if ( property_exists( $post_count, $post_status->name ) ) : ?>
45
								(<?php echo absint( $post_count->{ $post_status->name } ) . ' ', __( 'Posts', 'bulk-delete' ); ?>)
46
							<?php endif; ?>
47
						</label>
48
					</td>
49
				</tr>
50
			<?php endforeach; ?>
51
52
			<?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

52
			<?php $sticky_post_count = count( /** @scrutinizer ignore-type */ get_option( 'sticky_posts' ) ); ?>
Loading history...
53
54
			<tr>
55
				<td>
56
					<input name="smbd_sticky" id="smbd_sticky" value="on" type="checkbox">
57
					<label for="smbd_sticky">
58
						<?php echo __( 'All Sticky Posts', 'bulk-delete' ), ' '; ?>
59
						(<?php echo absint( $sticky_post_count ), ' ', __( 'Posts', 'bulk-delete' ); ?>)
60
						<?php echo '<strong>', __( 'Note', 'bulk-delete' ), '</strong>: ', __( 'The date filter will not work for sticky posts', 'bulk-delete' ); ?>
61
					</label>
62
				</td>
63
			</tr>
64
65
		</table>
66
67
		<table class="optiontable">
68
			<?php
69
			$this->render_filtering_table_header();
70
			$this->render_restrict_settings();
71
			$this->render_delete_settings();
72
			$this->render_limit_settings();
73
			$this->render_cron_settings();
74
			?>
75
		</table>
76
77
		</fieldset>
78
<?php
79
		$this->render_submit_button();
80
	}
81
82
	protected function convert_user_input_to_options( $request, $options ) {
83
		$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

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