Completed
Pull Request — dev/6.0.0 (#223)
by Sudar
06:40 queued 02:58
created

DeletePostsByCategoryMetabox::render()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 53
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 46
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 53
rs 9.5797

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
namespace BulkWP\BulkDelete\Core\Posts\Metabox;
3
4
use BulkWP\BulkDelete\Core\Posts\PostsMetabox;
5
6
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
	private $cat_limit = 50;
15
	protected function initialize() {
16
		$this->item_type     = 'posts';
17
		$this->field_slug    = 'cats';
18
		$this->meta_box_slug = 'bd_by_category';
19
		$this->action        = 'delete_posts_by_category';
20
		$this->cron_hook     = 'do-bulk-delete-cat';
21
		$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';
22
		$this->messages      = array(
23
			'box_label' => __( 'By Post Category', 'bulk-delete' ),
24
			'scheduled' => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
25
		);
26
	}
27
28
	public function render() {
29
        ?>
30
        <!-- Category Start-->
31
        <h4><?php _e( 'Select the post type from which you want to delete posts by category', 'bulk-delete' ); ?></h4>
32
        <fieldset class="options">
33
            <table class="optiontable">
34
				<?php bd_render_post_type_dropdown( 'cats' ); ?>
35
            </table>
36
37
            <h4><?php _e( 'Select the categories from which you wan to delete posts', 'bulk-delete' ); ?></h4>
38
            <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>
39
			<?php
40
			$bd_select2_ajax_limit_categories = apply_filters( 'bd_select2_ajax_limit_categories', $this->cat_limit );
41
42
			$categories = get_categories( array(
43
					'hide_empty'    => false,
44
					'number'        => $bd_select2_ajax_limit_categories,
45
				)
46
			);
47
			?>
48
            <table class="form-table">
49
                <tr>
50
                    <td scope="row">
51
						<?php if( count($categories) >= $bd_select2_ajax_limit_categories ){?>
52
                            <select class="select2Ajax" name="smbd_cats[]" data-taxonomy="category" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>">
53
                                <option value="all" selected="selected"><?php _e( 'All Categories', 'bulk-delete' ); ?></option>
54
                            </select>
55
						<?php }else{?>
56
                            <select class="select2" name="smbd_cats[]" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>">
57
                                <option value="all" selected="selected"><?php _e( 'All Categories', 'bulk-delete' ); ?></option>
58
								<?php foreach ( $categories as $category ) { ?>
59
                                    <option value="<?php echo $category->cat_ID; ?>"><?php echo $category->cat_name, ' (', $category->count, ' ', __( 'Posts', 'bulk-delete' ), ')'; ?></option>
60
								<?php } ?>
61
                            </select>
62
						<?php }?>
63
                    </td>
64
                </tr>
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_private_post_settings();
73
				$this->render_limit_settings();
74
				$this->render_cron_settings();
75
				?>
76
			</table>
77
78
		</fieldset>
79
<?php
80
		$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

80
		$this->/** @scrutinizer ignore-call */ 
81
         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...
81
	}
82
83
	protected function convert_user_input_to_options( $request, $options ) {
84
		$options['post_type']     = bd_array_get( $_POST, 'smbd_cats_post_type', 'post' );
85
		$options['selected_cats'] = bd_array_get( $_POST, 'smbd_cats' );
86
		$options['private']       = bd_array_get_bool( $_POST, 'smbd_cats_private', false );
87
88
		return $options;
89
	}
90
91
	public function delete( $delete_options ) {
92
		$posts_deleted               = 0;
93
		$delete_options['post_type'] = bd_array_get( $delete_options, 'post_type', 'post' );
94
95
		if ( array_key_exists( 'cats_op', $delete_options ) ) {
96
			$delete_options['date_op'] = $delete_options['cats_op'];
97
			$delete_options['days']    = $delete_options['cats_days'];
98
		}
99
100
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
101
102
		$options       = array();
103
		$selected_cats = $delete_options['selected_cats'];
104
		if ( in_array( 'all', $selected_cats ) ) {
105
			$options['category__not__in'] = array(0);
106
		} else {
107
			$options['category__in'] = $selected_cats;
108
		}
109
110
		$options  = bd_build_query_options( $delete_options, $options );
111
		$post_ids = bd_query( $options );
112
113
		foreach ( $post_ids as $post_id ) {
114
			// $force delete parameter to custom post types doesn't work
115
			if ( $delete_options['force_delete'] ) {
116
				wp_delete_post( $post_id, true );
117
			} else {
118
				wp_trash_post( $post_id );
119
			}
120
		}
121
122
		$posts_deleted += count( $post_ids );
123
124
		return $posts_deleted;
125
	}
126
127
	protected function get_success_message( $items_deleted ) {
128
		/* translators: 1 Number of posts deleted */
129
		return _n( 'Deleted %d post with the selected post category', 'Deleted %d posts with the selected post category', $items_deleted, 'bulk-delete' );
130
	}
131
}
132