Passed
Push — master ( e95607...5b55be )
by Sudar
15:04
created

DeleteTermsByPostCountModule   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 8 1
A render() 0 22 1
A should_delete_term_based_on_post_count() 0 10 5
A convert_user_input_to_options() 0 5 1
A get_term_ids_to_delete() 0 11 3
A append_to_js_array() 0 9 1
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 Post Count.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeleteTermsByPostCountModule extends TermsModule {
15
	protected function initialize() {
16
		$this->item_type     = 'terms';
17
		$this->field_slug    = 'terms_by_post_count';
18
		$this->meta_box_slug = 'bd_delete_terms_by_post_count';
19
		$this->action        = 'delete_terms_by_post_count';
20
		$this->messages      = array(
21
			'box_label' => __( 'Delete Terms by Post Count', '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 post count is ', 'bulk-delete' ); ?>
37
			<?php $this->render_number_comparison_operators(); ?>
38
			<input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>" placeholder="Post count" min="0">
39
			<?php
40
			$markup  = '';
41
			$content = __( 'Post count is the number of posts that are assigned to a term.', 'bulk-delete' );
42
			echo '&nbsp' . bd_generate_help_tooltip( $markup, $content );
43
			?>
44
		</fieldset>
45
46
		<?php
47
		$this->render_submit_button();
48
	}
49
50
	protected function append_to_js_array( $js_array ) {
51
		$js_array['validators'][ $this->action ] = 'validatePostCount';
52
		$js_array['error_msg'][ $this->action ]  = 'validPostCount';
53
		$js_array['msg']['validPostCount']       = __( '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' );
54
55
		$js_array['pre_action_msg'][ $this->action ] = 'deleteTermsWarning';
56
		$js_array['msg']['deleteTermsWarning']       = __( 'Are you sure you want to delete all the terms based on the selected option?', 'bulk-delete' );
57
58
		return $js_array;
59
	}
60
61
	protected function convert_user_input_to_options( $request, $options ) {
62
		$options['operator']   = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' ) );
63
		$options['post_count'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug ) );
64
65
		return $options;
66
	}
67
68
	protected function get_term_ids_to_delete( $options ) {
69
		$term_ids = array();
70
71
		$terms = $this->get_all_terms( $options['taxonomy'] );
72
		foreach ( $terms as $term ) {
73
			if ( $this->should_delete_term_based_on_post_count( $term->count, $options['operator'], $options['post_count'] ) ) {
74
				$term_ids[] = $term->term_id;
75
			}
76
		}
77
78
		return $term_ids;
79
	}
80
81
	/**
82
	 * Determine if a term should be deleted based on post count.
83
	 *
84
	 * @param int    $term_post_count Number of posts associated with a term.
85
	 * @param string $operator        Operator.
86
	 * @param int    $compared_to     The user entered value to which the comparison should be made.
87
	 *
88
	 * @return int term id.
89
	 */
90
	protected function should_delete_term_based_on_post_count( $term_post_count, $operator, $compared_to ) {
91
		switch ( $operator ) {
92
			case '=':
93
				return $term_post_count === $compared_to;
94
			case '!=':
95
				return $term_post_count !== $compared_to;
96
			case '<':
97
				return $term_post_count < $compared_to;
98
			case '>':
99
				return $term_post_count > $compared_to;
100
		}
101
	}
102
}
103