Completed
Push — 217-feature/delete-posts-by-po... ( 2b6cb8...003725 )
by Sudar
06:50
created

DeletePostsByStatusMetabox::delete()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 6
nop 1
dl 0
loc 29
ccs 0
cts 16
cp 0
crap 20
rs 8.5806
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( 'delete_posts_by_status' );
0 ignored issues
show
Unused Code introduced by
The call to BulkWP\BulkDelete\Core\B...:render_submit_button() has too many arguments starting with 'delete_posts_by_status'. ( Ignorable by Annotation )

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

79
		$this->/** @scrutinizer ignore-call */ 
80
         render_submit_button( 'delete_posts_by_status' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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 ( $delete_options['delete-sticky-posts'] ) {
97
			$posts_deleted += self::delete_sticky_posts( $delete_options['force_delete'] );
0 ignored issues
show
Bug introduced by
The method delete_sticky_posts() does not exist on BulkWP\BulkDelete\Core\P...etePostsByStatusMetabox. Did you maybe mean delete()? ( Ignorable by Annotation )

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

97
			$posts_deleted += self::/** @scrutinizer ignore-call */ delete_sticky_posts( $delete_options['force_delete'] );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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