Completed
Push — 247-fix/delete-term-meta ( 2a1fe3...132475 )
by Sudar
10:50 queued 05:24
created

DeletePostsByTaxonomyModule   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 44.07%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 106
c 4
b 0
f 0
dl 0
loc 148
ccs 26
cts 59
cp 0.4407
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A build_query() 0 20 1
B render() 0 94 8
A convert_user_input_to_options() 0 6 1
A initialize() 0 18 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 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 96
			'confirm_deletion'  => __( 'Are you sure you want to delete posts from the selected taxonomy?', 'bulk-delete' ),
27 96
			'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the posts from the selected taxonomy?', 'bulk-delete' ),
28 96
			'validation_error'  => __( 'Please select the taxonomy from which posts should be deleted', 'bulk-delete' ),
29
			/* translators: 1 Number of posts deleted */
30 96
			'deleted_one'       => __( 'Deleted %d post from the selected taxonomy', 'bulk-delete' ),
31
			/* translators: 1 Number of posts deleted */
32 96
			'deleted_multiple'  => __( 'Deleted %d posts from the selected taxonomy', 'bulk-delete' ),
33
		);
34
	}
35
36
	public function render() {
37
		$taxs = get_taxonomies( array(), 'objects'
38
		);
39
40
		$terms_array = array();
41
		if ( count( $taxs ) > 0 ) {
42
			foreach ( $taxs as $tax ) {
43
				$terms = get_terms( $tax->name );
44
				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

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