is_legacy_url( $current_path ) ) { // Allow the redirect to happen first return; } // Check if this is a public page if ( $this->is_public_page( $current_path ) ) { return; } // Check if this is a trainer page if ( $this->is_trainer_page( $current_path ) ) { $this->check_trainer_access( $current_path ); } // Check if this is a master trainer page if ( $this->is_master_trainer_page( $current_path ) ) { $this->check_master_trainer_access( $current_path ); } } /** * Check if current URL is a legacy URL that should be redirected * * @param string $path Current page path * @return bool */ private function is_legacy_url( $path ) { // Get the route manager instance if ( ! class_exists( 'HVAC_Route_Manager' ) ) { return false; } $route_manager = HVAC_Route_Manager::instance(); // Check if this path needs a redirect return $route_manager->needs_redirect( $path ) !== false; } /** * Check if current page is public * * @param string $path Current page path * @return bool */ private function is_public_page( $path ) { foreach ( self::$public_pages as $public_page ) { if ( $path === $public_page || strpos( $path, $public_page ) === 0 ) { return true; } } return false; } /** * Check if current page is a trainer page * * @param string $path Current page path * @return bool */ private function is_trainer_page( $path ) { foreach ( self::$trainer_pages as $trainer_page ) { if ( $path === $trainer_page || strpos( $path, $trainer_page ) === 0 ) { return true; } } // Also check for pages that start with 'trainer/' if ( strpos( $path, 'trainer/' ) === 0 ) { return true; } return false; } /** * Check if current page is a master trainer page * * @param string $path Current page path * @return bool */ private function is_master_trainer_page( $path ) { foreach ( self::$master_trainer_pages as $master_page ) { if ( $path === $master_page || strpos( $path, $master_page ) === 0 ) { return true; } } // Also check for pages that start with 'master-trainer/' if ( strpos( $path, 'master-trainer/' ) === 0 ) { return true; } return false; } /** * Check trainer access to protected pages * * @param string $path Current page path */ private function check_trainer_access( $path ) { // First check if user is logged in if ( ! is_user_logged_in() ) { // Preserve the original URL for redirect after login $redirect_url = home_url( '/' . $path . '/' ); $login_url = add_query_arg( 'redirect_to', urlencode( $redirect_url ), home_url( '/training-login/' ) ); wp_safe_redirect( $login_url ); exit; } $user_id = get_current_user_id(); $user = wp_get_current_user(); // Allow administrators full access if ( current_user_can( 'manage_options' ) ) { return; } // Check if user has trainer role if ( ! in_array( 'hvac_trainer', $user->roles ) && ! in_array( 'hvac_master_trainer', $user->roles ) ) { // Not a trainer, show access denied $this->show_access_denied(); return; } // Get trainer status if ( ! class_exists( 'HVAC_Trainer_Status' ) ) { require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-trainer-status.php'; } $status = HVAC_Trainer_Status::get_trainer_status( $user_id ); // Handle based on status switch ( $status ) { case HVAC_Trainer_Status::STATUS_PENDING: // Redirect to pending page if ( $path !== 'trainer-account-pending' && strpos( $path, 'trainer-account-pending' ) !== 0 ) { wp_safe_redirect( home_url( '/trainer-account-pending/' ) ); exit; } break; case HVAC_Trainer_Status::STATUS_DISABLED: // Redirect to disabled page if ( $path !== 'trainer-account-disabled' && strpos( $path, 'trainer-account-disabled' ) !== 0 ) { wp_safe_redirect( home_url( '/trainer-account-disabled/' ) ); exit; } break; case HVAC_Trainer_Status::STATUS_APPROVED: case HVAC_Trainer_Status::STATUS_ACTIVE: case HVAC_Trainer_Status::STATUS_INACTIVE: // Allow access break; default: // Unknown status, treat as pending wp_safe_redirect( home_url( '/trainer-account-pending/' ) ); exit; } } /** * Check master trainer access to protected pages * * @param string $path Current page path */ private function check_master_trainer_access( $path ) { // First check if user is logged in if ( ! is_user_logged_in() ) { // Preserve the original URL for redirect after login $redirect_url = home_url( '/' . $path . '/' ); $login_url = add_query_arg( 'redirect_to', urlencode( $redirect_url ), home_url( '/training-login/' ) ); wp_safe_redirect( $login_url ); exit; } $user = wp_get_current_user(); // Allow administrators full access if ( current_user_can( 'manage_options' ) ) { return; } // Check if user has master trainer role if ( ! in_array( 'hvac_master_trainer', $user->roles ) ) { // Not a master trainer, show access denied $this->show_access_denied(); return; } // Master trainers have access to all their pages // No need to check status for master trainers } /** * Show access denied page */ private function show_access_denied() { get_header(); ?>