PickTaxonomy.php
1.28 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 ACP;
use ACP\Editing\PaginatedOptions;
use ACP\Editing\Storage;
use ACP\Editing\View;
class PickTaxonomy implements ACP\Editing\Service, PaginatedOptions {
/**
* @var boolean
*/
private $multiple;
/**
* @var string
*/
private $taxonomy;
/**
* @var string
*/
private $storage;
public function __construct( Storage $storage, $multiple, $taxonomy ) {
$this->storage = $storage;
$this->multiple = (bool) $multiple;
$this->taxonomy = $taxonomy;
}
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 ) {
$term_ids = $this->storage->get( $id );
if ( empty( $term_ids ) ) {
return [];
}
$value = [];
foreach ( $term_ids as $term_id ) {
$term = get_term_by( 'id', $term_id, $this->taxonomy );
$value[ $term_id ] = $term ? htmlspecialchars_decode( $term->name ) : $term_id;
}
return $value;
}
public function update( int $id, $data ): void {
$this->storage->update( $id, $data );
}
public function get_paginated_options( $search, $page, $id = null ) {
return new ACP\Helper\Select\Paginated\Terms( $search, $page, (array) $this->taxonomy );
}
}