Completed
Push — 330-fix/delete-user-meta-add-a... ( d70968...8c3efe )
by Sudar
45:44 queued 42:41
created

DeleteTermsByPostCountModule::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 8
ccs 8
cts 8
cp 1
crap 1
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 14
	protected function initialize() {
16 14
		$this->item_type     = 'terms';
17 14
		$this->field_slug    = 'terms_by_post_count';
18 14
		$this->meta_box_slug = 'bd_delete_terms_by_post_count';
19 14
		$this->action        = 'delete_terms_by_post_count';
20 14
		$this->messages      = array(
21 14
			'box_label' => __( 'Delete Terms by Post Count', 'bulk-delete' ),
22 14
			'scheduled' => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
23
		);
24 14
	}
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 ); ?>_post_count" placeholder="<?php _e( 'Post count', 'bulk-delete' ); ?>">
39
		</fieldset>
40
41
		<?php
42
		$this->render_submit_button();
43
	}
44
45
	public function filter_js_array( $js_array ) {
46
		$js_array['validators'][ $this->action ] = 'validatePostTypeSelect2';
47
		$js_array['error_msg'][ $this->action ]  = 'selectPostType';
48
		$js_array['msg']['selectPostType']       = __( 'Please select at least one post type', 'bulk-delete' );
49
50
		$js_array['pre_action_msg'][ $this->action ] = 'deleteTermsWarning';
51
		$js_array['msg']['deleteTermsWarning']       = __( 'Are you sure you want to delete all the terms based on the selected option?', 'bulk-delete' );
52
53
		return $js_array;
54
	}
55
56
	protected function convert_user_input_to_options( $request, $options ) {
57
		$options['operator']   = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' ) );
58
		$options['post_count'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_count' ) );
59
60
		return $options;
61
	}
62
63 14
	protected function get_term_ids_to_delete( $options ) {
64 14
		$term_ids = array();
65
66 14
		$terms = $this->get_all_terms( $options['taxonomy'] );
67 14
		foreach ( $terms as $term ) {
68 14
			if ( $this->should_delete_term_based_on_post_count( $term->count, $options['operator'], $options['post_count'] ) ) {
69 14
				$term_ids[] = $term->term_id;
70
			}
71
		}
72
73 14
		return $term_ids;
74
	}
75
76
	/**
77
	 * Determine if a term should be deleted based on post count.
78
	 *
79
	 * @param int    $term_post_count Number of posts associated with a term.
80
	 * @param string $operator        Operator.
81
	 * @param int    $compared_to     The user entered value to which the comparison should be made.
82
	 *
83
	 * @return int term id.
84
	 */
85 14
	protected function should_delete_term_based_on_post_count( $term_post_count, $operator, $compared_to ) {
86 14
		switch ( $operator ) {
87 14
			case 'equal_to':
88 4
				return $term_post_count === $compared_to;
89 10
			case 'not_equal_to':
90 4
				return $term_post_count !== $compared_to;
91 6
			case 'less_than':
92 3
				return $term_post_count < $compared_to;
93 3
			case 'greater_than':
94 3
				return $term_post_count > $compared_to;
95
		}
96
	}
97
}
98