Completed
Push — 272-feature/delete-user-post-m... ( f8ddd2...118da4 )
by Sudar
09:39 queued 05:57
created

DeletePostsByStatusModule::get_success_message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Posts\Modules;
4
5
use BulkWP\BulkDelete\Core\Posts\PostsModule;
6
7 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Status Module.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByStatusModule extends PostsModule {
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 11
			'cron_label' => __( 'Delete Post By Status', 'bulk-delete' ),
26
		);
27 11
	}
28
29
	public function render() {
30
		$post_statuses = $this->get_post_statuses();
31
		$post_count    = wp_count_posts();
32
		?>
33
		<h4><?php _e( 'Select the post statuses from which you want to delete posts', 'bulk-delete' ); ?></h4>
34
35
		<fieldset class="options">
36
		<table class="optiontable">
37
38
			<?php foreach ( $post_statuses as $post_status ) : ?>
39
				<tr>
40
					<td>
41
						<input name="smbd_post_status[]" id="smbd_<?php echo esc_attr( $post_status->name ); ?>"
42
							value="<?php echo esc_attr( $post_status->name ); ?>" type="checkbox">
43
44
						<label for="smbd_<?php echo esc_attr( $post_status->name ); ?>">
45
							<?php echo esc_html( $post_status->label ), ' '; ?>
46
							<?php if ( property_exists( $post_count, $post_status->name ) ) : ?>
47
								(<?php echo absint( $post_count->{ $post_status->name } ) . ' ', __( 'Posts', 'bulk-delete' ); ?>)
48
							<?php endif; ?>
49
						</label>
50
					</td>
51
				</tr>
52
			<?php endforeach; ?>
53
54
			<?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

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

85
		$options['post_status'] = array_map( 'sanitize_text_field', bd_array_get( $request, 'smbd_post_status', /** @scrutinizer ignore-type */ array() ) );
Loading history...
86
87
		$options['delete-sticky-posts'] = bd_array_get_bool( $request, 'smbd_sticky', false );
88
89
		return $options;
90
	}
91
92
	/**
93
	 * Delete Sticky post in addition to posts by status.
94
	 *
95
	 * @param array $options Delete options.
96
	 *
97
	 * @return int Number of posts deleted.
98
	 */
99 11
	public function delete( $options ) {
100 11
		$posts_deleted = parent::delete( $options );
101
102 11
		if ( isset( $options['delete-sticky-posts'] ) ) {
103
			$posts_deleted += $this->delete_sticky_posts( $options['force_delete'] );
104
		}
105
106 11
		return $posts_deleted;
107
	}
108
109 11
	protected function build_query( $options ) {
110 11
		if ( empty( $options['post_status'] ) ) {
111
			return array();
112
		}
113
114
		$query = array(
115 11
			'post_status'  => $options['post_status'],
116 11
			'post__not_in' => get_option( 'sticky_posts' ),
117
		);
118
119 11
		return $query;
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