Passed
Pull Request — dev/6.0.0 (#382)
by Rajan
15:18
created

DeletePostsByTaxonomyModule::render()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 116
Code Lines 97

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 35

Importance

Changes 0
Metric Value
cc 8
eloc 97
nc 14
nop 0
dl 0
loc 116
ccs 10
cts 40
cp 0.25
crap 35
rs 6.821
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 2
	/**
16 2
	 * Initialize and setup variables.
17 2
	 */
18 2
	protected function initialize() {
19 2
		$this->item_type     = 'posts';
20 2
		$this->field_slug    = 'taxs';
21 2
		$this->meta_box_slug = 'bd_posts_by_taxonomy';
22 2
		$this->action        = 'bd_delete_posts_by_taxonomy';
23 2
		$this->cron_hook     = 'do-bulk-delete-taxonomy';
24 2
		$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-taxonomy/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=addonlist&utm_content=bd-stx';
25 2
		$this->messages      = array(
26
			'box_label'  => __( 'By Taxonomy', 'bulk-delete' ),
27 2
			'scheduled'  => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
28
			'cron_label' => __( 'Delete Post By Taxonomy', 'bulk-delete' ),
29
		);
30
	}
31
32
	/**
33
	 * Render Delete Posts by Taxonomy box.
34
	 */
35
	public function render() {
36
		$taxs = get_taxonomies( array(), 'objects' );
37
38
		$terms_array = array();
39
		if ( count( $taxs ) > 0 ) {
40
			foreach ( $taxs as $tax ) {
41
				$terms = get_terms( $tax->name );
42
				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

42
				if ( count( /** @scrutinizer ignore-type */ $terms ) > 0 ) {
Loading history...
43
					$terms_array[ $tax->name ] = $terms;
44
				}
45
			}
46
		}
47
48
		if ( count( $terms_array ) > 0 ) {
49
			?>
50
			<!-- Custom tax Start-->
51
			<h4>
52
			<?php
53
			_e(
54
				'Select the post type from which you want to delete posts by taxonomy',
55
				'bulk-delete'
56
			);
57
			?>
58
					</h4>
59
60
			<fieldset class="options">
61
				<table class="optiontable">
62
					<?php $this->render_post_type_dropdown(); ?>
63
				</table>
64
65
				<h4><?php _e( 'Select the taxonomies from which you want to delete posts', 'bulk-delete' ); ?></h4>
66
67
				<table class="optiontable">
68
					<?php
69
					foreach ( $terms_array as $tax => $terms ) {
70
						?>
71
						<tr>
72
							<td scope="row">
73
								<input name="smbd_taxs" value="<?php echo esc_html( $tax ); ?>" type="radio" class="custom-tax">
74
							</td>
75
							<td>
76
								<label for="smbd_taxs"><?php echo esc_html( $taxs[ $tax ]->labels->name ); ?> </label>
77
							</td>
78
						</tr>
79
						<?php
80
					}
81
					?>
82
				</table>
83
84
				<h4>
85
				<?php
86
				_e(
87
					'The selected taxonomy has the following terms. Select the terms from which you want to delete posts',
88
					'bulk-delete'
89
				)
90
				?>
91
						</h4>
92
				<p>
93
				<?php
94
				_e(
95
					'Note: The post count below for each term is the total number of posts in that term, irrespective of post type',
96
					'bulk-delete'
97
				);
98
				?>
99
						.</p>
100
				<?php
101
				foreach ( $terms_array as $tax => $terms ) {
102
					?>
103
					<table class="optiontable terms_<?php echo esc_html( $tax ); ?> terms">
104
						<?php
105
						foreach ( $terms as $term ) {
106
							?>
107
							<tr>
108
								<td scope="row">
109
									<input name="smbd_taxs_terms[]" value="<?php echo esc_html( $term->slug ); ?>" type="checkbox" class="terms">
110
								</td>
111
								<td>
112
									<label for="smbd_taxs_terms"><?php echo esc_html( $term->name ); ?>
113
										(
114
										<?php
115
										echo esc_html( $term->count ) . ' ';
116
										_e( 'Posts', 'bulk-delete' );
117
										?>
118
										)</label>
119
								</td>
120
							</tr>
121
							<?php
122
						}
123
						?>
124 2
					</table>
125
					<?php
126 2
				}
127
				?>
128 2
				<table class="optiontable">
129 2
					<?php
130
					$this->render_filtering_table_header();
131
					$this->render_restrict_settings();
132 2
					$this->render_delete_settings();
133 2
					$this->render_limit_settings();
134
					$this->render_cron_settings();
135
					?>
136 2
				</table>
137 2
138 2
			</fieldset>
139
			<?php
140
			$this->render_submit_button();
141
		} else {
142
			?>
143 2
			<h4>
144
			<?php
145
			_e(
146
				"This WordPress installation doesn't have any non-empty taxonomies defined",
147
				'bulk-delete'
148
			)
149
			?>
150
					</h4>
151
			<?php
152
		}
153
	}
154
155
	/**
156
	 * Process user input and create metabox options.
157
	 *
158
	 * @param array $request Request array.
159
	 * @param array $options User options.
160
	 *
161
	 * @return array User options.
162
	 */
163
	protected function convert_user_input_to_options( $request, $options ) {
164
		$options['post_type']          = bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' );
165
		$options['selected_taxs']      = bd_array_get( $request, 'smbd_' . $this->field_slug );
166
		$options['selected_tax_terms'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_terms' );
167
168
		return $options;
169
	}
170
171
	/**
172
	 * Build query from delete options.
173
	 *
174
	 * @param array $delete_options Delete options.
175
	 *
176
	 * @return array Query.
177
	 */
178
	protected function build_query( $delete_options ) {
179
		// For compatibility reasons set default post type to 'post'.
180
		$post_type = bd_array_get( $delete_options, 'post_type', 'post' );
181
182
		$taxonomy = $delete_options['selected_taxs'];
183
		$terms    = $delete_options['selected_tax_terms'];
184
185
		$options = array(
186
			'post_status' => 'publish',
187
			'post_type'   => $post_type,
188
			'tax_query'   => array(
189
				array(
190
					'taxonomy' => $taxonomy,
191
					'terms'    => $terms,
192
					'field'    => 'slug',
193
				),
194
			),
195
		);
196
197
		return $options;
198
	}
199
200
	/**
201
	 * Get Success Message.
202
	 *
203
	 * @param int $items_deleted Number of items that were deleted.
204
	 *
205
	 * @return string Success message.
206
	 */
207
	protected function get_success_message( $items_deleted ) {
208
		/* translators: 1 Number of pages deleted */
209
		return _n( 'Deleted %d post with the selected taxonomy', 'Deleted %d posts with the selected post taxonomy', $items_deleted, 'bulk-delete' );
210
	}
211
}
212