Completed
Push — 383-fix/add-tests-for-comparis... ( a2ea6f...61deeb )
by Sudar
42:36 queued 27:33
created

DeleteTermsByNameModule::get_terms_that_contains()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 11
rs 10
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
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
	protected function initialize() {
16
		$this->item_type     = 'terms';
17
		$this->field_slug    = 'terms_by_name';
18
		$this->meta_box_slug = 'bd_delete_terms_by_name';
19
		$this->action        = 'delete_terms_by_name';
20
		$this->messages      = array(
21
			'box_label'  => __( 'Delete Terms by Name', 'bulk-delete' ),
22
			'scheduled'  => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
23
		);
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
	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
	protected function get_term_ids_to_delete( $options ) {
62
		$term_ids = array();
63
		$value    = $options['value'];
64
		$operator = $options['operator'];
65
66
		switch ( $operator ) {
67
			case 'equal_to':
68
				$term_ids = $this->get_terms_that_are_equal_to( $value, $options );
69
				break;
70
71
			case 'not_equal_to':
72
				$term_ids = $this->get_terms_that_are_not_equal_to( $value, $options );
73
				break;
74
75
			case 'starts_with':
76
				$term_ids = $this->get_terms_that_starts_with( $value, $options );
77
				break;
78
79
			case 'ends_with':
80
				$term_ids = $this->get_terms_that_ends_with( $value, $options );
81
				break;
82
83
			case 'contains':
84
				$term_ids = $this->get_terms_that_contains( $value, $options );
85
				break;
86
87
			case 'not_contains':
88
				$term_ids = $this->get_terms_that_not_contains( $value, $options );
89
				break;
90
		}
91
92
		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
	protected function get_terms_that_are_equal_to( $value, $options ) {
104
		$query = array(
105
			'taxonomy'   => $options['taxonomy'],
106
			'name__like' => $value,
107
		);
108
109
		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
	protected function get_terms_that_are_not_equal_to( $value, $options ) {
121
		$name_like_args = array(
122
			'name__like' => $value,
123
			'taxonomy'   => $options['taxonomy'],
124
		);
125
126
		$query = array(
127
			'taxonomy' => $options['taxonomy'],
128
			'exclude'  => $this->query_terms( $name_like_args ),
129
		);
130
131
		return $this->query_terms( $query );
132
	}
133
134
	/**
135
	 * Get terms with name that start with a specific string.
136
	 *
137
	 * @param string $starts_with Substring to search.
138
	 * @param array  $options     User options.
139
	 *
140
	 * @return int[] Term ids.
141
	 */
142
	protected function get_terms_that_starts_with( $starts_with, $options ) {
143
		$term_ids = array();
144
		$terms    = $this->get_all_terms( $options['taxonomy'] );
145
146
		foreach ( $terms as $term ) {
147
			if ( bd_starts_with( $term->name, $starts_with ) ) {
148
				$term_ids[] = $term->term_id;
149
			}
150
		}
151
152
		return $term_ids;
153
	}
154
155
	/**
156
	 * Get terms with name that ends with a specific string.
157
	 *
158
	 * @param string $ends_with Substring to search.
159
	 * @param array  $options   User options.
160
	 *
161
	 * @return int[] Term ids.
162
	 */
163
	protected function get_terms_that_ends_with( $ends_with, $options ) {
164
		$term_ids = array();
165
		$terms    = $this->get_all_terms( $options['taxonomy'] );
166
167
		foreach ( $terms as $term ) {
168
			if ( bd_ends_with( $term->name, $ends_with ) ) {
169
				$term_ids[] = $term->term_id;
170
			}
171
		}
172
173
		return $term_ids;
174
	}
175
176
	/**
177
	 * Get terms with name that contains a specific string.
178
	 *
179
	 * @param string $contains Substring to search.
180
	 * @param array  $options  User options.
181
	 *
182
	 * @return int[] Term ids.
183
	 */
184
	protected function get_terms_that_contains( $contains, $options ) {
185
		$term_ids = array();
186
		$terms    = $this->get_all_terms( $options['taxonomy'] );
187
188
		foreach ( $terms as $term ) {
189
			if ( bd_contains( $term->name, $contains ) ) {
190
				$term_ids[] = $term->term_id;
191
			}
192
		}
193
194
		return $term_ids;
195
	}
196
197
	/**
198
	 * Get terms with name that doesn't contain a specific string.
199
	 *
200
	 * @param string $contains Substring to search.
201
	 * @param array  $options  User options.
202
	 *
203
	 * @return int[] Term ids.
204
	 */
205
	protected function get_terms_that_not_contains( $contains, $options ) {
206
		$term_ids = array();
207
		$terms    = $this->get_all_terms( $options['taxonomy'] );
208
209
		foreach ( $terms as $term ) {
210
			if ( ! bd_contains( $term->name, $contains ) ) {
211
				$term_ids[] = $term->term_id;
212
			}
213
		}
214
215
		return $term_ids;
216
	}
217
}
218