- Add interactive modal popup for announcement 'Read More' functionality - Fix nonce conflict by creating separate hvac_announcements_ajax object - Implement secure AJAX handler with rate limiting and permission checks - Add comprehensive modal CSS with smooth animations and responsive design - Include accessibility features (ARIA, keyboard navigation, screen reader support) - Create detailed documentation in docs/ANNOUNCEMENT-MODAL-SYSTEM.md - Update API-REFERENCE.md with new modal endpoints and security details - Add automated Playwright E2E testing for modal functionality - All modal interactions working: click to open, X to close, ESC to close, outside click - Production-ready with full error handling and content sanitization 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
372 lines
No EOL
12 KiB
PHP
372 lines
No EOL
12 KiB
PHP
<?php
|
|
/**
|
|
* HVAC Menu System
|
|
*
|
|
* Implements proper WordPress menu API for HVAC trainer pages
|
|
* Following WordPress best practices for menu registration and display
|
|
*
|
|
* @package HVAC_Community_Events
|
|
* @since 2.0.0
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class HVAC_Menu_System {
|
|
|
|
/**
|
|
* Plugin instance
|
|
*
|
|
* @var HVAC_Menu_System
|
|
*/
|
|
private static $instance = null;
|
|
|
|
/**
|
|
* Get plugin instance
|
|
*
|
|
* @return HVAC_Menu_System
|
|
*/
|
|
public static function instance() {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
/**
|
|
* Alias for instance() to match template calls
|
|
*
|
|
* @return HVAC_Menu_System
|
|
*/
|
|
public static function get_instance() {
|
|
return self::instance();
|
|
}
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct() {
|
|
add_action('init', array($this, 'register_menu_locations'));
|
|
add_action('wp', array($this, 'setup_trainer_menu'));
|
|
add_action('wp_enqueue_scripts', array($this, 'enqueue_menu_styles'));
|
|
add_filter('wp_nav_menu_items', array($this, 'add_logout_to_menu'), 10, 2);
|
|
}
|
|
|
|
/**
|
|
* Detect if user is using Safari browser
|
|
* Uses centralized browser detection service
|
|
*
|
|
* @return bool
|
|
*/
|
|
private function is_safari_browser() {
|
|
return HVAC_Browser_Detection::instance()->is_safari_browser();
|
|
}
|
|
|
|
/**
|
|
* Get script path based on browser compatibility
|
|
* Uses centralized browser detection service
|
|
*
|
|
* @param string $script_name
|
|
* @return string
|
|
*/
|
|
private function get_compatible_script_path($script_name) {
|
|
$browser_detection = HVAC_Browser_Detection::instance();
|
|
|
|
// If Safari and doesn't support ES6, load Safari-compatible version
|
|
if ($browser_detection->is_safari_browser() && !$browser_detection->safari_supports_es6()) {
|
|
$safari_script = HVAC_PLUGIN_DIR . 'assets/js/' . $script_name . '-safari-compatible.js';
|
|
if (file_exists($safari_script)) {
|
|
return HVAC_PLUGIN_URL . 'assets/js/' . $script_name . '-safari-compatible.js';
|
|
}
|
|
}
|
|
|
|
// Default to standard version
|
|
return HVAC_PLUGIN_URL . 'assets/js/' . $script_name . '.js';
|
|
}
|
|
|
|
/**
|
|
* Register menu locations
|
|
*/
|
|
public function register_menu_locations() {
|
|
register_nav_menus(array(
|
|
'hvac_trainer_menu' => __('HVAC Trainer Navigation', 'hvac-community-events'),
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Setup trainer menu on appropriate pages
|
|
*/
|
|
public function setup_trainer_menu() {
|
|
// Menu will be rendered directly in templates, not injected via JavaScript
|
|
}
|
|
|
|
/**
|
|
* Check if current page is a trainer page
|
|
*/
|
|
private function is_trainer_page() {
|
|
global $wp;
|
|
$current_url = home_url(add_query_arg(array(), $wp->request));
|
|
return (strpos($current_url, '/trainer/') !== false || strpos($current_url, '/master-trainer/') !== false);
|
|
}
|
|
|
|
/**
|
|
* Check if user can access trainer area
|
|
*/
|
|
private function user_can_access_trainer_area() {
|
|
if (!is_user_logged_in()) {
|
|
return false;
|
|
}
|
|
|
|
return current_user_can('hvac_trainer') ||
|
|
current_user_can('hvac_master_trainer') ||
|
|
current_user_can('manage_options');
|
|
}
|
|
|
|
/**
|
|
* Render the trainer menu
|
|
*/
|
|
public function render_trainer_menu() {
|
|
if (!$this->user_can_access_trainer_area()) {
|
|
return;
|
|
}
|
|
|
|
$menu_items = $this->get_menu_structure();
|
|
|
|
echo '<div class="hvac-trainer-menu-wrapper">';
|
|
echo '<nav class="hvac-trainer-nav" role="navigation">';
|
|
|
|
// Add hamburger button for mobile
|
|
echo '<button class="hvac-hamburger-menu" id="hvac-hamburger-menu" aria-label="Toggle menu" aria-expanded="false">';
|
|
echo '<span class="hvac-hamburger-line"></span>';
|
|
echo '<span class="hvac-hamburger-line"></span>';
|
|
echo '<span class="hvac-hamburger-line"></span>';
|
|
echo '</button>';
|
|
|
|
echo '<ul class="hvac-trainer-menu" id="hvac-trainer-menu">';
|
|
|
|
foreach ($menu_items as $item) {
|
|
$this->render_menu_item($item);
|
|
}
|
|
|
|
echo '</ul>';
|
|
echo '</nav>';
|
|
echo '</div>';
|
|
}
|
|
|
|
/**
|
|
* Alias for render_trainer_menu() to match template calls
|
|
*/
|
|
public function render_navigation_menu() {
|
|
return $this->render_trainer_menu();
|
|
}
|
|
|
|
/**
|
|
* Get menu structure based on user capabilities
|
|
*/
|
|
private function get_menu_structure() {
|
|
$user = wp_get_current_user();
|
|
$is_master = in_array('hvac_master_trainer', $user->roles) || current_user_can('manage_options');
|
|
|
|
$menu = array();
|
|
|
|
// Add Master Dashboard conditionally (only if user has master trainer role)
|
|
if ($is_master) {
|
|
$menu[] = array(
|
|
'title' => 'Master Dashboard',
|
|
'url' => home_url('/master-trainer/master-dashboard/'),
|
|
'icon' => 'dashicons-star-filled'
|
|
);
|
|
|
|
$menu[] = array(
|
|
'title' => 'Announcements',
|
|
'url' => home_url('/master-trainer/announcements/'),
|
|
'icon' => 'dashicons-megaphone'
|
|
);
|
|
}
|
|
|
|
// Events section
|
|
$menu[] = array(
|
|
'title' => 'Events',
|
|
'url' => '#',
|
|
'icon' => 'dashicons-calendar-alt',
|
|
'children' => array(
|
|
array(
|
|
'title' => 'Dashboard',
|
|
'url' => home_url('/trainer/dashboard/'),
|
|
'icon' => 'dashicons-dashboard'
|
|
),
|
|
array(
|
|
'title' => 'New Event',
|
|
'url' => home_url('/trainer/event/manage/'),
|
|
'icon' => 'dashicons-plus-alt'
|
|
)
|
|
)
|
|
);
|
|
|
|
// Certificates section
|
|
$menu[] = array(
|
|
'title' => 'Certificates',
|
|
'url' => '#',
|
|
'icon' => 'dashicons-awards',
|
|
'children' => array(
|
|
array(
|
|
'title' => 'Reports',
|
|
'url' => home_url('/trainer/certificate-reports/'),
|
|
'icon' => 'dashicons-analytics'
|
|
),
|
|
array(
|
|
'title' => 'New Certificate',
|
|
'url' => home_url('/trainer/generate-certificates/'),
|
|
'icon' => 'dashicons-plus-alt'
|
|
)
|
|
)
|
|
);
|
|
|
|
// Profile section (previously Customize)
|
|
$menu[] = array(
|
|
'title' => 'Profile',
|
|
'url' => '#',
|
|
'icon' => 'dashicons-admin-users',
|
|
'children' => array(
|
|
array(
|
|
'title' => 'Trainer Profile',
|
|
'url' => home_url('/trainer/profile/'),
|
|
'icon' => 'dashicons-admin-users'
|
|
),
|
|
array(
|
|
'title' => 'Training Leads',
|
|
'url' => home_url('/trainer/training-leads/'),
|
|
'icon' => 'dashicons-email-alt'
|
|
),
|
|
array(
|
|
'title' => 'Resources',
|
|
'url' => home_url('/trainer/resources/'),
|
|
'icon' => 'dashicons-media-default'
|
|
),
|
|
array(
|
|
'title' => 'Training Organizers',
|
|
'url' => home_url('/trainer/organizer/list/'),
|
|
'icon' => 'dashicons-groups',
|
|
'children' => array(
|
|
array(
|
|
'title' => 'Add New Organizer',
|
|
'url' => home_url('/trainer/organizer/manage/'),
|
|
'icon' => 'dashicons-plus-alt'
|
|
)
|
|
)
|
|
),
|
|
array(
|
|
'title' => 'Training Venues',
|
|
'url' => home_url('/trainer/venue/list/'),
|
|
'icon' => 'dashicons-location-alt',
|
|
'children' => array(
|
|
array(
|
|
'title' => 'Add New Venue',
|
|
'url' => home_url('/trainer/venue/manage/'),
|
|
'icon' => 'dashicons-plus-alt'
|
|
)
|
|
)
|
|
),
|
|
array(
|
|
'title' => 'Logout',
|
|
'url' => wp_logout_url(home_url('/training-login/')),
|
|
'icon' => 'dashicons-exit'
|
|
)
|
|
)
|
|
);
|
|
|
|
// Help section (moved to end, with question mark icon)
|
|
$menu[] = array(
|
|
'title' => '?',
|
|
'url' => home_url('/trainer/documentation/'),
|
|
'icon' => 'dashicons-editor-help',
|
|
'class' => 'hvac-help-menu-item'
|
|
);
|
|
|
|
return $menu;
|
|
}
|
|
|
|
|
|
/**
|
|
* Render individual menu item
|
|
*/
|
|
private function render_menu_item($item, $level = 0) {
|
|
$has_children = !empty($item['children']);
|
|
$icon = !empty($item['icon']) ? '<span class="dashicons ' . esc_attr($item['icon']) . '"></span>' : '';
|
|
$classes = array('menu-item');
|
|
|
|
if ($has_children) {
|
|
$classes[] = 'has-children';
|
|
}
|
|
|
|
if ($level > 0) {
|
|
$classes[] = 'sub-menu-item';
|
|
$classes[] = 'level-' . $level;
|
|
}
|
|
|
|
// Add custom class if specified
|
|
if (!empty($item['class'])) {
|
|
$classes[] = esc_attr($item['class']);
|
|
}
|
|
|
|
echo '<li class="' . implode(' ', $classes) . '">';
|
|
|
|
// Check if this is the help menu item (show only icon)
|
|
$is_help_menu = !empty($item['class']) && strpos($item['class'], 'hvac-help-menu-item') !== false;
|
|
$title_content = $is_help_menu ? '' : esc_html($item['title']);
|
|
|
|
if ($item['url'] === '#' && $has_children) {
|
|
echo '<span class="menu-toggle">' . $icon . $title_content . '<span class="dropdown-arrow">▼</span></span>';
|
|
} else {
|
|
echo '<a href="' . esc_url($item['url']) . '">' . $icon . $title_content . '</a>';
|
|
}
|
|
|
|
if ($has_children) {
|
|
echo '<ul class="sub-menu">';
|
|
foreach ($item['children'] as $child) {
|
|
$this->render_menu_item($child, $level + 1);
|
|
}
|
|
echo '</ul>';
|
|
}
|
|
|
|
echo '</li>';
|
|
}
|
|
|
|
/**
|
|
* Enqueue menu styles and scripts
|
|
*/
|
|
public function enqueue_menu_styles() {
|
|
if ($this->is_trainer_page() && $this->user_can_access_trainer_area()) {
|
|
wp_enqueue_style(
|
|
'hvac-menu-system',
|
|
HVAC_PLUGIN_URL . 'assets/css/hvac-menu-system.css',
|
|
array(),
|
|
HVAC_PLUGIN_VERSION
|
|
);
|
|
|
|
wp_enqueue_script(
|
|
'hvac-menu-system',
|
|
$this->get_compatible_script_path('hvac-menu-system'),
|
|
array('jquery'),
|
|
HVAC_PLUGIN_VERSION,
|
|
true
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add logout link to WordPress menu (if using wp_nav_menu)
|
|
*/
|
|
public function add_logout_to_menu($items, $args) {
|
|
if ($args->theme_location === 'hvac_trainer_menu' && $this->user_can_access_trainer_area()) {
|
|
$logout_link = '<li class="menu-item menu-item-logout"><a href="' . wp_logout_url(home_url('/training-login/')) . '"><span class="dashicons dashicons-exit"></span>Logout</a></li>';
|
|
$items .= $logout_link;
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
}
|
|
|
|
// Initialize
|
|
HVAC_Menu_System::instance(); |