Passed
Pull Request — dev/6.1.0 (#654)
by Sudar
04:47
created

DeleteCommentsByIPModule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 46
dl 0
loc 84
ccs 0
cts 58
cp 0
rs 10
c 2
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 27 1
A append_to_js_array() 0 4 1
A convert_user_input_to_options() 0 4 1
A initialize() 0 18 1
A build_query() 0 16 3
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Comments\Modules;
4
5
use BulkWP\BulkDelete\Core\Comments\CommentsModule;
6
use BulkWP\BulkDelete\Core\Comments\QueryOverriders\DeleteCommentsQueryOverrider;
7
8
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
9
10
/**
11
 * Delete Comments by Author Module.
12
 *
13
 * @since 6.1.0
14
 */
15
class DeleteCommentsByIPModule extends CommentsModule {
16
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
17
	protected function initialize() {
18
		$this->item_type     = 'comments';
19
		$this->field_slug    = 'comments_by_ip';
20
		$this->meta_box_slug = 'bd_comments_by_ip';
21
		$this->action        = 'delete_comments_by_ip';
22
		$this->cron_hook     = 'do-bulk-delete-comments-by-ip';
23
		$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-comments/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sp';
24
		$this->messages      = array(
25
			'box_label'         => __( 'By IP Address', 'bulk-delete' ),
26
			'scheduled'         => __( 'The selected comments are scheduled for deletion', 'bulk-delete' ),
27
			'cron_label'        => __( 'Delete Comments By IP Address', 'bulk-delete' ),
28
			'confirm_deletion'  => __( 'Are you sure you want to delete all the comments from the selected condition?', 'bulk-delete' ),
29
			'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the comments from the selected condition?', 'bulk-delete' ),
30
			'validation_error'  => __( 'Please enter valid IP Address based on which comments should be deleted', 'bulk-delete' ),
31
			/* translators: 1 Number of comments deleted */
32
			'deleted_one'       => __( 'Deleted %d comment with selected condition', 'bulk-delete' ),
33
			/* translators: 1 Number of comments deleted */
34
			'deleted_multiple'  => __( 'Deleted %d comments with selected condition', 'bulk-delete' ),
35
		);
36
	}
37
38
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
39
	public function render() {
40
		?>
41
		<!-- Pages start-->
42
		<h4><?php _e( 'Enter IP Address based on which you want to delete comments', 'bulk-delete' ); ?></h4>
43
44
		<fieldset class="options">
45
			<table class="optiontable">
46
				<tr>
47
					<td>
48
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_address" type="text" placeholder="<?php _e( 'IP Address', 'bulk-delete' ); ?>" class="validate">
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_limit_settings();
59
				$this->render_cron_settings();
60
				?>
61
			</table>
62
		</fieldset>
63
64
		<?php
65
		$this->render_submit_button();
66
	}
67
68
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
69
	protected function append_to_js_array( $js_array ) {
70
		$js_array['validators'][ $this->action ] = 'validateTextbox';
71
72
		return $js_array;
73
	}
74
75
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
76
	protected function convert_user_input_to_options( $request, $options ) {
77
		$options['ip_address'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_address', '' ) );
78
79
		return $options;
80
	}
81
82
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
83
	protected function build_query( $options ) {
84
		$query = array();
85
86
		if ( ! empty( $options['ip_address'] ) ) {
87
			$query['bd_db_column_name']  = 'comment_author_IP';
88
			$query['bd_db_column_value'] = $options['ip_address'];
89
			$query_overrider             = new DeleteCommentsQueryOverrider();
90
			$query_overrider->load();
91
		}
92
93
		$date_query = $this->get_date_query( $options );
94
		if ( ! empty( $date_query ) ) {
95
			$query['date_query'] = $date_query;
96
		}
97
98
		return $query;
99
	}
100
}
101