# Template System Refactor Plan
## Current Issues
1. **Master Dashboard Bug**: The master dashboard page (ID: 5505) is showing trainer dashboard content instead of master dashboard content
2. **Competing Systems**: Both page templates and shortcodes are trying to render the same content
3. **Complex Routing**: Multiple redirect systems and rewrite rules causing confusion
4. **CSS Loading Issues**: Styles not consistently applied to logged-in pages
5. **Debug Code**: Production code contains debug statements
## Root Cause Analysis
The plugin uses two competing systems:
- **Page Templates**: Custom PHP templates that override theme templates
- **Shortcodes**: WordPress shortcodes that render content within pages
When a page has both a custom template AND a shortcode, conflicts arise. The master dashboard page has:
- Page template: `page-master-dashboard.php` 
- Page content: `[hvac_master_dashboard]`
- But somehow the trainer dashboard content is being rendered
## Recommended Solution: Shortcode-Only Architecture
### Phase 1: Remove Template System (Immediate Fix)
1. **Update class-hvac-community-events.php**:
   - Remove `load_custom_templates()` method
   - Remove `add_filter('template_include', ...)` hook
   - Keep all shortcode registrations
2. **Update class-hvac-page-manager.php**:
   - Change all page definitions to use 'default' template
   - Ensure content includes appropriate shortcode
3. **Delete template files**:
   - Remove all `page-*.php` files from templates/
   - Keep `template-*.php` files as they're view partials
4. **Remove class-hvac-template-loader.php**:
   - This entire class becomes unnecessary
### Phase 2: Clean Up Authentication (Immediate)
1. **Consolidate authentication in shortcodes**:
   - Each shortcode callback handles its own authentication
   - Remove redundant `template_redirect` authentication hooks
   - Keep access control for non-page routes only
2. **Standardize authentication pattern**:
   ```php
   public function render_[feature]($atts = array()) {
       if (!is_user_logged_in()) {
           return '
Please log in to view this content.
';
       }
       
       if (!current_user_can('required_capability')) {
           return 'Access denied.
';
       }
       
       // Render content
   }
   ```
### Phase 3: Fix CSS Loading (Next Priority)
1. **Remove conditional CSS loading**:
   - Enqueue base styles globally for consistency
   - Use CSS classes to show/hide elements rather than conditional loading
2. **Consolidate CSS files**:
   - Merge `hvac-harmonized.css` and `hvac-common.css`
   - Remove duplicate style definitions
### Phase 4: Clean Up Routing (Lower Priority)
1. **Simplify rewrite rules**:
   - Remove complex regex patterns
   - Use WordPress's built-in page hierarchy
   - Keep only essential redirects for backwards compatibility
2. **Update legacy redirects**:
   - Move from PHP redirects to .htaccess/nginx rules
   - Document all redirects for future removal
### Phase 5: Remove Debug Code (Final)
1. **Remove all debug statements**:
   - `error_log()` calls
   - HTML comments with DEBUG
   - Console.log statements in JS
2. **Add proper logging system**:
   - Use the existing HVAC_Logger class consistently
   - Add environment-based logging levels
## Implementation Order
1. **Fix Master Dashboard (Today)**:
   - Disable template loading for master dashboard page
   - Ensure shortcode renders correct content
   - Test with all user roles
2. **Refactor Templates (This Week)**:
   - Implement Phase 1 completely
   - Update all existing pages
   - Run comprehensive tests
3. **Clean Up (Next Week)**:
   - Implement Phases 2-5
   - Remove deprecated code
   - Update documentation
## Testing Plan
1. **Functional Tests**:
   - Login as each user role
   - Verify all dashboards show correct content
   - Test all CRUD operations
   - Verify email notifications
2. **Visual Tests**:
   - Check CSS loads on all pages
   - Verify responsive design
   - Test in multiple browsers
3. **Performance Tests**:
   - Measure page load times
   - Check database query count
   - Verify caching works correctly
## Rollback Plan
1. Keep backup of current code
2. Document all database changes
3. Test rollback procedure on staging
4. Have quick revert script ready
## Success Metrics
- Master dashboard shows correct content for master trainers
- No template-related errors in logs
- Page load time improves by 20%
- Code complexity reduced by 30%
- All existing functionality preserved
## Timeline
- Day 1: Fix master dashboard bug
- Day 2-3: Remove template system
- Day 4-5: Clean up authentication and CSS
- Week 2: Complete remaining phases
- Week 3: Documentation and training
## Notes
- This refactor aligns with WordPress best practices
- Reduces maintenance burden significantly
- Makes future features easier to implement
- Improves theme compatibility