PickComments.php
1.32 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
<?php
namespace ACA\Pods\Editing\Service;
use AC;
use ACP;
use ACP\Editing\PaginatedOptions;
use ACP\Editing\Storage;
use ACP\Editing\View;
use ACP\Helper\Select\Entities;
class PickComments implements ACP\Editing\Service, PaginatedOptions {
/**
* @var Storage
*/
private $storage;
/**
* @var boolean
*/
private $multiple;
public function __construct( Storage $storage, $multiple ) {
$this->storage = $storage;
$this->multiple = (bool) $multiple;
}
public function get_view( string $context ): ?View {
return ( new ACP\Editing\View\AjaxSelect() )
->set_multiple( $this->multiple )
->set_clear_button( true );
}
public function get_value( $id ) {
$comment_ids = $this->storage->get( $id );
if ( empty( $comment_ids ) ) {
return false;
}
$value = [];
foreach ( $comment_ids as $comment_id ) {
$comment = get_comment( $comment_id );
$value[ $comment_id ] = $comment->comment_date ?? $comment_id;
}
return $value;
}
public function update( int $id, $data ): void {
$this->storage->update( $id, $data );
}
public function get_paginated_options( $search, $paged, $id = null ) {
$entities = new Entities\Comment( compact( 'search', 'paged' ) );
return new AC\Helper\Select\Options\Paginated(
$entities,
new ACP\Helper\Select\Formatter\CommentSummary( $entities )
);
}
}