PrimaryTaxonomy.php
1.2 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
<?php
namespace ACA\YoastSeo\Editing\Service\Post;
use ACP\Editing;
use ACP\Editing\Storage;
use ACP\Editing\View;
use ACP\Helper\Select;
class PrimaryTaxonomy implements Editing\Service, Editing\PaginatedOptions {
/**
* @var string
*/
private $taxonomy;
/**
* @var Storage
*/
private $storage;
public function __construct( $taxonomy ) {
$this->storage = new Storage\Post\Meta( '_yoast_wpseo_primary_' . $taxonomy );
$this->taxonomy = $taxonomy;
}
public function get_value( $id ) {
$term = $this->storage->get( $id );
if ( ! $term ) {
$terms = wp_get_post_terms( $id, $this->taxonomy );
return empty( $terms ) || is_wp_error( $terms )
? null
: false;
}
$term = get_term( $term, $this->taxonomy );
return [
$term->term_id => $term->name,
];
}
public function update( int $id, $data ): void {
$this->storage->update( $id, $data );
}
public function get_view( string $context ): ?View {
return self::CONTEXT_SINGLE === $context ? new Editing\View\AjaxSelect() : null;
}
public function get_paginated_options( $search, $page, $id = null ) {
return new Select\Paginated\Terms( $search, $page, [ $this->taxonomy ], [
'object_ids' => [ $id ],
] );
}
}