upskill-event-manager/includes/class-hvac-event-form-builder.php
ben 63d7f5efa3 fix: implement code review fixes for form builder
Critical fixes:
- Implement get_current_form_data() method for template saving functionality
- Add sanitize_field_value() method with comprehensive field sanitization

High priority fixes:
- Add pagination limits (100) to venue and organizer queries to prevent performance issues
- Add capability checks to AJAX template handler for proper access control

Medium priority fixes:
- Add comprehensive documentation for hvac_event_form_after_basic_fields integration hook
- Add debug logging for cache initialization failures when WP_DEBUG is enabled

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

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

1342 lines
No EOL
47 KiB
PHP

<?php
declare(strict_types=1);
/**
* HVAC Event Form Builder
*
* Extended form builder with event-specific functionality and template integration
* Extends the base HVAC_Form_Builder with datetime, venue, organizer fields
* and comprehensive template support for Phase 2A implementation
*
* @package HVAC_Community_Events
* @subpackage Includes
* @since 3.0.0 (Phase 1) / 3.1.0 (Phase 2A Template Integration)
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Class HVAC_Event_Form_Builder
*
* Event-specific form builder with template integration capabilities
*/
class HVAC_Event_Form_Builder extends HVAC_Form_Builder {
/**
* Template manager instance
*
* @var HVAC_Event_Template_Manager
*/
private HVAC_Event_Template_Manager $template_manager;
/**
* Current selected template
*
* @var array|null
*/
private ?array $current_template = null;
/**
* Template integration mode
*
* @var bool
*/
private bool $template_mode_enabled = false;
/**
* Event-specific field defaults
*
* @var array
*/
private array $event_field_defaults = [
'datetime-local' => [
'type' => 'datetime-local',
'class' => 'hvac-datetime-field',
'validate' => ['datetime'],
'sanitize' => 'datetime',
],
'event-title' => [
'type' => 'text',
'class' => 'hvac-event-title',
'validate' => ['min_length' => 3, 'max_length' => 200],
'sanitize' => 'text',
],
'event-description' => [
'type' => 'textarea',
'class' => 'hvac-event-description',
'validate' => ['max_length' => 2000],
'sanitize' => 'textarea',
],
'venue-select' => [
'type' => 'select',
'class' => 'hvac-venue-select',
'options' => [],
'validate' => [],
'sanitize' => 'int',
],
'organizer-select' => [
'type' => 'select',
'class' => 'hvac-organizer-select',
'options' => [],
'validate' => [],
'sanitize' => 'int',
],
'capacity' => [
'type' => 'number',
'class' => 'hvac-capacity-field',
'validate' => ['min_value' => 1, 'max_value' => 10000],
'sanitize' => 'int',
],
'cost' => [
'type' => 'number',
'step' => '0.01',
'class' => 'hvac-cost-field',
'validate' => ['min_value' => 0],
'sanitize' => 'float',
],
'template-selector' => [
'type' => 'template-select',
'class' => 'hvac-template-selector',
'options' => [],
'validate' => [],
'sanitize' => 'text',
],
];
/**
* Cache instance for performance optimization (optional)
*
* @var mixed
*/
private $cache = null;
/**
* Constructor
*
* @param string $nonce_action Nonce action for form security
* @param bool $enable_templates Whether to enable template functionality
*/
public function __construct(string $nonce_action, bool $enable_templates = true) {
parent::__construct($nonce_action);
$this->template_manager = HVAC_Event_Template_Manager::instance();
$this->template_mode_enabled = $enable_templates;
// Initialize cache if available
$this->cache = class_exists('HVAC_Event_Cache') ? HVAC_Event_Cache::instance() : null;
if (!$this->cache && defined('WP_DEBUG') && WP_DEBUG) {
error_log('HVAC Event Cache unavailable - performance may be impacted');
}
$this->init_event_form_hooks();
}
/**
* Initialize event form specific hooks
*/
private function init_event_form_hooks(): void {
if ($this->template_mode_enabled) {
add_action('wp_enqueue_scripts', [$this, 'enqueue_template_assets']);
add_action('wp_ajax_hvac_load_template_data', [$this, 'ajax_load_template_data']);
add_action('wp_ajax_hvac_save_as_template', [$this, 'ajax_save_as_template']);
}
}
/**
* Create complete event form with template integration
*
* @param array $config Form configuration options
* @return self
*/
public function create_event_form(array $config = []): self {
$defaults = [
'include_template_selector' => $this->template_mode_enabled,
'include_venue_fields' => true,
'include_organizer_fields' => true,
'include_cost_fields' => true,
'include_capacity_fields' => true,
'include_datetime_fields' => true,
'template_categories' => ['general', 'training', 'workshop'],
];
$config = array_merge($defaults, $config);
// Add template selector first if enabled
if ($config['include_template_selector']) {
$this->add_template_selector($config['template_categories']);
}
// Basic event fields
$this->add_basic_event_fields();
/**
* Action hook for TEC ticketing integration
*
* Allows other components to add fields after basic event fields
* are rendered but before optional field groups like datetime fields.
*
* @param HVAC_Event_Form_Builder $form_builder Current form instance
* @since 3.2.0 (Phase 2B - TEC Integration)
*/
do_action('hvac_event_form_after_basic_fields', $this);
// Optional field groups
if ($config['include_datetime_fields']) {
$this->add_datetime_fields();
}
if ($config['include_venue_fields']) {
$this->add_venue_fields();
}
if ($config['include_organizer_fields']) {
$this->add_organizer_fields();
}
if ($config['include_capacity_fields']) {
$this->add_capacity_field();
}
if ($config['include_cost_fields']) {
$this->add_cost_fields();
}
// Add progressive disclosure toggle
$this->add_progressive_disclosure();
// Mark certain fields as advanced
$this->mark_field_as_advanced('event_capacity')
->mark_field_as_advanced('event_cost')
->mark_field_as_advanced('event_timezone');
// Template actions if enabled
if ($this->template_mode_enabled) {
$this->add_template_actions();
// Mark template actions as advanced
$this->mark_field_as_advanced('save_as_template');
}
return $this;
}
/**
* Add template selector field
*
* @param array $categories Template categories to include
*/
public function add_template_selector(array $categories = []): self {
if (!$this->template_mode_enabled) {
return $this;
}
// Get available templates
$filters = [];
if (!empty($categories)) {
$templates = [];
foreach ($categories as $category) {
$category_templates = $this->template_manager->get_templates(['category' => $category]);
$templates = array_merge($templates, $category_templates);
}
} else {
$templates = $this->template_manager->get_templates();
}
// Group templates by category for enhanced UI
$templates_by_category = [];
foreach ($templates as $template) {
$category = $template['category'] ?? 'general';
if (!isset($templates_by_category[$category])) {
$templates_by_category[$category] = [];
}
$templates_by_category[$category][] = $template;
}
// Prepare enhanced template options with categories
$template_options = ['0' => '-- Select a Template --'];
foreach ($templates_by_category as $category => $category_templates) {
$template_options['optgroup_' . $category] = [
'label' => ucfirst($category) . ' Templates',
'options' => []
];
foreach ($category_templates as $template) {
$template_options['optgroup_' . $category]['options'][$template['id']] =
esc_html($template['name']) .
(!empty($template['description']) ? ' - ' . wp_trim_words($template['description'], 8) : '');
}
}
// Create accordion-style template selector
$accordion_template_field = [
'type' => 'custom',
'name' => 'accordion_template_selector',
'custom_html' => $this->render_accordion_template_selector($template_options, $templates),
'wrapper_class' => 'form-row template-selector-row',
];
$this->add_field($accordion_template_field);
// Add template preview area
$preview_field = [
'type' => 'custom',
'name' => 'template_preview',
'custom_html' => $this->render_template_preview_area(),
'wrapper_class' => 'form-row template-preview-row',
];
$this->add_field($preview_field);
return $this;
}
/**
* Add basic event fields
*/
public function add_basic_event_fields(): self {
// Event title
$title_field = array_merge($this->event_field_defaults['event-title'], [
'name' => 'event_title',
'label' => 'Event Title',
'placeholder' => 'Enter event title...',
'required' => true,
]);
// Event description
$description_field = array_merge($this->event_field_defaults['event-description'], [
'name' => 'event_description',
'label' => 'Event Description',
'placeholder' => 'Describe your event...',
'rows' => 6,
]);
$this->add_field($title_field);
$this->add_field($description_field);
return $this;
}
/**
* Add datetime fields for event scheduling
*/
public function add_datetime_fields(): self {
// Start date/time
$start_datetime_field = array_merge($this->event_field_defaults['datetime-local'], [
'name' => 'event_start_datetime',
'label' => 'Start Date & Time',
'required' => true,
'wrapper_class' => 'form-row datetime-row start-datetime',
]);
// End date/time
$end_datetime_field = array_merge($this->event_field_defaults['datetime-local'], [
'name' => 'event_end_datetime',
'label' => 'End Date & Time',
'required' => true,
'wrapper_class' => 'form-row datetime-row end-datetime',
]);
// Timezone
$timezone_field = [
'type' => 'select',
'name' => 'event_timezone',
'label' => 'Timezone',
'options' => $this->get_timezone_options(),
'value' => wp_timezone_string(),
'class' => 'hvac-timezone-select',
'wrapper_class' => 'form-row timezone-row',
];
$this->add_field($start_datetime_field);
$this->add_field($end_datetime_field);
$this->add_field($timezone_field);
return $this;
}
/**
* Add venue selection and management fields
*/
public function add_venue_fields(): self {
// Get venue options with caching
$venue_options = $this->get_venue_options();
$venue_field = array_merge($this->event_field_defaults['venue-select'], [
'name' => 'event_venue',
'label' => 'Venue',
'options' => $venue_options,
'description' => 'Select an existing venue or create a new one',
'wrapper_class' => 'form-row venue-row',
]);
$this->add_field($venue_field);
// Add venue creation fields (initially hidden)
$this->add_venue_creation_fields();
return $this;
}
/**
* Add organizer selection and management fields
*/
public function add_organizer_fields(): self {
// Get organizer options with caching
$organizer_options = $this->get_organizer_options();
$organizer_field = array_merge($this->event_field_defaults['organizer-select'], [
'name' => 'event_organizer',
'label' => 'Organizer',
'options' => $organizer_options,
'description' => 'Select an existing organizer or create a new one',
'wrapper_class' => 'form-row organizer-row',
]);
$this->add_field($organizer_field);
// Add organizer creation fields (initially hidden)
$this->add_organizer_creation_fields();
return $this;
}
/**
* Add capacity field
*/
public function add_capacity_field(): self {
$capacity_field = array_merge($this->event_field_defaults['capacity'], [
'name' => 'event_capacity',
'label' => 'Capacity',
'placeholder' => 'Maximum attendees',
'min' => 1,
'max' => 10000,
'wrapper_class' => 'form-row capacity-row',
]);
$this->add_field($capacity_field);
return $this;
}
/**
* Add cost-related fields
*/
public function add_cost_fields(): self {
$cost_field = array_merge($this->event_field_defaults['cost'], [
'name' => 'event_cost',
'label' => 'Event Cost ($)',
'placeholder' => '0.00',
'min' => 0,
'wrapper_class' => 'form-row cost-row',
]);
$this->add_field($cost_field);
return $this;
}
/**
* Add template action buttons
*/
public function add_template_actions(): self {
if (!$this->template_mode_enabled) {
return $this;
}
// Save as template button
$save_template_field = [
'type' => 'button',
'name' => 'save_as_template',
'label' => '',
'value' => 'Save as Template',
'class' => 'button button-secondary hvac-save-template',
'wrapper_class' => 'form-row template-actions',
'onclick' => 'hvacShowSaveTemplateDialog(event)',
];
$this->add_field($save_template_field);
// Add save template dialog
$save_dialog_field = [
'type' => 'custom',
'name' => 'save_template_dialog',
'custom_html' => $this->render_save_template_dialog(),
'wrapper_class' => 'form-row template-dialog-row',
];
$this->add_field($save_dialog_field);
return $this;
}
/**
* Add progressive disclosure section
*/
public function add_progressive_disclosure(): self {
// Advanced options toggle
$advanced_toggle_field = [
'type' => 'custom',
'name' => 'advanced_options_toggle',
'custom_html' => $this->render_advanced_options_toggle(),
'wrapper_class' => 'form-row advanced-toggle-row',
];
$this->add_field($advanced_toggle_field);
return $this;
}
/**
* Mark field as advanced (for progressive disclosure)
*/
public function mark_field_as_advanced(string $field_name): self {
foreach ($this->fields as &$field) {
if ($field['name'] === $field_name) {
$field['wrapper_class'] = ($field['wrapper_class'] ?? '') . ' advanced-field';
$field['data_attributes'] = array_merge($field['data_attributes'] ?? [], [
'advanced' => 'true'
]);
break;
}
}
return $this;
}
/**
* Render advanced options toggle button
*/
private function render_advanced_options_toggle(): string {
$html = '<div class="hvac-advanced-options-toggle">';
$html .= '<button type="button" class="toggle-advanced-options" onclick="hvacToggleAdvancedOptions()">';
$html .= '<span class="dashicons dashicons-arrow-down-alt2 toggle-icon"></span>';
$html .= '<span class="toggle-text">Show Advanced Options</span>';
$html .= '</button>';
$html .= '<small class="toggle-description">Additional settings for power users</small>';
$html .= '</div>';
return $html;
}
/**
* Load template data into form
*
* @param string $template_id Template ID to load
* @return bool Success status
*/
public function load_template(string $template_id): bool {
if (!$this->template_mode_enabled) {
return false;
}
$template = $this->template_manager->get_template($template_id);
if (!$template) {
return false;
}
$this->current_template = $template;
// Set form data from template
if (!empty($template['field_data'])) {
$this->set_data($template['field_data']);
}
// Apply template-specific validation rules
if (!empty($template['validation_rules'])) {
$this->apply_template_validation_rules($template['validation_rules']);
}
return true;
}
/**
* Save current form configuration as template
*
* @param array $template_config Template configuration
* @return array Result with success status
*/
public function save_as_template(array $template_config): array {
if (!$this->template_mode_enabled) {
return [
'success' => false,
'error' => __('Template functionality is not enabled', 'hvac-community-events')
];
}
// Get current form data
$current_data = $this->get_current_form_data();
// Prepare template data
$template_data = [
'name' => sanitize_text_field($template_config['name']),
'description' => sanitize_textarea_field($template_config['description']),
'category' => sanitize_text_field($template_config['category']),
'is_public' => (bool) ($template_config['is_public'] ?? false),
'field_data' => $current_data,
'meta_data' => $template_config['meta_data'] ?? [],
];
return $this->template_manager->create_template($template_data);
}
/**
* Get current form data from fields
*
* @return array Current form data
*/
private function get_current_form_data(): array {
$data = [];
foreach ($this->fields as $field) {
if (isset($_POST[$field['name']])) {
$data[$field['name']] = $this->sanitize_field_value($field, $_POST[$field['name']]);
}
}
return $data;
}
/**
* Sanitize field value based on field type
*
* @param array $field Field configuration
* @param mixed $value Field value to sanitize
* @return mixed Sanitized value
*/
private function sanitize_field_value(array $field, $value) {
$sanitize_type = $field['sanitize'] ?? 'text';
return match($sanitize_type) {
'text' => sanitize_text_field($value),
'textarea' => sanitize_textarea_field($value),
'int' => absint($value),
'float' => floatval($value),
'datetime' => sanitize_text_field($value),
'url' => esc_url_raw($value),
'email' => sanitize_email($value),
default => sanitize_text_field($value)
};
}
/**
* Apply template validation rules to form fields
*
* @param array $validation_rules Template validation rules
*/
private function apply_template_validation_rules(array $validation_rules): void {
// Apply additional validation rules from template
foreach ($validation_rules as $field_name => $rules) {
// Find field and update validation rules
foreach ($this->fields as &$field) {
if ($field['name'] === $field_name) {
$field['validate'] = array_merge($field['validate'], $rules);
break;
}
}
}
}
/**
* Render accordion-style template selector
*
* @param array $template_options Template options array
* @param array $templates Raw templates data
* @return string Accordion HTML
*/
private function render_accordion_template_selector(array $template_options, array $templates): string {
$html = '<div class="hvac-template-selector">';
$html .= '<div class="hvac-template-selector-header">';
$html .= '<h4>Use Template</h4>';
$html .= '<span class="hvac-template-selector-toggle">▼</span>';
$html .= '</div>';
$html .= '<div class="hvac-template-selector-content">';
// Build select options HTML
$html .= '<select name="event_template" class="hvac-template-select" data-templates="' . esc_attr(json_encode($templates)) . '">';
foreach ($template_options as $value => $label) {
if (strpos((string)$value, 'optgroup_') === 0) {
// This is an optgroup
$html .= '<optgroup label="' . esc_attr($label['label']) . '">';
foreach ($label['options'] as $opt_value => $opt_label) {
$html .= '<option value="' . esc_attr($opt_value) . '">' . esc_html($opt_label) . '</option>';
}
$html .= '</optgroup>';
} else {
// Regular option
$html .= '<option value="' . esc_attr($value) . '">' . esc_html($label) . '</option>';
}
}
$html .= '</select>';
$html .= '<p class="description">Select a template to pre-fill form fields with common settings.</p>';
$html .= '</div>';
$html .= '</div>';
return $html;
}
/**
* Render template preview area
*
* @return string Preview HTML
*/
private function render_template_preview_area(): string {
$html = '<div class="hvac-template-preview" id="hvac-template-preview" style="display: none;">';
$html .= '<div class="preview-header">';
$html .= '<h4><span class="dashicons dashicons-visibility"></span> Template Preview</h4>';
$html .= '<button type="button" class="preview-close" onclick="hvacCloseTemplatePreview()">&times;</button>';
$html .= '</div>';
$html .= '<div class="preview-content">';
$html .= '<div class="preview-info">';
$html .= '<p class="template-name"><strong></strong></p>';
$html .= '<p class="template-description"></p>';
$html .= '<p class="template-category">Category: <span></span></p>';
$html .= '</div>';
$html .= '<div class="preview-fields">';
$html .= '<h5>Pre-filled Fields:</h5>';
$html .= '<ul class="field-list"></ul>';
$html .= '</div>';
$html .= '<div class="preview-actions">';
$html .= '<button type="button" class="button button-primary" onclick="hvacApplyTemplate()">Apply This Template</button>';
$html .= '<button type="button" class="button" onclick="hvacCloseTemplatePreview()">Cancel</button>';
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
return $html;
}
/**
* Render save template dialog
*
* @return string Dialog HTML
*/
private function render_save_template_dialog(): string {
$html = '<div class="hvac-save-template-dialog" id="hvac-save-template-dialog" style="display: none;">';
$html .= '<div class="dialog-header">';
$html .= '<h4><span class="dashicons dashicons-saved"></span> Save as Template</h4>';
$html .= '<button type="button" class="dialog-close" onclick="hvacCloseSaveDialog()">&times;</button>';
$html .= '</div>';
$html .= '<div class="dialog-content">';
// Template name field
$html .= '<div class="form-group">';
$html .= '<label for="template-name"><strong>Template Name *</strong></label>';
$html .= '<input type="text" id="template-name" name="template_name" required maxlength="100" ';
$html .= 'placeholder="Enter template name..." class="template-input">';
$html .= '</div>';
// Template description field
$html .= '<div class="form-group">';
$html .= '<label for="template-description"><strong>Description</strong></label>';
$html .= '<textarea id="template-description" name="template_description" rows="3" maxlength="500" ';
$html .= 'placeholder="Brief description of this template..." class="template-input"></textarea>';
$html .= '</div>';
// Template category field
$html .= '<div class="form-group">';
$html .= '<label for="template-category"><strong>Category *</strong></label>';
$html .= '<select id="template-category" name="template_category" required class="template-input">';
$html .= '<option value="">Select Category...</option>';
$html .= '<option value="general">General</option>';
$html .= '<option value="training">Training</option>';
$html .= '<option value="workshop">Workshop</option>';
$html .= '<option value="certification">Certification</option>';
$html .= '<option value="conference">Conference</option>';
$html .= '<option value="webinar">Webinar</option>';
$html .= '</select>';
$html .= '</div>';
// Public checkbox
$html .= '<div class="form-group checkbox-group">';
$html .= '<label for="template-public">';
$html .= '<input type="checkbox" id="template-public" name="template_public" value="1">';
$html .= ' Make this template available to other trainers';
$html .= '</label>';
$html .= '<small class="description">If unchecked, only you will be able to use this template</small>';
$html .= '</div>';
$html .= '</div>';
$html .= '<div class="dialog-actions">';
$html .= '<button type="button" class="button button-primary" onclick="hvacSaveAsTemplate()">Save Template</button>';
$html .= '<button type="button" class="button" onclick="hvacCloseSaveDialog()">Cancel</button>';
$html .= '</div>';
$html .= '</div>';
return $html;
}
/**
* Get timezone options for select field
*
* @return array Timezone options
*/
private function get_timezone_options(): array {
// Check cache first
$cached_timezones = $this->cache ? $this->cache->get_timezone_list() : false;
if ($cached_timezones !== false) {
return $cached_timezones;
}
// Generate timezone options
$timezone_options = [];
$zones = wp_timezone_choice('UTC');
if (preg_match_all('/<option value="([^"]*)"[^>]*>([^<]*)<\/option>/', $zones, $matches)) {
foreach ($matches[1] as $index => $value) {
$timezone_options[$value] = $matches[2][$index];
}
}
// Cache the results
if ($this->cache) {
$this->cache->cache_timezone_list($timezone_options);
}
return $timezone_options;
}
/**
* Get venue options for select field
*
* @return array Venue options
*/
private function get_venue_options(): array {
// Check cache first
$cache_key = 'venue_options_' . get_current_user_id();
$cached_venues = $this->cache ? $this->cache->get_venue_search($cache_key) : false;
if ($cached_venues !== false) {
return $cached_venues;
}
// Get venues from database
$venues = get_posts([
'post_type' => 'tribe_venue',
'post_status' => 'publish',
'posts_per_page' => 100, // Limit to prevent performance issues
'orderby' => 'title',
'order' => 'ASC',
]);
$venue_options = ['0' => '-- Select Venue --', 'new' => '+ Create New Venue'];
foreach ($venues as $venue) {
$venue_city = get_post_meta($venue->ID, '_VenueCity', true);
$venue_state = get_post_meta($venue->ID, '_VenueState', true);
$location_info = '';
if ($venue_city || $venue_state) {
$location_info = ' (' . trim($venue_city . ', ' . $venue_state, ', ') . ')';
}
$venue_options[$venue->ID] = $venue->post_title . $location_info;
}
// Cache the results
if ($this->cache) {
$this->cache->cache_venue_search($cache_key, $venue_options);
}
return $venue_options;
}
/**
* Get organizer options for select field
*
* @return array Organizer options
*/
private function get_organizer_options(): array {
// Get organizers from database
$organizers = get_posts([
'post_type' => 'tribe_organizer',
'post_status' => 'publish',
'posts_per_page' => 100, // Limit to prevent performance issues
'orderby' => 'title',
'order' => 'ASC',
]);
$organizer_options = ['0' => '-- Select Organizer --', 'new' => '+ Create New Organizer'];
foreach ($organizers as $organizer) {
$organizer_email = get_post_meta($organizer->ID, '_OrganizerEmail', true);
$email_info = $organizer_email ? ' (' . $organizer_email . ')' : '';
$organizer_options[$organizer->ID] = $organizer->post_title . $email_info;
}
return $organizer_options;
}
/**
* Add venue creation fields (shown when "Create New" is selected)
*/
private function add_venue_creation_fields(): self {
$venue_fields = [
[
'type' => 'text',
'name' => 'new_venue_name',
'label' => 'Venue Name',
'class' => 'hvac-venue-field',
'wrapper_class' => 'form-row venue-creation-field hvac-hidden-field',
'data_attributes' => ['conditional' => 'venue-create'],
'required' => false,
],
[
'type' => 'text',
'name' => 'new_venue_address',
'label' => 'Address',
'class' => 'hvac-venue-field',
'wrapper_class' => 'form-row venue-creation-field hvac-hidden-field',
'data_attributes' => ['conditional' => 'venue-create'],
],
[
'type' => 'text',
'name' => 'new_venue_city',
'label' => 'City',
'class' => 'hvac-venue-field',
'wrapper_class' => 'form-row venue-creation-field hvac-hidden-field',
'data_attributes' => ['conditional' => 'venue-create'],
],
[
'type' => 'text',
'name' => 'new_venue_state',
'label' => 'State',
'class' => 'hvac-venue-field',
'wrapper_class' => 'form-row venue-creation-field hvac-hidden-field',
'data_attributes' => ['conditional' => 'venue-create'],
],
[
'type' => 'text',
'name' => 'new_venue_zip',
'label' => 'Zip Code',
'class' => 'hvac-venue-field',
'wrapper_class' => 'form-row venue-creation-field hvac-hidden-field',
'data_attributes' => ['conditional' => 'venue-create'],
],
];
foreach ($venue_fields as $field) {
$this->add_field($field);
}
return $this;
}
/**
* Add organizer creation fields (shown when "Create New" is selected)
*/
private function add_organizer_creation_fields(): self {
$organizer_fields = [
[
'type' => 'text',
'name' => 'new_organizer_name',
'label' => 'Organizer Name',
'class' => 'hvac-organizer-field',
'wrapper_class' => 'form-row organizer-creation-field hvac-hidden-field',
'data_attributes' => ['conditional' => 'organizer-create'],
'required' => false,
],
[
'type' => 'email',
'name' => 'new_organizer_email',
'label' => 'Email',
'class' => 'hvac-organizer-field',
'wrapper_class' => 'form-row organizer-creation-field hvac-hidden-field',
'data_attributes' => ['conditional' => 'organizer-create'],
'validate' => ['email'],
],
[
'type' => 'text',
'name' => 'new_organizer_phone',
'label' => 'Phone',
'class' => 'hvac-organizer-field',
'wrapper_class' => 'form-row organizer-creation-field hvac-hidden-field',
'data_attributes' => ['conditional' => 'organizer-create'],
],
[
'type' => 'url',
'name' => 'new_organizer_website',
'label' => 'Website',
'class' => 'hvac-organizer-field',
'wrapper_class' => 'form-row organizer-creation-field hvac-hidden-field',
'data_attributes' => ['conditional' => 'organizer-create'],
'validate' => ['url'],
],
];
foreach ($organizer_fields as $field) {
$this->add_field($field);
}
return $this;
}
/**
* Override parent render method to include template functionality
*
* @return string Rendered form HTML
*/
public function render(): string {
ob_start();
?>
<form <?php echo $this->get_form_attributes(); ?> data-template-enabled="<?php echo $this->template_mode_enabled ? '1' : '0'; ?>">
<?php wp_nonce_field($this->nonce_action, $this->nonce_action . '_nonce'); ?>
<?php if ($this->template_mode_enabled && $this->current_template): ?>
<div class="template-info">
<p><strong>Using Template:</strong> <?php echo esc_html($this->current_template['name']); ?></p>
<input type="hidden" name="current_template_id" value="<?php echo esc_attr($this->current_template['id']); ?>">
</div>
<?php endif; ?>
<?php foreach ($this->fields as $field): ?>
<?php echo $this->render_field($field); ?>
<?php endforeach; ?>
<div class="form-submit">
<button type="submit" class="button button-primary">
<?php echo $this->current_template ? 'Create Event from Template' : 'Create Event'; ?>
</button>
<?php if ($this->template_mode_enabled): ?>
<button type="button" class="button button-secondary hvac-clear-template" onclick="hvacClearTemplate()">
Clear Template
</button>
<?php endif; ?>
</div>
</form>
<?php if ($this->template_mode_enabled): ?>
<!-- Template save modal -->
<div id="hvac-save-template-modal" class="hvac-modal hidden">
<div class="hvac-modal-content">
<h3>Save as Template</h3>
<form id="hvac-save-template-form">
<div class="form-row">
<label for="template-name">Template Name *</label>
<input type="text" id="template-name" name="template_name" required>
</div>
<div class="form-row">
<label for="template-description">Description</label>
<textarea id="template-description" name="template_description" rows="3"></textarea>
</div>
<div class="form-row">
<label for="template-category">Category</label>
<select id="template-category" name="template_category">
<option value="general">General</option>
<option value="training">Training</option>
<option value="workshop">Workshop</option>
<option value="certification">Certification</option>
<option value="webinar">Webinar</option>
</select>
</div>
<div class="form-row">
<label>
<input type="checkbox" name="template_public" value="1">
Make template public (available to all users)
</label>
</div>
<div class="form-actions">
<button type="submit" class="button button-primary">Save Template</button>
<button type="button" class="button button-secondary hvac-close-modal">Cancel</button>
</div>
</form>
</div>
</div>
<?php endif; ?>
<?php
return ob_get_clean();
}
/**
* Override render_field to handle template-specific field types
*
* @param array $field Field configuration
* @return string Rendered field HTML
*/
protected function render_field($field): string {
// Handle template-specific field types
if ($field['type'] === 'template-select') {
return $this->render_template_select($field);
}
if ($field['type'] === 'button') {
return $this->render_button($field);
}
// Use parent implementation for standard fields
return parent::render_field($field);
}
/**
* Render template selection field
*
* @param array $field Field configuration
* @return string Rendered field HTML
*/
private function render_template_select($field): string {
$output = sprintf('<div class="%s">', esc_attr($field['wrapper_class']));
// Label
if (!empty($field['label'])) {
$output .= sprintf(
'<label for="%s">%s%s</label>',
esc_attr($field['id']),
esc_html($field['label']),
$field['required'] ? ' <span class="required">*</span>' : ''
);
}
// Template select with AJAX loading
$value = $this->get_field_value($field['name'], $field['value']);
$output .= sprintf(
'<select name="%s" id="%s" class="%s" onchange="hvacLoadTemplate(this.value)" %s>',
esc_attr($field['name']),
esc_attr($field['id']),
esc_attr($field['class']),
$field['required'] ? 'required' : ''
);
foreach ($field['options'] as $option_value => $option_label) {
$output .= sprintf(
'<option value="%s" %s>%s</option>',
esc_attr($option_value),
selected($value, $option_value, false),
esc_html($option_label)
);
}
$output .= '</select>';
// Loading indicator
$output .= '<span class="hvac-template-loading hidden">Loading template...</span>';
// Description
if (!empty($field['description'])) {
$output .= sprintf('<small class="description">%s</small>', esc_html($field['description']));
}
// Error
if (isset($this->errors[$field['name']])) {
$output .= sprintf(
'<span class="error">%s</span>',
esc_html($this->errors[$field['name']])
);
}
$output .= '</div>';
return $output;
}
/**
* Render button field
*
* @param array $field Field configuration
* @return string Rendered field HTML
*/
private function render_button($field): string {
$output = sprintf('<div class="%s">', esc_attr($field['wrapper_class']));
$output .= sprintf(
'<button type="button" name="%s" id="%s" class="%s" %s>%s</button>',
esc_attr($field['name']),
esc_attr($field['id']),
esc_attr($field['class']),
isset($field['onclick']) ? 'onclick="' . esc_attr($field['onclick']) . '"' : '',
esc_html($field['value'])
);
$output .= '</div>';
return $output;
}
/**
* Enqueue template-related assets
*/
public function enqueue_template_assets(): void {
if (!$this->template_mode_enabled) {
return;
}
wp_enqueue_script(
'hvac-event-form-templates',
HVAC_PLUGIN_URL . 'assets/js/hvac-event-form-templates.js',
['jquery', 'hvac-ajax-optimizer'],
HVAC_VERSION,
true
);
wp_enqueue_style(
'hvac-event-form-templates',
HVAC_PLUGIN_URL . 'assets/css/hvac-event-form-templates.css',
[],
HVAC_VERSION
);
// Localize script for AJAX operations
wp_localize_script('hvac-event-form-templates', 'hvacEventTemplates', [
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('hvac_template_nonce'),
'strings' => [
'loadingTemplate' => __('Loading template...', 'hvac-community-events'),
'templateLoaded' => __('Template loaded successfully', 'hvac-community-events'),
'templateCleared' => __('Template cleared', 'hvac-community-events'),
'templateSaved' => __('Template saved successfully', 'hvac-community-events'),
'templateNameRequired' => __('Template name is required', 'hvac-community-events'),
'error' => __('An error occurred. Please try again.', 'hvac-community-events'),
'confirmClear' => __('Are you sure you want to clear the current template?', 'hvac-community-events'),
'fillRequiredFields' => __('Please fill in all required fields before saving as template.', 'hvac-community-events'),
]
]);
}
/**
* AJAX handler for loading template data
*/
public function ajax_load_template_data(): void {
// Security check
if (!wp_verify_nonce($_GET['nonce'] ?? '', 'hvac_template_nonce')) {
wp_send_json_error(['message' => __('Security check failed', 'hvac-community-events')]);
return;
}
// Capability check - ensure user can create/edit events
if (!current_user_can('edit_posts') && !array_intersect(['hvac_trainer', 'hvac_master_trainer'], wp_get_current_user()->roles)) {
wp_send_json_error(['message' => __('Permission denied', 'hvac-community-events')]);
return;
}
$template_id = sanitize_text_field($_GET['template_id'] ?? '');
if (empty($template_id) || $template_id === '0') {
wp_send_json_success(['template_data' => [], 'message' => __('Template cleared', 'hvac-community-events')]);
return;
}
$template = $this->template_manager->get_template($template_id);
if (!$template) {
wp_send_json_error(['message' => __('Template not found', 'hvac-community-events')]);
return;
}
// Increment usage count
$this->template_manager->update_template($template_id, [
'usage_count' => $template['usage_count'] + 1
]);
wp_send_json_success([
'template_data' => $template['field_data'],
'template_info' => [
'name' => $template['name'],
'description' => $template['description'],
],
'message' => __('Template loaded successfully', 'hvac-community-events')
]);
}
/**
* AJAX handler for saving form as template
*/
public function ajax_save_as_template(): void {
// Security check
if (!wp_verify_nonce($_POST['nonce'] ?? '', 'hvac_template_nonce')) {
wp_send_json_error(['message' => __('Security check failed', 'hvac-community-events')]);
return;
}
$template_config = [
'name' => sanitize_text_field($_POST['template_name'] ?? ''),
'description' => sanitize_textarea_field($_POST['template_description'] ?? ''),
'category' => sanitize_text_field($_POST['template_category'] ?? 'general'),
'is_public' => (bool) ($_POST['template_public'] ?? false),
'meta_data' => []
];
$form_data = $_POST['form_data'] ?? [];
// Validate required fields
if (empty($template_config['name'])) {
wp_send_json_error(['message' => __('Template name is required', 'hvac-community-events')]);
return;
}
// Create template with form data
$template_data = array_merge($template_config, ['field_data' => $form_data]);
$result = $this->template_manager->create_template($template_data);
if ($result['success']) {
wp_send_json_success($result);
} else {
wp_send_json_error($result);
}
}
/**
* Add custom validation rule for datetime fields
*
* @param mixed $value Value to validate
* @param string $rule Validation rule
* @param mixed $params Rule parameters
* @param array $field Field configuration
* @return string|false Error message or false if valid
*/
protected function apply_validation_rule($value, $rule, $params, $field) {
if ($rule === 'datetime') {
$datetime = DateTime::createFromFormat('Y-m-d\TH:i', $value);
if (!$datetime || $datetime->format('Y-m-d\TH:i') !== $value) {
return sprintf('%s must be a valid date and time.', $field['label']);
}
}
if ($rule === 'min_value') {
if (is_numeric($value) && floatval($value) < floatval($params)) {
return sprintf('%s must be at least %s.', $field['label'], $params);
}
}
if ($rule === 'max_value') {
if (is_numeric($value) && floatval($value) > floatval($params)) {
return sprintf('%s must not exceed %s.', $field['label'], $params);
}
}
// Use parent validation for standard rules
return parent::apply_validation_rule($value, $rule, $params, $field);
}
/**
* Add custom sanitization for datetime fields
*
* @param array $data Raw form data
* @return array Sanitized data
*/
public function sanitize(array $data): array {
$sanitized = parent::sanitize($data);
// Additional sanitization for event-specific fields
foreach ($this->fields as $field) {
if (!isset($data[$field['name']])) {
continue;
}
$value = $data[$field['name']];
switch ($field['sanitize']) {
case 'datetime':
// Validate and sanitize datetime-local format
$datetime = DateTime::createFromFormat('Y-m-d\TH:i', $value);
if ($datetime) {
$sanitized[$field['name']] = $datetime->format('Y-m-d\TH:i');
} else {
$sanitized[$field['name']] = '';
}
break;
}
}
return $sanitized;
}
}