Completed
Push — 146-feature/bulk-delete-commen... ( b77e3f...7b1f01 )
by
unknown
79:15 queued 76:31
created

DeleteCommentsQueryOverrider::parse_query()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
3
namespace BulkWP\BulkDelete\Addon\Modules\QueryOverriders;
4
5
use BulkWP\BulkDelete\Core\Base\BaseQueryOverrider;
6
7
/**
8
 * Overrides query for deleting comments.
9
 */
10
class DeleteCommentsQueryOverrider extends BaseQueryOverrider {
11
	protected $field;
12
	protected $value;
13
14
	/**
15
	 * Parse the query and retrieve the values.
16
	 *
17
	 * @param array $query WP_Comment_Query arguments.
18
	 */
19
	public function parse_query( $query ) {
20
		if ( isset( $query->query_vars['field_name'], $query->query_vars['value'] ) ) {
21
			$this->field = $query->query_vars['field_name'];
22
			$this->value = $query->query_vars['value'];
23
24
			// TODO: Use WP_Comment_Query related filters.
25
			add_filter( 'posts_where', array( $this, 'filter_where' ) );
26
			add_filter( 'posts_selection', array( $this, 'remove_where_filter' ) );
27
		}
28
	}
29
30
	/**
31
	 * Modify the where clause.
32
	 *
33
	 * @param string $where (optional) Where clause.
34
	 *
35
	 * @return string Modified Where clause
36
	 */
37
	public function filter_where( $where = '' ) {
38
		global $wpdb;
39
40
		$where .= sprintf( " AND %s.post_title %s = '%s' ", $wpdb->comments, esc_sql( $this->field ), esc_sql( $this->value ) );
0 ignored issues
show
Bug introduced by
It seems like esc_sql($this->field) 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

40
		$where .= sprintf( " AND %s.post_title %s = '%s' ", $wpdb->comments, /** @scrutinizer ignore-type */ esc_sql( $this->field ), esc_sql( $this->value ) );
Loading history...
41
42
		return $where;
43
	}
44
45
	/**
46
	 * Remove the `posts_where` filter.
47
	 */
48
	public function remove_where_filter() {
49
		remove_filter( 'posts_where', array( $this, 'filter_where' ) );
50
	}
51
}
52