-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTaxonomyField.php
More file actions
51 lines (40 loc) · 1.23 KB
/
TaxonomyField.php
File metadata and controls
51 lines (40 loc) · 1.23 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
<?php
namespace OWC\OpenPub\Base\RestAPI\ItemFields;
use OWC\OpenPub\Base\Support\CreatesFields;
use WP_Post;
class TaxonomyField extends CreatesFields
{
/**
* Create an additional field on an array.
*/
public function create(WP_Post $post): array
{
$result = [];
$taxonomies = apply_filters('owc/openpub-base/before-register-extended-taxonomies', $this->plugin->config->get('taxonomies'));
if (! is_array($taxonomies) || 1 > count($taxonomies)) {
return $result;
}
$taxonomiesKeys = array_unique(array_keys($taxonomies));
foreach ($taxonomiesKeys as $taxonomy) {
$result[$taxonomy] = $this->getTerms($post->ID, $taxonomy);
}
return $result;
}
/**
* Get terms of a taxonomy to which the post is connected.
*/
private function getTerms(int $postID, string $taxonomy): array
{
$terms = \wp_get_post_terms($postID, $taxonomy);
if (\is_wp_error($terms)) {
return [];
}
return array_map(function ($term) {
return [
'id' => $term->term_id,
'name' => $term->name,
'slug' => $term->slug
];
}, $terms);
}
}