Completed
Pull Request — dev/6.0.0 (#223)
by
unknown
12:26 queued 06:14
created

DeletePostsByCategoryMetabox::render()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 54
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 46
nc 2
nop 0
dl 0
loc 54
ccs 0
cts 18
cp 0
crap 12
rs 9.6716
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
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
	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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
				bd_render_private_post_settings( $this->field_slug );
74
				$this->render_limit_settings();
75
				$this->render_cron_settings();
76
				?>
77
			</table>
78
79
		</fieldset>
80
<?php
81
		$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

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