Menu.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Admin\Section\Partial; |
| 4 | |
| 5 | use AC\ListScreen; |
| 6 | use AC\ListScreenGroups; |
| 7 | use AC\ListScreenTypes; |
| 8 | use AC\View; |
| 9 | |
| 10 | class Menu { |
| 11 | |
| 12 | /** @var bool */ |
| 13 | private $is_network; |
| 14 | |
| 15 | public function __construct( $is_network = false ) { |
| 16 | $this->is_network = (bool) $is_network; |
| 17 | } |
| 18 | |
| 19 | public function render( ListScreen $list_screen, $is_hidden = false ) { |
| 20 | $menu = new View( [ |
| 21 | 'items' => $this->get_grouped_list_screens(), |
| 22 | 'current' => $list_screen->get_key(), |
| 23 | 'screen_link' => $list_screen->get_screen_link(), |
| 24 | 'class' => $is_hidden ? 'hidden' : '', |
| 25 | ] ); |
| 26 | |
| 27 | $menu->set_template( 'admin/edit-menu' ); |
| 28 | |
| 29 | return $menu->render(); |
| 30 | } |
| 31 | |
| 32 | private function get_network_list_screens() { |
| 33 | return ListScreenTypes::instance()->get_list_screens( [ 'network_only' => true ] ); |
| 34 | } |
| 35 | |
| 36 | private function get_site_list_screens() { |
| 37 | return ListScreenTypes::instance()->get_list_screens( [ 'site_only' => true ] ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @return array |
| 42 | */ |
| 43 | private function get_grouped_list_screens() { |
| 44 | |
| 45 | $list_screens = $this->is_network |
| 46 | ? $this->get_network_list_screens() |
| 47 | : $this->get_site_list_screens(); |
| 48 | |
| 49 | $list_screens_grouped = []; |
| 50 | foreach ( $list_screens as $list_screen ) { |
| 51 | $list_screens_grouped[ $list_screen->get_group() ][ $list_screen->get_key() ] = $list_screen->get_label(); |
| 52 | } |
| 53 | |
| 54 | $grouped = []; |
| 55 | |
| 56 | foreach ( ListScreenGroups::get_groups()->get_groups_sorted() as $group ) { |
| 57 | $slug = $group['slug']; |
| 58 | |
| 59 | if ( empty( $list_screens_grouped[ $slug ] ) ) { |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | if ( ! isset( $grouped[ $slug ] ) ) { |
| 64 | $grouped[ $slug ]['title'] = $group['label']; |
| 65 | } |
| 66 | |
| 67 | natcasesort( $list_screens_grouped[ $slug ] ); |
| 68 | |
| 69 | $grouped[ $slug ]['options'] = $list_screens_grouped[ $slug ]; |
| 70 | |
| 71 | unset( $list_screens_grouped[ $slug ] ); |
| 72 | } |
| 73 | |
| 74 | return $grouped; |
| 75 | } |
| 76 | |
| 77 | } |