DeleteTermsByNameModule::get_term_ids_to_delete()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 25
nc 8
nop 1
dl 0
loc 35
ccs 26
cts 26
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0
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 24
	protected function initialize() {
16 24
		$this->item_type     = 'terms';
17 24
		$this->field_slug    = 'terms_by_name';
18 24
		$this->meta_box_slug = 'bd_delete_terms_by_name';
19 24
		$this->action        = 'delete_terms_by_name';
20 24
		$this->messages      = array(
21 24
			'box_label' => __( 'Delete Terms by Name', 'bulk-delete' ),
22 24
			'scheduled' => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
23
		);
24 24
	}
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
	protected function append_to_js_array( $js_array ) {
46
		$js_array['validators'][ $this->action ] = 'validateTermName';
47
		$js_array['error_msg'][ $this->action ]  = 'enterTermName';
48
		$js_array['msg']['enterTermName']        = __( 'Please enter the term name that should be deleted', 'bulk-delete' );
49
50
		$js_array['pre_action_msg'][ $this->action ] = 'deleteTermsWarning';
51
		$js_array['msg']['deleteTermsWarning']       = __( 'Are you sure you want to delete all the terms based on the selected option?', 'bulk-delete' );
52
53
		return $js_array;
54
	}
55
56
	protected function convert_user_input_to_options( $request, $options ) {
57
		$options['operator'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' ) );
58
		$options['value']    = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value' ) );
59
60
		return $options;
61
	}
62
63 24
	protected function get_term_ids_to_delete( $options ) {
64 24
		$term_ids = array();
65 24
		$value    = $options['value'];
66 24
		$operator = $options['operator'];
67 24
		if ( empty( $value ) ) {
68 1
			return $term_ids;
69
		}
70
71 23
		switch ( $operator ) {
72 23
			case 'equal_to':
73 3
				$term_ids = $this->get_terms_that_are_equal_to( $value, $options );
74 3
				break;
75
76 20
			case 'not_equal_to':
77 4
				$term_ids = $this->get_terms_that_are_not_equal_to( $value, $options );
78 4
				break;
79
80 16
			case 'starts_with':
81 4
				$term_ids = $this->get_terms_that_starts_with( $value, $options );
82 4
				break;
83
84 12
			case 'ends_with':
85 4
				$term_ids = $this->get_terms_that_ends_with( $value, $options );
86 4
				break;
87
88 8
			case 'contains':
89 4
				$term_ids = $this->get_terms_that_contains( $value, $options );
90 4
				break;
91
92 4
			case 'not_contains':
93 4
				$term_ids = $this->get_terms_that_not_contains( $value, $options );
94 4
				break;
95
		}
96
97 23
		return $term_ids;
98
	}
99
100
	/**
101
	 * Get terms with name that are equal to a specific string.
102
	 *
103
	 * @param string $value   Value to compare.
104
	 * @param array  $options User options.
105
	 *
106
	 * @return int[] Term ids.
107
	 */
108 3
	protected function get_terms_that_are_equal_to( $value, $options ) {
109
		$query = array(
110 3
			'taxonomy' => $options['taxonomy'],
111 3
			'name'     => $value,
112
		);
113
114 3
		return $this->query_terms( $query );
115
	}
116
117
	/**
118
	 * Get terms with that name that is not equal to a specific string.
119
	 *
120
	 * @param string $value   Value to compare.
121
	 * @param array  $options User options.
122
	 *
123
	 * @return int[] Term ids.
124
	 */
125 4
	protected function get_terms_that_are_not_equal_to( $value, $options ) {
126
		$name_like_args = array(
127 4
			'name'     => $value,
128 4
			'taxonomy' => $options['taxonomy'],
129
		);
130
131
		$query = array(
132 4
			'taxonomy' => $options['taxonomy'],
133 4
			'exclude'  => $this->query_terms( $name_like_args ),
134
		);
135
136 4
		return $this->query_terms( $query );
137
	}
138
139
	/**
140
	 * Get terms with name that start with a specific string.
141
	 *
142
	 * @param string $starts_with Substring to search.
143
	 * @param array  $options     User options.
144
	 *
145
	 * @return int[] Term ids.
146
	 */
147 4
	protected function get_terms_that_starts_with( $starts_with, $options ) {
148 4
		$term_ids = array();
149 4
		$terms    = $this->get_all_terms( $options['taxonomy'] );
150
151 4
		foreach ( $terms as $term ) {
152 4
			if ( bd_starts_with( $term->name, $starts_with ) ) {
153 2
				$term_ids[] = $term->term_id;
154
			}
155
		}
156
157 4
		return $term_ids;
158
	}
159
160
	/**
161
	 * Get terms with name that ends with a specific string.
162
	 *
163
	 * @param string $ends_with Substring to search.
164
	 * @param array  $options   User options.
165
	 *
166
	 * @return int[] Term ids.
167
	 */
168 4
	protected function get_terms_that_ends_with( $ends_with, $options ) {
169 4
		$term_ids = array();
170 4
		$terms    = $this->get_all_terms( $options['taxonomy'] );
171
172 4
		foreach ( $terms as $term ) {
173 4
			if ( bd_ends_with( $term->name, $ends_with ) ) {
174 2
				$term_ids[] = $term->term_id;
175
			}
176
		}
177
178 4
		return $term_ids;
179
	}
180
181
	/**
182
	 * Get terms with name that contains a specific string.
183
	 *
184
	 * @param string $contains Substring to search.
185
	 * @param array  $options  User options.
186
	 *
187
	 * @return int[] Term ids.
188
	 */
189 4
	protected function get_terms_that_contains( $contains, $options ) {
190 4
		$term_ids = array();
191 4
		$terms    = $this->get_all_terms( $options['taxonomy'] );
192
193 4
		foreach ( $terms as $term ) {
194 4
			if ( bd_contains( $term->name, $contains ) ) {
195 2
				$term_ids[] = $term->term_id;
196
			}
197
		}
198
199 4
		return $term_ids;
200
	}
201
202
	/**
203
	 * Get terms with name that doesn't contain a specific string.
204
	 *
205
	 * @param string $contains Substring to search.
206
	 * @param array  $options  User options.
207
	 *
208
	 * @return int[] Term ids.
209
	 */
210 4
	protected function get_terms_that_not_contains( $contains, $options ) {
211 4
		$term_ids = array();
212 4
		$terms    = $this->get_all_terms( $options['taxonomy'] );
213
214 4
		foreach ( $terms as $term ) {
215 4
			if ( ! bd_contains( $term->name, $contains ) ) {
216 2
				$term_ids[] = $term->term_id;
217
			}
218
		}
219
220 4
		return $term_ids;
221
	}
222
}
223