upskill-event-manager/includes/class-hvac-plugin.php
bengizmo a58ea1603c fix: Resolve duplicate initialization and jQuery selector errors
- Implement singleton pattern for HVAC_Enhanced_Settings to prevent duplicate initialization
- Fix jQuery selector error by checking for valid hash selectors before using $(href)
- Add default email templates with professional copy for trainer notifications
- Update plugin version to 1.0.1 for cache busting
- Remove duplicate Enhanced Settings initialization from HVAC_Community_Events
- Add force cache refresh suffix to admin scripts

This resolves the duplicate content issue on email templates page and fixes
JavaScript errors in the admin interface.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-28 17:58:39 -03:00

724 lines
No EOL
23 KiB
PHP

<?php
/**
* Main Plugin Class for HVAC Community Events
*
* @package HVAC_Community_Events
* @since 1.0.0
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* HVAC_Plugin class
*/
class HVAC_Plugin {
/**
* Plugin instance
*
* @var HVAC_Plugin
*/
private static $instance = null;
/**
* Get plugin instance
*
* @return HVAC_Plugin
*/
public static function instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
$this->define_constants();
$this->includes();
$this->init_hooks();
}
/**
* Define plugin constants
*
* @return void
*/
private function define_constants() {
if (!defined('HVAC_PLUGIN_VERSION')) {
define('HVAC_PLUGIN_VERSION', '1.0.1');
}
if (!defined('HVAC_PLUGIN_FILE')) {
define('HVAC_PLUGIN_FILE', dirname(__DIR__) . '/hvac-community-events.php');
}
if (!defined('HVAC_PLUGIN_DIR')) {
define('HVAC_PLUGIN_DIR', plugin_dir_path(HVAC_PLUGIN_FILE));
}
if (!defined('HVAC_PLUGIN_URL')) {
define('HVAC_PLUGIN_URL', plugin_dir_url(HVAC_PLUGIN_FILE));
}
if (!defined('HVAC_PLUGIN_BASENAME')) {
define('HVAC_PLUGIN_BASENAME', plugin_basename(HVAC_PLUGIN_FILE));
}
}
/**
* Include required files
*
* @return void
*/
private function includes() {
// Core includes
require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-logger.php';
require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-activator.php';
require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-deactivator.php';
require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-page-manager.php';
require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-template-loader.php';
require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-community-events.php';
// Check which roles manager exists
if (file_exists(HVAC_PLUGIN_DIR . 'includes/class-hvac-roles-manager.php')) {
require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-roles-manager.php';
} elseif (file_exists(HVAC_PLUGIN_DIR . 'includes/class-hvac-roles.php')) {
require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-roles.php';
}
// Feature includes - check if files exist before including
$feature_includes = [
'class-hvac-trainer-status.php',
'class-hvac-access-control.php',
'class-hvac-registration.php',
'class-hvac-manage-event.php',
'class-hvac-event-summary.php',
'class-hvac-trainer-profile.php',
'class-hvac-master-dashboard.php',
'class-hvac-master-dashboard-data.php',
'class-hvac-settings.php',
'class-hvac-dashboard.php',
'class-hvac-dashboard-data.php',
'class-hvac-approval-workflow.php',
'class-hvac-event-navigation.php',
'class-hvac-event-manage-header.php',
'class-hvac-help-system.php',
'class-event-form-handler.php',
'class-event-author-fixer.php',
'class-attendee-profile.php',
];
foreach ($feature_includes as $file) {
$file_path = HVAC_PLUGIN_DIR . 'includes/' . $file;
if (file_exists($file_path)) {
require_once $file_path;
}
}
// Community includes
if (file_exists(HVAC_PLUGIN_DIR . 'includes/community/class-login-handler.php')) {
require_once HVAC_PLUGIN_DIR . 'includes/community/class-login-handler.php';
// Initialize Login_Handler to register shortcode
new \HVAC_Community_Events\Community\Login_Handler();
}
if (file_exists(HVAC_PLUGIN_DIR . 'includes/community/class-event-handler.php')) {
require_once HVAC_PLUGIN_DIR . 'includes/community/class-event-handler.php';
}
// Certificate system
$certificate_files = [
'certificates/class-certificate-security.php',
'certificates/class-certificate-installer.php',
'certificates/class-certificate-manager.php',
'certificates/class-certificate-generator.php',
'certificates/class-certificate-url-handler.php',
];
foreach ($certificate_files as $file) {
$file_path = HVAC_PLUGIN_DIR . 'includes/' . $file;
if (file_exists($file_path)) {
require_once $file_path;
}
}
// Admin includes
$admin_files = [
'admin/class-zoho-admin.php',
'admin/class-admin-dashboard.php',
'admin/class-hvac-enhanced-settings.php',
];
foreach ($admin_files as $file) {
$file_path = HVAC_PLUGIN_DIR . 'includes/' . $file;
if (file_exists($file_path)) {
require_once $file_path;
}
}
// Google Sheets integration
$google_files = [
'google-sheets/class-google-sheets-auth.php',
'google-sheets/class-google-sheets-admin.php',
];
foreach ($google_files as $file) {
$file_path = HVAC_PLUGIN_DIR . 'includes/' . $file;
if (file_exists($file_path)) {
require_once $file_path;
}
}
// Communication system
$communication_files = [
'communication/class-communication-installer.php',
'communication/class-communication-scheduler.php',
'communication/class-communication-templates.php',
];
foreach ($communication_files as $file) {
$file_path = HVAC_PLUGIN_DIR . 'includes/' . $file;
if (file_exists($file_path)) {
require_once $file_path;
}
}
// Helper includes
if (file_exists(HVAC_PLUGIN_DIR . 'includes/helpers/attendee-profile-link.php')) {
require_once HVAC_PLUGIN_DIR . 'includes/helpers/attendee-profile-link.php';
}
// Legacy support
$this->include_legacy_files();
}
/**
* Include legacy files for backward compatibility
*
* @return void
*/
private function include_legacy_files() {
// Include legacy functions if they exist
$legacy_files = [
'includes/hvac-ce-functions.php',
'includes/hvac-ce-admin.php',
'includes/hvac-ce-certificates.php'
];
foreach ($legacy_files as $file) {
$file_path = HVAC_PLUGIN_DIR . $file;
if (file_exists($file_path)) {
require_once $file_path;
}
}
}
/**
* Initialize hooks
*
* @return void
*/
private function init_hooks() {
// Activation/Deactivation hooks
register_activation_hook(HVAC_PLUGIN_FILE, [$this, 'activate']);
register_deactivation_hook(HVAC_PLUGIN_FILE, [$this, 'deactivate']);
// Init hook
add_action('init', [$this, 'init'], 0);
// Plugin loaded
add_action('plugins_loaded', [$this, 'plugins_loaded']);
// Admin init
add_action('admin_init', [$this, 'admin_init']);
// Scripts and styles
add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']);
add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts']);
// AJAX handlers
add_action('wp_ajax_hvac_master_dashboard_events', [$this, 'ajax_master_dashboard_events']);
add_action('wp_ajax_nopriv_hvac_master_dashboard_events', [$this, 'ajax_master_dashboard_events']);
}
/**
* Plugin activation
*
* @return void
*/
public function activate() {
HVAC_Activator::activate();
}
/**
* Plugin deactivation
*
* @return void
*/
public function deactivate() {
HVAC_Deactivator::deactivate();
}
/**
* Init hook
*
* @return void
*/
public function init() {
// Initialize template loader
HVAC_Template_Loader::init();
// Initialize access control
new HVAC_Access_Control();
// Initialize other components
$this->init_components();
// Handle legacy redirects
$this->handle_legacy_redirects();
// Handle parent page redirects
add_action('template_redirect', [$this, 'redirect_parent_pages']);
// Ensure registration page access
add_action('template_redirect', [$this, 'ensure_registration_access'], 5);
}
/**
* Initialize plugin components
*
* @return void
*/
private function init_components() {
// Initialize the main community events class (registers shortcodes)
if (class_exists('HVAC_Community_Events')) {
HVAC_Community_Events::instance();
}
// Initialize registration if class exists
if (class_exists('HVAC_Registration')) {
new HVAC_Registration();
}
// Initialize event management
if (class_exists('HVAC_Manage_Event')) {
new HVAC_Manage_Event();
}
if (class_exists('HVAC_Event_Summary')) {
new HVAC_Event_Summary();
}
// Initialize trainer profile
if (class_exists('HVAC_Trainer_Profile')) {
new HVAC_Trainer_Profile();
}
// Initialize dashboards
if (class_exists('HVAC_Dashboard')) {
new HVAC_Dashboard();
}
if (class_exists('HVAC_Master_Dashboard')) {
new HVAC_Master_Dashboard();
}
// Initialize settings
if (class_exists('HVAC_Settings')) {
new HVAC_Settings();
}
// Initialize approval workflow
if (class_exists('HVAC_Approval_Workflow')) {
new HVAC_Approval_Workflow();
}
// Initialize event navigation
if (class_exists('HVAC_Event_Navigation')) {
new HVAC_Event_Navigation();
}
// Initialize help system
if (class_exists('HVAC_Help_System')) {
HVAC_Help_System::instance();
}
// Initialize certificate security
if (class_exists('HVAC_Certificate_Security')) {
HVAC_Certificate_Security::instance();
}
// Initialize certificate URL handler
if (class_exists('HVAC_Certificate_URL_Handler')) {
HVAC_Certificate_URL_Handler::instance();
}
// Initialize attendee profile
if (class_exists('HVAC_Attendee_Profile')) {
HVAC_Attendee_Profile::instance();
}
// Initialize Google Sheets
if (class_exists('HVAC_Google_Sheets_Auth')) {
new HVAC_Google_Sheets_Auth();
}
if (class_exists('HVAC_Google_Sheets_Admin')) {
new HVAC_Google_Sheets_Admin();
}
// Initialize communication system
if (class_exists('HVAC_Communication_Installer')) {
HVAC_Communication_Installer::maybe_update();
}
if (class_exists('HVAC_Communication_Scheduler')) {
hvac_communication_scheduler();
}
// Initialize admin components
if (is_admin()) {
if (class_exists('HVAC_Zoho_Admin')) {
HVAC_Zoho_Admin::instance();
}
if (class_exists('HVAC_Admin_Dashboard')) {
new HVAC_Admin_Dashboard();
}
if (class_exists('HVAC_Enhanced_Settings')) {
HVAC_Enhanced_Settings::instance();
}
}
}
/**
* Plugins loaded hook
*
* @return void
*/
public function plugins_loaded() {
// Load text domain
load_plugin_textdomain('hvac-community-events', false, dirname(HVAC_PLUGIN_BASENAME) . '/languages');
}
/**
* Admin init hook
*
* @return void
*/
public function admin_init() {
// Check for plugin updates
$this->check_plugin_updates();
}
/**
* Enqueue frontend scripts and styles
*
* @return void
*/
public function enqueue_scripts() {
// Enqueue only on plugin pages
if (!HVAC_Template_Loader::is_plugin_template()) {
return;
}
// Plugin styles
wp_enqueue_style(
'hvac-community-events',
HVAC_PLUGIN_URL . 'assets/css/hvac-community-events.css',
[],
HVAC_PLUGIN_VERSION
);
// Plugin scripts
wp_enqueue_script(
'hvac-community-events',
HVAC_PLUGIN_URL . 'assets/js/hvac-community-events.js',
['jquery'],
HVAC_PLUGIN_VERSION,
true
);
// Localize script
wp_localize_script('hvac-community-events', 'hvac_ajax', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('hvac_ajax_nonce')
]);
}
/**
* Enqueue admin scripts and styles
*
* @param string $hook Current admin page hook
* @return void
*/
public function admin_enqueue_scripts($hook) {
// Admin styles
wp_enqueue_style(
'hvac-admin',
HVAC_PLUGIN_URL . 'assets/css/hvac-admin.css',
[],
HVAC_PLUGIN_VERSION
);
// Admin scripts
wp_enqueue_script(
'hvac-admin',
HVAC_PLUGIN_URL . 'assets/js/hvac-admin.js',
['jquery'],
HVAC_PLUGIN_VERSION . '.1', // Force cache refresh
true
);
// Localize admin script
wp_localize_script('hvac-admin', 'hvac_admin', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('hvac_admin_nonce')
]);
}
/**
* Handle legacy URL redirects
*
* @return void
*/
private function handle_legacy_redirects() {
// Hook early to catch URLs before 404
add_action('init', [$this, 'register_legacy_rewrite_rules']);
add_action('template_redirect', [$this, 'legacy_redirects'], 5);
}
/**
* Register rewrite rules for legacy URLs
*
* @return void
*/
public function register_legacy_rewrite_rules() {
// Legacy URL to new URL mapping
$legacy_redirects = $this->get_legacy_redirects();
// Add rewrite rules for each legacy URL
foreach ($legacy_redirects as $legacy => $new) {
add_rewrite_rule(
'^' . $legacy . '/?$',
'index.php?hvac_legacy_redirect=' . $legacy,
'top'
);
}
// Register the query var
add_filter('query_vars', function($vars) {
$vars[] = 'hvac_legacy_redirect';
return $vars;
});
// Handle the redirect
add_action('parse_request', [$this, 'handle_legacy_redirect_request']);
}
/**
* Get legacy redirect mappings
*
* @return array
*/
private function get_legacy_redirects() {
return [
'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',
];
}
/**
* 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'];
$legacy_redirects = $this->get_legacy_redirects();
if (isset($legacy_redirects[$legacy_slug])) {
$new_url = home_url('/' . $legacy_redirects[$legacy_slug] . '/');
// Preserve query parameters
if (!empty($_SERVER['QUERY_STRING'])) {
$new_url .= '?' . $_SERVER['QUERY_STRING'];
}
wp_redirect($new_url, 301);
exit;
}
}
/**
* Process legacy redirects (fallback for existing pages)
*
* @return void
*/
public function legacy_redirects() {
// Get current URL path
$current_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
// Get legacy redirects
$legacy_redirects = $this->get_legacy_redirects();
// Check if current path matches a legacy URL
if (isset($legacy_redirects[$current_path])) {
$new_url = home_url('/' . $legacy_redirects[$current_path] . '/');
// Preserve query parameters
if (!empty($_SERVER['QUERY_STRING'])) {
$new_url .= '?' . $_SERVER['QUERY_STRING'];
}
// Perform 301 redirect
wp_redirect($new_url, 301);
exit;
}
// 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($legacy_redirects[$current_slug])) {
$new_url = home_url('/' . $legacy_redirects[$current_slug] . '/');
// Preserve query parameters
if (!empty($_SERVER['QUERY_STRING'])) {
$new_url .= '?' . $_SERVER['QUERY_STRING'];
}
wp_redirect($new_url, 301);
exit;
}
}
}
/**
* Check for plugin updates
*
* @return void
*/
private function check_plugin_updates() {
$current_version = get_option('hvac_plugin_version', '0.0.0');
if (version_compare($current_version, HVAC_PLUGIN_VERSION, '<')) {
// Run upgrade routines
$this->upgrade($current_version);
// Update version
update_option('hvac_plugin_version', HVAC_PLUGIN_VERSION);
}
}
/**
* Run upgrade routines
*
* @param string $from_version Version upgrading from
* @return void
*/
private function upgrade($from_version) {
HVAC_Logger::info("Upgrading from version {$from_version} to " . HVAC_PLUGIN_VERSION, 'Upgrade');
// Version-specific upgrades can be added here
}
/**
* Redirect parent pages to their dashboards
*
* @return void
*/
public function redirect_parent_pages() {
// Get the current URL path
$current_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
// Check if we're on the trainer parent page (not a child page)
if ($current_path === 'trainer' || $current_path === 'trainer/') {
// Redirect to the dashboard
wp_redirect(home_url('/trainer/dashboard/'), 301);
exit;
}
// Also redirect master-trainer to master-trainer/dashboard
if ($current_path === 'master-trainer' || $current_path === 'master-trainer/') {
wp_redirect(home_url('/master-trainer/dashboard/'), 301);
exit;
}
}
/**
* Ensure trainer registration page is publicly accessible
*
* @return void
*/
public function ensure_registration_access() {
// If we're on the trainer registration page, don't apply any authentication checks
if (is_page('trainer/registration')) {
// Remove any potential authentication hooks that might be added by other code
remove_all_actions('template_redirect', 10);
}
}
/**
* Handle AJAX request for master dashboard events table
*
* @return void
*/
public function ajax_master_dashboard_events() {
// Verify nonce
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'hvac_master_dashboard_nonce')) {
wp_die('Security check failed');
}
// Check permissions
if (!current_user_can('view_master_dashboard') && !current_user_can('view_all_trainer_data') && !current_user_can('manage_options')) {
wp_send_json_error('Insufficient permissions');
}
// Load master dashboard data class if needed
if (!class_exists('HVAC_Master_Dashboard_Data')) {
$data_file = HVAC_PLUGIN_DIR . 'includes/class-hvac-master-dashboard-data.php';
if (file_exists($data_file)) {
require_once $data_file;
}
}
if (!class_exists('HVAC_Master_Dashboard_Data')) {
wp_send_json_error('Master dashboard data class not found');
}
// Initialize data handler
$master_data = new HVAC_Master_Dashboard_Data();
// Get table data with filters
$args = array(
'status' => sanitize_text_field($_POST['status'] ?? 'all'),
'search' => sanitize_text_field($_POST['search'] ?? ''),
'orderby' => sanitize_text_field($_POST['orderby'] ?? 'date'),
'order' => sanitize_text_field($_POST['order'] ?? 'DESC'),
'page' => absint($_POST['page'] ?? 1),
'per_page' => absint($_POST['per_page'] ?? 10),
'date_from' => sanitize_text_field($_POST['date_from'] ?? ''),
'date_to' => sanitize_text_field($_POST['date_to'] ?? ''),
'trainer_id' => absint($_POST['trainer_id'] ?? 0),
);
$table_data = $master_data->get_events_table_data($args);
wp_send_json_success($table_data);
}
}