upskill-event-manager/includes/class-hvac-route-manager.php
bengizmo 005a35d89e refactor: Implement new plugin architecture with single-responsibility classes
- Create HVAC_Shortcodes class to centralize shortcode management
- Create HVAC_Scripts_Styles class for all script/style enqueuing
- Create HVAC_Route_Manager class for URL routing and redirects
- Update HVAC_Plugin to use new architecture components
- Remove duplicate functionality from HVAC_Community_Events
- Add comprehensive refactoring plan documentation

This refactoring resolves duplicate initialization issues and creates
a cleaner, more maintainable architecture with clear separation of concerns.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-28 18:04:48 -03:00

341 lines
No EOL
9.9 KiB
PHP

<?php
/**
* HVAC Route Manager
*
* Centralized management of URL routing, redirects, and rewrite rules
*
* @package HVAC_Community_Events
* @since 2.0.0
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* HVAC_Route_Manager class
*/
class HVAC_Route_Manager {
/**
* Instance
*
* @var HVAC_Route_Manager
*/
private static $instance = null;
/**
* Legacy redirect mappings
*
* @var array
*/
private $legacy_redirects = array();
/**
* Parent page redirects
*
* @var array
*/
private $parent_redirects = array();
/**
* Get instance
*
* @return HVAC_Route_Manager
*/
public static function instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
$this->define_redirects();
$this->init_hooks();
}
/**
* Define redirect mappings
*
* @return void
*/
private function define_redirects() {
// Legacy URL to new URL mappings
$this->legacy_redirects = array(
'community-login' => 'training-login',
'hvac-dashboard' => 'trainer/dashboard',
'master-dashboard' => 'master-trainer/dashboard',
'manage-event' => 'trainer/event/manage',
'trainer-profile' => 'trainer/my-profile',
'event-summary' => 'trainer/event/summary',
'email-attendees' => 'trainer/email-attendees',
'certificate-reports' => 'trainer/certificate-reports',
'generate-certificates' => 'trainer/generate-certificates',
'certificate-fix' => 'master-trainer/certificate-fix',
'hvac-documentation' => 'trainer/documentation',
'attendee-profile' => 'trainer/attendee-profile',
'google-sheets' => 'master-trainer/google-sheets',
'communication-templates' => 'trainer/communication-templates',
'communication-schedules' => 'trainer/communication-schedules',
'trainer-registration' => 'trainer/registration',
);
// Parent pages that redirect to dashboards
$this->parent_redirects = array(
'trainer' => 'trainer/dashboard',
'master-trainer' => 'master-trainer/dashboard',
);
// Allow filtering
$this->legacy_redirects = apply_filters('hvac_legacy_redirects', $this->legacy_redirects);
$this->parent_redirects = apply_filters('hvac_parent_redirects', $this->parent_redirects);
}
/**
* Initialize hooks
*
* @return void
*/
private function init_hooks() {
// Register rewrite rules
add_action('init', array($this, 'register_rewrite_rules'), 5);
// Handle redirects
add_action('template_redirect', array($this, 'handle_redirects'), 5);
// Register query vars
add_filter('query_vars', array($this, 'register_query_vars'));
// Handle legacy redirect requests
add_action('parse_request', array($this, 'handle_legacy_redirect_request'));
// Flush rewrite rules on activation
add_action('hvac_plugin_activated', array($this, 'flush_rewrite_rules'));
}
/**
* Register rewrite rules
*
* @return void
*/
public function register_rewrite_rules() {
// Add rewrite rules for legacy URLs
foreach ($this->legacy_redirects as $legacy => $new) {
add_rewrite_rule(
'^' . $legacy . '/?$',
'index.php?hvac_legacy_redirect=' . $legacy,
'top'
);
}
// Add rewrite rules for hierarchical URLs
$hierarchical_rules = array(
// Trainer pages
'trainer/?$' => 'index.php?hvac_route=trainer',
'trainer/([^/]+)/?$' => 'index.php?hvac_route=trainer&hvac_page=$matches[1]',
'trainer/([^/]+)/([^/]+)/?$' => 'index.php?hvac_route=trainer&hvac_page=$matches[1]&hvac_subpage=$matches[2]',
// Master trainer pages
'master-trainer/?$' => 'index.php?hvac_route=master-trainer',
'master-trainer/([^/]+)/?$' => 'index.php?hvac_route=master-trainer&hvac_page=$matches[1]',
'master-trainer/([^/]+)/([^/]+)/?$' => 'index.php?hvac_route=master-trainer&hvac_page=$matches[1]&hvac_subpage=$matches[2]',
);
foreach ($hierarchical_rules as $regex => $redirect) {
add_rewrite_rule($regex, $redirect, 'top');
}
// Log registration
HVAC_Logger::info('Registered ' . count($this->legacy_redirects) . ' legacy redirects and ' . count($hierarchical_rules) . ' hierarchical rules', 'Routes');
}
/**
* Register query vars
*
* @param array $vars Query vars
* @return array
*/
public function register_query_vars($vars) {
$vars[] = 'hvac_legacy_redirect';
$vars[] = 'hvac_route';
$vars[] = 'hvac_page';
$vars[] = 'hvac_subpage';
return $vars;
}
/**
* Handle legacy redirect requests
*
* @param WP $wp WordPress environment instance
* @return void
*/
public function handle_legacy_redirect_request($wp) {
if (!isset($wp->query_vars['hvac_legacy_redirect'])) {
return;
}
$legacy_slug = $wp->query_vars['hvac_legacy_redirect'];
if (isset($this->legacy_redirects[$legacy_slug])) {
$new_url = home_url('/' . $this->legacy_redirects[$legacy_slug] . '/');
// Preserve query parameters
if (!empty($_SERVER['QUERY_STRING'])) {
$new_url .= '?' . $_SERVER['QUERY_STRING'];
}
// Log redirect
HVAC_Logger::info("Redirecting legacy URL: {$legacy_slug} to {$new_url}", 'Routes');
wp_redirect($new_url, 301);
exit;
}
}
/**
* Handle redirects
*
* @return void
*/
public function handle_redirects() {
// Get current URL path
$current_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
// Handle legacy redirects
if (isset($this->legacy_redirects[$current_path])) {
$this->perform_redirect($this->legacy_redirects[$current_path]);
}
// Handle parent page redirects
if (isset($this->parent_redirects[$current_path])) {
$this->perform_redirect($this->parent_redirects[$current_path]);
}
// Also check if this is a page that exists with a legacy slug
global $post;
if (is_page() && $post) {
$current_slug = $post->post_name;
if (isset($this->legacy_redirects[$current_slug])) {
$this->perform_redirect($this->legacy_redirects[$current_slug]);
}
}
}
/**
* Perform redirect
*
* @param string $target Target path
* @return void
*/
private function perform_redirect($target) {
$new_url = home_url('/' . $target . '/');
// Preserve query parameters
if (!empty($_SERVER['QUERY_STRING'])) {
$new_url .= '?' . $_SERVER['QUERY_STRING'];
}
// Log redirect
HVAC_Logger::info("Performing redirect to: {$new_url}", 'Routes');
wp_redirect($new_url, 301);
exit;
}
/**
* Get legacy redirects
*
* @return array
*/
public function get_legacy_redirects() {
return $this->legacy_redirects;
}
/**
* Get parent redirects
*
* @return array
*/
public function get_parent_redirects() {
return $this->parent_redirects;
}
/**
* Add custom redirect
*
* @param string $from From path
* @param string $to To path
* @param string $type Type of redirect (legacy or parent)
* @return void
*/
public function add_redirect($from, $to, $type = 'legacy') {
if ($type === 'legacy') {
$this->legacy_redirects[$from] = $to;
} elseif ($type === 'parent') {
$this->parent_redirects[$from] = $to;
}
// Re-register rules if already initialized
if (did_action('init')) {
$this->register_rewrite_rules();
flush_rewrite_rules();
}
}
/**
* Remove redirect
*
* @param string $from From path
* @param string $type Type of redirect (legacy or parent)
* @return void
*/
public function remove_redirect($from, $type = 'legacy') {
if ($type === 'legacy' && isset($this->legacy_redirects[$from])) {
unset($this->legacy_redirects[$from]);
} elseif ($type === 'parent' && isset($this->parent_redirects[$from])) {
unset($this->parent_redirects[$from]);
}
// Re-register rules if already initialized
if (did_action('init')) {
$this->register_rewrite_rules();
flush_rewrite_rules();
}
}
/**
* Flush rewrite rules
*
* @return void
*/
public function flush_rewrite_rules() {
flush_rewrite_rules();
HVAC_Logger::info('Flushed rewrite rules', 'Routes');
}
/**
* Check if URL needs redirect
*
* @param string $url URL to check
* @return string|false Redirect target or false
*/
public function needs_redirect($url) {
$path = trim(parse_url($url, PHP_URL_PATH), '/');
if (isset($this->legacy_redirects[$path])) {
return $this->legacy_redirects[$path];
}
if (isset($this->parent_redirects[$path])) {
return $this->parent_redirects[$path];
}
return false;
}
}