TaxonomiesAdvanced.php
1.26 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
<?php
namespace ACA\MetaBox\Editing\Service;
use ACA;
use ACP;
use ACP\Editing\PaginatedOptions;
use ACP\Editing\View;
use ACP\Helper\Select\Paginated\Terms;
use WP_Term;
class TaxonomiesAdvanced implements ACP\Editing\Service, PaginatedOptions {
/**
* @var ACP\Editing\Storage
*/
private $storage;
/**
* @var string|array
*/
protected $taxonomy;
public function __construct( ACP\Editing\Storage $storage, $taxonomy ) {
$this->storage = $storage;
$this->taxonomy = $taxonomy;
}
public function get_view( string $context ): ?View {
return ( new ACP\Editing\View\AjaxSelect() )->set_clear_button( true )->set_multiple( true );
}
public function get_value( $id ) {
$value = $this->storage->get( $id );
if ( empty( $value ) ) {
return false;
}
$value = is_array( $value ) ? $value[0] : $value;
$result = [];
foreach ( explode( ',', $value ) as $term_id ) {
$term = get_term( $term_id );
if ( $term instanceof WP_Term ) {
$result[ $term_id ] = $term->name;
}
}
return $result;
}
public function update( int $id, $data ): void {
$this->storage->update( $id, implode( ',', $data ?? [] ) );
}
public function get_paginated_options( $search, $page, $id = null ) {
return new Terms( $search, $page, $this->taxonomy );
}
}