Passed
Push — 178-feature/delete-terms--by-p... ( 784e3a...63324f )
by Rajan
11:22
created

DeleteTermsByNameModule::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 16
rs 9.8333
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 Name.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeleteTermsByNameModule extends TermsModule {
15
	/**
16
	 * Initialize the values.
17
	 */
18
	protected function initialize() {
19
		$this->item_type     = 'terms';
20
		$this->field_slug    = 'terms_by_name';
21
		$this->meta_box_slug = 'bd_delete_terms_by_name';
22
		$this->action        = 'delete_terms_by_name';
23
		$this->cron_hook     = 'do-bulk-delete-term-by-name';
24
		$this->scheduler_url = '';
25
		$this->messages      = array(
26
			'box_label'  => __( 'Delete Terms by Name', 'bulk-delete' ),
27
			'scheduled'  => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
28
			'cron_label' => __( 'Delete Terms By Name', 'bulk-delete' ),
29
		);
30
	}
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
	protected function build_query( $options ) {
100
		$query     = array();
101
		$term_text = $options['term_text'];
102
		$term_opt  = $options['term_opt'];
103
104
		switch ( $term_opt ) {
105
			case 'equal_to':
106
				$query['name__like'] = $term_text;
107
				break;
108
109
			case 'not_equal_to':
110
				$term_ids         = $this->term_query( array( 'name__like' => $term_text ), $options['taxonomy'] );
111
				$query['exclude'] = $term_ids;
112
				break;
113
114
			case 'starts':
115
				$term_ids         = $this->term_starts( $term_text, $options );
116
				$query['include'] = $term_ids['include'];
117
				$query['exclude'] = $term_ids['exclude'];
118
				break;
119
120
			case 'ends':
121
				$term_ids         = $this->term_ends( $term_text, $options );
122
				$query['include'] = $term_ids['include'];
123
				$query['exclude'] = $term_ids['exclude'];
124
				break;
125
126
			case 'contains':
127
				$term_ids         = $this->term_contains( $term_text, $options );
128
				$query['include'] = $term_ids['include'];
129
				$query['exclude'] = $term_ids['exclude'];
130
				break;
131
132
			case 'not_contains':
133
				$term_ids         = $this->term_contains( $term_text, $options );
134
				$query['exclude'] = $term_ids['include'];
135
				$query['include'] = $term_ids['exclude'];
136
				break;
137
		}
138
		return $query;
139
	}
140
141
	/**
142
	 * Response message for deleting posts.
143
	 *
144
	 * @param int $items_deleted Total number of posts deleted.
145
	 *
146
	 * @return string Response message
147
	 */
148
	protected function get_success_message( $items_deleted ) {
149
		/* translators: 1 Number of posts deleted */
150
		return _n( 'Deleted %d term with the selected options', 'Deleted %d terms with the selected terms', $items_deleted, 'bulk-delete' );
151
	}
152
}
153