Completed
Push — 247-fix/delete-term-meta ( 2a1fe3...132475 )
by Sudar
10:50 queued 05:24
created

DeleteTermsQueryOverrider::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Terms\QueryOverriders;
4
5
use BulkWP\BulkDelete\Core\Base\BaseQueryOverrider;
6
7
/**
8
 * Overrides query that is used for retrieving terms.
9
 *
10
 * @since 6.1.0
11
 */
12
class DeleteTermsQueryOverrider extends BaseQueryOverrider {
13
	/**
14
	 * Comparison operator.
15
	 *
16
	 * @var string
17
	 */
18
	protected $operator;
19
20
	/**
21
	 * The value that should be compared.
22
	 *
23
	 * @var string
24
	 */
25
	protected $db_column_value;
26
27
	/**
28
	 * The db column name that should be compared.
29
	 *
30
	 * @var string
31
	 */
32
	protected $db_column_name;
33
34 28
	public function load() {
35 28
		add_action( 'parse_term_query', array( $this, 'parse_query' ) );
36
	}
37
38
	/**
39
	 * Parse the query and retrieve the values.
40
	 *
41
	 * @param array $query WP_Comment_Query arguments.
42
	 */
43 28
	public function parse_query( $query ) {
44 28
		if ( isset( $query->query_vars['bd_operator'], $query->query_vars['bd_value'], $query->query_vars['bd_column_name'] ) ) {
45 28
			$this->operator        = $query->query_vars['bd_operator'];
46 28
			$this->db_column_value = $query->query_vars['bd_value'];
47 28
			$this->db_column_name  = $query->query_vars['bd_column_name'];
48
49 28
			add_filter( 'terms_clauses', array( $this, 'filter_where' ) );
50
		}
51
	}
52
53
	/**
54
	 * Modify the where clause.
55
	 *
56
	 * @param array $clauses Term clauses.
57
	 *
58
	 * @return array Modified term clauses.
59
	 */
60 28
	public function filter_where( $clauses ) {
61 28
		if ( 'name' === $this->db_column_name ) {
62 16
			$this->db_column_value = "'" . $this->db_column_value . "'";
63
		}
64 28
		$clauses['where'] .= sprintf( ' AND %s %s %s ', esc_sql( $this->db_column_name ), esc_sql( $this->operator ), $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

64
		$clauses['where'] .= sprintf( ' AND %s %s %s ', /** @scrutinizer ignore-type */ esc_sql( $this->db_column_name ), esc_sql( $this->operator ), $this->db_column_value );
Loading history...
65
66 28
		$this->remove_where_filter();
67
68 28
		return $clauses;
69
	}
70
71
	/**
72
	 * Remove the where filter.
73
	 */
74 28
	public function remove_where_filter() {
75 28
		remove_filter( 'terms_clauses', array( $this, 'filter_where' ) );
76
	}
77
}
78