Passed
Push — 358-fix/delete-posts-by-post-s... ( 397ce3 )
by Rajan
10:52
created

DeleteTermsByPostCountModule::build_query()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 15
ccs 0
cts 12
cp 0
crap 6
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
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->cron_hook     = 'do-bulk-delete-term-by-post-count';
21
		$this->scheduler_url = '';
22
		$this->messages      = array(
23
			'box_label'  => __( 'Delete Terms by Post Count', 'bulk-delete' ),
24
			'scheduled'  => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
25
			'cron_label' => __( 'Delete Terms By Post Count', 'bulk-delete' ),
26
		);
27
	}
28
29
	/**
30
	 * Render Delete terms by postfix and prefix box.
31
	 */
32
	public function render() {
33
		?>
34
		<!-- Category Start-->
35
		<h4><?php _e( 'Select the taxonomy from which you want to delete terms', 'bulk-delete' ); ?></h4>
36
		<fieldset class="options">
37
			<table class="optiontable">
38
				<?php $this->render_taxonomy_dropdown(); ?>
39
			</table>
40
41
			<h4><?php _e( 'Select the post type', 'bulk-delete' ); ?></h4>
42
			<table class="optiontable">
43
				<?php $this->render_post_type_dropdown(); ?>
44
			</table>
45
46
			<table class="optiontable">
47
				<?php $this->render_term_options(); ?>
48
			</table>
49
50
		</fieldset>
51
		<?php
52
		$this->render_submit_button();
53
	}
54
55
	public function filter_js_array( $js_array ) {
56
		$js_array['validators'][ $this->action ] = 'validatePostTypeSelect2';
57
		$js_array['error_msg'][ $this->action ]  = 'selectPostType';
58
		$js_array['msg']['selectPostType']       = __( 'Please select at least one post type', 'bulk-delete' );
59
60
		$js_array['dt_iterators'][] = '_' . $this->field_slug;
61
62
		return $js_array;
63
	}
64
65
	/**
66
	 * Process delete posts user inputs by category.
67
	 *
68
	 * @param array $request Request array.
69
	 * @param array $options Options for deleting posts.
70
	 *
71
	 * @return array $options  Inputs from user for posts that were need to delete
72
	 */
73
	protected function convert_user_input_to_options( $request, $options ) {
74
		$options['taxonomy']      = bd_array_get( $request, 'smbd_' . $this->field_slug . '_taxonomy' );
75
		$options['post_type']     = bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type' );
76
		$options['term_opt']      = bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_opt' );
77
		$options['term_text']     = bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_text' );
78
79
		return $options;
80
	}
81
82
	/**
83
	 * Build query from delete options.
84
	 *
85
	 * @param array $options Delete options.
86
	 *
87
	 * @return array Query.
88
	 */
89
	protected function build_query( $options ) {
90
		$query      = array();
91
		$taxonomy   = $options['taxonomy'];
92
		$post_type  = $options['post_type'];
0 ignored issues
show
Unused Code introduced by
The assignment to $post_type is dead and can be removed.
Loading history...
93
		$term_opt   = $options['term_opt'];
0 ignored issues
show
Unused Code introduced by
The assignment to $term_opt is dead and can be removed.
Loading history...
94
		$term_text  = $options['term_text'];
95
96
		if( isset( $term_text ) ){
97
			$query['taxonomy'] = $taxonomy;
98
		}
99
100
		$term_ids            = $this->term_count_query( $options );
101
		$query['include']    = $term_ids;
102
103
		return $query;
104
	}
105
106
	/**
107
	 * Response message for deleting posts.
108
	 *
109
	 * @param int $items_deleted Total number of posts deleted.
110
	 *
111
	 * @return string Response message
112
	 */
113
	protected function get_success_message( $items_deleted ) {
114
		/* translators: 1 Number of posts deleted */
115
		return _n( 'Deleted %d term with the selected options', 'Deleted %d terms with the selected options', $items_deleted, 'bulk-delete' );
116
	}
117
}
118