Passed
Pull Request — dev/6.0.0 (#332)
by Rajan
23:08 queued 19:58
created

get_success_message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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    = 'by_name';
18
		$this->meta_box_slug = 'bd_by_name';
19
		$this->action        = 'delete_terms_by_name';
20
		$this->cron_hook     = 'do-bulk-delete-term-name';
21
		$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-category/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sc';
22
		$this->messages      = array(
23
			'box_label'  => __( 'By Terms by Name', 'bulk-delete' ),
24
			'scheduled'  => __( 'The selected posts 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', '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
			<table class="optiontable">
46
				<?php
47
				// $this->render_have_post_settings(); // TODO
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
				?>
49
			</table>
50
51
		</fieldset>
52
		<?php
53
		$this->render_submit_button();
54
	}
55
56
	public function filter_js_array( $js_array ) {
57
		$js_array['validators'][ $this->action ] = 'validatePostTypeSelect2';
58
		$js_array['error_msg'][ $this->action ]  = 'selectPostType';
59
		$js_array['msg']['selectPostType']       = __( 'Please select at least one post type', 'bulk-delete' );
60
61
		$js_array['dt_iterators'][] = '_' . $this->field_slug;
62
63
		return $js_array;
64
	}
65
66
	/**
67
	 * Process delete posts user inputs by category.
68
	 *
69
	 * @param array $request Request array.
70
	 * @param array $options Options for deleting posts.
71
	 *
72
	 * @return array $options  Inputs from user for posts that were need to delete
73
	 */
74
	protected function convert_user_input_to_options( $request, $options ) {
75
		$options['taxonomy']     = bd_array_get( $request, 'smbd_' . $this->field_slug . '_taxonomy' );
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
		$options['no_posts']     = bd_array_get( $request, 'smbd_' . $this->field_slug . '_no_posts' );
79
80
		return $options;
81
	}
82
83
	/**
84
	 * Build query from delete options.
85
	 *
86
	 * @param array $options Delete options.
87
	 *
88
	 * @return array Query.
89
	 */
90
	protected function build_query( $options ) {
91
		$query     = array();
92
		$term_text = $options['term_text'];
93
		$term_opt  = $options['term_opt'];
94
95
		switch ( $term_opt ) {
96
			case 'equal_to':
97
				$query['name__like'] = $term_text;
98
				break;
99
100
			case 'not_equal_to':
101
				$term_ids         = bd_term_query( array( 'name__like' => $term_text ), $options['taxonomy'] );
102
				$query['exclude'] = $term_ids;
103
				break;
104
105
			case 'starts':
106
				$term_ids            = bd_term_starts( $term_text , $options );
107
				$query['include']    = $term_ids;
108
				break;
109
110
			case 'ends':
111
				$term_ids            = bd_term_ends( $term_text , $options );
112
				$query['include']    = $term_ids;
113
				break;
114
115
			case 'contains':
116
				$term_ids            = bd_term_contains( $term_text , $options );
117
				$query['include']    = $term_ids;
118
				break;
119
120
			case 'non_contains':
121
				$term_ids         = bd_term_query( array( 'name__like' => "%$term_text%" ), $options['taxonomy'] );
122
				$query['exclude'] = $term_ids;
123
				break;
124
		}
125
126
		return $query;
127
	}
128
129
	/**
130
	 * Response message for deleting posts.
131
	 *
132
	 * @param int $items_deleted Total number of posts deleted.
133
	 *
134
	 * @return string Response message
135
	 */
136
	protected function get_success_message( $items_deleted ) {
137
		/* translators: 1 Number of posts deleted */
138
		return _n( 'Deleted %d term with the selected options', 'Deleted %d terms with the selected options', $items_deleted, 'bulk-delete' );
139
	}
140
}
141