Completed
Push — 247-fix/delete-term-meta ( d5f818...2a1fe3 )
by Sudar
67:09 queued 61:16
created

DeleteTermsByPostCountModule::append_to_js_array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 10
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 Post Count.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeleteTermsByPostCountModule extends TermsModule {
15 12
	protected function initialize() {
16 12
		$this->item_type     = 'terms';
17 12
		$this->field_slug    = 'terms_by_post_count';
18 12
		$this->meta_box_slug = 'bd_delete_terms_by_post_count';
19 12
		$this->action        = 'delete_terms_by_post_count';
20 12
		$this->messages      = array(
21 12
			'box_label' => __( 'Delete Terms by Post Count', 'bulk-delete' ),
22 12
			'scheduled' => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
23
		);
24 12
	}
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 12
	protected function get_term_ids_to_delete( $options ) {
69 12
		$term_ids = array();
70
71 12
		$terms = $this->get_all_terms( $options['taxonomy'] );
72 12
		foreach ( $terms as $term ) {
73 12
			if ( $this->should_delete_term_based_on_post_count( $term->count, $options['operator'], $options['post_count'] ) ) {
74 9
				$term_ids[] = $term->term_id;
75
			}
76
		}
77
78 12
		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 12
	protected function should_delete_term_based_on_post_count( $term_post_count, $operator, $compared_to ) {
91 12
		switch ( $operator ) {
92 12
			case '=':
93 3
				return $term_post_count === $compared_to;
94 9
			case '!=':
95 3
				return $term_post_count !== $compared_to;
96 6
			case '<':
97 3
				return $term_post_count < $compared_to;
98 3
			case '>':
99 3
				return $term_post_count > $compared_to;
100
		}
101
	}
102
}
103