RelationshipLegacy.php
1.83 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
<?php
namespace ACA\JetEngine\Editing\Service;
use AC;
use ACA\JetEngine\Utils\Api;
use ACP;
use ACP\Editing\View;
class RelationshipLegacy implements ACP\Editing\Service, ACP\Editing\PaginatedOptions {
/**
* @var string
*/
private $related_key;
/**
* @var string
*/
private $current_post_type;
/**
* @var string
*/
private $related_post_type;
/**
* @var boolean
*/
private $multiple;
public function __construct( $related_key, $current_post_type, $related_post_type, $multiple ) {
$this->related_key = (string) $related_key;
$this->current_post_type = (string) $current_post_type;
$this->related_post_type = (string) $related_post_type;
$this->multiple = (bool) $multiple;
}
public function get_view( string $context ): ?View {
return self::CONTEXT_BULK === $context
? null
: ( new ACP\Editing\View\AjaxSelect() )->set_multiple( $this->multiple );
}
public function get_value( $id ) {
$post_ids = Api::Relations()->get_related_posts( [
'hash' => $this->related_key,
'current' => $this->current_post_type,
'post_id' => $id,
] );
if ( empty( $post_ids ) ) {
return [];
}
$result = [];
foreach ( (array) $post_ids as $post_id ) {
$result[ $post_id ] = get_the_title( $post_id );
}
return $result;
}
public function update( int $id, $data ): void {
$ids = is_array( $data ) ? $data : [ $data ];
Api::Relations()->process_meta( true, $id, $this->related_key, $ids );
}
public function get_paginated_options( $search, $page, $id = null ) {
$entities = new ACP\Helper\Select\Entities\Post( [
's' => $search,
'paged' => $page,
'post_type' => $this->related_post_type,
] );
return new AC\Helper\Select\Options\Paginated(
$entities,
new ACP\Helper\Select\Group\PostType(
new ACP\Helper\Select\Formatter\PostTitle( $entities )
)
);
}
}