upskill-event-manager/hvac-community-events/includes/class-hvac-community-events.php

152 lines
No EOL
4.5 KiB
PHP

<?php
/**
* Main plugin class for HVAC Community Events
*/
if (!defined('ABSPATH')) {
exit;
}
class HVAC_Community_Events {
/**
* The single instance of the class
*/
private static $instance = null;
/**
* Main instance
*/
public static function instance() {
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
public function __construct() {
error_log('[HVAC DEBUG] HVAC_Community_Events constructor running.'); // ADDED LOG
$this->define_constants();
$this->includes();
$this->init_hooks();
}
/**
* Define constants
*/
private function define_constants() {
// Additional constants can be defined here
}
/**
* Include required files
*/
private function includes() {
require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-roles.php';
require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-registration.php';
require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-settings.php';
require_once HVAC_CE_PLUGIN_DIR . 'includes/community/class-login-handler.php'; // Add Login Handler
require_once HVAC_CE_PLUGIN_DIR . 'includes/community/class-event-handler.php'; // Add Event Handler
// Include dashboard data class if it's not autoloaded
if ( ! class_exists('HVAC_Dashboard_Data') ) {
require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-dashboard-data.php';
}
}
/**
* Initialize hooks
*/
private function init_hooks() {
// Register activation/deactivation hooks
// Note: These hooks are typically registered outside the class instance context
// register_activation_hook(__FILE__, array($this, 'activate')); // This won't work correctly here
// register_deactivation_hook(__FILE__, array($this, 'deactivate')); // This won't work correctly here
// Initialize other hooks
add_action('init', array($this, 'init'));
// Template loading for custom pages
add_filter('template_include', array($this, 'load_custom_templates'));
} // End init_hooks
/**
* Plugin activation (Should be called statically or from the main plugin file context)
*/
public static function activate() {
// Activation code here (e.g., page creation, role creation)
// Note: This method might need to be moved or called differently
}
/**
* Plugin deactivation (Should be called statically or from the main plugin file context)
*/
public static function deactivate() {
// Remove the hvac_trainer role
require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-roles.php'; // Ensure class is available
$roles = new HVAC_Roles();
$roles->remove_trainer_role();
// Additional deactivation tasks
// ...
}
/**
* Initialize plugin actions attached to 'init' hook
*/
public function init() {
static $initialized = false; // Flag to run only once
if ($initialized) {
return;
}
// Initialize handlers
error_log('[HVAC DEBUG] HVAC_Community_Events::init() - Before new Login_Handler()'); // Updated Log
new \HVAC_Community_Events\Community\Login_Handler();
error_log('[HVAC DEBUG] HVAC_Community_Events::init() - Before new HVAC_Registration()'); // Updated Log
new HVAC_Registration(); // Instantiate Registration class to register shortcode
error_log('[HVAC DEBUG] HVAC_Community_Events::init() - After new HVAC_Registration()'); // Updated Log
// Prevent trainers from accessing wp-admin
add_action('admin_init', array($this, 'redirect_trainers_from_admin'));
$initialized = true; // Mark as initialized
}
/**
* Redirect HVAC trainers from admin area to frontend dashboard
*/
public function redirect_trainers_from_admin() {
if (defined('DOING_AJAX') && DOING_AJAX) {
return;
}
// Check if user is trying to access wp-admin and has trainer role but not admin caps
if ( is_admin() && ! current_user_can('manage_options') && current_user_can('view_hvac_dashboard') ) {
wp_redirect(home_url('/hvac-dashboard/')); // Corrected slug
exit;
}
}
/**
* Load custom templates for plugin pages.
*
* @param string $template The path of the template to include.
* @return string The path of the template to include.
*/
public function load_custom_templates( $template ) {
// Check if we are on the HVAC Dashboard page
if ( is_page( 'hvac-dashboard' ) ) {
$new_template = HVAC_CE_PLUGIN_DIR . 'templates/template-hvac-dashboard.php';
if ( file_exists( $new_template ) ) {
return $new_template;
}
}
// Add checks for other custom pages here if needed
return $template;
}
} // End class HVAC_Community_Events