displayLocation = $menuName; $this->topLevelNavItems = $this->getTopLevelNavItems(); $this->hasChildren = function ( $item ) { return $this->hasChildren( $item ); }; $this->nestedNavItems = function ( $parentItem ) { return $this->getNestedNavItems( $parentItem ); }; $this->currentPage = function ( $item ) { return $this->currentPage( $item ); }; } /** * Retrieves the list of menus. * * @return array An array containing menu items. */ public function getMenus() { $menus = get_nav_menu_locations(); return empty( $menus ) ? array() : $menus; } /** * Retrieves the navigation items. * * @return array An array of navigation items. */ public function getNavItems() { $locations = $this->getMenus(); // If the menu location doesn't exist, return empty array if ( ! isset( $locations[ $this->displayLocation ] ) ) { return array(); } // Get the menu ID for this location $menuId = $locations[ $this->displayLocation ]; // Direct call to wp_get_nav_menu_items with the menu ID $items = wp_get_nav_menu_items( $menuId ); // Return empty array if no items return is_array( $items ) ? $items : array(); } /** * Checks if the given parent item has child items. * * @param mixed $parentItem The parent item to check for children. * @return bool Returns true if the parent item has children, false otherwise. */ public function hasChildren( $parentItem ) { foreach ( $this->getNavItems() as $item ) { if ( $item->menu_item_parent === strval( $parentItem->ID ) ) { return true; } } return false; } /** * Retrieves the top-level navigation items. * * @return array An array of top-level navigation items. */ public function getTopLevelNavItems() { $items = array(); foreach ( $this->getNavItems() as $item ) { if ( $item->menu_item_parent === '0' ) { array_push( $items, $item ); } } return $items; } /** * Retrieves the nested navigation items for a given parent item. * * @param mixed $parentItem The parent navigation item for which nested items are to be retrieved. * @return array The list of nested navigation items. */ public function getNestedNavItems( $parentItem ) { $childrenItems = array(); foreach ( $this->getNavItems() as $item ) { if ( $item->menu_item_parent === strval( $parentItem->ID ) ) { array_push( $childrenItems, $item ); } } return $childrenItems; } /** * Determines the current page based on the provided item. * * @param mixed $item The item used to determine the current page. * @return mixed The current page information. */ public function currentPage( $item ) { global $wp; return ( $item->url === home_url( $wp->request ) . '/' ) ? 'true' : 'false'; } /** * Renders the output for the current context. * * This method is responsible for generating and returning the * appropriate output for the current context or request. * * @return void */ public function render() { global $views; // Extract class properties to local variables $location = $this->displayLocation; $topLevelNavItems = $this->topLevelNavItems; $hasChildren = $this->hasChildren; $nestedNavItems = $this->nestedNavItems; $currentPage = $this->currentPage; include $views . '/components/menu-items/index.php'; } }