Completed
Push — analysis-zYwr3y ( 770b36 )
by Sudar
28:55 queued 13:56
created

DeleteTermsByPostfixAndPrefixModule   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 11 1
A get_success_message() 0 3 1
A render() 0 22 1
A filter_js_array() 0 8 1
C build_query() 0 22 8
A convert_user_input_to_options() 0 7 1
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();
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
93
		if ( $options['term_opt'] == 'equal_to' ) {
94
			$query['name__like'] = $options['term_text'];
95
		}elseif ( $options['term_opt'] == 'not_equal_to' ) {
96
			$query['name__like'] = '';
97
		}elseif ( $options['term_opt'] == 'starts' ) {
98
			$query['name__like'] = '';
99
		}elseif ( $options['term_opt'] == 'ends' ) {
100
			$query['name__like'] = '';
101
		}elseif ( $options['term_opt'] == 'contains' ) {
102
			$query['name__like'] = '';
103
		}elseif ( $options['term_opt'] == 'non_contains' ) {
104
			$query['name__like'] = '';
105
		}
106
107
		if( isset($options['no_posts']) ){
108
			$query['count'] = '';
109
		}
110
111
		return $query;
112
	}
113
114
	/**
115
	 * Response message for deleting posts.
116
	 *
117
	 * @param int $items_deleted Total number of posts deleted.
118
	 *
119
	 * @return string Response message
120
	 */
121
	protected function get_success_message( $items_deleted ) {
122
		/* translators: 1 Number of posts deleted */
123
		return _n( 'Deleted %d post with the selected post category', 'Deleted %d posts with the selected post category', $items_deleted, 'bulk-delete' );
124
	}
125
}
126