Completed
Pull Request — dev/6.0.0 (#223)
by Rajan
19:42 queued 12:28
created

DeletePostsByCategoryMetabox::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 10
cp 0
crap 2
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 Category Metabox.
10
 *
11
 * @since 6.0.0
12
 */
13
class DeletePostsByCategoryMetabox extends PostsMetabox {
14
	/**
15
	 * @var int Limit for the categories.
16
	 */
17
	private $cat_limit = 50;
18
	protected function initialize() {
19
		$this->item_type     = 'posts';
20
		$this->field_slug    = 'cats';
21
		$this->meta_box_slug = 'bd_by_category';
22
		$this->action        = 'delete_posts_by_category';
23
		$this->cron_hook     = 'do-bulk-delete-cat';
24
		$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-category/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sc';
25
		$this->messages      = array(
26
			'box_label' => __( 'By Post Category', 'bulk-delete' ),
27
			'scheduled' => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
28
		);
29
	}
30
31
	/**
32
	 * Render Delete posts by category box.
33
	 */
34
	public function render() {
35
        ?>
36
        <!-- Category Start-->
37
        <h4><?php _e( 'Select the post type from which you want to delete posts by category', 'bulk-delete' ); ?></h4>
38
        <fieldset class="options">
39
            <table class="optiontable">
40
				<?php bd_render_post_type_dropdown( 'cats' ); ?>
41
            </table>
42
43
            <h4><?php _e( 'Select the categories from which you wan to delete posts', 'bulk-delete' ); ?></h4>
44
            <p><?php _e( 'Note: The post count below for each category is the total number of posts in that category, irrespective of post type', 'bulk-delete' ); ?>.</p>
45
46
			<?php
47
			/**
48
			 * Filter to modify select2 ajax call category limit.
49
			 *
50
			 * @param int $cat_limit
51
			 */
52
			$bd_select2_ajax_limit_categories = apply_filters( 'bd_select2_ajax_limit_categories', $this->cat_limit );
53
54
			$categories = get_categories( array(
55
					'hide_empty'    => false,
56
					'number'        => $bd_select2_ajax_limit_categories,
57
				)
58
			);
59
			?>
60
            <table class="form-table">
61
                <tr>
62
                    <td scope="row">
63
						<?php if( count($categories) >= $bd_select2_ajax_limit_categories ){?>
64
                            <select class="select2Ajax" name="smbd_cats[]" data-taxonomy="category" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>">
65
                                <option value="all" selected="selected"><?php _e( 'All Categories', 'bulk-delete' ); ?></option>
66
                            </select>
67
						<?php }else{?>
68
                            <select class="select2" name="smbd_cats[]" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>">
69
                                <option value="all" selected="selected"><?php _e( 'All Categories', 'bulk-delete' ); ?></option>
70
								<?php foreach ( $categories as $category ) { ?>
71
                                    <option value="<?php echo $category->cat_ID; ?>"><?php echo $category->cat_name, ' (', $category->count, ' ', __( 'Posts', 'bulk-delete' ), ')'; ?></option>
72
								<?php } ?>
73
                            </select>
74
						<?php }?>
75
                    </td>
76
                </tr>
77
            </table>
78
79
			<table class="optiontable">
80
				<?php
81
				$this->render_filtering_table_header();
82
				$this->render_restrict_settings();
83
				$this->render_delete_settings();
84
				$this->render_private_post_settings();
85
				$this->render_limit_settings();
86
				$this->render_cron_settings();
87
				?>
88
			</table>
89
90
		</fieldset>
91
<?php
92
		$this->render_submit_button( 'delete_posts_by_category' );
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_category'. ( Ignorable by Annotation )

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

92
		$this->/** @scrutinizer ignore-call */ 
93
         render_submit_button( 'delete_posts_by_category' );

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...
93
	}
94
95
	/**
96
	 * Process delete posts user inputs by category.
97
	 *
98
	 * @param array $options Options for deleting posts
99
	 * @param mixed $request
100
	 *
101
	 * @return array $options  Inputs from user for posts that were need to delete
102
	 */
103
	protected function convert_user_input_to_options( $request, $options ) {
104
		$options['post_type']     = bd_array_get( $_POST, 'smbd_cats_post_type', 'post' );
105
		$options['selected_cats'] = bd_array_get( $_POST, 'smbd_cats' );
106
		$options['private']       = bd_array_get_bool( $_POST, 'smbd_cats_private', false );
107
108
		return $options;
109
	}
110
111
	/**
112
	 * Delete posts by category.
113
	 *
114
	 * @param array $delete_options Options for deleting posts
115
	 *
116
	 * @return int $posts_deleted  Number of posts that were deleted
117
	 */
118
	public function delete( $delete_options ) {
119
		$posts_deleted               = 0;
120
		$delete_options['post_type'] = bd_array_get( $delete_options, 'post_type', 'post' );
121
122
		if ( array_key_exists( 'cats_op', $delete_options ) ) {
123
			$delete_options['date_op'] = $delete_options['cats_op'];
124
			$delete_options['days']    = $delete_options['cats_days'];
125
		}
126
127
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
128
129
		$options       = array();
130
		$selected_cats = $delete_options['selected_cats'];
131
		if ( in_array( 'all', $selected_cats ) ) {
132
			$options['category__not__in'] = array(0);
133
		} else {
134
			$options['category__in'] = $selected_cats;
135
		}
136
137
		$options  = bd_build_query_options( $delete_options, $options );
138
		$post_ids = bd_query( $options );
139
140
		foreach ( $post_ids as $post_id ) {
141
			// $force delete parameter to custom post types doesn't work
142
			if ( $delete_options['force_delete'] ) {
143
				wp_delete_post( $post_id, true );
144
			} else {
145
				wp_trash_post( $post_id );
146
			}
147
		}
148
149
		$posts_deleted += count( $post_ids );
150
151
		return $posts_deleted;
152
	}
153
154
	/**
155
	 * Response message for deleting posts.
156
	 *
157
	 * @param int $items_deleted
158
	 *
159
	 * @return string Response message
160
	 */
161
	protected function get_success_message( $items_deleted ) {
162
		/* translators: 1 Number of posts deleted */
163
		return _n( 'Deleted %d post with the selected post category', 'Deleted %d posts with the selected post category', $items_deleted, 'bulk-delete' );
164
	}
165
}
166