Completed
Pull Request — dev/6.0.0 (#502)
by Rajan
24:47 queued 21:13
created

DeleteTermsByNameModule   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
eloc 84
dl 0
loc 201
ccs 72
cts 84
cp 0.8571
rs 10
c 0
b 0
f 0
wmc 27

11 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 17 1
A filter_js_array() 0 7 1
A initialize() 0 8 1
A convert_user_input_to_options() 0 5 1
B get_term_ids_to_delete() 0 32 7
A get_terms_that_are_equal_to() 0 7 1
A get_terms_that_ends_with() 0 11 3
A get_terms_that_contains() 0 11 3
A get_terms_that_are_not_equal_to() 0 11 3
A get_terms_that_not_contains() 0 11 3
A get_terms_that_starts_with() 0 11 3
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Terms\Modules;
4
5
use BulkWP\BulkDelete\Core\Terms\TermsModule;
6
7 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Terms by Name.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeleteTermsByNameModule extends TermsModule {
15 12
	protected function initialize() {
16 12
		$this->item_type     = 'terms';
17 12
		$this->field_slug    = 'terms_by_name';
18 12
		$this->meta_box_slug = 'bd_delete_terms_by_name';
19 12
		$this->action        = 'delete_terms_by_name';
20 12
		$this->messages      = array(
21 12
			'box_label'  => __( 'Delete Terms by Name', 'bulk-delete' ),
22 12
			'scheduled'  => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
23
		);
24 12
	}
25
26
	public function render() {
27
		?>
28
29
		<fieldset class="options">
30
			<h4><?php _e( 'Select the taxonomy from which you want to delete terms', 'bulk-delete' ); ?></h4>
31
32
			<?php $this->render_taxonomy_dropdown(); ?>
33
34
			<h4><?php _e( 'Choose your filtering options', 'bulk-delete' ); ?></h4>
35
36
			<?php _e( 'Delete Terms if the name ', 'bulk-delete' ); ?>
37
			<?php $this->render_string_comparison_operators(); ?>
38
			<input type="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value" placeholder="<?php _e( 'Term Name', 'bulk-delete' ); ?>">
39
		</fieldset>
40
41
		<?php
42
		$this->render_submit_button();
43
	}
44
45
	public function filter_js_array( $js_array ) {
46
		$js_array['validators'][ $this->action ] = 'noValidation';
47
48
		$js_array['pre_action_msg'][ $this->action ] = 'deleteTermsWarning';
49
		$js_array['msg']['deleteTermsWarning']       = __( 'Are you sure you want to delete all the terms based on the selected option?', 'bulk-delete' );
50
51
		return $js_array;
52
	}
53
54
	protected function convert_user_input_to_options( $request, $options ) {
55
		$options['operator'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' ) );
56
		$options['value']    = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value' ) );
57
58
		return $options;
59
	}
60
61 12
	protected function get_term_ids_to_delete( $options ) {
62 12
		$term_ids = array();
63 12
		$value    = $options['value'];
64 12
		$operator = $options['operator'];
65
66 12
		switch ( $operator ) {
67 12
			case 'equal_to':
68 1
				$term_ids = $this->get_terms_that_are_equal_to( $value, $options );
69 1
				break;
70
71 11
			case 'not_equal_to':
72 2
				$term_ids = $this->get_terms_that_are_not_equal_to( $value, $options );
73 2
				break;
74
75 9
			case 'starts_with':
76 2
				$term_ids = $this->get_terms_that_starts_with( $value, $options );
77 2
				break;
78
79 7
			case 'ends_with':
80 2
				$term_ids = $this->get_terms_that_ends_with( $value, $options );
81 2
				break;
82
83 5
			case 'contains':
84 2
				$term_ids = $this->get_terms_that_contains( $value, $options );
85 2
				break;
86
87 3
			case 'not_contains':
88 3
				$term_ids = $this->get_terms_that_not_contains( $value, $options );
89 3
				break;
90
		}
91
92 12
		return $term_ids;
93
	}
94
95
	/**
96
	 * Get terms with name that are equal to a specific string.
97
	 *
98
	 * @param string $value   Value to compare.
99
	 * @param array  $options User options.
100
	 *
101
	 * @return int[] Term ids.
102
	 */
103 1
	protected function get_terms_that_are_equal_to( $value, $options ) {
104
		$query = array(
105 1
			'taxonomy'   => $options['taxonomy'],
106 1
			'name__like' => $value,
107
		);
108
109 1
		return $this->query_terms( $query );
110
	}
111
112
	/**
113
	 * Get terms with that name that is not equal to a specific string.
114
	 *
115
	 * @param string $value   Value to compare.
116
	 * @param array  $options User options.
117
	 *
118
	 * @return int[] Term ids.
119
	 */
120 2
	protected function get_terms_that_are_not_equal_to( $value, $options ) {
121 2
		$term_ids = array();
122 2
		$terms    = $this->get_all_terms( $options['taxonomy'] );
123
124 2
		foreach ( $terms as $term ) {
125 2
			if ( $term->name != $value ) {
126 2
				$term_ids[] = $term->term_id;
127
			}
128
		}
129
130 2
		return $term_ids;
131
	}
132
133
	/**
134
	 * Get terms with name that start with a specific string.
135
	 *
136
	 * @param string $starts_with Substring to search.
137
	 * @param array  $options     User options.
138
	 *
139
	 * @return int[] Term ids.
140
	 */
141 2
	protected function get_terms_that_starts_with( $starts_with, $options ) {
142 2
		$term_ids = array();
143 2
		$terms    = $this->get_all_terms( $options['taxonomy'] );
144
145 2
		foreach ( $terms as $term ) {
146 2
			if ( bd_starts_with( $term->name, $starts_with ) ) {
147 2
				$term_ids[] = $term->term_id;
148
			}
149
		}
150
151 2
		return $term_ids;
152
	}
153
154
	/**
155
	 * Get terms with name that ends with a specific string.
156
	 *
157
	 * @param string $ends_with Substring to search.
158
	 * @param array  $options   User options.
159
	 *
160
	 * @return int[] Term ids.
161
	 */
162 2
	protected function get_terms_that_ends_with( $ends_with, $options ) {
163 2
		$term_ids = array();
164 2
		$terms    = $this->get_all_terms( $options['taxonomy'] );
165
166 2
		foreach ( $terms as $term ) {
167 2
			if ( bd_ends_with( $term->name, $ends_with ) ) {
168 2
				$term_ids[] = $term->term_id;
169
			}
170
		}
171
172 2
		return $term_ids;
173
	}
174
175
	/**
176
	 * Get terms with name that contains a specific string.
177
	 *
178
	 * @param string $contains Substring to search.
179
	 * @param array  $options  User options.
180
	 *
181
	 * @return int[] Term ids.
182
	 */
183 2
	protected function get_terms_that_contains( $contains, $options ) {
184 2
		$term_ids = array();
185 2
		$terms    = $this->get_all_terms( $options['taxonomy'] );
186
187 2
		foreach ( $terms as $term ) {
188 2
			if ( bd_contains( $term->name, $contains ) ) {
189 2
				$term_ids[] = $term->term_id;
190
			}
191
		}
192
193 2
		return $term_ids;
194
	}
195
196
	/**
197
	 * Get terms with name that doesn't contain a specific string.
198
	 *
199
	 * @param string $contains Substring to search.
200
	 * @param array  $options  User options.
201
	 *
202
	 * @return int[] Term ids.
203
	 */
204 3
	protected function get_terms_that_not_contains( $contains, $options ) {
205 3
		$term_ids = array();
206 3
		$terms    = $this->get_all_terms( $options['taxonomy'] );
207
208 3
		foreach ( $terms as $term ) {
209 3
			if ( ! bd_contains( $term->name, $contains ) ) {
210 3
				$term_ids[] = $term->term_id;
211
			}
212
		}
213
214 3
		return $term_ids;
215
	}
216
}
217