|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Library\System\PostType; |
|
4
|
|
|
|
|
5
|
|
|
use Leonidas\Contracts\System\PostType\PostTypeInterface; |
|
6
|
|
|
use Leonidas\Contracts\System\PostType\PostTypeOptionHandlerCollectionInterface; |
|
7
|
|
|
use Leonidas\Contracts\System\PostType\PostTypeRegistrarInterface; |
|
8
|
|
|
use Leonidas\Library\System\AbstractSystemModelTypeRegistrar; |
|
9
|
|
|
|
|
10
|
|
|
class PostTypeRegistrar extends AbstractSystemModelTypeRegistrar implements PostTypeRegistrarInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected ?PostTypeOptionHandlerCollectionInterface $optionHandlers = null; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(?PostTypeOptionHandlerCollectionInterface $optionHandlers = null) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->optionHandlers = $optionHandlers; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function registerOne(PostTypeInterface $postType) |
|
20
|
|
|
{ |
|
21
|
|
|
$registered = register_post_type( |
|
|
|
|
|
|
22
|
|
|
$postType->getName(), |
|
23
|
|
|
$this->getArgs($postType) |
|
24
|
|
|
); |
|
25
|
|
|
|
|
26
|
|
|
$registered->options = $postType->getOptions(); |
|
27
|
|
|
|
|
28
|
|
|
if (isset($this->optionHandlers)) { |
|
29
|
|
|
$this->registerOptions($postType); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function registerMany(PostTypeInterface ...$postTypes) |
|
34
|
|
|
{ |
|
35
|
|
|
foreach ($postTypes as $postType) { |
|
36
|
|
|
$this->registerOne($postType); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function getArgs(PostTypeInterface $postType) |
|
41
|
|
|
{ |
|
42
|
|
|
$args = [ |
|
43
|
|
|
'exclude_from_search' => $postType->isExcludedFromSearch(), |
|
44
|
|
|
'show_in_admin_bar' => $postType->isShownInAdminBar(), |
|
45
|
|
|
'menu_position' => $postType->getMenuPosition(), |
|
46
|
|
|
'menu_icon' => $postType->getMenuIcon(), |
|
47
|
|
|
'capability_type' => $postType->getCapabilityType(), |
|
48
|
|
|
'map_meta_cap' => $postType->usesMapMetaCap(), |
|
49
|
|
|
'supports' => $postType->getSupports(), |
|
50
|
|
|
'register_meta_box_cb' => $postType->getRegisterMetaBoxCb(), |
|
51
|
|
|
'taxonomies' => $postType->getTaxonomies(), |
|
52
|
|
|
'has_archive' => $postType->getArchive(), |
|
53
|
|
|
'can_export' => $postType->canBeExported(), |
|
54
|
|
|
'delete_with_user' => $postType->isDeletedWithUser(), |
|
55
|
|
|
'template' => $postType->getTemplate(), |
|
56
|
|
|
'template_lock' => $postType->getTemplateLock(), |
|
57
|
|
|
] + $this->getBaseArgs($postType); |
|
58
|
|
|
|
|
59
|
|
|
return array_filter($args, fn ($arg) => $arg !== null); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function registerOptions(PostTypeInterface $postType) |
|
63
|
|
|
{ |
|
64
|
|
|
foreach ($postType->getOptions() as $option => $value) { |
|
65
|
|
|
if ($this->optionHandlers->has($option)) { |
|
|
|
|
|
|
66
|
|
|
$this->optionHandlers->get($option)->handle($postType, $value); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
throw $this->unregisteredOptionException($option); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|