Completed
Push — 233-feature/posts-by-post-stat... ( 0c4f56...dd14ac )
by Sudar
10:31 queued 06:45
created

DeletePostsByCategoryMetabox   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

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