Completed
Push — 630-fix/refactor-messages-sett... ( 9fbb7b )
by
unknown
12s queued 10s
created

DeleteTermsByNameModule::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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