Passed
Push — 677-feature/add-wp-cli-support ( 2c69cc...edf7f9 )
by
unknown
02:58
created

DeletePostsByTaxonomyModule::prepare_cli_input()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
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
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
16 115
	protected function initialize() {
17 115
		$this->item_type     = 'posts';
18 115
		$this->field_slug    = 'taxs';
19 115
		$this->meta_box_slug = 'bd_posts_by_taxonomy';
20 115
		$this->action        = 'bd_delete_posts_by_taxonomy';
21 115
		$this->cron_hook     = 'do-bulk-delete-taxonomy';
22 115
		$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';
23 115
		$this->messages      = array(
24 115
			'box_label'         => __( 'Delete Posts by Taxonomy (Category, Tag or custom taxonomy)', 'bulk-delete' ),
25 115
			'scheduled'         => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
26 115
			'cron_label'        => __( 'Delete Post By Taxonomy', 'bulk-delete' ),
27 115
			'confirm_deletion'  => __( 'Are you sure you want to delete posts from the selected taxonomy?', 'bulk-delete' ),
28 115
			'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the posts from the selected taxonomy?', 'bulk-delete' ),
29 115
			'validation_error'  => __( 'Please select the taxonomy from which posts should be deleted', 'bulk-delete' ),
30
			/* translators: 1 Number of posts deleted */
31 115
			'deleted_one'       => __( 'Deleted %d post from the selected taxonomy', 'bulk-delete' ),
32
			/* translators: 1 Number of posts deleted */
33 115
			'deleted_multiple'  => __( 'Deleted %d posts from the selected taxonomy', 'bulk-delete' ),
34
		);
35
	}
36
37
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
38
	public function render() {
39
		$taxs = get_taxonomies( array(), 'objects'
40
		);
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
			<h4><?php _e( 'Select the post type from which you want to delete posts by taxonomy', 'bulk-delete' ); ?></h4>
55
56
			<fieldset class="options">
57
				<table class="optiontable">
58
					<?php $this->render_post_type_dropdown(); ?>
59
				</table>
60
61
				<h4>
62
					<?php _e( 'Select the taxonomies from which you want to delete posts', 'bulk-delete' ); ?>
63
				</h4>
64
65
				<table class="optiontable">
66
					<?php
67
					foreach ( $terms_array as $tax => $terms ) {
68
						?>
69
						<tr>
70
							<td scope="row">
71
								<input name="smbd_taxs" value="<?php echo esc_attr( $tax ); ?>" type="radio" class="custom-tax">
72
							</td>
73
							<td>
74
								<label for="smbd_taxs"><?php echo esc_attr( $taxs[ $tax ]->labels->name ); ?></label>
75
							</td>
76
						</tr>
77
						<?php
78
					}
79
					?>
80
				</table>
81
82
				<h4>
83
					<?php _e( 'The selected taxonomy has the following terms. Select the terms from which you want to delete posts', 'bulk-delete' ); ?>
84
				</h4>
85
86
				<p>
87
					<?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' ); ?>.
88
				</p>
89
90
				<?php
91
				foreach ( $terms_array as $tax => $terms ) {
92
					?>
93
					<table class="optiontable terms_<?php echo $tax; ?> terms">
94
						<?php
95
						foreach ( $terms as $term ) {
96
							?>
97
							<tr>
98
								<td scope="row">
99
									<input name="smbd_taxs_terms[]" value="<?php echo $term->slug; ?>" type="checkbox"
100
										class="terms">
101
								</td>
102
								<td>
103
									<label for="smbd_taxs_terms"><?php echo $term->name; ?>
104
										(<?php echo $term->count . ' ';
105
										_e( 'Posts', 'bulk-delete' ); ?>)</label>
106
								</td>
107
							</tr>
108
							<?php
109
						}
110
						?>
111
					</table>
112
					<?php
113
				}
114
				?>
115
				<table class="optiontable">
116
					<?php
117
					$this->render_filtering_table_header();
118
					$this->render_restrict_settings();
119
					$this->render_exclude_sticky_settings();
120
					$this->render_delete_settings();
121
					$this->render_limit_settings();
122
					$this->render_cron_settings();
123
					?>
124
				</table>
125
126
			</fieldset>
127
			<?php
128
			$this->render_submit_button();
129
		} else {
130
			?>
131
			<h4><?php _e( "This WordPress installation doesn't have any non-empty taxonomies defined", 'bulk-delete' ) ?></h4>
132
			<?php
133
		}
134
	}
135
136
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
137
	protected function convert_user_input_to_options( $request, $options ) {
138
		$options['post_type']          = bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' );
139
		$options['selected_taxs']      = bd_array_get( $request, 'smbd_' . $this->field_slug );
140
		$options['selected_tax_terms'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_terms' );
141
142
		return $options;
143
	}
144
145
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
146 96
	protected function build_query( $delete_options ) {
147
		// For compatibility reasons set default post type to 'post'
148 96
		$post_type = bd_array_get( $delete_options, 'post_type', 'post' );
149
150 96
		$taxonomy = $delete_options['selected_taxs'];
151 96
		$terms    = $delete_options['selected_tax_terms'];
152
153
		$options = array(
154 96
			'post_status' => 'publish',
155 96
			'post_type'   => $post_type,
156
			'tax_query'   => array(
157
				array(
158 96
					'taxonomy' => $taxonomy,
159 96
					'terms'    => $terms,
160 96
					'field'    => 'slug',
161
				),
162
			),
163
		);
164
165 96
		return $options;
166
	}
167
168
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
169
	protected function get_non_standard_input_key_map() {
170
		$prefix = $this->get_ui_input_prefix();
171
172
		$prefix_without_underscore_at_end = substr( $prefix, 0, -1 );
173
174
		return array(
175
			$prefix_without_underscore_at_end => $prefix . 'taxonomy',
176
		);
177
	}
178
179
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
180
	protected function prepare_cli_input( $input ) {
181
		// Handle multiple terms.
182
		$input['terms'] = explode( ',', $input['terms'] );
183
184
		return parent::prepare_cli_input( $input );
185
	}
186
}
187