should_show_breadcrumbs()) {
wp_enqueue_style(
'hvac-breadcrumbs',
HVAC_PLUGIN_URL . 'assets/css/hvac-breadcrumbs.css',
array(),
HVAC_PLUGIN_VERSION
);
}
}
/**
* Get breadcrumb trail
*/
private function get_breadcrumb_trail() {
$trail = array();
$current_url = home_url(add_query_arg(array(), $GLOBALS['wp']->request));
// Always start with home
$trail[] = array(
'label' => 'Home',
'url' => home_url('/')
);
// Check if we're on a trainer page
if (strpos($current_url, '/trainer/') !== false) {
$trail[] = array(
'label' => 'Trainer',
'url' => '/trainer/dashboard/'
);
// Parse the current path
$path = parse_url($current_url, PHP_URL_PATH);
$path = trim($path, '/');
$segments = explode('/', $path);
// Remove 'trainer' from segments as we've already added it
array_shift($segments);
// Build breadcrumb based on URL segments
if (!empty($segments)) {
$breadcrumb_map = $this->get_breadcrumb_map();
$current_path = '/trainer';
foreach ($segments as $index => $segment) {
$current_path .= '/' . $segment;
// Check if we have a mapping for this path
if (isset($breadcrumb_map[$current_path])) {
$is_last = ($index === count($segments) - 1);
$trail[] = array(
'label' => $breadcrumb_map[$current_path]['label'],
'url' => $is_last ? null : $current_path . '/',
'current' => $is_last
);
}
}
}
} elseif (strpos($current_url, '/master-trainer/') !== false) {
$trail[] = array(
'label' => 'Master Trainer',
'url' => '/master-trainer/master-dashboard/'
);
// Similar logic for master trainer pages
$path = parse_url($current_url, PHP_URL_PATH);
$path = trim($path, '/');
$segments = explode('/', $path);
array_shift($segments); // Remove 'master-trainer'
if (!empty($segments)) {
$breadcrumb_map = $this->get_breadcrumb_map();
$current_path = '/master-trainer';
foreach ($segments as $index => $segment) {
$current_path .= '/' . $segment;
if (isset($breadcrumb_map[$current_path])) {
$is_last = ($index === count($segments) - 1);
$trail[] = array(
'label' => $breadcrumb_map[$current_path]['label'],
'url' => $is_last ? null : $current_path . '/',
'current' => $is_last
);
}
}
}
} elseif (strpos($current_url, '/trainer-registration/') !== false) {
$trail[] = array(
'label' => 'Registration',
'url' => null,
'current' => true
);
}
// Allow filtering of breadcrumb trail
return apply_filters('hvac_breadcrumb_trail', $trail, $current_url);
}
/**
* Get breadcrumb mapping
*/
private function get_breadcrumb_map() {
return array(
// Trainer pages
'/trainer/dashboard' => array('label' => 'Dashboard'),
'/trainer/event' => array('label' => 'Events'),
'/trainer/event/manage' => array('label' => 'Manage Event'),
'/trainer/event-summary' => array('label' => 'Event Summary'),
'/trainer/generate-certificates' => array('label' => 'Generate Certificates'),
'/trainer/certificate-reports' => array('label' => 'Certificate Reports'),
'/trainer/venue' => array('label' => 'Venues'),
'/trainer/venue/list' => array('label' => 'List'),
'/trainer/venue/manage' => array('label' => 'Manage'),
'/trainer/organizer' => array('label' => 'Organizers'),
'/trainer/organizer/list' => array('label' => 'List'),
'/trainer/organizer/manage' => array('label' => 'Manage'),
'/trainer/profile' => array('label' => 'Profile'),
'/trainer/profile/edit' => array('label' => 'Edit'),
// Master trainer pages
'/master-trainer/master-dashboard' => array('label' => 'Master Dashboard'),
'/master-trainer/trainers' => array('label' => 'All Trainers'),
'/master-trainer/reports' => array('label' => 'Global Reports')
);
}
/**
* Render breadcrumbs shortcode
*/
public function render_breadcrumbs($atts = array()) {
$atts = shortcode_atts(array(
'separator' => '›',
'show_home' => 'yes',
'home_label' => 'Home',
'class' => 'hvac-breadcrumb'
), $atts);
$trail = $this->get_breadcrumb_trail();
if (empty($trail)) {
return '';
}
// Remove home if requested
if ($atts['show_home'] === 'no' && !empty($trail)) {
array_shift($trail);
} else if (!empty($trail) && $atts['home_label'] !== 'Home') {
$trail[0]['label'] = $atts['home_label'];
}
ob_start();
?>
should_show_breadcrumbs()) {
echo do_shortcode('[hvac_breadcrumbs]');
}
}
/**
* Check if breadcrumbs should be shown
*/
private function should_show_breadcrumbs() {
// Show on all trainer and master trainer pages
$current_url = home_url(add_query_arg(array(), $GLOBALS['wp']->request));
return strpos($current_url, '/trainer/') !== false ||
strpos($current_url, '/master-trainer/') !== false ||
strpos($current_url, '/trainer-registration/') !== false;
}
/**
* Get structured data for breadcrumbs (SEO)
*/
public function get_structured_data() {
$trail = $this->get_breadcrumb_trail();
if (empty($trail)) {
return '';
}
$items = array();
foreach ($trail as $index => $item) {
$items[] = array(
'@type' => 'ListItem',
'position' => $index + 1,
'name' => $item['label'],
'item' => !empty($item['url']) ? home_url($item['url']) : null
);
}
$structured_data = array(
'@context' => 'https://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => $items
);
return '';
}
}