Completed
Pull Request — dev/6.0.0 (#332)
by Rajan
14:58 queued 12:02
created

DeleteTermsByNameModule::build_query()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 7
nop 1
dl 0
loc 41
ccs 33
cts 33
cp 1
crap 7
rs 8.4746
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 Name.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeleteTermsByNameModule extends TermsModule {
15
	/**
16
	 * Initialize the values.
17
	 */
18 11
	protected function initialize() {
19 11
		$this->item_type     = 'terms';
20 11
		$this->field_slug    = 'terms_by_name';
21 11
		$this->meta_box_slug = 'bd_delete_terms_by_name';
22 11
		$this->action        = 'delete_terms_by_name';
23 11
		$this->cron_hook     = 'do-bulk-delete-term-by-name';
24 11
		$this->scheduler_url = '';
25 11
		$this->messages      = array(
26 11
			'box_label'  => __( 'Delete Terms by Name', 'bulk-delete' ),
27 11
			'scheduled'  => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
28 11
			'cron_label' => __( 'Delete Terms By Name', 'bulk-delete' ),
29
		);
30 11
	}
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
			<table class="optiontable">
45
				<?php $this->render_term_options(); ?>
46
			</table>
47
48
		</fieldset>
49
		<?php
50
		$this->render_submit_button();
51
	}
52
53
	/**
54
	 * Filter the js array.
55
	 *
56
	 * @param array $js_array JavaScript Array.
57
	 *
58
	 * @return array Modified JavaScript Array
59
	 */
60
	public function filter_js_array( $js_array ) {
61
		$js_array['validators'][ $this->action ] = 'validatePostTypeSelect2';
62
		$js_array['error_msg'][ $this->action ]  = 'selectPostType';
63
		$js_array['msg']['selectPostType']       = __( 'Please select at least one post type', 'bulk-delete' );
64
65
		$js_array['dt_iterators'][] = '_' . $this->field_slug;
66
67
		$js_array['validators'][ $this->action ] = 'noValidation';
68
69
		$js_array['pre_action_msg'][ $this->action ] = 'deleteTermsWarning';
70
		$js_array['msg']['deleteTermsWarning']       = __( 'Are you sure you want to delete all the terms based on the selected option?', 'bulk-delete' );
71
72
		return $js_array;
73
	}
74
75
	/**
76
	 * Process delete posts user inputs by category.
77
	 *
78
	 * @param array $request Request array.
79
	 * @param array $options Options for deleting posts.
80
	 *
81
	 * @return array $options  Inputs from user for posts that were need to delete
82
	 */
83
	protected function convert_user_input_to_options( $request, $options ) {
84
		$options['taxonomy']  = bd_array_get( $request, 'smbd_' . $this->field_slug . '_taxonomy' );
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
		$options['no_posts']  = bd_array_get( $request, 'smbd_' . $this->field_slug . '_no_posts' );
88
89
		return $options;
90
	}
91
92
	/**
93
	 * Build query from delete options.
94
	 *
95
	 * @param array $options Delete options.
96
	 *
97
	 * @return array Query.
98
	 */
99 11
	protected function build_query( $options ) {
100 11
		$query     = array();
101 11
		$term_text = $options['term_text'];
102 11
		$term_opt  = $options['term_opt'];
103
104 11
		switch ( $term_opt ) {
105 11
			case 'equal_to':
106 1
				$query['name__like'] = $term_text;
107 1
				break;
108
109 10
			case 'not_equal_to':
110 1
				$term_ids         = $this->term_query( array( 'name__like' => $term_text ), $options['taxonomy'] );
111 1
				$query['exclude'] = $term_ids;
112 1
				break;
113
114 9
			case 'starts':
115 2
				$term_ids         = $this->term_starts( $term_text, $options );
116 2
				$query['include'] = $term_ids['include'];
117 2
				$query['exclude'] = $term_ids['exclude'];
118 2
				break;
119
120 7
			case 'ends':
121 2
				$term_ids         = $this->term_ends( $term_text, $options );
122 2
				$query['include'] = $term_ids['include'];
123 2
				$query['exclude'] = $term_ids['exclude'];
124 2
				break;
125
126 5
			case 'contains':
127 2
				$term_ids         = $this->term_contains( $term_text, $options );
128 2
				$query['include'] = $term_ids['include'];
129 2
				$query['exclude'] = $term_ids['exclude'];
130 2
				break;
131
132 3
			case 'not_contains':
133 3
				$term_ids         = $this->term_contains( $term_text, $options );
134 3
				$query['exclude'] = $term_ids['include'];
135 3
				$query['include'] = $term_ids['exclude'];
136 3
				break;
137
		}
138
139 11
		return $query;
140
	}
141
142
	/**
143
	 * Response message for deleting posts.
144
	 *
145
	 * @param int $items_deleted Total number of posts deleted.
146
	 *
147
	 * @return string Response message
148
	 */
149
	protected function get_success_message( $items_deleted ) {
150
		/* translators: 1 Number of posts deleted */
151
		return _n( 'Deleted %d term with the selected options', 'Deleted %d terms with the selected options', $items_deleted, 'bulk-delete' );
152
	}
153
}
154