Completed
Push — 192-feature/delete-posts-by-UR... ( 8c9c60...3f0497 )
by Rajan
12:40 queued 09:41
created

DeletePostsByCustomTaxonomyMetabox::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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 20
ccs 0
cts 16
cp 0
crap 2
rs 9.4285
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Posts\Metabox;
4
5
use BulkWP\BulkDelete\Core\Posts\PostsMetabox;
6
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Custom Taxonomy Metabox.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByCustomTaxonomyMetabox extends PostsMetabox {
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
		);
26
	}
27
28
	public function render() {
29
		$taxs =  get_taxonomies( array(
30
				'public'   => true,
31
				'_builtin' => false,
32
			), 'objects'
33
		);
34
35
		$terms_array = array();
36
		if ( count( $taxs ) > 0 ) {
37
			foreach ( $taxs as $tax ) {
38
				$terms = get_terms( $tax->name );
39
				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

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