Completed
Push — 233-feature/posts-by-post-stat... ( d2a551...3016f6 )
by Sudar
11:19 queued 07:05
created

DeletePostsByStatusModule::render()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 53
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 47
nc 3
nop 0
dl 0
loc 53
ccs 0
cts 13
cp 0
crap 12
rs 9.5797
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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