Completed
Pull Request — dev/6.0.0 (#223)
by Rajan
07:41 queued 03:51
created

DeletePostsByCategoryMetabox::render()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 53
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 45
nc 2
nop 0
dl 0
loc 53
ccs 0
cts 17
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
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
	protected function initialize() {
15
		$this->item_type     = 'posts';
16
		$this->field_slug    = 'cats';
17
		$this->meta_box_slug = 'bd_by_category';
18
		$this->action        = 'delete_posts_by_category';
19
		$this->cron_hook     = 'do-bulk-delete-cat';
20
		$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';
21
		$this->messages      = array(
22
			'box_label' => __( 'By Post Category', 'bulk-delete' ),
23
			'scheduled' => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
24
		);
25
	}
26
27
	public function render() {
28
        ?>
29
        <!-- Category Start-->
30
        <h4><?php _e( 'Select the post type from which you want to delete posts by category', 'bulk-delete' ); ?></h4>
31
        <fieldset class="options">
32
            <table class="optiontable">
33
				<?php bd_render_post_type_dropdown( 'cats' ); ?>
34
            </table>
35
36
            <h4><?php _e( 'Select the categories from which you wan to delete posts', 'bulk-delete' ); ?></h4>
37
            <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>
38
			<?php
39
			$bd_select2_ajax_limit_categories = apply_filters( 'bd_select2_ajax_limit_categories', 50 );
40
41
			$categories = get_categories( array(
42
					'hide_empty'    => false,
43
					'number'        => $bd_select2_ajax_limit_categories,
44
				)
45
			);
46
			?>
47
            <table class="form-table">
48
                <tr>
49
                    <td scope="row">
50
						<?php if( count($categories) >= $bd_select2_ajax_limit_categories ){?>
51
                            <select class="select2Ajax" name="smbd_cats[]" data-taxonomy="category" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>">
52
                                <option value="all" selected="selected"><?php _e( 'All Categories', 'bulk-delete' ); ?></option>
53
                            </select>
54
						<?php }else{?>
55
                            <select class="select2" name="smbd_cats[]" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>">
56
                                <option value="all" selected="selected"><?php _e( 'All Categories', 'bulk-delete' ); ?></option>
57
								<?php foreach ( $categories as $category ) { ?>
58
                                    <option value="<?php echo $category->cat_ID; ?>"><?php echo $category->cat_name, ' (', $category->count, ' ', __( 'Posts', 'bulk-delete' ), ')'; ?></option>
59
								<?php } ?>
60
                            </select>
61
						<?php }?>
62
                    </td>
63
                </tr>
64
            </table>
65
66
			<table class="optiontable">
67
				<?php
68
				$this->render_filtering_table_header();
69
				$this->render_restrict_settings();
70
				$this->render_delete_settings();
71
				//$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...
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_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

79
		$this->/** @scrutinizer ignore-call */ 
80
         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...
80
	}
81
82
	protected function convert_user_input_to_options( $request, $options ) {
83
		$options['post_type']     = bd_array_get( $_POST, 'smbd_cats_post_type', 'post' );
84
		$options['selected_cats'] = bd_array_get( $_POST, 'smbd_cats' );
85
		$options['private']       = bd_array_get_bool( $_POST, 'smbd_cats_private', false );
86
87
		return $options;
88
	}
89
90
	public function delete( $delete_options ) {
91
		if ( array_key_exists( 'tags_op', $delete_options ) ) {
92
			$delete_options['date_op'] = $delete_options['tags_op'];
93
			$delete_options['days']    = $delete_options['tags_days'];
94
		}
95
96
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
97
98
		$options       = array();
99
		$selected_tags = $delete_options['selected_tags'];
100
		if ( in_array( 'all', $selected_tags ) ) {
101
			$options['tag__not__in'] = array(0);
102
		} else {
103
			$options['tag__in'] = $selected_tags;
104
		}
105
106
		$options  = bd_build_query_options( $delete_options, $options );
107
		$post_ids = bd_query( $options );
108
		foreach ( $post_ids as $post_id ) {
109
			wp_delete_post( $post_id, $delete_options['force_delete'] );
110
		}
111
112
		$posts_deleted += count( $post_ids );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $posts_deleted seems to be never defined.
Loading history...
113
114
		return $posts_deleted;
115
	}
116
117
	protected function get_success_message( $items_deleted ) {
118
		/* translators: 1 Number of posts deleted */
119
		return _n( 'Deleted %d post with the selected post category', 'Deleted %d posts with the selected post category', $items_deleted, 'bulk-delete' );
120
	}
121
}
122