'charitable-dashboard', 'fallback_cb' => false, ); $args = wp_parse_args( $args, $defaults ); return wp_nav_menu( $args ); } /** * Return the menu ID based on the theme location. * * @since 1.0.0 * * @return int 0 if no menu found. Menu ID otherwise. */ public function get_nav_id() { $locations = get_nav_menu_locations(); if ( ! isset( $locations['charitable-dashboard'] ) ) { return 0; } return wp_get_nav_menu_object( $locations['charitable-dashboard'] ); } /** * Returns all objects in the user dashboard navigation. * * @uses wp_get_nav_menu_items * @since 1.0.0 * * @return WP_Post[] */ public function nav_objects() { $objects = get_transient( 'charitable_user_dashboard_objects' ); if ( false === $objects ) { $objects = array(); $nav_menu_items = wp_get_nav_menu_items( $this->get_nav_id() ); if ( is_array( $nav_menu_items ) ) { foreach ( $nav_menu_items as $nav_menu_item ) { switch ( $nav_menu_item->type ) { case 'custom': $identifier = trailingslashit( $nav_menu_item->url ); break; default: $identifier = apply_filters( 'charitable_nav_menu_object_identifier', $nav_menu_item->object_id, $nav_menu_item ); } $objects[] = $identifier; } } set_transient( 'charitable_user_dashboard_objects', $objects ); } return $objects; } /** * Flushes the menu object cache after updating a menu or menu item. * * @since 1.0.0 * * @param int $menu_id The menu id. * @return void */ public function flush_menu_object_cache( $menu_id ) { $dashboard_menu = $this->get_nav_id(); if ( ! $dashboard_menu ) { delete_transient( 'charitable_user_dashboard_objects' ); return; } $nav_menu = wp_get_nav_menu_object( $dashboard_menu ); /* If $nav_menu is not set that means the location 'charitable-dashboard' is not checked. */ if ( $menu_id == $nav_menu->term_id ) { delete_transient( 'charitable_user_dashboard_objects' ); } } /** * Checks whether the current requested page is in the user dashboard nav. * * @since 1.0.0 * * @return boolean */ public function in_nav() { global $wp; if ( ! isset( $this->on_user_dashboard_page ) ) { $current_url = trailingslashit( charitable_get_current_url() ); $this->on_user_dashboard_page = in_array( get_queried_object_id(), $this->nav_objects() ) || in_array( $current_url, $this->nav_objects() ); /** * Set whether we're in the user dashboard. * * @param boolean $on_user_dashboard_page Whether we're in the user dashboard. * @param array $nav_objects The navigation menu objects for the user dashboard. */ $this->on_user_dashboard_page = apply_filters( 'charitable_is_in_user_dashboard', $this->on_user_dashboard_page, $this->nav_objects() ); } return $this->on_user_dashboard_page; } /** * Loads the user dashboard template. * * @since 1.0.0 * * @param string $template The template to use for the user dashboard. * @return string */ public function load_user_dashboard_template( $template ) { /** * The user dashboard template is not loaded by default; this has to be enabled. */ if ( false === apply_filters( 'charitable_force_user_dashboard_template', false ) ) { return $template; } /** * The current object isn't in the nav, so return the template. */ if ( ! $this->in_nav() ) { return $template; } do_action( 'charitable_is_user_dashboard' ); $new_template = apply_filters( 'charitable_user_dashboard_template', 'user-dashboard.php' ); $template = charitable_get_template_path( $new_template, $template ); return $template; } /** * Add the user-dashboard class to the body if we're looking at it. * * @since 1.0.0 * * @param array $classes Body classes. * @return array */ public function add_body_class( $classes ) { if ( $this->in_nav() ) { $classes[] = 'user-dashboard'; } return $classes; } } endif;