Passed
Push — 221-tests/show-built-in-taxono... ( 6cde01 )
by Rajan
08:17
created

DeletePostsByTaxonomyModule::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 11
ccs 11
cts 11
cp 1
crap 1
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 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Taxonomy Module.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByTaxonomyModule extends PostsModule {
15 1
	protected function initialize() {
16 1
		$this->item_type     = 'posts';
17 1
		$this->field_slug    = 'taxs';
18 1
		$this->meta_box_slug = 'bd_posts_by_taxonomy';
19 1
		$this->action        = 'bd_delete_posts_by_taxonomy';
20 1
		$this->cron_hook     = 'do-bulk-delete-taxonomy';
21 1
		$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 1
		$this->messages      = array(
23 1
			'box_label'  => __( 'By Taxonomy', 'bulk-delete' ),
24 1
			'scheduled'  => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
25 1
			'cron_label' => __( 'Delete Post By Taxonomy', 'bulk-delete' ),
26
		);
27 1
	}
28
29
	public function render() {
30
		$taxs =  get_taxonomies( array(), 'objects'
31
		);
32
33
		$terms_array = array();
34
		if ( count( $taxs ) > 0 ) {
35
			foreach ( $taxs as $tax ) {
36
				$terms = get_terms( $tax->name );
37
				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

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