AdminMenu.php
11 years ago
AdminSubmenu.php
11 years ago
AdminSubmenuDefault.php
11 years ago
AdminSubmenuExpander.php
11 years ago
EnabledMenus.php
11 years ago
EnabledMenus.php
148 lines
| 1 | <?php namespace NestedPages\Entities\AdminMenu; |
| 2 | |
| 3 | use NestedPages\Entities\PostType\PostTypeRepository; |
| 4 | use NestedPages\Entities\Listing\Listing; |
| 5 | use NestedPages\Entities\AdminMenu\AdminSubmenu; |
| 6 | use NestedPages\Entities\AdminMenu\AdminSubmenuDefault; |
| 7 | use NestedPages\Entities\User\UserRepository; |
| 8 | |
| 9 | /** |
| 10 | * Other User-Enabled Post Types |
| 11 | */ |
| 12 | class EnabledMenus { |
| 13 | |
| 14 | /** |
| 15 | * Post Type |
| 16 | */ |
| 17 | private $post_type; |
| 18 | |
| 19 | /** |
| 20 | * Post Type Repository |
| 21 | * @var object |
| 22 | */ |
| 23 | private $post_type_repo; |
| 24 | |
| 25 | /** |
| 26 | * Enabled Post Types |
| 27 | */ |
| 28 | private $enabled_types; |
| 29 | |
| 30 | /** |
| 31 | * User Repository |
| 32 | */ |
| 33 | private $user; |
| 34 | |
| 35 | |
| 36 | public function __construct() |
| 37 | { |
| 38 | $this->post_type_repo = new PostTypeRepository; |
| 39 | $this->user = new UserRepository; |
| 40 | $this->setEnabled(); |
| 41 | $this->loopEnabledTypes(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Set Enabled Post Types |
| 46 | */ |
| 47 | private function setEnabled() |
| 48 | { |
| 49 | $this->enabled_types = $this->post_type_repo->getPostTypesObject(); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Set the Menus for each of the enabled post types |
| 55 | */ |
| 56 | private function loopEnabledTypes() |
| 57 | { |
| 58 | $c = 1; // Counter for position |
| 59 | foreach($this->enabled_types as $key => $type){ |
| 60 | //var_dump($type); |
| 61 | if ( $type->np_enabled !== true ) continue; |
| 62 | if ( $type->replace_menu ) { |
| 63 | $this->post_type = get_post_type_object($key); |
| 64 | if ( (current_user_can($this->post_type->cap->edit_posts)) || ($this->user->canSortPages()) ){ |
| 65 | $this->addMenu($c); |
| 66 | $this->addSubmenu(); |
| 67 | $this->removeExistingMenu(); |
| 68 | } |
| 69 | } else { |
| 70 | $default = new AdminSubmenuDefault($type); |
| 71 | } |
| 72 | $c++; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * Add the primary top-level menu item |
| 79 | * @param int counter |
| 80 | */ |
| 81 | private function addMenu($c) |
| 82 | { |
| 83 | add_menu_page( |
| 84 | __($this->post_type->labels->name), |
| 85 | __($this->post_type->labels->name), |
| 86 | $this->post_type->cap->edit_posts, |
| 87 | $this->getSlug(), |
| 88 | Listing::admin_menu($this->post_type->name), |
| 89 | $this->menuIcon(), |
| 90 | $this->menuPosition($c) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Add Submenus |
| 96 | */ |
| 97 | private function addSubmenu() |
| 98 | { |
| 99 | $submenu = new AdminSubmenu($this->post_type); |
| 100 | $submenu->addSubmenu(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Remove Default Menus |
| 105 | */ |
| 106 | private function removeExistingMenu() |
| 107 | { |
| 108 | remove_menu_page('edit.php?post_type=' . $this->post_type->name); |
| 109 | if (in_array('post', $this->enabled_types)) remove_menu_page('edit.php'); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Get the correct icon to use in menu |
| 115 | * @return string |
| 116 | */ |
| 117 | private function menuIcon() |
| 118 | { |
| 119 | if ( $this->post_type->name == 'page' ) return 'dashicons-admin-page'; |
| 120 | if ( $this->post_type->menu_icon ) return $this->post_type->menu_icon; |
| 121 | return 'dashicons-admin-post'; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Get the correct menu position for item |
| 127 | * @param int counter |
| 128 | */ |
| 129 | private function menuPosition($c) |
| 130 | { |
| 131 | global $_wp_last_object_menu; |
| 132 | if ( $this->post_type->name == 'post' ) return 5; |
| 133 | if ( $this->post_type->name == 'page') return 20; |
| 134 | if ( $this->post_type->menu_position ) return $this->post_type->menu_position + 1; |
| 135 | return $_wp_last_object_menu + $c; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /** |
| 140 | * Get the Edit Slug for post type |
| 141 | */ |
| 142 | private function getSlug() |
| 143 | { |
| 144 | return $this->post_type_repo->getMenuSlug($this->post_type); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | } |