Passed
Push — 178-feature/delete-terms--by-p... ( a57d30...18363f )
by Rajan
12:03
created

DeleteTermsByPostCountModule   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 52.38%

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 110
ccs 22
cts 42
cp 0.5238
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 11 1
A convert_user_input_to_options() 0 7 1
A filter_js_array() 0 8 1
A get_success_message() 0 3 1
A build_query() 0 14 4
A render() 0 20 1
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
	/**
16
	 * Initialize the values.
17
	 */
18 14
	protected function initialize() {
19 14
		$this->item_type     = 'terms';
20 14
		$this->field_slug    = 'terms_by_post_count';
21 14
		$this->meta_box_slug = 'bd_delete_terms_by_post_count';
22 14
		$this->action        = 'delete_terms_by_post_count';
23 14
		$this->cron_hook     = 'do-bulk-delete-term-by-post-count';
24 14
		$this->scheduler_url = '';
25 14
		$this->messages      = array(
26 14
			'box_label'  => __( 'Delete Terms by Post Count', 'bulk-delete' ),
27 14
			'scheduled'  => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
28 14
			'cron_label' => __( 'Delete Terms By Post Count', 'bulk-delete' ),
29
		);
30 14
	}
31
32
	/**
33
	 * Render Delete terms by postfix and prefix box.
34
	 */
35
	public function render() {
36
		?>
37
		<h4><?php _e( 'Select the taxonomy from which you want to delete terms', 'bulk-delete' ); ?></h4>
38
		<fieldset class="options">
39
			<table class="optiontable">
40
				<?php $this->render_taxonomy_dropdown(); ?>
41
			</table>
42
43
			<h4><?php _e( 'Select the post type', 'bulk-delete' ); ?></h4>
44
			<table class="optiontable">
45
				<?php $this->render_post_type_dropdown(); ?>
46
			</table>
47
48
			<table class="optiontable">
49
				<?php $this->render_term_options(); ?>
50
			</table>
51
52
		</fieldset>
53
		<?php
54
		$this->render_submit_button();
55
	}
56
57
	/**
58
	 * Filter the js array.
59
	 *
60
	 * @param array $js_array JavaScript Array.
61
	 *
62
	 * @return array Modified JavaScript Array
63
	 */
64
	public function filter_js_array( $js_array ) {
65
		$js_array['validators'][ $this->action ] = 'validatePostTypeSelect2';
66
		$js_array['error_msg'][ $this->action ]  = 'selectPostType';
67
		$js_array['msg']['selectPostType']       = __( 'Please select at least one post type', 'bulk-delete' );
68
69
		$js_array['dt_iterators'][] = '_' . $this->field_slug;
70
71
		return $js_array;
72
	}
73
74
	/**
75
	 * Process delete posts user inputs by category.
76
	 *
77
	 * @param array $request Request array.
78
	 * @param array $options Options for deleting posts.
79
	 *
80
	 * @return array $options  Inputs from user for posts that were need to delete
81
	 */
82
	protected function convert_user_input_to_options( $request, $options ) {
83
		$options['taxonomy']  = bd_array_get( $request, 'smbd_' . $this->field_slug . '_taxonomy' );
84
		$options['post_type'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type' );
85
		$options['term_opt']  = bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_opt' );
86
		$options['term_text'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_text' );
87
88
		return $options;
89
	}
90
91
	/**
92
	 * Build query from delete options.
93
	 *
94
	 * @param array $options Delete options.
95
	 *
96
	 * @return array Query.
97
	 */
98 14
	protected function build_query( $options ) {
99 14
		$query     = array();
100 14
		$taxonomy  = $options['taxonomy'];
101 14
		$term_text = $options['term_text'];
102
103 14
		if ( isset( $term_text ) ) {
104 14
			$query['taxonomy'] = $taxonomy;
105
		}
106
107 14
		$term_ids         = $this->term_count_query( $options );
108 14
		$query['include'] = isset( $term_ids['include'] ) ? $term_ids['include'] : '';
109 14
		$query['exclude'] = isset( $term_ids['exclude'] ) ? $term_ids['exclude'] : '';
110
111 14
		return $query;
112
	}
113
114
	/**
115
	 * Response message for deleting posts.
116
	 *
117
	 * @param int $items_deleted Total number of posts deleted.
118
	 *
119
	 * @return string Response message
120
	 */
121
	protected function get_success_message( $items_deleted ) {
122
		/* translators: 1 Number of posts deleted */
123
		return _n( 'Deleted %d term with the selected options', 'Deleted %d terms with the selected options', $items_deleted, 'bulk-delete' );
124
	}
125
}
126