Passed
Push — 190-feature/delete-posts-by-cu... ( 35585d...88e0e7 )
by Rajan
05:10
created

delete_posts_by_taxonomy()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 6
nop 1
dl 0
loc 38
ccs 0
cts 20
cp 0
crap 20
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Posts\Metabox;
4
5
use BulkWP\BulkDelete\Core\Posts\PostsMetabox;
6
7 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Status Metabox.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByCustomTaxonomMetabox 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 ) {
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 bd_render_post_type_dropdown( 'tax' ); ?>
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_tax_terms[]" value="<?php echo $term->slug; ?>" type="checkbox" class="terms">
86
                        </td>
87
                        <td>
88
                            <label for="smbd_tax_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                       = array();
120
		$options['post_type']          = bd_array_get( $request, 'smbd_tax_post_type', 'post' );
121
		$options['selected_taxs']      = bd_array_get( $request, 'smbd_taxs' );
122
		$options['selected_tax_terms'] = bd_array_get( $request, 'smbd_tax_terms' );
123
		$options['restrict']           = bd_array_get_bool( $request, 'smbd_taxs_restrict', false );
124
		$options['private']            = bd_array_get_bool( $request, 'smbd_taxs_private' );
125
		$options['limit_to']           = absint( bd_array_get( $request, 'smbd_taxs_limit_to', 0 ) );
126
		$options['force_delete']       = bd_array_get_bool( $request, 'smbd_taxs_force_delete', false );
127
128
		$options['date_op']            = bd_array_get( $request, 'smbd_taxs_op' );
129
		$options['days']               = absint( bd_array_get( $request, 'smbd_taxs_days' ) );
130
131
		return $options;
132
	}
133
134
	public function delete( $delete_options ) {
135
136
		if ( bd_array_get( $_POST, 'smbd_taxs_cron', 'false' ) == 'true' ) {
137
			$freq = $_POST['smbd_taxs_cron_freq'];
138
			$time = strtotime( $_POST['smbd_taxs_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 );
139
140
			if ( $freq == -1 ) {
141
				wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_TAXONOMY, array( $delete_options ) );
0 ignored issues
show
Bug introduced by
The type BulkWP\BulkDelete\Core\Posts\Metabox\Bulk_Delete was not found. Did you mean Bulk_Delete? If so, make sure to prefix the type with \.
Loading history...
142
			} else {
143
				wp_schedule_event( $time, $freq, Bulk_Delete::CRON_HOOK_TAXONOMY, array( $delete_options ) );
144
			}
145
			$msg = __( 'Posts from the selected custom taxonomies are scheduled for deletion.', 'bulk-delete' ) . ' ' .
0 ignored issues
show
Unused Code introduced by
The assignment to $msg is dead and can be removed.
Loading history...
146
				sprintf( __( 'See the full list of <a href = "%s">scheduled tasks</a>' , 'bulk-delete' ), get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG );
147
		} else {
148
			return $deleted_count = $this->delete_posts_by_taxonomy( $delete_options );
0 ignored issues
show
Unused Code introduced by
The assignment to $deleted_count is dead and can be removed.
Loading history...
149
		}
150
151
	}
152
153
	public static function delete_posts_by_taxonomy( $delete_options ) {
154
		// For compatibility reasons set default post type to 'post'
155
		$post_type = bd_array_get( $delete_options, 'post_type', 'post' );
156
157
		if ( array_key_exists( 'taxs_op', $delete_options ) ) {
158
			$delete_options['date_op'] = $delete_options['taxs_op'];
159
			$delete_options['days']    = $delete_options['taxs_days'];
160
		}
161
162
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
163
164
		$selected_taxs      = $delete_options['selected_taxs'];
165
		$selected_tax_terms = $delete_options['selected_tax_terms'];
166
167
		$options = array(
168
			'post_status' => 'publish',
169
			'post_type'   => $post_type,
170
			'tax_query'   => array(
171
				array(
172
					'taxonomy' => $selected_taxs,
173
					'terms'    => $selected_tax_terms,
174
					'field'    => 'slug',
175
				),
176
			),
177
		);
178
179
		$options  = bd_build_query_options( $delete_options, $options );
180
		$post_ids = bd_query( $options );
181
		foreach ( $post_ids as $post_id ) {
182
			// $force delete parameter to custom post types doesn't work
183
			if ( $delete_options['force_delete'] ) {
184
				wp_delete_post( $post_id, true );
185
			} else {
186
				wp_trash_post( $post_id );
187
			}
188
		}
189
190
		return count( $post_ids );
191
	}
192
193
	protected function get_success_message( $items_deleted ) {
194
		/* translators: 1 Number of pages deleted */
195
		return _n( 'Deleted %d post with the selected taxonomy', 'Deleted %d posts with the selected post taxonomy', $items_deleted, 'bulk-delete' );
196
	}
197
}
198