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

DeleteTermsByPostCountModule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 59.38%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 82
ccs 19
cts 32
cp 0.5938
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 25 1
A append_to_js_array() 0 4 1
A initialize() 0 13 1
A convert_user_input_to_options() 0 5 1
A get_term_ids_to_delete() 0 12 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 Post Count.
12
 *
13
 * @since 6.0.0
14
 */
15
class DeleteTermsByPostCountModule extends TermsModule {
16
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
17 12
	protected function initialize() {
18 12
		$this->item_type     = 'terms';
19 12
		$this->field_slug    = 'terms_by_post_count';
20 12
		$this->meta_box_slug = 'bd_delete_terms_by_post_count';
21 12
		$this->action        = 'delete_terms_by_post_count';
22 12
		$this->messages      = array(
23 12
			'box_label'        => __( 'Delete Terms by Post Count', 'bulk-delete' ),
24 12
			'confirm_deletion' => __( 'Are you sure you want to delete all the terms based on the selected option?', 'bulk-delete' ),
25 12
			'validation_error' => __( 'Please enter the post count based on which terms should be deleted. A valid post count will be greater than or equal to zero', 'bulk-delete' ),
26
			/* translators: 1 Number of terms deleted */
27 12
			'deleted_one'      => __( 'Deleted %d term with the selected options', 'bulk-delete' ),
28
			/* translators: 1 Number of terms deleted */
29 12
			'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
41
				<h4><?php _e( 'Choose your filtering options', 'bulk-delete' ); ?></h4>
42
				<tr>
43
					<td><?php _e( 'Delete Terms if the post count is ', 'bulk-delete' ); ?></td>
44
					<td><?php $this->render_numeric_operators_dropdown( 'numeric', array( '=', '!=', '<', '>' ) ); ?></td>
45
					<td><input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>" placeholder="Post count" min="0" class="validate"></td>
46
					<td>
47
						<?php
48
						$markup  = '';
49
						$content = __( 'Post count is the number of posts that are assigned to a term.', 'bulk-delete' );
50
						echo '&nbsp' . bd_generate_help_tooltip( $markup, $content );
51
						?>
52
					</td>
53
				</tr>
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['post_count'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug ) );
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 12
	protected function get_term_ids_to_delete( $options ) {
86
		$query = array(
87 12
			'taxonomy'       => $options['taxonomy'],
88 12
			'bd_operator'    => $options['operator'],
89 12
			'bd_value'       => $options['post_count'],
90 12
			'bd_column_name' => 'count',
91
		);
92
93 12
		$query_overrider = new DeleteTermsQueryOverrider();
94 12
		$query_overrider->load();
95
96 12
		return $this->query_terms( $query );
97
	}
98
}
99