TaxonomyParent.php
1.38 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
<?php
namespace ACP\Editing\Service\Taxonomy;
use AC;
use AC\Request;
use ACP\Editing\PaginatedOptions;
use ACP\Editing\Service;
use ACP\Editing\Storage;
use ACP\Editing\View\AjaxSelect;
use ACP\Helper\Select;
class TaxonomyParent implements Service, PaginatedOptions {
/**
* @var string
*/
private $taxonomy;
/**
* @var Storage
*/
private $storage;
public function __construct( $taxonomy ) {
$this->taxonomy = $taxonomy;
$this->storage = new Storage\Taxonomy\Field( $taxonomy, 'parent' );
}
public function get_value( $id ) {
$parent_id = $this->storage->get( $id );
if ( ! $parent_id ) {
return false;
}
$parent = get_term_by( 'id', $parent_id, $this->taxonomy );
if ( ! $parent ) {
return false;
}
return [
$parent->term_id => $parent->name,
];
}
public function get_paginated_options( $search, $page, $id = null ) {
$entities = new Select\Entities\Taxonomy( [
'search' => $search,
'page' => $page,
'exclude_tree' => $id,
'taxonomy' => $this->taxonomy,
] );
return new AC\Helper\Select\Options\Paginated(
$entities,
new Select\Formatter\TermName( $entities )
);
}
public function get_view( $context ) {
return ( new AjaxSelect() )->set_clear_button( true );
}
public function update( Request $request ) {
return $this->storage->update( $request->get( 'id' ), $request->get( 'value', '' ) );
}
}