Completed
Push — 249-fix/delete-posts-by-custom... ( d9803b...cd3a71 )
by Rajan
23:01 queued 19:13
created

DeletePostsByTaxonomyModule   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
wmc 12
eloc 125
dl 0
loc 200
ccs 22
cts 77
cp 0.2857
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A build_query() 0 20 1
C render() 0 116 8
A convert_user_input_to_options() 0 6 1
A initialize() 0 11 1
A get_success_message() 0 3 1
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
	/**
16
	 * Initialize and setup variables.
17
	 *
18
	 * @return void
19
	 */
20 13
	protected function initialize() {
21 13
		$this->item_type     = 'posts';
22 13
		$this->field_slug    = 'taxs';
23 13
		$this->meta_box_slug = 'bd_posts_by_taxonomy';
24 13
		$this->action        = 'bd_delete_posts_by_taxonomy';
25 13
		$this->cron_hook     = 'do-bulk-delete-taxonomy';
26 13
		$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';
27 13
		$this->messages      = array(
28 13
			'box_label'  => __( 'By Taxonomy', 'bulk-delete' ),
29 13
			'scheduled'  => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
30 13
			'cron_label' => __( 'Delete Post By Taxonomy', 'bulk-delete' ),
31
		);
32 13
	}
33
34
	/**
35
	 * Render the Modules.
36
	 *
37
	 * @return void
38
	 */
39
	public function render() {
40
		$taxs = get_taxonomies( array(), 'objects' );
41
42
		$terms_array = array();
43
		if ( count( $taxs ) > 0 ) {
44
			foreach ( $taxs as $tax ) {
45
				$terms = get_terms( $tax->name );
46
				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

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