Relation.php
1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace ACA\JetEngine\Search\Comparison;
use ACP;
use ACP\Search\Comparison\SearchableValues;
use ACP\Search\Operators;
use ACP\Search\Value;
use Jet_Engine\Relations\Relation as JetEngineRelation;
use Jet_Engine\Relations\Storage;
abstract class Relation extends ACP\Search\Comparison implements SearchableValues {
/**
* @var JetEngineRelation
*/
private $relation;
/**
* @var bool
*/
private $is_parent;
public function __construct( JetEngineRelation $relation, $is_parent ) {
parent::__construct( new Operators( [
Operators::EQ,
] ) );
$this->relation = $relation;
$this->is_parent = (bool) $is_parent;
}
private function get_db_id_column() {
global $wpdb;
$argument = $this->is_parent ? 'parent_object' : 'child_object';
$field = explode( '::', $this->relation->get_args( $argument ) )[0];
switch ( $field ) {
case 'mix':
return sprintf( '%s.%s', $wpdb->users, 'ID' );
case 'terms':
return 't.term_id';
case 'posts':
return sprintf( '%s.%s', $wpdb->posts, 'ID' );
}
return '';
}
protected function create_query_bindings( $operator, Value $value ) {
$bindings = new ACP\Search\Query\Bindings();
$ids = array_unique( $this->get_related_ids( $value ) );
$in_type = $operator === Operators::EQ ? 'IN' : 'NOT IN';
$ids = implode( "','", array_map( 'esc_sql', $ids ) );
$column = $this->get_db_id_column();
$bindings->where( "{$column} {$in_type}( '{$ids}' )" );
return $bindings;
}
protected function get_related_ids( Value $value ) {
$query_arg = $this->is_parent ? 'child_object_id' : 'parent_object_id';
$field = $this->is_parent ? 'parent_object_id' : 'child_object_id';
/** @var Storage\DB $db */
$db = $this->relation->db;
$results = $db->query( [
'rel_id' => $this->relation->get_id(),
$query_arg => $value->get_value(),
] );
return wp_list_pluck( $results, $field );
}
}