ChildRelation.php
1.58 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
<?php
namespace ACA\JetEngine\Search\Comparison;
use ACP;
use ACP\Helper\Select;
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;
class ChildRelation extends ACP\Search\Comparison implements SearchableValues {
/**
* @var JetEngineRelation
*/
private $relation;
/**
* @var string
*/
private $related_post_type;
public function __construct( JetEngineRelation $relation, $post_type ) {
parent::__construct( new Operators( [
Operators::EQ,
] ) );
$this->relation = $relation;
$this->related_post_type = $post_type;
}
protected function create_query_bindings( $operator, Value $value ) {
global $wpdb;
$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 ) );
$bindings->where( "{$wpdb->posts}.ID {$in_type}( '{$ids}' )" );
return $bindings;
}
protected function get_related_ids( Value $value ) {
/** @var Storage\DB $db */
$db = $this->relation->db;
$results = $db->query( [
'rel_id' => $this->relation->get_id(),
'parent_object_id' => $value->get_value(),
] );
return wp_list_pluck( $results, 'child_object_id' );
}
public function get_values( $search, $page ) {
$args = [
'post_type' => $this->related_post_type,
'search_fields' => [ 'post_title', 'ID' ],
];
return new Select\Paginated\Posts( $search, $page, $args );
}
}