Passed
Push — 178-feature/delete-terms--by-p... ( fe64b6...870088 )
by Sudar
11:08
created

DeleteTermsByNameModule::build_query()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 7
nop 1
dl 0
loc 41
ccs 33
cts 33
cp 1
crap 7
rs 8.4746
c 0
b 0
f 0

2 Methods

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