DeletePostsByTaxonomyModule::render()   B
last analyzed

Complexity

Conditions 8
Paths 14

Size

Total Lines 94
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 74
nc 14
nop 0
dl 0
loc 94
ccs 0
cts 28
cp 0
crap 72
rs 7.3228
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
3
namespace BulkWP\BulkDelete\Core\Posts\Modules;
4
5
use BulkWP\BulkDelete\Core\Posts\PostsModule;
6
7 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Taxonomy Module.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByTaxonomyModule extends PostsModule {
15 96
	protected function initialize() {
16 96
		$this->item_type     = 'posts';
17 96
		$this->field_slug    = 'taxs';
18 96
		$this->meta_box_slug = 'bd_posts_by_taxonomy';
19 96
		$this->action        = 'bd_delete_posts_by_taxonomy';
20 96
		$this->cron_hook     = 'do-bulk-delete-taxonomy';
21 96
		$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-posts-by-taxonomy/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=addonlist&utm_content=bd-stx';
22 96
		$this->messages      = array(
23 96
			'box_label'  => __( 'By Taxonomy', 'bulk-delete' ),
24 96
			'scheduled'  => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
25 96
			'cron_label' => __( 'Delete Post By Taxonomy', 'bulk-delete' ),
26
		);
27 96
	}
28
29
	public function render() {
30
		$taxs = get_taxonomies( array(), 'objects'
31
		);
32
33
		$terms_array = array();
34
		if ( count( $taxs ) > 0 ) {
35
			foreach ( $taxs as $tax ) {
36
				$terms = get_terms( $tax->name );
37
				if ( count( $terms ) > 0 ) {
0 ignored issues
show
Bug introduced by
It seems like $terms can also be of type WP_Error; 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

37
				if ( count( /** @scrutinizer ignore-type */ $terms ) > 0 ) {
Loading history...
38
					$terms_array[ $tax->name ] = $terms;
39
				}
40
			}
41
		}
42
43
		if ( count( $terms_array ) > 0 ) {
44
			?>
45
			<h4><?php _e( 'Select the post type from which you want to delete posts by taxonomy', 'bulk-delete' ); ?></h4>
46
47
			<fieldset class="options">
48
				<table class="optiontable">
49
					<?php $this->render_post_type_dropdown(); ?>
50
				</table>
51
52
				<h4>
53
					<?php _e( 'Select the taxonomies from which you want to delete posts', 'bulk-delete' ); ?>
54
				</h4>
55
56
				<table class="optiontable">
57
					<?php
58
					foreach ( $terms_array as $tax => $terms ) {
59
						?>
60
						<tr>
61
							<td scope="row">
62
								<input name="smbd_taxs" value="<?php echo esc_attr( $tax ); ?>" type="radio" class="custom-tax">
63
							</td>
64
							<td>
65
								<label for="smbd_taxs"><?php echo esc_attr( $taxs[ $tax ]->labels->name ); ?></label>
66
							</td>
67
						</tr>
68
						<?php
69
					}
70
					?>
71
				</table>
72
73
				<h4>
74
					<?php _e( 'The selected taxonomy has the following terms. Select the terms from which you want to delete posts', 'bulk-delete' ); ?>
75
				</h4>
76
77
				<p>
78
					<?php _e( 'Note: The post count below for each term is the total number of posts in that term, irrespective of post type', 'bulk-delete' ); ?>.
79
				</p>
80
81
				<?php
82
				foreach ( $terms_array as $tax => $terms ) {
83
					?>
84
					<table class="optiontable terms_<?php echo $tax; ?> terms">
85
						<?php
86
						foreach ( $terms as $term ) {
87
							?>
88
							<tr>
89
								<td scope="row">
90
									<input name="smbd_taxs_terms[]" value="<?php echo $term->slug; ?>" type="checkbox"
91
									       class="terms">
92
								</td>
93
								<td>
94
									<label for="smbd_taxs_terms"><?php echo $term->name; ?>
95
										(<?php echo $term->count . ' ';
96
										_e( 'Posts', 'bulk-delete' ); ?>)</label>
97
								</td>
98
							</tr>
99
							<?php
100
						}
101
						?>
102
					</table>
103
					<?php
104
				}
105
				?>
106
				<table class="optiontable">
107
					<?php
108
					$this->render_filtering_table_header();
109
					$this->render_restrict_settings();
110
					$this->render_exclude_sticky_settings();
111
					$this->render_delete_settings();
112
					$this->render_limit_settings();
113
					$this->render_cron_settings();
114
					?>
115
				</table>
116
117
			</fieldset>
118
			<?php
119
			$this->render_submit_button();
120
		} else {
121
			?>
122
			<h4><?php _e( "This WordPress installation doesn't have any non-empty taxonomies defined", 'bulk-delete' ) ?></h4>
123
			<?php
124
		}
125
	}
126
127
	protected function convert_user_input_to_options( $request, $options ) {
128
		$options['post_type']          = bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' );
129
		$options['selected_taxs']      = bd_array_get( $request, 'smbd_' . $this->field_slug );
130
		$options['selected_tax_terms'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_terms' );
131
132
		return $options;
133
	}
134
135 96
	protected function build_query( $delete_options ) {
136
		// For compatibility reasons set default post type to 'post'
137 96
		$post_type = bd_array_get( $delete_options, 'post_type', 'post' );
138
139 96
		$taxonomy = $delete_options['selected_taxs'];
140 96
		$terms    = $delete_options['selected_tax_terms'];
141
142
		$options = array(
143 96
			'post_status' => 'publish',
144 96
			'post_type'   => $post_type,
145
			'tax_query'   => array(
146
				array(
147 96
					'taxonomy' => $taxonomy,
148 96
					'terms'    => $terms,
149 96
					'field'    => 'slug',
150
				),
151
			),
152
		);
153
154 96
		return $options;
155
	}
156
157
	protected function get_success_message( $items_deleted ) {
158
		/* translators: 1 Number of pages deleted */
159
		return _n( 'Deleted %d post with the selected taxonomy', 'Deleted %d posts with the selected post taxonomy', $items_deleted, 'bulk-delete' );
160
	}
161
}
162