Passed
Push — 677-feature/add-wp-cli-support ( d30ec9...64c504 )
by
unknown
05:28
created

DeletePostsByCommentsModule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 56%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 58
c 3
b 0
f 1
dl 0
loc 119
ccs 28
cts 50
cp 0.56
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A convert_user_input_to_options() 0 6 1
A render() 0 31 1
A build_query() 0 18 2
A initialize() 0 18 1
A append_to_js_array() 0 4 1
A get_non_standard_input_key_map() 0 7 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 with the selected comments count', 'bulk-delete' ),
34
			/* translators: 1 Number of posts deleted */
35 8
			'deleted_multiple'  => __( 'Deleted %d posts with the selected comments count', 'bulk-delete' ),
36
		);
37
	}
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
				<?php $this->render_post_type_with_status( false, 'comments' ); ?>
50
				<tr>
51
					<td>
52
						<?php _e( 'Delete posts that have comments', 'bulk-delete' ); ?>
53
						<?php $this->render_number_comparison_operators(); ?>
54
						<input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_count_value"
55
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_count_value" placeholder="Comments Count" min="0" class="comments_count_num">
56
					</td>
57
				</tr>
58
			</table>
59
60
			<table class="optiontable">
61
				<?php
62
				$this->render_filtering_table_header();
63
				$this->render_restrict_settings();
64
				$this->render_delete_settings();
65
				$this->render_private_post_settings();
66
				$this->render_limit_settings();
67
				$this->render_cron_settings();
68
				?>
69
			</table>
70
		</fieldset>
71
		<?php
72
		$this->render_submit_button();
73
	}
74
75
	/**
76
	 * Process delete posts, user inputs by comments count.
77
	 *
78
	 * @param array $request Request array.
79
	 * @param array $options Options for deleting posts.
80
	 *
81
	 * @return array $options Inputs from user for posts that were need to be deleted.
82
	 */
83
	protected function convert_user_input_to_options( $request, $options ) {
84
		$options['operator']           = bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' );
85
		$options['comment_count']      = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_count_value' ) );
86
		$options['selected_post_type'] = bd_array_get( $request, 'smbd_' . $this->field_slug );
87
88
		return $options;
89
	}
90
91
	/**
92
	 * Build the Query from user input.
93
	 *
94
	 * @param array $options User Input.
95
	 *
96
	 * @return array $query Query Params.
97
	 */
98 8
	protected function build_query( $options ) {
99 8
		$query = array();
100
101 8
		if ( array_key_exists( 'selected_post_type', $options ) ) {
102 4
			$type_status = $this->split_post_type_and_status( $options['selected_post_type'] );
103 4
			$type        = $type_status['type'];
104 4
			$status      = $type_status['status'];
105
106 4
			$query['post_type']   = $type;
107 4
			$query['post_status'] = $status;
108
		}
109
110 8
		$query['comment_count'] = array(
111 8
			'compare' => $options['operator'],
112 8
			'value'   => $options['comment_count'],
113
		);
114
115 8
		return $query;
116
	}
117
118
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
119
	protected function append_to_js_array( $js_array ) {
120
		$js_array['validators'][ $this->action ] = 'validateCommentsCount';
121
122
		return $js_array;
123
	}
124
125
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
126
	protected function get_non_standard_input_key_map() {
127
		$prefix = $this->get_ui_input_prefix();
128
129
		$prefix_without_underscore_at_end = substr( $prefix, 0, -1 );
130
131
		return [
132
			$prefix_without_underscore_at_end => $prefix . 'selected_post_type',
133
		];
134
	}
135
}
136