Completed
Pull Request — dev/6.0.0 (#332)
by Rajan
16:24 queued 13:11
created

DeleteTermsByPostfixAndPrefixModule::build_query()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 28
nc 7
nop 1
dl 0
loc 37
ccs 0
cts 30
cp 0
crap 56
rs 8.5386
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 Postfix and Prefix.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeleteTermsByPostfixAndPrefixModule 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
		return $js_array;
68
	}
69
70
	/**
71
	 * Process delete posts user inputs by category.
72
	 *
73
	 * @param array $request Request array.
74
	 * @param array $options Options for deleting posts.
75
	 *
76
	 * @return array $options  Inputs from user for posts that were need to delete
77
	 */
78
	protected function convert_user_input_to_options( $request, $options ) {
79
		$options['taxonomy']  = bd_array_get( $request, 'smbd_' . $this->field_slug . '_taxonomy' );
80
		$options['term_opt']  = bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_opt' );
81
		$options['term_text'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_text' );
82
		$options['no_posts']  = bd_array_get( $request, 'smbd_' . $this->field_slug . '_no_posts' );
83
84
		return $options;
85
	}
86
87
	/**
88
	 * Build query from delete options.
89
	 *
90
	 * @param array $options Delete options.
91
	 *
92
	 * @return array Query.
93
	 */
94
	protected function build_query( $options ) {
95
		$query     = array();
96
		$term_text = $options['term_text'];
97
		$term_opt  = $options['term_opt'];
98
99
		switch ( $term_opt ) {
100
			case 'equal_to':
101
				$query['name__like'] = $term_text;
102
				break;
103
104
			case 'not_equal_to':
105
				$term_ids         = $this->term_query( array( 'name__like' => $term_text ), $options['taxonomy'] );
106
				$query['exclude'] = $term_ids;
107
				break;
108
109
			case 'starts':
110
				$term_ids         = $this->term_starts( $term_text, $options );
111
				$query['include'] = $term_ids;
112
				break;
113
114
			case 'ends':
115
				$term_ids         = $this->term_ends( $term_text, $options );
116
				$query['include'] = $term_ids;
117
				break;
118
119
			case 'contains':
120
				$term_ids         = $this->term_contains( $term_text, $options );
121
				$query['include'] = $term_ids;
122
				break;
123
124
			case 'non_contains':
125
				$term_ids         = $this->term_query( array( 'name__like' => "%$term_text%" ), $options['taxonomy'] );
126
				$query['exclude'] = $term_ids;
127
				break;
128
		}
129
130
		return $query;
131
	}
132
133
	/**
134
	 * Response message for deleting posts.
135
	 *
136
	 * @param int $items_deleted Total number of posts deleted.
137
	 *
138
	 * @return string Response message
139
	 */
140
	protected function get_success_message( $items_deleted ) {
141
		/* translators: 1 Number of posts deleted */
142
		return _n( 'Deleted %d term with the selected options', 'Deleted %d terms with the selected options', $items_deleted, 'bulk-delete' );
143
	}
144
}
145