upskill-event-manager/includes/class-hvac-shortcodes.php
ben 09a15f874c feat: complete Phase 2B template system enhancements
 Enhanced Template Selector:
- Grouped templates by category with descriptions
- Template preview modal with field data display
- Apply template functionality with AJAX loading
- Enhanced UI with preview icons and better UX

 Save as Template Functionality:
- Complete save template dialog with validation
- Template name, description, and category fields
- Public/private template sharing options
- AJAX integration with error handling and success feedback

 Progressive Disclosure:
- Advanced options toggle with smooth animations
- Fields marked as advanced (capacity, cost, timezone)
- Local storage for user preference persistence
- Staggered reveal animations for better UX

 Enhanced Auto-save:
- Intelligent auto-save with field-type specific delays
- Draft recovery with age information and user confirmation
- Error handling with fallback to essential fields only
- Visual feedback with status indicator and animations
- Auto-save on page visibility change

 AJAX Infrastructure:
- Template preview handler (hvac_get_template_preview)
- Template loading handler (hvac_load_template_data)
- Template saving handler (hvac_save_template)
- Comprehensive error handling and security validation

🎨 UI/UX Enhancements:
- Modern modal dialogs with backdrop overlays
- Responsive design for mobile devices
- Smooth animations and transitions
- Status indicators with rotating save icons
- Comprehensive styling for all new components

🚀 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-25 09:06:03 -03:00

1035 lines
No EOL
38 KiB
PHP

<?php
/**
* HVAC Shortcodes Manager
*
* Centralized management of all plugin shortcodes
*
* @package HVAC_Community_Events
* @since 2.0.0
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* HVAC_Shortcodes class
*/
class HVAC_Shortcodes {
/**
* Instance
*
* @var HVAC_Shortcodes
*/
private static $instance = null;
/**
* Registered shortcodes
*
* @var array
*/
private $shortcodes = array();
/**
* Get instance
*
* @return HVAC_Shortcodes
*/
public static function instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
$this->define_shortcodes();
$this->register_shortcodes();
$this->register_ajax_handlers();
}
/**
* Define all plugin shortcodes
*
* @return void
*/
private function define_shortcodes() {
$this->shortcodes = array(
// Dashboard shortcodes
'hvac_dashboard' => array(
'callback' => array($this, 'render_dashboard'),
'description' => 'Trainer dashboard'
),
'hvac_master_dashboard' => array(
'callback' => array($this, 'render_master_dashboard'),
'description' => 'Master trainer dashboard'
),
// Event management shortcodes
// ENABLED - Phase 2 Native HVAC Implementation
'hvac_manage_event' => array(
'callback' => array($this, 'render_manage_event'),
'description' => 'Event management hub with integrated navigation'
),
'hvac_create_event' => array(
'callback' => array($this, 'render_create_event'),
'description' => 'Create new event with HVAC Event Form Builder'
),
'hvac_edit_event' => array(
'callback' => array($this, 'render_edit_event'),
'description' => 'Edit event with HVAC Event Form Builder'
),
'hvac_event_summary' => array(
'callback' => array($this, 'render_event_summary'),
'description' => 'Event summary page'
),
// Authentication shortcodes
'hvac_community_login' => array(
'callback' => array($this, 'render_login'),
'description' => 'Community login form'
),
'hvac_trainer_registration' => array(
'callback' => array($this, 'render_registration'),
'description' => 'Trainer registration form'
),
'hvac_edit_profile' => array(
'callback' => array($this, 'render_edit_profile'),
'description' => 'Edit trainer profile form'
),
// Profile shortcodes
'hvac_trainer_profile' => array(
'callback' => array($this, 'render_trainer_profile'),
'description' => 'Trainer profile page'
),
// Certificate shortcodes
'hvac_certificate_reports' => array(
'callback' => array($this, 'render_certificate_reports'),
'description' => 'Certificate reports page'
),
'hvac_generate_certificates' => array(
'callback' => array($this, 'render_generate_certificates'),
'description' => 'Certificate generation page'
),
// Communication shortcodes
'hvac_email_attendees' => array(
'callback' => array($this, 'render_email_attendees'),
'description' => 'Email attendees interface'
),
'hvac_communication_templates' => array(
'callback' => array($this, 'render_communication_templates'),
'description' => 'Communication templates management'
),
'hvac_communication_schedules' => array(
'callback' => array($this, 'render_communication_schedules'),
'description' => 'Communication schedules management'
),
// Venue shortcodes
'hvac_trainer_venues_list' => array(
'callback' => array($this, 'render_venues_list'),
'description' => 'Trainer venues listing page'
),
'hvac_trainer_venue_manage' => array(
'callback' => array($this, 'render_venue_manage'),
'description' => 'Trainer venue management page'
),
// Organizer shortcodes - Handled by HVAC_Organizers class
'hvac_trainer_organizers_list' => array(
'callback' => array($this, 'render_organizers_list'),
'description' => 'Trainer organizers listing page'
),
'hvac_trainer_organizer_manage' => array(
'callback' => array($this, 'render_organizer_manage'),
'description' => 'Trainer organizer management page'
),
// Profile shortcodes - additional ones beyond hvac_trainer_profile
'hvac_trainer_profile_view' => array(
'callback' => array($this, 'render_trainer_profile_view'),
'description' => 'Trainer profile view page'
),
'hvac_trainer_profile_edit' => array(
'callback' => array($this, 'render_trainer_profile_edit'),
'description' => 'Trainer profile edit page'
),
// Admin shortcodes
'hvac_google_sheets' => array(
'callback' => array($this, 'render_google_sheets_admin'),
'description' => 'Google Sheets integration admin'
),
);
// Allow filtering of shortcode definitions
$this->shortcodes = apply_filters('hvac_shortcode_definitions', $this->shortcodes);
}
/**
* Register all shortcodes
*
* @return void
*/
private function register_shortcodes() {
foreach ($this->shortcodes as $tag => $config) {
add_shortcode($tag, $config['callback']);
}
// Log registration
HVAC_Logger::info('Registered ' . count($this->shortcodes) . ' shortcodes', 'Shortcodes');
}
/**
* Get registered shortcodes
*
* @return array
*/
public function get_shortcodes() {
return $this->shortcodes;
}
/**
* Check if shortcode is registered
*
* @param string $tag Shortcode tag
* @return bool
*/
public function is_registered($tag) {
return isset($this->shortcodes[$tag]);
}
// ========================================
// Shortcode Render Methods
// ========================================
/**
* Render dashboard shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_dashboard($atts = array()) {
// Add debug comment to verify this method is being called
$debug = '<!-- HVAC_Shortcodes::render_dashboard called -->';
// Use the HVAC_Community_Events instance method
if (class_exists('HVAC_Community_Events')) {
$hvac = HVAC_Community_Events::get_instance();
if (method_exists($hvac, 'render_dashboard')) {
return $debug . $hvac->render_dashboard();
}
}
// Fallback if class not available
if (!is_user_logged_in()) {
return $debug . '<p>' . __('Please log in to view the dashboard.', 'hvac-community-events') . '</p>';
}
// Include the dashboard template
ob_start();
include HVAC_PLUGIN_DIR . 'templates/template-hvac-dashboard.php';
return ob_get_clean();
}
/**
* Render master dashboard shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_master_dashboard($atts = array()) {
// Add debug comment to verify this method is being called
$debug = '<!-- HVAC_Shortcodes::render_master_dashboard called -->';
// Use the HVAC_Community_Events instance method
if (class_exists('HVAC_Community_Events')) {
$hvac = HVAC_Community_Events::get_instance();
if (method_exists($hvac, 'render_master_dashboard')) {
return $debug . $hvac->render_master_dashboard();
}
}
// Fallback if class not available
if (!is_user_logged_in()) {
return $debug . '<p>' . __('Please log in to view the master dashboard.', 'hvac-community-events') . '</p>';
}
$user = wp_get_current_user();
if (!in_array('hvac_master_trainer', $user->roles) && !current_user_can('manage_options')) {
return $debug . '<div class="hvac-error">' . __('You do not have permission to view the master dashboard. This dashboard is only available to Master Trainers and Administrators.', 'hvac-community-events') . '</div>';
}
// Include the master dashboard template
ob_start();
include HVAC_PLUGIN_DIR . 'templates/template-hvac-master-dashboard.php';
return ob_get_clean();
}
/**
* Render manage event shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_manage_event($atts = array()) {
// The manage event page uses The Events Calendar Community Events shortcode
if (!shortcode_exists('tribe_community_events')) {
return '<p>' . __('Event management requires The Events Calendar Community Events add-on.', 'hvac-community-events') . '</p>';
}
// Get event ID from URL parameter to determine if we're creating or editing
$event_id = isset($_GET['event_id']) ? intval($_GET['event_id']) : null;
if ($event_id && $event_id > 0) {
// Editing existing event - use edit_event view with event ID
// This is the proper TEC way to edit events with full field population
return do_shortcode('[tribe_community_events view="edit_event" id="' . $event_id . '"]');
} else {
// Creating new event - use submission_form view
return do_shortcode('[tribe_community_events view="submission_form"]');
}
}
/**
* Render create event shortcode using native HVAC Event Form Builder
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_create_event($atts = array()) {
// Check permissions
if (!is_user_logged_in()) {
return '<div class="hvac-error-message"><p>' . __('Please log in to create events.', 'hvac-community-events') . '</p></div>';
}
$user = wp_get_current_user();
if (!array_intersect(['hvac_trainer', 'hvac_master_trainer'], $user->roles)) {
return '<div class="hvac-error-message"><p>' . __('You must be a trainer to create events.', 'hvac-community-events') . '</p></div>';
}
// Parse shortcode attributes
$atts = shortcode_atts(array(
'include_templates' => 'true',
'template_categories' => 'general,training,workshop,certification',
'show_navigation' => 'true'
), $atts);
// Start output buffering
ob_start();
try {
// Initialize HVAC Event Form Builder
if (class_exists('HVAC_Event_Form_Builder')) {
$form_builder = new HVAC_Event_Form_Builder('hvac_event_form', $atts['include_templates'] === 'true');
$template_categories = explode(',', $atts['template_categories']);
$template_categories = array_map('trim', $template_categories);
$form_builder->create_event_form([
'include_template_selector' => ($atts['include_templates'] === 'true'),
'include_venue_fields' => true,
'include_organizer_fields' => true,
'include_cost_fields' => true,
'include_capacity_fields' => true,
'include_datetime_fields' => true,
'template_categories' => $template_categories
]);
?>
<div class="hvac-shortcode-create-event">
<?php if ($atts['show_navigation'] === 'true'): ?>
<div class="hvac-navigation-header">
<?php
// Display trainer navigation menu
if (class_exists('HVAC_Menu_System')) {
echo '<div class="hvac-navigation-wrapper">';
HVAC_Menu_System::instance()->render_trainer_menu();
echo '</div>';
}
// Display breadcrumbs
if (class_exists('HVAC_Breadcrumbs')) {
echo '<div class="hvac-breadcrumbs-wrapper">';
HVAC_Breadcrumbs::instance()->render();
echo '</div>';
}
?>
</div>
<?php endif; ?>
<div class="hvac-form-header">
<h2>Create New Training Event</h2>
<div class="hvac-form-notice">
<p><strong>✓ Phase 2 Native Implementation</strong> - Using HVAC Event Form Builder with template system integration</p>
</div>
</div>
<div class="hvac-form-container">
<?php
// Display any success/error messages
if (isset($_GET['success'])) {
echo '<div class="hvac-success-message">Event created successfully!</div>';
}
if (isset($_GET['error'])) {
echo '<div class="hvac-error-message">Error creating event. Please try again.</div>';
}
// Render the form
echo $form_builder->render();
?>
</div>
</div>
<style>
.hvac-shortcode-create-event {
max-width: 100%;
margin: 0;
}
.hvac-form-header {
margin-bottom: 30px;
}
.hvac-form-header h2 {
color: #0073aa;
font-size: 24px;
margin-bottom: 10px;
}
.hvac-form-notice {
background: #e8f5e8;
border: 1px solid #4CAF50;
border-radius: 4px;
padding: 12px;
margin-bottom: 20px;
}
.hvac-form-notice p {
margin: 0;
color: #2e7d2e;
}
.hvac-form-container {
background: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.hvac-success-message {
background: #d4edda;
border: 1px solid #c3e6cb;
border-radius: 4px;
padding: 15px;
margin-bottom: 20px;
color: #155724;
}
.hvac-error-message {
background: #f8d7da;
border: 1px solid #f5c6cb;
border-radius: 4px;
padding: 15px;
margin-bottom: 20px;
color: #721c24;
}
.hvac-navigation-header {
margin-bottom: 20px;
}
</style>
<script>
jQuery(document).ready(function($) {
// Form enhancement script for shortcode usage
console.log('[HVAC Create Event Shortcode] Form loaded with native implementation');
// Template selector functionality
$('#hvac-template-selector').on('change', function() {
const templateId = $(this).val();
if (templateId) {
$.ajax({
url: ajaxurl || '/wp-admin/admin-ajax.php',
type: 'POST',
data: {
action: 'hvac_load_template_data',
template_id: templateId,
nonce: $('[name="hvac_event_form_nonce"]').val()
},
success: function(response) {
if (response.success && response.data) {
Object.keys(response.data).forEach(function(field) {
const $field = $('[name="' + field + '"]');
if ($field.length) {
$field.val(response.data[field]);
}
});
}
},
error: function() {
console.log('Error loading template data');
}
});
}
});
});
</script>
<?php
} else {
?>
<div class="hvac-error-message">
<p><strong>Form Builder Not Available</strong></p>
<p>The HVAC Event Form Builder class is not loaded. Please ensure the plugin is properly activated.</p>
</div>
<?php
}
} catch (Exception $e) {
?>
<div class="hvac-error-message">
<p><strong>Form Loading Error</strong></p>
<p>There was an error loading the event creation form: <?php echo esc_html($e->getMessage()); ?></p>
</div>
<?php
}
return ob_get_clean();
}
// NOTE: render_edit_event method removed - handled by HVAC_Edit_Event_Shortcode class
/**
* Render event summary shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_event_summary($atts = array()) {
if (!class_exists('HVAC_Event_Summary')) {
return '<p>' . __('Event summary functionality not available.', 'hvac-community-events') . '</p>';
}
$event_summary = new HVAC_Event_Summary();
return $event_summary->render_summary($atts);
}
/**
* Render login shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_login($atts = array()) {
if (!class_exists('\\HVAC_Community_Events\\Community\\Login_Handler')) {
return '<p>' . __('Login functionality not available.', 'hvac-community-events') . '</p>';
}
$login_handler = new \HVAC_Community_Events\Community\Login_Handler();
return $login_handler->render_login_form($atts);
}
/**
* Render registration shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_registration($atts = array()) {
// Dependencies are loaded during plugin initialization - no need for conditional require_once
if (!class_exists('HVAC_Registration')) {
return '<p>' . __('Registration functionality not available.', 'hvac-community-events') . '</p>';
}
$registration = new HVAC_Registration();
return $registration->render_registration_form($atts);
}
/**
* Render edit profile shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_edit_profile($atts = array()) {
if (!class_exists('HVAC_Registration')) {
return '<p>' . __('Profile editing functionality not available.', 'hvac-community-events') . '</p>';
}
$registration = new HVAC_Registration();
return $registration->render_edit_profile_form($atts);
}
/**
* Render trainer profile shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_trainer_profile($atts = array()) {
if (!class_exists('HVAC_Trainer_Profile')) {
return '<p>' . __('Profile functionality not available.', 'hvac-community-events') . '</p>';
}
$trainer_profile = new HVAC_Trainer_Profile();
return $trainer_profile->render_profile($atts);
}
/**
* Render certificate reports shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_certificate_reports($atts = array()) {
// Check permissions
if (!is_user_logged_in()) {
return '<p>' . __('Please log in to view certificate reports.', 'hvac-community-events') . '</p>';
}
// Allow trainers, master trainers, or WordPress admins
if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('manage_options')) {
return '<p>' . __('You must be a trainer to access this page.', 'hvac-community-events') . '</p>';
}
// Use output buffering to capture template output properly
ob_start();
// Set flag to prevent template from echoing directly
define('HVAC_SHORTCODE_CONTEXT', true);
include HVAC_PLUGIN_DIR . 'templates/certificates/certificate-reports-content.php';
$content = ob_get_clean();
// Return the content for embedding in the WordPress template
return $content;
}
/**
* Render generate certificates shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_generate_certificates($atts = array()) {
// Check permissions
if (!is_user_logged_in()) {
return '<p>' . __('Please log in to generate certificates.', 'hvac-community-events') . '</p>';
}
// Allow trainers, master trainers, or WordPress admins
if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('manage_options')) {
return '<p>' . __('You must be a trainer to access this page.', 'hvac-community-events') . '</p>';
}
// Include the generate certificates content template
ob_start();
include HVAC_PLUGIN_DIR . 'templates/certificates/generate-certificates-content.php';
return ob_get_clean();
}
/**
* Render email attendees shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_email_attendees($atts = array()) {
// Check if user has appropriate permissions
// Allow trainers, master trainers, or WordPress admins
if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('manage_options')) {
return '<p>' . __('You must be a trainer to access this page.', 'hvac-community-events') . '</p>';
}
ob_start();
include HVAC_PLUGIN_DIR . 'templates/email-attendees.php';
return ob_get_clean();
}
/**
* Render communication templates shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_communication_templates($atts = array()) {
if (!class_exists('HVAC_Trainer_Communication_Templates')) {
return '<p>' . __('Communication templates functionality not available.', 'hvac-community-events') . '</p>';
}
// Use the new trainer communication templates class for read-only access
return HVAC_Trainer_Communication_Templates::instance()->render_templates_interface();
}
/**
* Render communication schedules shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_communication_schedules($atts = array()) {
if (!class_exists('HVAC_Communication_Scheduler')) {
return '<p>' . __('Communication scheduler functionality not available.', 'hvac-community-events') . '</p>';
}
// Check permissions
if (!current_user_can('edit_tribe_events') && !current_user_can('manage_options')) {
return '<p>' . __('You do not have permission to access this page.', 'hvac-community-events') . '</p>';
}
$scheduler = hvac_communication_scheduler();
ob_start();
$scheduler->render_schedules_page();
return ob_get_clean();
}
/**
* Render Google Sheets admin shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_google_sheets_admin($atts = array()) {
if (!class_exists('HVAC_Google_Sheets_Admin')) {
return '<p>' . __('Google Sheets functionality not available.', 'hvac-community-events') . '</p>';
}
// Check permissions
if (!current_user_can('manage_options') && !current_user_can('view_all_trainer_data')) {
return '<p>' . __('You do not have permission to access this page.', 'hvac-community-events') . '</p>';
}
$google_sheets = new HVAC_Google_Sheets_Admin();
ob_start();
$google_sheets->render_admin_page();
return ob_get_clean();
}
/**
* Render venues list shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_venues_list($atts = array()) {
// Check permissions
if (!is_user_logged_in()) {
return '<p>' . __('Please log in to view venues.', 'hvac-community-events') . '</p>';
}
// Allow trainers, master trainers, or WordPress admins
if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('manage_options')) {
return '<p>' . __('You must be a trainer to access this page.', 'hvac-community-events') . '</p>';
}
if (!class_exists('HVAC_Venues')) {
return '<p>' . __('Venues functionality not available.', 'hvac-community-events') . '</p>';
}
return HVAC_Venues::instance()->render_venues_list($atts);
}
/**
* Render venue manage shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_venue_manage($atts = array()) {
// Check permissions
if (!is_user_logged_in()) {
return '<p>' . __('Please log in to manage venues.', 'hvac-community-events') . '</p>';
}
// Allow trainers, master trainers, or WordPress admins
if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('manage_options')) {
return '<p>' . __('You must be a trainer to access this page.', 'hvac-community-events') . '</p>';
}
if (!class_exists('HVAC_Venues')) {
return '<p>' . __('Venues functionality not available.', 'hvac-community-events') . '</p>';
}
return HVAC_Venues::instance()->render_venue_manage($atts);
}
/**
* Render organizers list shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_organizers_list($atts = array()) {
// Check permissions
if (!is_user_logged_in()) {
return '<p>' . __('Please log in to view organizers.', 'hvac-community-events') . '</p>';
}
// Allow trainers, master trainers, or WordPress admins
if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('manage_options')) {
return '<p>' . __('You must be a trainer to access this page.', 'hvac-community-events') . '</p>';
}
if (!class_exists('HVAC_Organizers')) {
return '<p>' . __('Organizers functionality not available.', 'hvac-community-events') . '</p>';
}
$organizers = HVAC_Organizers::instance();
return $organizers->render_organizers_list($atts);
}
/**
* Render organizer manage shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_organizer_manage($atts = array()) {
// Check permissions
if (!is_user_logged_in()) {
return '<p>' . __('Please log in to manage organizers.', 'hvac-community-events') . '</p>';
}
// Allow trainers, master trainers, or WordPress admins
if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('manage_options')) {
return '<p>' . __('You must be a trainer to access this page.', 'hvac-community-events') . '</p>';
}
if (!class_exists('HVAC_Organizers')) {
return '<p>' . __('Organizers functionality not available.', 'hvac-community-events') . '</p>';
}
$organizers = HVAC_Organizers::instance();
return $organizers->render_organizer_manage($atts);
}
/**
* Render trainer profile view shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_trainer_profile_view($atts = array()) {
// Check permissions
if (!is_user_logged_in()) {
return '<p>' . __('Please log in to view your profile.', 'hvac-community-events') . '</p>';
}
// Allow trainers, master trainers, or WordPress admins
if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('manage_options')) {
return '<p>' . __('You must be a trainer to access this page.', 'hvac-community-events') . '</p>';
}
if (!class_exists('HVAC_Trainer_Profile_Manager')) {
return '<p>' . __('Profile functionality not available.', 'hvac-community-events') . '</p>';
}
$profile_manager = HVAC_Trainer_Profile_Manager::get_instance();
return $profile_manager->render_profile_view($atts);
}
/**
* Render trainer profile edit shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
public function render_trainer_profile_edit($atts = array()) {
// Check permissions
if (!is_user_logged_in()) {
return '<p>' . __('Please log in to edit your profile.', 'hvac-community-events') . '</p>';
}
// Allow trainers, master trainers, or WordPress admins
if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('manage_options')) {
return '<p>' . __('You must be a trainer to access this page.', 'hvac-community-events') . '</p>';
}
if (!class_exists('HVAC_Trainer_Profile_Manager')) {
return '<p>' . __('Profile functionality not available.', 'hvac-community-events') . '</p>';
}
$profile_manager = HVAC_Trainer_Profile_Manager::get_instance();
return $profile_manager->render_profile_edit($atts);
}
/**
* Register AJAX handlers
*/
private function register_ajax_handlers() {
// Template preview AJAX handler
add_action('wp_ajax_hvac_get_template_preview', array($this, 'ajax_get_template_preview'));
add_action('wp_ajax_nopriv_hvac_get_template_preview', array($this, 'ajax_get_template_preview'));
// Template loading AJAX handler
add_action('wp_ajax_hvac_load_template_data', array($this, 'ajax_load_template_data'));
add_action('wp_ajax_nopriv_hvac_load_template_data', array($this, 'ajax_load_template_data'));
// Template saving AJAX handler
add_action('wp_ajax_hvac_save_template', array($this, 'ajax_save_template'));
add_action('wp_ajax_nopriv_hvac_save_template', array($this, 'ajax_save_template'));
}
/**
* AJAX handler for getting template preview data
*/
public function ajax_get_template_preview() {
// Verify nonce
if (!wp_verify_nonce($_POST['nonce'] ?? '', 'hvac_event_form_nonce')) {
wp_send_json_error('Security check failed');
return;
}
$template_id = sanitize_text_field($_POST['template_id'] ?? '');
if (empty($template_id)) {
wp_send_json_error('No template ID provided');
return;
}
// Get template manager instance
if (!class_exists('HVAC_Event_Template_Manager')) {
wp_send_json_error('Template manager not available');
return;
}
$template_manager = HVAC_Event_Template_Manager::instance();
$template = $template_manager->get_template($template_id);
if (!$template) {
wp_send_json_error('Template not found');
return;
}
// Return template data
wp_send_json_success($template);
}
/**
* AJAX handler for loading template data into form
*/
public function ajax_load_template_data() {
// Verify nonce
if (!wp_verify_nonce($_POST['nonce'] ?? '', 'hvac_event_form_nonce')) {
wp_send_json_error('Security check failed');
return;
}
$template_id = sanitize_text_field($_POST['template_id'] ?? '');
if (empty($template_id)) {
wp_send_json_error('No template ID provided');
return;
}
// Get template manager instance
if (!class_exists('HVAC_Event_Template_Manager')) {
wp_send_json_error('Template manager not available');
return;
}
$template_manager = HVAC_Event_Template_Manager::instance();
$template = $template_manager->get_template($template_id);
if (!$template || empty($template['field_data'])) {
wp_send_json_error('Template data not found');
return;
}
// Return field data
wp_send_json_success($template['field_data']);
}
/**
* AJAX handler for saving new template
*/
public function ajax_save_template() {
// Verify nonce
if (!wp_verify_nonce($_POST['nonce'] ?? '', 'hvac_event_form_nonce')) {
wp_send_json_error('Security check failed');
return;
}
// Check user permissions
if (!is_user_logged_in()) {
wp_send_json_error('Authentication required');
return;
}
$user = wp_get_current_user();
if (!array_intersect(['hvac_trainer', 'hvac_master_trainer'], $user->roles)) {
wp_send_json_error('Insufficient permissions');
return;
}
// Validate and sanitize input
$template_name = sanitize_text_field($_POST['template_name'] ?? '');
$template_description = sanitize_textarea_field($_POST['template_description'] ?? '');
$template_category = sanitize_text_field($_POST['template_category'] ?? '');
$is_public = (bool) ($_POST['is_public'] ?? false);
$field_data = $_POST['field_data'] ?? [];
if (empty($template_name)) {
wp_send_json_error('Template name is required');
return;
}
if (empty($template_category)) {
wp_send_json_error('Template category is required');
return;
}
// Sanitize field data
$sanitized_field_data = [];
foreach ($field_data as $key => $value) {
$sanitized_key = sanitize_key($key);
if (is_array($value)) {
$sanitized_field_data[$sanitized_key] = array_map('sanitize_text_field', $value);
} else {
$sanitized_field_data[$sanitized_key] = sanitize_text_field($value);
}
}
// Get template manager instance
if (!class_exists('HVAC_Event_Template_Manager')) {
wp_send_json_error('Template manager not available');
return;
}
$template_manager = HVAC_Event_Template_Manager::instance();
// Prepare template data
$template_data = [
'name' => $template_name,
'description' => $template_description,
'category' => $template_category,
'is_public' => $is_public,
'field_data' => $sanitized_field_data,
'created_by' => $user->ID,
'meta_data' => [
'source' => 'event_form',
'created_at' => current_time('mysql'),
'user_ip' => $_SERVER['REMOTE_ADDR'] ?? '',
]
];
// Create the template
$result = $template_manager->create_template($template_data);
if ($result['success']) {
wp_send_json_success([
'message' => 'Template saved successfully',
'template_id' => $result['template_id'] ?? null
]);
} else {
wp_send_json_error($result['error'] ?? 'Failed to save template');
}
}
}