Completed
Push — 247-fix/delete-term-meta ( 2a1fe3...132475 )
by Sudar
10:50 queued 05:24
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
43
		<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>
44
45
		<fieldset class="options">
46
			<table class="optiontable">
47
				<tr>
48
					<td>
49
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_details" type="text" placeholder="<?php _e( 'Author Details', 'bulk-delete' ); ?>" class="validate">
50
					</td>
51
				</tr>				
52
			</table>
53
54
			<table class="optiontable">
55
				<?php
56
				$this->render_filtering_table_header();
57
				$this->render_restrict_settings();
58
				$this->render_delete_settings();
59
				$this->render_limit_settings();
60
				$this->render_cron_settings();
61
				?>
62
			</table>
63
		</fieldset>
64
65
		<?php
66
		$this->render_submit_button();
67
	}
68
69
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
70
	protected function append_to_js_array( $js_array ) {
71
		$js_array['validators'][ $this->action ] = 'validateTextbox';
72
73
		return $js_array;
74
	}
75
76
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
77
	protected function convert_user_input_to_options( $request, $options ) {
78
		$options['author_info'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_details', '' ) );
79
80
		return $options;
81
	}
82
83
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
84
	protected function build_query( $options ) {
85
		$query = array();
86
87
		if ( is_email( $options['author_info'] ) ) {
88
			$query['author_email'] = $options['author_info'];
89
		} elseif ( esc_url_raw( $options['author_info'] ) === $options['author_info'] ) {
90
			$query['author_url'] = esc_url_raw( $options['author_info'] );
91
		} else {
92
			$query['bd_db_column_name']  = 'comment_author';
93
			$query['bd_db_column_value'] = $options['author_info'];
94
			$query_overrider             = new DeleteCommentsQueryOverrider();
95
			$query_overrider->load();
96
		}
97
98
		$date_query = $this->get_date_query( $options );
99
100
		if ( ! empty( $date_query ) ) {
101
			$query['date_query'] = $date_query;
102
		}
103
104
		return $query;
105
	}
106
}
107