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