Relation.php
2.81 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace ACA\MetaBox\Entity;
use MB_Relationships_API;
use MBR_Relationship;
use WP_Post;
use WP_Term;
use WP_User;
class Relation {
/**
* @var MBR_Relationship
*/
private $relation;
/**
* @var string
*/
private $type;
/**
* @param MBR_Relationship $relation
* @param 'from'|'to' $relation
*/
public function __construct( MBR_Relationship $relation, $type ) {
$this->relation = $relation;
$this->type = $type;
}
public function get_id() {
return $this->relation->id;
}
public function get_type() {
return $this->type;
}
public function get_related_type() {
return $this->type === 'from' ? 'to' : 'from';
}
public function get_meta_type() {
return $this->relation->get_object_type( $this->type );
}
public function get_related_meta_type() {
return $this->relation->get_object_type( $this->get_related_type() );
}
public function get_title() {
if ( $this->relation->menu_title ) {
return $this->relation->menu_title;
}
switch ( $this->get_related_meta_type() ) {
case 'user':
return sprintf( 'User (%s)', $this->relation->id );
case 'post':
$field = $this->get_related_field_settings();
if ( empty( $field['post_type'] ) ) {
break;
}
$post_type_object = get_post_type_object( $field['post_type'] );
return $post_type_object
? $post_type_object->label
: $field['post_type'];
case 'term':
$field = $this->get_related_field_settings();
if ( empty( $field['taxonomy'] ) ) {
break;
}
$taxonomy = get_taxonomy( $field['taxonomy'] );
return $taxonomy ? $taxonomy->label : $field['taxonomy'];
}
return empty( $this->relation->menu_title )
? $this->relation->id
: $this->relation->menu_title;
}
/**
* @return array
*/
public function get_related_field_settings() {
return 'to' === $this->get_related_type()
? $this->relation->to['field']
: $this->relation->from['field'];
}
/**
* @param $object_id
*
* @return int[]
*/
public function get_related_ids( $object_id ) {
$ids = [];
$items = MB_Relationships_API::get_connected( [
'id' => $this->get_id(),
$this->type => $object_id,
] );
foreach ( $items as $item ) {
switch ( true ) {
case $item instanceof WP_Term:
$ids[] = $item->term_id;
break;
case $item instanceof WP_Post:
case $item instanceof WP_User:
$ids[] = $item->ID;
break;
}
}
return $ids;
}
public function add_relation( $from_id, $to_id ) {
return MB_Relationships_API::add(
$this->type === 'from' ? $from_id : $to_id,
$this->type === 'from' ? $to_id : $from_id,
$this->get_id()
);
}
public function delete_relation( $from_id, $to_id ) {
return MB_Relationships_API::delete(
$this->type === 'from' ? $from_id : $to_id,
$this->type === 'from' ? $to_id : $from_id,
$this->get_id()
);
}
}