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

DeleteTermsByPostfixAndPrefixModule::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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