Completed
Push — 247-fix/delete-term-meta ( d5f818...2a1fe3 )
by Sudar
67:09 queued 61:16
created

DeletePostsByCommentsModule   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 52.27%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 113
ccs 23
cts 44
cp 0.5227
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A append_to_js_array() 0 4 1
A convert_user_input_to_options() 0 5 1
A initialize() 0 18 1
A render() 0 34 1
A build_query() 0 9 1
A get_success_message() 0 3 1
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 Comments Module.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByCommentsModule extends PostsModule {
15
	/**
16
	 * Base parameters setup.
17
	 */
18 8
	protected function initialize() {
19 8
		$this->item_type     = 'posts';
20 8
		$this->field_slug    = 'comments';
21 8
		$this->meta_box_slug = 'bd_by_comments';
22 8
		$this->action        = 'delete_posts_by_comments';
23 8
		$this->cron_hook     = 'do-bulk-delete-posts-by-comments';
24 8
		$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-posts-by-comments/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bds-p-c';
25 8
		$this->messages      = array(
26 8
			'box_label'         => __( 'By Comment count', 'bulk-delete' ),
27 8
			'scheduled'         => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
28 8
			'cron_label'        => __( 'Delete Post By Comments', 'bulk-delete' ),
29 8
			'confirm_deletion'  => __( 'Are you sure you want to delete all the posts based on the selected comment count setting?', 'bulk-delete' ),
30 8
			'confirm_scheduled' => __( 'Are you sure you want to schedule the deletion of all the posts based on the selected comment count setting?', 'bulk-delete' ),
31 8
			'validation_error'  => __( 'Please enter the comments count based on which posts should be deleted. A valid comment count will be greater than or equal to zero', 'bulk-delete' ),
32
			/* translators: 1 Number of posts deleted */
33 8
			'deleted_one'       => __( 'Deleted %d post from the selected post type and post status', 'bulk-delete' ),
34
			/* translators: 1 Number of posts deleted */
35 8
			'deleted_multiple'  => __( 'Deleted %d posts from the selected post type and post status', 'bulk-delete' ),
36
		);
37 8
	}
38
39
	/**
40
	 * Render Delete posts by comments box.
41
	 */
42
	public function render() {
43
		?>
44
		<h4><?php _e( 'Delete Posts based on the number of comments', 'bulk-delete' ); ?></h4>
45
46
		<!-- Comments start-->
47
		<fieldset class="options">
48
			<table class="optiontable">
49
				<tr>
50
					<td scope="row" colspan="2">
51
						<?php _e( 'Delete posts that have comments', 'bulk-delete' ); ?>
52
					</td>
53
					<td>
54
						<?php $this->render_number_comparison_operators(); ?>
55
					</td>
56
					<td>
57
						<input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_count_value"
58
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_count_value" placeholder="Comments Count" min="0" class="comments_count_num">
59
					</td>
60
				</tr>
61
			</table>
62
63
			<table class="optiontable">
64
				<?php
65
				$this->render_filtering_table_header();
66
				$this->render_restrict_settings();
67
				$this->render_delete_settings();
68
				$this->render_private_post_settings();
69
				$this->render_limit_settings();
70
				$this->render_cron_settings();
71
				?>
72
			</table>
73
		</fieldset>
74
		<?php
75
		$this->render_submit_button();
76
	}
77
78
	/**
79
	 * Process delete posts, user inputs by comments count.
80
	 *
81
	 * @param array $request Request array.
82
	 * @param array $options Options for deleting posts.
83
	 *
84
	 * @return array $options Inputs from user for posts that were need to be deleted.
85
	 */
86
	protected function convert_user_input_to_options( $request, $options ) {
87
		$options['operator']      = bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' );
88
		$options['comment_count'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_count_value' ) );
89
90
		return $options;
91
	}
92
93
	/**
94
	 * Build the Query from user input.
95
	 *
96
	 * @param array $options User Input.
97
	 *
98
	 * @return array $query Query Params.
99
	 */
100 8
	protected function build_query( $options ) {
101 8
		$query = array();
102
103 8
		$query['comment_count'] = array(
104 8
			'compare' => $options['operator'],
105 8
			'value'   => $options['comment_count'],
106
		);
107
108 8
		return $query;
109
	}
110
111
	protected function append_to_js_array( $js_array ) {
112
		$js_array['validators'][ $this->action ] = 'validateCommentsCount';
113
114
		return $js_array;
115
	}
116
117
	/**
118
	 * Response message for deleting posts.
119
	 *
120
	 * @param int $items_deleted count of items deleted.
121
	 *
122
	 * @return string Response message
123
	 */
124
	protected function get_success_message( $items_deleted ) {
125
		/* translators: 1 Number of posts deleted */
126
		return _n( 'Deleted %d post with the selected comments count', 'Deleted %d posts with the selected comments count', $items_deleted, 'bulk-delete' );
127
	}
128
}
129