Passed
Pull Request — dev/6.1.0 (#740)
by
unknown
10:55 queued 11s
created

DeleteTermsByNameModule   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Test Coverage

Coverage 79.17%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 80
c 1
b 0
f 0
dl 0
loc 160
ccs 57
cts 72
cp 0.7917
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A append_to_js_array() 0 4 1
A render() 0 20 1
A get_matching_terms_for_other_operators() 0 26 4
A get_terms_that_contains() 0 7 1
A initialize() 0 18 1
A convert_user_input_to_options() 0 5 1
A get_term_ids_to_delete() 0 23 4
A 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\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->cron_hook     = 'do-bulk-delete-terms-by-name';
23 24
		$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-terms/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sp';
24 24
		$this->messages      = array(
25 24
			'box_label'         => __( 'Delete Terms by Name', 'bulk-delete' ),
26 24
			'scheduled'         => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
27 24
			'cron_label'        => __( 'Delete Terms By name', 'bulk-delete' ),
28 24
			'confirm_deletion'  => __( 'Are you sure you want to delete all the terms based on the selected option?', 'bulk-delete' ),
29 24
			'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the terms from the selected condition?', 'bulk-delete' ),
30 24
			'validation_error'  => __( 'Please enter the term name that should be deleted', 'bulk-delete' ),
31
			/* translators: 1 Number of terms deleted */
32 24
			'deleted_one'       => __( 'Deleted %d term with the selected options', 'bulk-delete' ),
33
			/* translators: 1 Number of terms deleted */
34 24
			'deleted_multiple'  => __( 'Deleted %d terms with the selected options', 'bulk-delete' ),
35
		);
36
	}
37
38
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
39
	public function render() {
40
		?>
41
		<h4><?php _e( 'Select the taxonomy from which you want to delete terms', 'bulk-delete' ); ?></h4>
42
		<fieldset class="options">
43
			<table class="optiontable">
44
				<tr><?php $this->render_taxonomy_dropdown(); ?></tr>
45
				<h4><?php _e( 'Choose your filtering options', 'bulk-delete' ); ?></h4>
46
				<tr>
47
					<td>
48
						<?php _e( 'Delete Terms if the name ', 'bulk-delete' ); ?>
49
						<?php $this->render_operators_dropdown( [ 'equals', 'string-all' ] ); ?>
50
						<input type="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value" placeholder="<?php _e( 'Term Name', 'bulk-delete' ); ?>" class="validate">
51
					</td>
52
				</tr>
53
				<?php $this->render_cron_settings(); ?>
54
			</table>
55
		</fieldset>
56
57
		<?php
58
		$this->render_submit_button();
59
	}
60
61
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
62
	protected function append_to_js_array( $js_array ) {
63
		$js_array['validators'][ $this->action ] = 'validateTextbox';
64
65
		return $js_array;
66
	}
67
68
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
69
	protected function convert_user_input_to_options( $request, $options ) {
70
		$options['operator'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' );
71
		$options['value']    = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value' ) );
72
73
		return $options;
74
	}
75
76
	/**
77
	 * Get the list of terms ids that need to be deleted.
78
	 *
79
	 * Return an empty query array to short-circuit deletion.
80
	 *
81
	 * @param array $options Delete options.
82
	 *
83
	 * @return int[] List of term ids to delete.
84
	 */
85 24
	protected function get_term_ids_to_delete( $options ) {
86 24
		$term_ids = array();
87 24
		$operator = $options['operator'];
88 24
		$value    = $options['value'];
89 24
		if ( empty( $value ) ) {
90 1
			return $term_ids;
91
		}
92
93 23
		switch ( $operator ) {
94
			case '=':
95 3
				$term_ids = $this->get_terms_that_are_equal_to( $options );
96 3
				break;
97
98
			case 'LIKE':
99 4
				$term_ids = $this->get_terms_that_contains( $options );
100 4
				break;
101
102
			default:
103 16
				$term_ids = $this->get_matching_terms_for_other_operators( $options );
104 16
				break;
105
		}
106
107 23
		return $term_ids;
108
	}
109
110
	/**
111
	 * Get terms with name that are equal to a specific string.
112
	 *
113
	 * @param array $options User options.
114
	 *
115
	 * @return int[] Term ids.
116
	 */
117 3
	protected function get_terms_that_are_equal_to( $options ) {
118
		$query = array(
119 3
			'taxonomy' => $options['taxonomy'],
120 3
			'name'     => $options['value'],
121
		);
122
123 3
		return $this->query_terms( $query );
124
	}
125
126
	/**
127
	 * Get terms with name that contains a specific string.
128
	 *
129
	 * @param array $options User options.
130
	 *
131
	 * @return int[] Term ids.
132
	 */
133 4
	protected function get_terms_that_contains( $options ) {
134
		$query = array(
135 4
			'taxonomy'   => $options['taxonomy'],
136 4
			'name__like' => $options['value'],
137
		);
138
139 4
		return $this->query_terms( $query );
140
	}
141
142
	/**
143
	 * Get matching terms for not equal to, not contains, starts with and ends with operators.
144
	 *
145
	 * @param array $options User options.
146
	 *
147
	 * @return int[] Term ids.
148
	 */
149 16
	protected function get_matching_terms_for_other_operators( $options ) {
150 16
		$operator = $options['operator'];
151 16
		$value    = $options['value'];
152 16
		switch ( $operator ) {
153
			case 'NOT LIKE':
154 4
				$value = '%' . $value . '%';
155 4
				break;
156
			case 'STARTS WITH':
157 4
				$operator = 'LIKE';
158 4
				$value    = $value . '%';
159 4
				break;
160
			case 'ENDS WITH':
161 4
				$operator = 'LIKE';
162 4
				$value    = '%' . $value;
163 4
				break;
164
		}
165
		$query           = array(
166 16
			'taxonomy'       => $options['taxonomy'],
167 16
			'bd_operator'    => $operator,
168 16
			'bd_value'       => $value,
169 16
			'bd_column_name' => 'name',
170
		);
171 16
		$query_overrider = new DeleteTermsQueryOverrider();
172 16
		$query_overrider->load();
173
174 16
		return $this->query_terms( $query );
175
	}
176
}
177