Completed
Pull Request — dev/6.1.0 (#654)
by
unknown
11:47 queued 08:49
created

DeleteCommentsByAuthorModule   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 56
c 5
b 2
f 0
dl 0
loc 99
ccs 0
cts 70
cp 0
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A convert_user_input_to_options() 0 6 1
A render() 0 35 1
A append_to_js_array() 0 4 1
A initialize() 0 18 1
A build_query() 0 21 5
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 DeleteCommentsByAuthorModule extends CommentsModule {
16
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
17
	protected function initialize() {
18
		$this->item_type     = 'comments';
19
		$this->field_slug    = 'comments_by_author';
20
		$this->meta_box_slug = 'bd_comments_by_author';
21
		$this->action        = 'delete_comments_by_author';
22
		$this->cron_hook     = 'do-bulk-delete-comments-by-author';
23
		$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-comments-by-author/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sp';
24
		$this->messages      = array(
25
			'box_label'         => __( 'By Author', 'bulk-delete' ),
26
			'scheduled'         => __( 'The selected comments are scheduled for deletion', 'bulk-delete' ),
27
			'cron_label'        => __( 'Delete Comments By author', '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 author name or email or URL 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 Author name or email or URL whose comments you want to delete', '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 ); ?>_name" type="text" placeholder="<?php _e( 'Author Name', 'bulk-delete' ); ?>">
49
					</td>
50
					<td> <?php _e( 'Or', 'bulk-delete' ); ?></td>
51
					<td>
52
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_email" type="text" placeholder="<?php _e( 'Author Email', 'bulk-delete' ); ?>">
53
					</td>
54
					<td> <?php _e( 'Or', 'bulk-delete' ); ?></td>
55
					<td>
56
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_url" type="text" placeholder="<?php _e( 'Author URL', 'bulk-delete' ); ?>">
57
					</td>
58
				</tr>				
59
			</table>
60
61
			<table class="optiontable">
62
				<?php
63
				$this->render_filtering_table_header();
64
				$this->render_restrict_settings();
65
				$this->render_delete_settings();
66
				$this->render_limit_settings();
67
				$this->render_cron_settings();
68
				?>
69
			</table>
70
		</fieldset>
71
72
		<?php
73
		$this->render_submit_button();
74
	}
75
76
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
77
	protected function append_to_js_array( $js_array ) {
78
		$js_array['validators'][ $this->action ] = 'noValidation';
79
80
		return $js_array;
81
	}
82
83
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
84
	protected function convert_user_input_to_options( $request, $options ) {
85
		$options['author_name']  = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_name', '' ) );
86
		$options['author_email'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_email', '' ) );
87
		$options['author_url']   = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_url', '' ) );
88
89
		return $options;
90
	}
91
92
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
93
	protected function build_query( $options ) {
94
		$query = array();
95
		if ( ! empty( $options['author_name'] ) ) {
96
			$query['bd_db_column_name']  = 'comment_author';
97
			$query['bd_db_column_value'] = $options['author_name'];
98
			$query_overrider             = new DeleteCommentsQueryOverrider();
99
			$query_overrider->load();
100
		}
101
		if ( ! empty( $options['author_email'] ) ) {
102
			$query['author_email'] = $options['author_email'];
103
		}
104
		if ( ! empty( $options['author_url'] ) ) {
105
			$query['author_url'] = $options['author_url'];
106
		}
107
		$date_query = $this->get_date_query( $options );
108
109
		if ( ! empty( $date_query ) ) {
110
			$query['date_query'] = $date_query;
111
		}
112
113
		return $query;
114
	}
115
}
116