294 lines
		
	
	
	
		
			10 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			294 lines
		
	
	
	
		
			10 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| /**
 | |
|  * Plugin Name: HVAC Community Events
 | |
|  * Plugin URI: https://upskillhvac.com
 | |
|  * Description: Custom plugin for HVAC trainer event management system
 | |
|  * Version: 1.0.0
 | |
|  * Author: Upskill HVAC
 | |
|  * Author URI: https://upskillhvac.com
 | |
|  * License: GPL-2.0+
 | |
|  * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 | |
|  * Text Domain: hvac-community-events
 | |
|  */
 | |
| 
 | |
| // Exit if accessed directly
 | |
| if (!defined('ABSPATH')) {
 | |
|     exit;
 | |
| }
 | |
| 
 | |
| // Define plugin constants
 | |
| define('HVAC_CE_VERSION', '1.0.0');
 | |
| define('HVAC_CE_PLUGIN_DIR', plugin_dir_path(__FILE__));
 | |
| define('HVAC_CE_PLUGIN_URL', plugin_dir_url(__FILE__));
 | |
| 
 | |
| // Include the logger class early
 | |
| require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-logger.php';
 | |
| 
 | |
| 
 | |
| /**
 | |
|  * Create required pages and roles upon plugin activation.
 | |
|  */
 | |
| function hvac_ce_create_required_pages() {
 | |
|     // Ensure the roles class is available
 | |
|     require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-roles.php';
 | |
|     HVAC_Logger::info('Starting page creation process', 'Activation');
 | |
|     
 | |
|     // Define required pages
 | |
|     $required_pages = [
 | |
|         'community-login' => [
 | |
|             'title' => 'Trainer Login',
 | |
|             'content' => '<!-- wp:shortcode -->[hvac_community_login]<!-- /wp:shortcode -->',
 | |
|             'template' => 'page-community-login.php',
 | |
|         ],
 | |
|         'trainer-registration' => [
 | |
|             'title' => 'Trainer Registration',
 | |
|             'content' => '<!-- wp:shortcode -->[hvac_trainer_registration]<!-- /wp:shortcode -->',
 | |
|         ],
 | |
|         'hvac-dashboard' => [
 | |
|             'title' => 'Trainer Dashboard',
 | |
|             'content' => '<!-- wp:shortcode -->[hvac_trainer_dashboard]<!-- /wp:shortcode -->',
 | |
|         ],
 | |
|         'manage-event' => [ // New page for TEC CE submission form shortcode
 | |
|             'title' => 'Manage Event',
 | |
|             'content' => '<!-- wp:shortcode -->[tribe_community_events view="submission_form"]<!-- /wp:shortcode -->',
 | |
|         ],
 | |
|         'my-events' => [ // New page for TEC CE event list shortcode
 | |
|             'title' => 'My Events',
 | |
|             'content' => '<!-- wp:shortcode -->[tribe_community_events view="my_events"]<!-- /wp:shortcode -->',
 | |
|         ],
 | |
|         'trainer-profile' => [ // Add trainer profile page
 | |
|             'title' => 'Trainer Profile',
 | |
|             'content' => '<!-- wp:shortcode -->[hvac_trainer_profile]<!-- /wp:shortcode -->',
 | |
|         ],
 | |
|         'event-summary' => [ // Add event summary page
 | |
|             'title' => 'Event Summary',
 | |
|             'content' => '<!-- wp:shortcode -->[hvac_event_summary]<!-- /wp:shortcode -->',
 | |
|         ],
 | |
|         'email-attendees' => [ // Add email attendees page
 | |
|             'title' => 'Email Attendees',
 | |
|             'content' => '<!-- wp:shortcode -->[hvac_email_attendees]<!-- /wp:shortcode -->',
 | |
|         ],
 | |
|         // REMOVED: 'submit-event' page creation. Will link to default TEC CE page.
 | |
|         // 'submit-event' => [
 | |
|         //     'title' => 'Submit Event',
 | |
|         //     'content' => '<!-- wp:shortcode -->[hvac_event_form]<!-- /wp:shortcode -->',
 | |
|         // ],
 | |
|         // Add future required pages here
 | |
|     ];
 | |
| 
 | |
|     // Get existing page IDs from options
 | |
|     $created_pages_option = 'hvac_community_pages';
 | |
|     $created_pages = get_option($created_pages_option, []);
 | |
| 
 | |
|     // Process each required page
 | |
|     foreach ($required_pages as $slug => $page_data) {
 | |
|         // Create the feature key
 | |
|         $feature_key = str_replace('-', '_', $slug);
 | |
|         
 | |
|         // Skip if already exists in our option
 | |
|         if (isset($created_pages[$feature_key]) && get_post($created_pages[$feature_key])) {
 | |
|             HVAC_Logger::info("Page '{$slug}' already exists in options with ID: {$created_pages[$feature_key]}", 'Activation');
 | |
|             continue;
 | |
|         }
 | |
| 
 | |
|         // Check if page already exists by slug
 | |
|         $existing_page = get_page_by_path($slug, OBJECT, 'page');
 | |
|         
 | |
|         // Safe handling of existing page
 | |
|         if (!empty($existing_page) && is_object($existing_page) && isset($existing_page->ID)) {
 | |
|             HVAC_Logger::info("Page '{$slug}' exists with ID: {$existing_page->ID}", 'Activation');
 | |
|             $created_pages[$feature_key] = $existing_page->ID;
 | |
|             continue;
 | |
|         }
 | |
|         
 | |
|         // Page doesn't exist, create it
 | |
|         HVAC_Logger::info("Page '{$slug}' not found. Creating it.", 'Activation');
 | |
|         
 | |
|         $post_data = [
 | |
|             'post_title'     => $page_data['title'],
 | |
|             'post_name'      => $slug,
 | |
|             'post_content'   => $page_data['content'],
 | |
|             'post_status'    => 'publish',
 | |
|             'post_type'      => 'page',
 | |
|             'comment_status' => 'closed',
 | |
|             'ping_status'    => 'closed',
 | |
|         ];
 | |
|         
 | |
|         // Add template if specified
 | |
|         if (!empty($page_data['template'])) {
 | |
|             $post_data['page_template'] = $page_data['template'];
 | |
|         }
 | |
| 
 | |
|         // Insert the page
 | |
|         $page_id = wp_insert_post($post_data);
 | |
| 
 | |
|         // Handle the result
 | |
|         if (is_wp_error($page_id)) {
 | |
|             HVAC_Logger::error("Error creating page '{$slug}': " . $page_id->get_error_message(), 'Activation');
 | |
|         } elseif (!empty($page_id)) {
 | |
|             HVAC_Logger::info("Successfully created page '{$slug}' with ID: {$page_id}", 'Activation');
 | |
|             $created_pages[$feature_key] = $page_id;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // Update the option with any newly created page IDs (and existing ones)
 | |
|     update_option($created_pages_option, $created_pages);
 | |
| 
 | |
|     // Create the custom role (Moved inside the activation function)
 | |
|     $roles_manager = new HVAC_Roles();
 | |
|     $result = $roles_manager->create_trainer_role();
 | |
|     if ($result) {
 | |
|         HVAC_Logger::info('Successfully created hvac_trainer role.', 'Activation');
 | |
|     } else {
 | |
|         HVAC_Logger::error('Failed to create hvac_trainer role.', 'Activation');
 | |
|     }
 | |
|     HVAC_Logger::info('Completed page creation and role setup process', 'Activation');
 | |
| 
 | |
| } // <<-- Brace moved here
 | |
| register_activation_hook(__FILE__, 'hvac_ce_create_required_pages');
 | |
| 
 | |
| 
 | |
| /**
 | |
|  * Remove custom roles upon plugin deactivation.
 | |
|  */
 | |
| function hvac_ce_remove_roles() {
 | |
|     require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-roles.php';
 | |
|     $roles_manager = new HVAC_Roles();
 | |
|     $roles_manager->remove_trainer_role();
 | |
|     HVAC_Logger::info('Deactivation hook fired, attempted to remove hvac_trainer role.', 'Deactivation');
 | |
| }
 | |
| register_deactivation_hook(__FILE__, 'hvac_ce_remove_roles');
 | |
| 
 | |
| 
 | |
| 
 | |
| /**
 | |
|  * Enqueue styles specifically for the HVAC Dashboard page.
 | |
|  */
 | |
| function hvac_ce_enqueue_dashboard_styles() {
 | |
|     // Check if we are on the specific dashboard page
 | |
|     // Assumes the page slug is 'hvac-dashboard' as created in the activation hook
 | |
|     if ( is_page( 'hvac-dashboard' ) ) {
 | |
|         wp_enqueue_style(
 | |
|             'hvac-dashboard-style',
 | |
|             HVAC_CE_PLUGIN_URL . 'assets/css/hvac-dashboard.css',
 | |
|             [], // No dependencies for now
 | |
|             HVAC_CE_VERSION
 | |
|         );
 | |
|     }
 | |
| }
 | |
| add_action( 'wp_enqueue_scripts', 'hvac_ce_enqueue_dashboard_styles' );
 | |
| 
 | |
| /**
 | |
|  * Enqueue styles and scripts for admin dashboard
 | |
|  */
 | |
| function hvac_ce_enqueue_admin_assets($hook) {
 | |
|     // Only load on our dashboard page
 | |
|     if ($hook !== 'hvac-community-events_page_hvac-ce-dashboard') {
 | |
|         return;
 | |
|     }
 | |
|     
 | |
|     // Enqueue dashboard CSS
 | |
|     wp_enqueue_style(
 | |
|         'hvac-admin-dashboard-style',
 | |
|         HVAC_CE_PLUGIN_URL . 'assets/css/admin-dashboard.css',
 | |
|         array('wp-admin'),
 | |
|         HVAC_CE_VERSION
 | |
|     );
 | |
|     
 | |
|     // Enqueue dashboard JS
 | |
|     wp_enqueue_script(
 | |
|         'hvac-admin-dashboard-script',
 | |
|         HVAC_CE_PLUGIN_URL . 'assets/js/admin-dashboard.js',
 | |
|         array('jquery', 'wp-util'),
 | |
|         HVAC_CE_VERSION,
 | |
|         true
 | |
|     );
 | |
|     
 | |
|     // Localize script with AJAX data
 | |
|     wp_localize_script('hvac-admin-dashboard-script', 'hvac_admin_dashboard', array(
 | |
|         'ajax_url' => admin_url('admin-ajax.php'),
 | |
|         'nonce' => wp_create_nonce('hvac_admin_nonce')
 | |
|     ));
 | |
| }
 | |
| add_action('admin_enqueue_scripts', 'hvac_ce_enqueue_admin_assets');
 | |
| 
 | |
| 
 | |
| /**
 | |
|  * Enqueue styles specifically for the HVAC Event Summary page.
 | |
|  */
 | |
| function hvac_ce_enqueue_event_summary_styles() {
 | |
|     // Check if we are on a single event page
 | |
|     if ( is_singular( Tribe__Events__Main::POSTTYPE ) ) {
 | |
|         wp_enqueue_style(
 | |
|             'hvac-event-summary-style',
 | |
|             HVAC_CE_PLUGIN_URL . 'assets/css/hvac-event-summary.css',
 | |
|             [], // No dependencies for now
 | |
|             HVAC_CE_VERSION
 | |
|         );
 | |
|     }
 | |
| }
 | |
| add_action( 'wp_enqueue_scripts', 'hvac_ce_enqueue_event_summary_styles' );
 | |
| 
 | |
| /**
 | |
|  * Enqueue styles specifically for the HVAC Email Attendees page.
 | |
|  */
 | |
| function hvac_ce_enqueue_email_attendees_styles() {
 | |
|     // Check if we are on the email attendees page
 | |
|     if ( is_page( 'email-attendees' ) ) {
 | |
|         wp_enqueue_style(
 | |
|             'hvac-email-attendees-style',
 | |
|             HVAC_CE_PLUGIN_URL . 'assets/css/hvac-email-attendees.css',
 | |
|             [], // No dependencies for now
 | |
|             HVAC_CE_VERSION
 | |
|         );
 | |
|     }
 | |
| }
 | |
| add_action( 'wp_enqueue_scripts', 'hvac_ce_enqueue_email_attendees_styles' );
 | |
| 
 | |
| 
 | |
| 
 | |
| // Include the main plugin class
 | |
| require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-community-events.php';
 | |
| 
 | |
| // Initialize the plugin
 | |
| function hvac_community_events_init() {
 | |
|     HVAC_Logger::info('Initializing HVAC Community Events plugin', 'Core');
 | |
|     return HVAC_Community_Events::instance();
 | |
| }
 | |
| add_action('plugins_loaded', 'hvac_community_events_init');
 | |
| 
 | |
| 
 | |
| /**
 | |
|  * Include custom template for single event summary page.
 | |
|  *
 | |
|  * @param string $template The path of the template to include.
 | |
|  * @return string The path of the template file.
 | |
|  */
 | |
| function hvac_ce_include_event_summary_template( $template ) {
 | |
|     // Check if it's a single event post type view
 | |
|     if ( is_singular( Tribe__Events__Main::POSTTYPE ) ) {
 | |
|         // Check if the custom template exists in the plugin's template directory
 | |
|         $custom_template = HVAC_CE_PLUGIN_DIR . 'templates/single-hvac-event-summary.php';
 | |
|         if ( file_exists( $custom_template ) ) {
 | |
|             // Return the path to the custom template
 | |
|             return $custom_template;
 | |
|         }
 | |
|     }
 | |
|     // Return the original template if not a single event or custom template doesn't exist
 | |
|     return $template;
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Template routing for Order Summary Page.
 | |
|  */
 | |
| function hvac_ce_include_order_summary_template( $template ) {
 | |
|     if ( is_page( 'order-summary' ) && isset( $_GET['order_id'] ) && absint( $_GET['order_id'] ) > 0 ) {
 | |
|         $custom_template = HVAC_CE_PLUGIN_DIR . 'templates/single-hvac-order-summary.php';
 | |
|         if ( file_exists( $custom_template ) ) {
 | |
|             return $custom_template;
 | |
|         }
 | |
|     }
 | |
|     return $template;
 | |
| }
 | |
| // Removed - template handling is now in the main class
 | |
| // add_filter( 'template_include', 'hvac_ce_include_event_summary_template', 99 );
 |