Completed
Push — dev/6.1.0 ( ff536c...d0f579 )
by Sudar
05:02
created

DeleteTermsByNameModule::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 13
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Terms\Modules;
4
5
use BulkWP\BulkDelete\Core\Terms\QueryOverriders\DeleteTermsQueryOverrider;
6
use BulkWP\BulkDelete\Core\Terms\TermsModule;
7
8 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
9
10
/**
11
 * Delete Terms by Name.
12
 *
13
 * @since 6.0.0
14
 */
15
class DeleteTermsByNameModule extends TermsModule {
16
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
17 24
	protected function initialize() {
18 24
		$this->item_type     = 'terms';
19 24
		$this->field_slug    = 'terms_by_name';
20 24
		$this->meta_box_slug = 'bd_delete_terms_by_name';
21 24
		$this->action        = 'delete_terms_by_name';
22 24
		$this->messages      = array(
23 24
			'box_label'        => __( 'Delete Terms by Name', 'bulk-delete' ),
24 24
			'confirm_deletion' => __( 'Are you sure you want to delete all the terms based on the selected option?', 'bulk-delete' ),
25 24
			'validation_error' => __( 'Please enter the term name that should be deleted', 'bulk-delete' ),
26
			/* translators: 1 Number of terms deleted */
27 24
			'deleted_one'      => __( 'Deleted %d term with the selected options', 'bulk-delete' ),
28
			/* translators: 1 Number of terms deleted */
29 24
			'deleted_multiple' => __( 'Deleted %d terms with the selected options', 'bulk-delete' ),
30
		);
31
	}
32
33
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
34
	public function render() {
35
		?>
36
		<h4><?php _e( 'Select the taxonomy from which you want to delete terms', 'bulk-delete' ); ?></h4>
37
		<fieldset class="options">
38
			<table class="optiontable">
39
				<tr><?php $this->render_taxonomy_dropdown(); ?></tr>
40
				<h4><?php _e( 'Choose your filtering options', 'bulk-delete' ); ?></h4>
41
				<tr>
42
					<td><?php _e( 'Delete Terms if the name ', 'bulk-delete' ); ?></td>
43
					<td><?php $this->render_string_operators_dropdown( 'string', array( '=', '!=', 'LIKE', 'NOT LIKE', 'STARTS_WITH', 'ENDS_WITH' ) ); ?></td>
44
					<td><input type="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value" placeholder="<?php _e( 'Term Name', 'bulk-delete' ); ?>" class="validate"></td>
45
				</tr>
46
			</table>
47
		</fieldset>
48
49
		<?php
50
		$this->render_submit_button();
51
	}
52
53
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
54
	protected function append_to_js_array( $js_array ) {
55
		$js_array['validators'][ $this->action ] = 'validateTextbox';
56
57
		return $js_array;
58
	}
59
60
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
61
	protected function convert_user_input_to_options( $request, $options ) {
62
		$options['operator'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' );
63
		$options['value']    = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value' ) );
64
65
		return $options;
66
	}
67
68
	/**
69
	 * Get the list of terms ids that need to be deleted.
70
	 *
71
	 * Return an empty query array to short-circuit deletion.
72
	 *
73
	 * @param array $options Delete options.
74
	 *
75
	 * @return int[] List of term ids to delete.
76
	 */
77 24
	protected function get_term_ids_to_delete( $options ) {
78 24
		$term_ids = array();
79 24
		$operator = $options['operator'];
80 24
		$value    = $options['value'];
81 24
		if ( empty( $value ) ) {
82 1
			return $term_ids;
83
		}
84
85 23
		switch ( $operator ) {
86
			case '=':
87 3
				$term_ids = $this->get_terms_that_are_equal_to( $options );
88 3
				break;
89
90
			case 'LIKE':
91 4
				$term_ids = $this->get_terms_that_contains( $options );
92 4
				break;
93
94
			default:
95 16
				$term_ids = $this->get_matching_terms_for_other_operators( $options );
96 16
				break;
97
		}
98
99 23
		return $term_ids;
100
	}
101
102
	/**
103
	 * Get terms with name that are equal to a specific string.
104
	 *
105
	 * @param array $options User options.
106
	 *
107
	 * @return int[] Term ids.
108
	 */
109 3
	protected function get_terms_that_are_equal_to( $options ) {
110
		$query = array(
111 3
			'taxonomy' => $options['taxonomy'],
112 3
			'name'     => $options['value'],
113
		);
114
115 3
		return $this->query_terms( $query );
116
	}
117
118
	/**
119
	 * Get terms with name that contains a specific string.
120
	 *
121
	 * @param array $options User options.
122
	 *
123
	 * @return int[] Term ids.
124
	 */
125 4
	protected function get_terms_that_contains( $options ) {
126
		$query = array(
127 4
			'taxonomy'   => $options['taxonomy'],
128 4
			'name__like' => $options['value'],
129
		);
130
131 4
		return $this->query_terms( $query );
132
	}
133
134
	/**
135
	 * Get matching terms for not equal to, not contains, starts with and ends with operators.
136
	 *
137
	 * @param array $options User options.
138
	 *
139
	 * @return int[] Term ids.
140
	 */
141 16
	protected function get_matching_terms_for_other_operators( $options ) {
142 16
		$operator = $options['operator'];
143 16
		$value    = $options['value'];
144 16
		switch ( $operator ) {
145
			case 'NOT LIKE':
146 4
				$value = '%' . $value . '%';
147 4
				break;
148
			case 'STARTS_WITH':
149 4
				$operator = 'LIKE';
150 4
				$value    = $value . '%';
151 4
				break;
152
			case 'ENDS_WITH':
153 4
				$operator = 'LIKE';
154 4
				$value    = '%' . $value;
155 4
				break;
156
		}
157
		$query           = array(
158 16
			'taxonomy'       => $options['taxonomy'],
159 16
			'bd_operator'    => $operator,
160 16
			'bd_value'       => $value,
161 16
			'bd_column_name' => 'name',
162
		);
163 16
		$query_overrider = new DeleteTermsQueryOverrider();
164 16
		$query_overrider->load();
165
166 16
		return $this->query_terms( $query );
167
	}
168
}
169