Authors.php
10 months ago
Categories.php
10 months ago
DeviceBrowsers.php
10 months ago
DeviceOperatingSystems.php
10 months ago
DeviceTypes.php
10 months ago
LinkPatterns.php
10 months ago
PageTypes.php
10 months ago
ReferrerTypes.php
10 months ago
Categories.php
29 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\ColumnOptions\Plugins; |
| 4 | |
| 5 | use IAWP\ColumnOptions\Option; |
| 6 | use IAWP\ColumnOptions\OptionsPlugin; |
| 7 | use IAWPSCOPED\Illuminate\Support\Collection; |
| 8 | /** @internal */ |
| 9 | class Categories implements OptionsPlugin |
| 10 | { |
| 11 | public function get_options() : array |
| 12 | { |
| 13 | $top_level_categories = \get_categories(['parent' => 0, 'hide_empty' => \false]); |
| 14 | $options = Collection::make(); |
| 15 | foreach ($top_level_categories as $top_level_category) { |
| 16 | $options->push(new Option($top_level_category->term_id, $top_level_category->name)); |
| 17 | $options->push(...self::get_subcategories_for($top_level_category->term_id)); |
| 18 | } |
| 19 | return $options->all(); |
| 20 | } |
| 21 | private static function get_subcategories_for(int $parent_category_id) : array |
| 22 | { |
| 23 | $subcategories = \get_categories(['child_of' => $parent_category_id, 'hide_empty' => \false]); |
| 24 | return Collection::make($subcategories)->sortBy('name')->map(function ($category) use($parent_category_id) { |
| 25 | return new Option($category->term_id, $category->name, $parent_category_id); |
| 26 | })->all(); |
| 27 | } |
| 28 | } |
| 29 |