Relation.php
1.92 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
<?php
namespace ACA\JetEngine\Column;
use AC;
use ACA\JetEngine\Service\ColumnGroups;
use ACP;
use Jet_Engine\Relations\Relation as JetEngineRelation;
abstract class Relation extends AC\Column implements ACP\Export\Exportable, ACP\Editing\Editable, ACP\Search\Searchable {
/**
* @var JetEngineRelation
*/
protected $relation;
public function __construct() {
$this->set_group( ColumnGroups::JET_ENGINE_RELATION )
->set_label( __( 'JetEngine Relation', 'codepress-admin-columns' ) );
}
public function set_config( JetEngineRelation $relation ) {
$this->relation = $relation;
}
public function get_raw_value( $id ) {
if ( $this->is_relation_parent() ) {
$items = wp_list_pluck( $this->relation->get_children( $id ), 'child_object_id' );
} else {
$items = wp_list_pluck( $this->relation->get_parents( $id ), 'parent_object_id' );
}
return new AC\Collection( $items );
}
public function has_many() {
switch ( $this->relation->get_args( 'type' ) ) {
case 'one_to_many':
return $this->is_relation_parent();
case 'many_to_many':
return true;
default:
return false;
}
}
/**
* @return string
*/
protected function get_related_object() {
return $this->is_relation_parent()
? explode( '::', $this->relation->get_args( 'child_object' ) )[1]
: explode( '::', $this->relation->get_args( 'parent_object' ) )[1];
}
public function is_relation_parent() {
$list_screen = $this->list_screen;
switch ( true ) {
case $list_screen instanceof AC\ListScreen\User:
return $this->relation->is_parent( 'mix', 'users' );
case $list_screen instanceof AC\ListScreen\Post:
return $this->relation->is_parent( 'posts', $this->get_post_type() );
case $list_screen instanceof ACP\ListScreen\Taxonomy:
return $this->relation->is_parent( 'terms', $this->get_taxonomy() );
}
return false;
}
public function export() {
return new ACP\Export\Model\StrippedValue( $this );
}
}