Completed
Push — 330-fix/delete-user-meta-add-a... ( d70968...8c3efe )
by Sudar
45:44 queued 42:41
created

DeleteTermsByNameModule::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 17
ccs 0
cts 2
cp 0
crap 2
rs 9.8666
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 11
	protected function initialize() {
16 11
		$this->item_type     = 'terms';
17 11
		$this->field_slug    = 'terms_by_name';
18 11
		$this->meta_box_slug = 'bd_delete_terms_by_name';
19 11
		$this->action        = 'delete_terms_by_name';
20 11
		$this->messages      = array(
21 11
			'box_label'  => __( 'Delete Terms by Name', 'bulk-delete' ),
22 11
			'scheduled'  => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
23
		);
24 11
	}
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 11
	protected function get_term_ids_to_delete( $options ) {
62 11
		$term_ids = array();
63 11
		$value    = $options['value'];
64 11
		$operator = $options['operator'];
65
66 11
		switch ( $operator ) {
67 11
			case 'equal_to':
68 1
				$term_ids = $this->get_terms_that_are_equal_to( $value, $options );
69 1
				break;
70
71 10
			case 'not_equal_to':
72 1
				$term_ids = $this->get_terms_that_are_not_equal_to( $value, $options );
73 1
				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 11
		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 1
	protected function get_terms_that_are_not_equal_to( $value, $options ) {
121
		$name_like_args = array(
122 1
			'name__like' => $value,
123 1
			'taxonomy'   => $options['taxonomy'],
124
		);
125
126
		$query = array(
127 1
			'taxonomy' => $options['taxonomy'],
128 1
			'exclude'  => $this->query_terms( $name_like_args ),
129
		);
130
131 1
		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 2
	protected function get_terms_that_starts_with( $starts_with, $options ) {
143 2
		$term_ids = array();
144 2
		$terms    = $this->get_all_terms( $options['taxonomy'] );
145
146 2
		foreach ( $terms as $term ) {
147 2
			if ( bd_starts_with( $term->name, $starts_with ) ) {
148 2
				$term_ids[] = $term->term_id;
149
			}
150
		}
151
152 2
		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 2
	protected function get_terms_that_ends_with( $ends_with, $options ) {
164 2
		$term_ids = array();
165 2
		$terms    = $this->get_all_terms( $options['taxonomy'] );
166
167 2
		foreach ( $terms as $term ) {
168 2
			if ( bd_ends_with( $term->name, $ends_with ) ) {
169 2
				$term_ids[] = $term->term_id;
170
			}
171
		}
172
173 2
		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 2
	protected function get_terms_that_contains( $contains, $options ) {
185 2
		$term_ids = array();
186 2
		$terms    = $this->get_all_terms( $options['taxonomy'] );
187
188 2
		foreach ( $terms as $term ) {
189 2
			if ( bd_contains( $term->name, $contains ) ) {
190 2
				$term_ids[] = $term->term_id;
191
			}
192
		}
193
194 2
		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 3
	protected function get_terms_that_not_contains( $contains, $options ) {
206 3
		$term_ids = array();
207 3
		$terms    = $this->get_all_terms( $options['taxonomy'] );
208
209 3
		foreach ( $terms as $term ) {
210 3
			if ( ! bd_contains( $term->name, $contains ) ) {
211 3
				$term_ids[] = $term->term_id;
212
			}
213
		}
214
215 3
		return $term_ids;
216
	}
217
}
218