DeletePostsByCategoryModule::initialize()   A
last analyzed

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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
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 Category Module.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByCategoryModule extends PostsModule {
15 11
	protected function initialize() {
16 11
		$this->item_type     = 'posts';
17 11
		$this->field_slug    = 'cats';
18 11
		$this->meta_box_slug = 'bd_by_category';
19 11
		$this->action        = 'delete_posts_by_category';
20 11
		$this->cron_hook     = 'do-bulk-delete-cat';
21 11
		$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-posts-by-category/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sc';
22 11
		$this->messages      = array(
23 11
			'box_label'  => __( 'By Post Category', 'bulk-delete' ),
24 11
			'scheduled'  => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
25 11
			'cron_label' => __( 'Delete Post By Category', '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="optiontable">
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_exclude_sticky_settings();
59
				$this->render_delete_settings();
60
				$this->render_private_post_settings();
61
				$this->render_limit_settings();
62
				$this->render_cron_settings();
63
				?>
64
			</table>
65
66
		</fieldset>
67
		<?php
68
		$this->render_submit_button();
69
	}
70
71
	protected function append_to_js_array( $js_array ) {
72
		$js_array['validators']['delete_posts_by_category'] = 'validateSelect2';
73
		$js_array['error_msg']['delete_posts_by_category']  = 'selectCategory';
74
		$js_array['msg']['selectCategory']                  = __( 'Please select at least one category', 'bulk-delete' );
75
76
		return $js_array;
77
	}
78
79
	/**
80
	 * Process delete posts user inputs by category.
81
	 *
82
	 * @param array $request Request array.
83
	 * @param array $options Options for deleting posts.
84
	 *
85
	 * @return array $options  Inputs from user for posts that were need to delete
86
	 */
87
	protected function convert_user_input_to_options( $request, $options ) {
88
		$options['post_type']     = bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' );
89
		$options['selected_cats'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_category' );
90
		$options['private']       = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_private', false );
91
92
		return $options;
93
	}
94
95
	/**
96
	 * Build query from delete options.
97
	 *
98
	 * @param array $options Delete options.
99
	 *
100
	 * @return array Query.
101
	 */
102 11
	protected function build_query( $options ) {
103 11
		$query = array();
104
105 11
		if ( in_array( 'all', $options['selected_cats'], true ) ) {
106 2
			$query['category__not__in'] = array( 0 );
107
		} else {
108 9
			$query['category__in'] = $options['selected_cats'];
109
		}
110
111 11
		return $query;
112
	}
113
114
	/**
115
	 * Response message for deleting posts.
116
	 *
117
	 * @param int $items_deleted Total number of posts deleted.
118
	 *
119
	 * @return string Response message
120
	 */
121
	protected function get_success_message( $items_deleted ) {
122
		/* translators: 1 Number of posts deleted */
123
		return _n( 'Deleted %d post with the selected post category', 'Deleted %d posts with the selected post category', $items_deleted, 'bulk-delete' );
124
	}
125
}
126