>get_secondary( array( 'parent_slug' => $parent_slug, 'slug' => $slug ), false ); if ( ! $sub_items ) { return false; } $sub_item = reset( $sub_items ); if ( empty( $sub_item->slug ) ) { return false; } // Delete the child. unset( $this->nav[ $this->object_id ][ $parent_slug . '/' . $slug ] ); // Return the deleted item's screen function. return array( $sub_item->screen_function ); // We're deleting a parent. } else { // Validate the nav. $nav_items = $this->get_primary( array( 'slug' => $slug ), false ); if ( ! $nav_items ) { return false; } $nav_item = reset( $nav_items ); if ( empty( $nav_item->slug ) ) { return false; } $screen_functions = array( $nav_item->screen_function ); // Life's unfair, children won't survive the parent :( $sub_items = $this->get_secondary( array( 'parent_slug' => $nav_item->slug ), false ); if ( ! empty( $sub_items ) ) { foreach ( $sub_items as $sub_item ) { $screen_functions[] = $sub_item->screen_function; // Delete the child. unset( $this->nav[ $this->object_id ][ $nav_item->slug . '/' . $sub_item->slug ] ); } } // Delete the parent. unset( $this->nav[ $this->object_id ][ $nav_item->slug ] ); // Return the deleted item's screen functions. return $screen_functions; } } /** * Sorts a list of nav items. * * @since 2.6.0 * * @param array $items The nav items to sort. * @return array */ public function sort_nav( $items ) { $sorted = array(); foreach ( $items as $item ) { // Default position. $position = 99; if ( isset( $item->position ) ) { $position = (int) $item->position; } // If position is already taken, move to the first next available. if ( isset( $sorted[ $position ] ) ) { $sorted_keys = array_keys( $sorted ); do { $position += 1; } while ( in_array( $position, $sorted_keys ) ); } $sorted[ $position ] = $item; } ksort( $sorted ); return $sorted; } /** * Gets the primary nav items. * * @since 2.6.0 * * @param array $args Filters to select the specific primary items. See wp_list_filter(). * @param bool $sort True to sort the nav items. False otherwise. * @return array The list of primary objects nav */ public function get_primary( $args = array(), $sort = true ) { $params = bp_parse_args( $args, array( 'primary' => true, ) ); // This parameter is not overridable. if ( empty( $params['primary'] ) ) { return false; } $primary_nav = wp_list_filter( $this->nav[ $this->object_id ], $params ); if ( ! $primary_nav ) { return false; } if ( true !== $sort ) { return $primary_nav; } return $this->sort_nav( $primary_nav ); } /** * Gets the secondary nav items. * * @since 2.6.0 * * @param array $args Filters to select the specific secondary items. See wp_list_filter(). * @param bool $sort True to sort the nav items. False otherwise. * @return bool|array The list of secondary objects nav, or false if none set. */ public function get_secondary( $args = array(), $sort = true ) { $params = bp_parse_args( $args, array( 'parent_slug' => '', ) ); // No need to search children if the parent is not set. if ( empty( $params['parent_slug'] ) && empty( $params['secondary'] ) ) { return false; } $secondary_nav = wp_list_filter( $this->nav[ $this->object_id ], $params ); if ( ! $secondary_nav ) { return false; } if ( true !== $sort ) { return $secondary_nav; } return $this->sort_nav( $secondary_nav ); } /** * Gets a nested list of visible nav items. * * @since 2.6.0 * * @return array The list of visible nav items. */ public function get_item_nav() { $primary_nav_items = $this->get_primary( array( 'show_for_displayed_user' => true ) ); if ( $primary_nav_items ) { foreach( $primary_nav_items as $key_nav => $primary_nav ) { // Try to get the children. $children = $this->get_secondary( array( 'parent_slug' => $primary_nav->slug, 'user_has_access' => true ) ); if ( $children ) { $primary_nav_items[ $key_nav ] = clone $primary_nav; $primary_nav_items[ $key_nav ]->children = $children; } } } return $primary_nav_items; } }