Passed
Push — 178-feature/delete-terms--by-p... ( 88fdf9...9ec577 )
by Rajan
10:04
created

DeleteTermsByPostCountModule::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 11
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
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
	/**
16
	 * Initialize the values.
17
	 */
18 4
	protected function initialize() {
19 4
		$this->item_type     = 'terms';
20 4
		$this->field_slug    = 'terms_by_post_count';
21 4
		$this->meta_box_slug = 'bd_delete_terms_by_post_count';
22 4
		$this->action        = 'delete_terms_by_post_count';
23 4
		$this->cron_hook     = 'do-bulk-delete-term-by-post-count';
24 4
		$this->scheduler_url = '';
25 4
		$this->messages      = array(
26 4
			'box_label'  => __( 'Delete Terms by Post Count', 'bulk-delete' ),
27 4
			'scheduled'  => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
28 4
			'cron_label' => __( 'Delete Terms By Post Count', 'bulk-delete' ),
29
		);
30 4
	}
31
32
	/**
33
	 * Render Delete terms by postfix and prefix box.
34
	 */
35
	public function render() {
36
		?>
37
		<!-- Category Start-->
38
		<h4><?php _e( 'Select the taxonomy from which you want to delete terms', 'bulk-delete' ); ?></h4>
39
		<fieldset class="options">
40
			<table class="optiontable">
41
				<?php $this->render_taxonomy_dropdown(); ?>
42
			</table>
43
44
			<h4><?php _e( 'Select the post type', 'bulk-delete' ); ?></h4>
45
			<table class="optiontable">
46
				<?php $this->render_post_type_dropdown(); ?>
47
			</table>
48
49
			<table class="optiontable">
50
				<?php $this->render_term_options(); ?>
51
			</table>
52
53
		</fieldset>
54
		<?php
55
		$this->render_submit_button();
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 4
	protected function build_query( $options ) {
99 4
		$query     = array();
100 4
		$taxonomy  = $options['taxonomy'];
101 4
		$term_text = $options['term_text'];
102
103 4
		if ( isset( $term_text ) ) {
104 4
			$query['taxonomy'] = $taxonomy;
105
		}
106
107 4
		$term_ids         = $this->term_count_query( $options );
108 4
		$query['include'] = isset( $term_ids['include'] ) ? $term_ids['include'] : '';
109 4
		$query['exclude'] = isset( $term_ids['exclude'] ) ? $term_ids['exclude'] : '';
110
111 4
		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 terms count', $items_deleted, 'bulk-delete' );
124
	}
125
}
126