Completed
Pull Request — dev/6.0.0 (#243)
by Sudar
08:58 queued 04:58
created

DeletePostsByCategoryMetabox   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 21
cts 42
cp 0.5
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 10 1
A convert_user_input_to_options() 0 6 1
A delete() 0 16 2
A get_success_message() 0 3 1
B render() 0 36 1
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 Category Metabox.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByCategoryMetabox extends PostsMetabox {
15
16 11
	protected function initialize() {
17 11
		$this->item_type     = 'posts';
18 11
		$this->field_slug    = 'cats';
19 11
		$this->meta_box_slug = 'bd_by_category';
20 11
		$this->action        = 'delete_posts_by_category';
21 11
		$this->cron_hook     = 'do-bulk-delete-cat';
22 11
		$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';
23 11
		$this->messages      = array(
24 11
			'box_label' => __( 'By Post Category', 'bulk-delete' ),
25 11
			'scheduled' => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
26
		);
27 11
	}
28
29
	/**
30
	 * Render Delete posts by category box.
31
	 */
32
	public function render() {
33
		?>
34
		<!-- Category Start-->
35
		<h4><?php _e( 'Select the post type from which you want to delete posts by category', 'bulk-delete' ); ?></h4>
36
		<fieldset class="options">
37
			<table class="optiontable">
38
				<?php $this->render_post_type_dropdown(); ?>
39
			</table>
40
41
			<h4><?php _e( 'Select the categories from which you want to delete posts', 'bulk-delete' ); ?></h4>
42
			<p>
43
				<?php _e( 'Note: The post count below for each category is the total number of posts in that category, irrespective of post type', 'bulk-delete' ); ?>
44
			.</p>
45
46
			<table class="form-table">
47
				<tr>
48
					<td scope="row">
49
						<?php $this->render_category_dropdown(); ?>
50
					</td>
51
				</tr>
52
			</table>
53
54
			<table class="optiontable">
55
				<?php
56
				$this->render_filtering_table_header();
57
				$this->render_restrict_settings();
58
				$this->render_delete_settings();
59
				$this->render_private_post_settings();
60
				$this->render_limit_settings();
61
				$this->render_cron_settings();
62
				?>
63
			</table>
64
65
		</fieldset>
66
		<?php
67
		$this->render_submit_button();
68
	}
69
70
	/**
71
	 * Process delete posts user inputs by category.
72
	 *
73
	 * @param array $request Request array.
74
	 * @param array $options Options for deleting posts.
75
	 *
76
	 * @return array $options  Inputs from user for posts that were need to delete
77
	 */
78
	protected function convert_user_input_to_options( $request, $options ) {
79
		$options['post_type']     = bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' );
80
		$options['selected_cats'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_category' );
81
		$options['private']       = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_private', false );
82
83
		return $options;
84
	}
85
86
	/**
87
	 * Delete posts by category.
88
	 *
89
	 * @param array $delete_options Options for deleting posts.
90
	 *
91
	 * @return int $posts_deleted  Number of posts that were deleted
92
	 */
93 11
	public function delete( $delete_options ) {
94 11
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
95
96 11
		$options       = array();
97 11
		$selected_cats = $delete_options['selected_cats'];
98
99 11
		if ( in_array( 'all', $selected_cats, true ) ) {
100 2
			$options['category__not__in'] = array( 0 );
101
		} else {
102 9
			$options['category__in'] = $selected_cats;
103
		}
104
105 11
		$options  = bd_build_query_options( $delete_options, $options );
106 11
		$post_ids = bd_query( $options );
107
108 11
		return $this->delete_posts_by_id( $post_ids, $delete_options['force_delete'] );
109
	}
110
111
	/**
112
	 * Response message for deleting posts.
113
	 *
114
	 * @param int $items_deleted Total number of posts deleted.
115
	 *
116
	 * @return string Response message
117
	 */
118
	protected function get_success_message( $items_deleted ) {
119
		/* translators: 1 Number of posts deleted */
120
		return _n( 'Deleted %d post with the selected post category', 'Deleted %d posts with the selected post category', $items_deleted, 'bulk-delete' );
121
	}
122
}
123