Passed
Push — 146-feature/bulk-delete-commen... ( 1189f4...ce5a13 )
by
unknown
08:47 queued 06:21
created

DeleteCommentsQueryOverrider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 53
ccs 0
cts 17
cp 0
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A filter_where() 0 6 1
A parse_query() 0 6 2
A load() 0 2 1
A remove_where_filter() 0 2 1
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Comments\QueryOverriders;
4
5
use BulkWP\BulkDelete\Core\Base\BaseQueryOverrider;
6
7
/**
8
 * Overrides query that is used for retrieving comments.
9
 *
10
 * @since 6.1.0
11
 */
12
class DeleteCommentsQueryOverrider extends BaseQueryOverrider {
13
	/**
14
	 * DB column name. Mostly either `comment_author_IP` or `comment_author`.
15
	 *
16
	 * @var string
17
	 */
18
	protected $db_column_name;
19
20
	/**
21
	 * The value that should be compared.
22
	 *
23
	 * @var string
24
	 */
25
	protected $db_column_value;
26
27
	public function load() {
28
		add_action( 'parse_comment_query', array( $this, 'parse_query' ) );
29
	}
30
31
	/**
32
	 * Parse the query and retrieve the values.
33
	 *
34
	 * @param array $query WP_Comment_Query arguments.
35
	 */
36
	public function parse_query( $query ) {
37
		if ( isset( $query->query_vars['bd_db_column_name'], $query->query_vars['bd_db_column_value'] ) ) {
38
			$this->db_column_name  = $query->query_vars['bd_db_column_name'];
39
			$this->db_column_value = $query->query_vars['bd_db_column_value'];
40
41
			add_filter( 'comments_clauses', array( $this, 'filter_where' ) );
42
		}
43
	}
44
45
	/**
46
	 * Modify the where clause.
47
	 *
48
	 * @param array $clauses Comment clauses.
49
	 *
50
	 * @return array Modified comment clauses.
51
	 */
52
	public function filter_where( $clauses ) {
53
		$clauses['where'] .= sprintf( " AND %s = '%s' ", esc_sql( $this->db_column_name ), esc_sql( $this->db_column_value ) );
0 ignored issues
show
Bug introduced by
It seems like esc_sql($this->db_column_name) can also be of type array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
		$clauses['where'] .= sprintf( " AND %s = '%s' ", /** @scrutinizer ignore-type */ esc_sql( $this->db_column_name ), esc_sql( $this->db_column_value ) );
Loading history...
54
55
		$this->remove_where_filter();
56
57
		return $clauses;
58
	}
59
60
	/**
61
	 * Remove the `posts_where` filter.
62
	 */
63
	public function remove_where_filter() {
64
		remove_filter( 'comments_clauses', array( $this, 'filter_where' ) );
65
	}
66
}
67