|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Roots\AcornPostTypes; |
| 4 | + |
| 5 | +use Illuminate\Support\Arr; |
| 6 | +use Illuminate\Support\Collection; |
| 7 | +use Roots\Acorn\Application; |
| 8 | + |
| 9 | +class AcornPostTypes |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The Application instance. |
| 13 | + */ |
| 14 | + protected Application $app; |
| 15 | + |
| 16 | + /** |
| 17 | + * The post types configuration. |
| 18 | + */ |
| 19 | + protected Collection $config; |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a new Acorn Post Types instance. |
| 23 | + * |
| 24 | + * @return void |
| 25 | + */ |
| 26 | + public function __construct(Application $app) |
| 27 | + { |
| 28 | + $this->app = $app; |
| 29 | + $this->config = Collection::make($this->app->config->get('post-types')); |
| 30 | + |
| 31 | + $this->registerPostTypes(); |
| 32 | + $this->registerTaxonomies(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Make a new instance of Acorn Post Types. |
| 37 | + */ |
| 38 | + public static function make(Application $app): self |
| 39 | + { |
| 40 | + return new static($app); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Register post types. |
| 45 | + */ |
| 46 | + protected function registerPostTypes(): void |
| 47 | + { |
| 48 | + Collection::make($this->config->get('post_types', [])) |
| 49 | + ->each(function ($args, $post_type) { |
| 50 | + register_extended_post_type( |
| 51 | + $post_type, |
| 52 | + $args, |
| 53 | + Arr::pull($args, 'names') |
| 54 | + ); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Register taxonomies. |
| 60 | + */ |
| 61 | + protected function registerTaxonomies(): void |
| 62 | + { |
| 63 | + Collection::make($this->config->get('taxonomies', [])) |
| 64 | + ->each(function ($args, $taxonomy) { |
| 65 | + register_extended_taxonomy( |
| 66 | + $taxonomy, |
| 67 | + Arr::pull($args, 'post_types'), |
| 68 | + $args, |
| 69 | + Arr::pull($args, 'names') |
| 70 | + ); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get registered post types from config. |
| 76 | + */ |
| 77 | + public function getPostTypes(): array |
| 78 | + { |
| 79 | + return $this->config->get('post_types', []); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get registered taxonomies from config. |
| 84 | + */ |
| 85 | + public function getTaxonomies(): array |
| 86 | + { |
| 87 | + return $this->config->get('taxonomies', []); |
| 88 | + } |
| 89 | +} |
0 commit comments