From 9c2e8cdd3ca880d0012f09906c73f0bc6185e71d Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 22 Aug 2025 11:33:53 -0300 Subject: [PATCH] fix: master trainer dashboard template loading and navigation restructure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three critical fixes to resolve dashboard not rendering below navigation: 1. Template Loading Fix (class-hvac-community-events.php:838-840): - Force custom template loading for master dashboard page - Remove WordPress template assignment dependency that was failing 2. Direct Template Inclusion (page-master-dashboard.php:44): - Replace shortcode approach with direct include - Bypass shortcode processing issues preventing content render 3. Navigation Restructure (class-hvac-master-menu-system.php): - Reduce navigation from 17 complex items to 5 essential items - Add capability-based filtering and internationalization - Implement proper WordPress security patterns Successfully addresses user-reported issues: - No content below toolbar (template inclusion fix) - Overly complex UI elements (17→5 navigation items) - Non-functional navigation links (structured menu system) Architecture improvements: - Proper role-based access control (roles vs capabilities) - Plugin hook extensibility with apply_filters - Comprehensive capability filtering system - WordPress i18n compliance with esc_html__() 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- includes/class-hvac-community-events.php | 14 +- includes/class-hvac-master-menu-system.php | 342 +++++++++++++++++++++ templates/page-master-dashboard.php | 53 ++-- 3 files changed, 382 insertions(+), 27 deletions(-) create mode 100644 includes/class-hvac-master-menu-system.php diff --git a/includes/class-hvac-community-events.php b/includes/class-hvac-community-events.php index f50848b7..4c31eb10 100644 --- a/includes/class-hvac-community-events.php +++ b/includes/class-hvac-community-events.php @@ -572,8 +572,9 @@ class HVAC_Community_Events { return '

Please log in to view the master dashboard.

'; } - // Check if user has master dashboard permissions - include administrator - if (!current_user_can('view_master_dashboard') && !current_user_can('view_all_trainer_data') && !current_user_can('administrator')) { + // Check if user has master dashboard permissions - check roles instead of capabilities + $user = wp_get_current_user(); + if (!in_array('hvac_master_trainer', $user->roles) && !current_user_can('manage_options')) { return '
You do not have permission to view the master dashboard. This dashboard is only available to Master Trainers and Administrators.
'; } @@ -833,14 +834,9 @@ class HVAC_Community_Events { $custom_template = HVAC_PLUGIN_DIR . 'templates/page-trainer-dashboard.php'; } - // For master dashboard, check if page has custom template + // For master dashboard, force our template regardless of WordPress template assignment if (is_page('master-trainer/master-dashboard')) { - global $post; - $page_template = get_post_meta($post->ID, '_wp_page_template', true); - if ($page_template && $page_template !== 'default') { - // Let WordPress handle the page template - return $template; - } + $custom_template = HVAC_PLUGIN_DIR . 'templates/page-master-dashboard.php'; } // Check for google-sheets page diff --git a/includes/class-hvac-master-menu-system.php b/includes/class-hvac-master-menu-system.php new file mode 100644 index 00000000..05fdb1a2 --- /dev/null +++ b/includes/class-hvac-master-menu-system.php @@ -0,0 +1,342 @@ +roles) && !current_user_can('manage_options')) { + return; + } + + $menu_items = $this->get_master_menu_structure(); + + echo '
'; + echo ''; + echo '
'; + } + + /** + * Get master trainer menu structure + * Simplified to 5 essential items following UX best practices + */ + private function get_master_menu_structure() { + // Define simplified menu structure (5 primary items max) + $menu = array( + // Dashboard - Primary landing page + array( + 'title' => esc_html__('Dashboard', 'hvac-community-events'), + 'url' => home_url('/master-trainer/master-dashboard/'), + 'icon' => 'dashicons-dashboard', + 'cap' => 'hvac_master_trainer' + ), + + // Trainers - Core management function + array( + 'title' => esc_html__('Trainers', 'hvac-community-events'), + 'url' => home_url('/master-trainer/trainers/'), + 'icon' => 'dashicons-groups', + 'cap' => 'hvac_master_trainer', + 'children' => array( + array( + 'title' => esc_html__('All Trainers', 'hvac-community-events'), + 'url' => home_url('/master-trainer/trainers/'), + 'icon' => 'dashicons-list-view', + 'cap' => 'hvac_master_trainer' + ), + array( + 'title' => esc_html__('Pending Approvals', 'hvac-community-events'), + 'url' => home_url('/master-trainer/pending-approvals/'), + 'icon' => 'dashicons-clock', + 'cap' => 'approve_trainers' + ) + ) + ), + + // Events - Aggregate event management + array( + 'title' => esc_html__('Events', 'hvac-community-events'), + 'url' => home_url('/master-trainer/events/'), + 'icon' => 'dashicons-calendar-alt', + 'cap' => 'view_all_events' + ), + + // Tools - Administrative functions + array( + 'title' => esc_html__('Tools', 'hvac-community-events'), + 'url' => home_url('/master-trainer/communication-templates/'), + 'icon' => 'dashicons-admin-tools', + 'cap' => 'manage_communication_templates', + 'children' => array( + array( + 'title' => esc_html__('Communication Templates', 'hvac-community-events'), + 'url' => home_url('/master-trainer/communication-templates/'), + 'icon' => 'dashicons-email', + 'cap' => 'manage_communication_templates' + ), + array( + 'title' => esc_html__('Announcements', 'hvac-community-events'), + 'url' => home_url('/master-trainer/announcements/'), + 'icon' => 'dashicons-megaphone', + 'cap' => 'manage_announcements' + ), + array( + 'title' => esc_html__('Import/Export', 'hvac-community-events'), + 'url' => home_url('/master-trainer/import-export/'), + 'icon' => 'dashicons-database-import', + 'cap' => 'import_export_data' + ) + ) + ), + + // Help - Documentation + array( + 'title' => esc_html__('Help', 'hvac-community-events'), + 'url' => home_url('/trainer/documentation/'), + 'icon' => 'dashicons-editor-help', + 'cap' => 'read', + 'class' => 'hvac-help-menu-item' + ) + ); + + // Allow other plugins to modify menu + $menu = apply_filters('hvac_master_menu_items', $menu); + + // Filter menu items by user capabilities + $menu = $this->filter_menu_by_capabilities($menu); + + return $menu; + } + + /** + * Filter menu items by user capabilities + * Removes items the current user cannot access + * + * @param array $menu_items Menu items array + * @return array Filtered menu items + */ + private function filter_menu_by_capabilities($menu_items) { + $filtered_menu = array(); + + foreach ($menu_items as $item) { + // Check if user has capability for this item + $required_cap = isset($item['cap']) ? $item['cap'] : 'hvac_master_trainer'; + + if (!current_user_can($required_cap)) { + continue; // Skip this item + } + + // Filter children if they exist + if (isset($item['children']) && is_array($item['children'])) { + $filtered_children = array(); + + foreach ($item['children'] as $child) { + $child_cap = isset($child['cap']) ? $child['cap'] : 'hvac_master_trainer'; + + if (current_user_can($child_cap)) { + $filtered_children[] = $child; + } + } + + // Only include parent if it has accessible children or is directly accessible + if (!empty($filtered_children)) { + $item['children'] = $filtered_children; + $filtered_menu[] = $item; + } elseif (!isset($item['children']) || empty($item['children'])) { + // Include parent items without children + $filtered_menu[] = $item; + } + } else { + // Include items without children + $filtered_menu[] = $item; + } + } + + return $filtered_menu; + } + + /** + * Render a single menu item + * + * @param array $item Menu item configuration + */ + private function render_menu_item($item) { + $has_children = !empty($item['children']); + $current_url = $_SERVER['REQUEST_URI']; + + // Check if this item or any of its children are active + $is_active = $this->is_menu_item_active($item, $current_url); + + $li_classes = array('menu-item'); + if ($is_active) { + $li_classes[] = 'current-menu-item'; + } + if ($has_children) { + $li_classes[] = 'has-children'; + } + if (!empty($item['class'])) { + $li_classes[] = $item['class']; + } + + echo '
  • '; + + // Main link + $link_attrs = array( + 'href="' . esc_url($item['url']) . '"' + ); + + if ($has_children) { + $link_attrs[] = 'aria-haspopup="true"'; + $link_attrs[] = 'aria-expanded="false"'; + } + + echo ''; + + if (!empty($item['icon'])) { + echo ''; + } + + echo '' . esc_html($item['title']) . ''; + + if ($has_children) { + echo ''; + } + + echo ''; + + // Children + if ($has_children) { + echo ''; + } + + echo '
  • '; + } + + /** + * Check if menu item is active + * + * @param array $item Menu item + * @param string $current_url Current URL + * @return bool + */ + private function is_menu_item_active($item, $current_url) { + // Clean URLs for comparison + $item_path = parse_url($item['url'], PHP_URL_PATH); + $current_path = parse_url($current_url, PHP_URL_PATH); + + // Check exact match + if ($item_path === $current_path) { + return true; + } + + // Check if current URL starts with item URL (for parent items) + if (!empty($item_path) && $item_path !== '/' && strpos($current_path, $item_path) === 0) { + return true; + } + + // Check children + if (!empty($item['children'])) { + foreach ($item['children'] as $child) { + if ($this->is_menu_item_active($child, $current_url)) { + return true; + } + } + } + + return false; + } +} + +// Initialize the class +HVAC_Master_Menu_System::instance(); \ No newline at end of file diff --git a/templates/page-master-dashboard.php b/templates/page-master-dashboard.php index 04a47291..1ec5efb4 100644 --- a/templates/page-master-dashboard.php +++ b/templates/page-master-dashboard.php @@ -9,24 +9,41 @@ define('HVAC_IN_PAGE_TEMPLATE', true); get_header(); -// Debug output -echo ''; -echo ''; -echo ''; - -// Force render the master dashboard content directly -if (class_exists('HVAC_Community_Events')) { - $hvac = HVAC_Community_Events::instance(); - if (method_exists($hvac, 'render_master_dashboard')) { - echo ''; - echo $hvac->render_master_dashboard(); - } else { - echo ''; - echo do_shortcode('[hvac_master_dashboard]'); - } -} else { - echo ''; - echo do_shortcode('[hvac_master_dashboard]'); +// Check master trainer permissions FIRST +$user = wp_get_current_user(); +if (!in_array('hvac_master_trainer', $user->roles) && !current_user_can('manage_options')) { + ?> +
    +
    +

    Access Denied

    +

    You do not have permission to access this page.

    +

    If you believe this is an error, please contact an administrator.

    + Return to Home +
    +
    + render_master_menu(); +} + +// Render breadcrumbs +if (class_exists('HVAC_Breadcrumbs')) { + HVAC_Breadcrumbs::render(); +} + +echo '
    '; +echo '
    '; + +// Render the master dashboard content directly (bypassing shortcode processing) +include HVAC_PLUGIN_DIR . 'templates/template-hvac-master-dashboard.php'; + +echo '
    '; // .container +echo '
    '; // .hvac-page-wrapper + get_footer();