Passed
Pull Request — dev/6.0.0 (#277)
by Rajan
14:36
created

DeletePostsByTaxonomyModule::build_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 20
ccs 0
cts 16
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Posts\Modules;
4
5
use BulkWP\BulkDelete\Core\Posts\PostsModule;
6
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Custom Taxonomy Module.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByTaxonomyModule extends PostsModule {
15
	protected function initialize() {
16
		$this->item_type     = 'posts';
17
		$this->field_slug    = 'taxs';
18
		$this->meta_box_slug = 'bd_posts_by_taxonomy';
19
		$this->action        = 'bd_delete_posts_by_taxonomy';
20
		$this->cron_hook     = 'do-bulk-delete-taxonomy';
21
		$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-taxonomy/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=addonlist&utm_content=bd-stx';
22
		$this->messages      = array(
23
			'box_label' => __( 'By Custom Taxonomy', 'bulk-delete' ),
24
			'scheduled' => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
25
			'cron_name' => __( 'Delete Post By Taxonomy', 'bulk-delete' ),
26
		);
27
	}
28
29
	public function render() {
30
		$taxs =  get_taxonomies( array(
31
				'public'   => true,
32
				'_builtin' => false,
33
			), 'objects'
34
		);
35
36
		$terms_array = array();
37
		if ( count( $taxs ) > 0 ) {
38
			foreach ( $taxs as $tax ) {
39
				$terms = get_terms( $tax->name );
40
				if ( count( $terms ) > 0 ) {
0 ignored issues
show
Bug introduced by
It seems like $terms can also be of type WP_Error; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
				if ( count( /** @scrutinizer ignore-type */ $terms ) > 0 ) {
Loading history...
41
					$terms_array[$tax->name] = $terms;
42
				}
43
			}
44
		}
45
46
		if ( count( $terms_array ) > 0 ) {
47
?>
48
        <!-- Custom tax Start-->
49
        <h4><?php _e( 'Select the post type from which you want to delete posts by custom taxonomy', 'bulk-delete' ); ?></h4>
50
51
        <fieldset class="options">
52
            <table class="optiontable">
53
				<?php $this->render_post_type_dropdown(); ?>
54
            </table>
55
56
            <h4><?php _e( 'Select the taxonomies from which you want to delete posts', 'bulk-delete' ) ?></h4>
57
58
            <table class="optiontable">
59
<?php
60
			foreach ( $terms_array as $tax => $terms ) {
61
?>
62
                <tr>
63
                    <td scope="row" >
64
                        <input name="smbd_taxs" value="<?php echo $tax; ?>" type="radio" class="custom-tax">
65
                    </td>
66
                    <td>
67
                        <label for="smbd_taxs"><?php echo $taxs[$tax]->labels->name; ?> </label>
68
                    </td>
69
                </tr>
70
<?php
71
			}
72
?>
73
            </table>
74
75
            <h4><?php _e( 'The selected taxonomy has the following terms. Select the terms from which you want to delete posts', 'bulk-delete' ) ?></h4>
76
            <p><?php _e( 'Note: The post count below for each term is the total number of posts in that term, irrespective of post type', 'bulk-delete' ); ?>.</p>
77
<?php
78
			foreach ( $terms_array as $tax => $terms ) {
79
?>
80
            <table class="optiontable terms_<?php echo $tax;?> terms">
81
<?php
82
				foreach ( $terms as $term ) {
83
?>
84
                    <tr>
85
                        <td scope="row" >
86
                            <input name="smbd_taxs_terms[]" value="<?php echo $term->slug; ?>" type="checkbox" class="terms">
87
                        </td>
88
                        <td>
89
                            <label for="smbd_taxs_terms"><?php echo $term->name; ?> (<?php echo $term->count . ' '; _e( 'Posts', 'bulk-delete' ); ?>)</label>
90
                        </td>
91
                    </tr>
92
<?php
93
				}
94
?>
95
            </table>
96
<?php
97
			}
98
?>
99
            <table class="optiontable">
100
				<?php
101
				$this->render_filtering_table_header();
102
				$this->render_restrict_settings();
103
				$this->render_delete_settings();
104
				$this->render_limit_settings();
105
				$this->render_cron_settings();
106
				?>
107
            </table>
108
109
            </fieldset>
110
<?php
111
			$this->render_submit_button();
112
		} else {
113
?>
114
            <h4><?php _e( "This WordPress installation doesn't have any non-empty custom taxonomies defined", 'bulk-delete' ) ?></h4>
115
<?php
116
		}
117
	}
118
119
	protected function convert_user_input_to_options( $request, $options ) {
120
		$options['post_type']          = bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' );
121
		$options['selected_taxs']      = bd_array_get( $request, 'smbd_' . $this->field_slug );
122
		$options['selected_tax_terms'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_terms' );
123
124
		return $options;
125
	}
126
127
	protected function build_query( $delete_options ) {
128
		// For compatibility reasons set default post type to 'post'
129
		$post_type = bd_array_get( $delete_options, 'post_type', 'post' );
130
131
		$taxonomy = $delete_options['selected_taxs'];
132
		$terms    = $delete_options['selected_tax_terms'];
133
134
		$options = array(
135
			'post_status' => 'publish',
136
			'post_type'   => $post_type,
137
			'tax_query'   => array(
138
				array(
139
					'taxonomy' => $taxonomy,
140
					'terms'    => $terms,
141
					'field'    => 'slug',
142
				),
143
			),
144
		);
145
146
		return $options;
147
	}
148
149
	protected function get_success_message( $items_deleted ) {
150
		/* translators: 1 Number of pages deleted */
151
		return _n( 'Deleted %d post with the selected taxonomy', 'Deleted %d posts with the selected post taxonomy', $items_deleted, 'bulk-delete' );
152
	}
153
154
	/**
155
	 * Schedule job title.
156
	 *
157
	 * @return string humane readable title
158
	 */
159
	protected function get_cron_name(){
160
		return $this->messages['cron_name'];
161
	}
162
}
163