Passed
Pull Request — dev/6.1.0 (#661)
by Sudar
08:49 queued 06:03
created

DeleteCommentsByAuthorModule::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
ccs 0
cts 16
cp 0
crap 2
rs 9.7666
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'         => __( 'Delete Comments 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 the comment author details based on which comments should be deleted. You can either enter the name, email or the url of the comment author.', '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 ); ?>_details" type="text" placeholder="<?php _e( 'Author Details', '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['author_info'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_details', '' ) );
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 ( is_email( $options['author_info'] ) ) {
87
			$query['author_email'] = $options['author_info'];
88
		} elseif ( esc_url_raw( $options['author_info'] ) === $options['author_info'] ) {
89
			$query['author_url'] = esc_url_raw( $options['author_info'] );
90
		} else {
91
			$query['bd_db_column_name']  = 'comment_author';
92
			$query['bd_db_column_value'] = $options['author_info'];
93
			$query_overrider             = new DeleteCommentsQueryOverrider();
94
			$query_overrider->load();
95
		}
96
97
		$date_query = $this->get_date_query( $options );
98
99
		if ( ! empty( $date_query ) ) {
100
			$query['date_query'] = $date_query;
101
		}
102
103
		return $query;
104
	}
105
}
106