|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Library\System\Taxonomy; |
|
4
|
|
|
|
|
5
|
|
|
use Leonidas\Contracts\System\Taxonomy\TaxonomyInterface; |
|
6
|
|
|
use Leonidas\Contracts\System\Taxonomy\TaxonomyOptionHandlerCollectionInterface; |
|
7
|
|
|
use Leonidas\Contracts\System\Taxonomy\TaxonomyRegistrarInterface; |
|
8
|
|
|
use Leonidas\Library\System\AbstractSystemModelTypeRegistrar; |
|
9
|
|
|
|
|
10
|
|
|
class TaxonomyRegistrar extends AbstractSystemModelTypeRegistrar implements TaxonomyRegistrarInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected ?TaxonomyOptionHandlerCollectionInterface $optionHandlers = null; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(?TaxonomyOptionHandlerCollectionInterface $optionHandlers = null) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->optionHandlers = $optionHandlers; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function registerOne(TaxonomyInterface $taxonomy) |
|
20
|
|
|
{ |
|
21
|
|
|
$registered = register_taxonomy( |
|
|
|
|
|
|
22
|
|
|
$name = $taxonomy->getName(), |
|
23
|
|
|
$types = $taxonomy->getObjectTypes(), |
|
24
|
|
|
$this->getArgs($taxonomy) |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
foreach ($types as $type) { |
|
28
|
|
|
register_taxonomy_for_object_type($name, $type); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$registered->options = $taxonomy->getOptions(); |
|
32
|
|
|
|
|
33
|
|
|
if (isset($this->optionHandlers)) { |
|
34
|
|
|
$this->registerOptions($taxonomy); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function registerMany(TaxonomyInterface ...$taxonomies) |
|
39
|
|
|
{ |
|
40
|
|
|
foreach ($taxonomies as $taxonomy) { |
|
41
|
|
|
$this->registerOne($taxonomy); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function getArgs(TaxonomyInterface $taxonomy) |
|
46
|
|
|
{ |
|
47
|
|
|
$args = [ |
|
48
|
|
|
"show_tagcloud" => $taxonomy->isShownInTagCloud(), |
|
49
|
|
|
"show_in_quick_edit" => $taxonomy->isShownInQuickEdit(), |
|
50
|
|
|
"show_admin_column" => $taxonomy->showsAdminColumn(), |
|
51
|
|
|
"meta_box_cb" => $taxonomy->getMetaBoxCb(), |
|
52
|
|
|
"meta_box_sanitize_cb" => $taxonomy->getMetaBoxSanitizeCb(), |
|
53
|
|
|
"update_count_callback" => $taxonomy->getUpdateCountCallback(), |
|
54
|
|
|
"default_term" => $taxonomy->getDefaultTerm(), |
|
55
|
|
|
"sort" => $taxonomy->shouldBeSorted(), |
|
56
|
|
|
"args" => $taxonomy->getArgs(), |
|
57
|
|
|
] + $this->getBaseArgs($taxonomy); |
|
58
|
|
|
|
|
59
|
|
|
return array_filter($args, fn ($arg) => $arg !== null); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function registerOptions(TaxonomyInterface $taxonomy) |
|
63
|
|
|
{ |
|
64
|
|
|
foreach ($taxonomy->getOptions() as $option => $value) { |
|
65
|
|
|
if ($this->optionHandlers->has($option)) { |
|
|
|
|
|
|
66
|
|
|
$this->optionHandlers->get($option)->handle($taxonomy, $value); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
throw $this->unregisteredOptionException($option); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|