getMessage()); update_option('hvac_activation_error', $e->getMessage()); } // Clear in-progress flag delete_option('hvac_activation_in_progress'); } /** * Create the main dashboard page */ function create_hvac_dashboard_page() { error_log('[HVAC Activation] Creating dashboard page'); // Check if the page already exists by slug $existing_page = get_page_by_path('hvac-dashboard', OBJECT, 'page'); if (!empty($existing_page) && is_object($existing_page)) { error_log('[HVAC Activation] Dashboard page already exists with ID: ' . $existing_page->ID); $created_pages = get_option('hvac_community_pages', []); $created_pages['hvac_dashboard'] = $existing_page->ID; update_option('hvac_community_pages', $created_pages); return; } // Create the dashboard page $page_data = [ 'post_title' => 'Trainer Dashboard', 'post_name' => 'hvac-dashboard', 'post_content' => '[hvac_trainer_dashboard]', 'post_status' => 'publish', 'post_type' => 'page', 'comment_status' => 'closed', 'ping_status' => 'closed', ]; $page_id = wp_insert_post($page_data); if (is_wp_error($page_id)) { throw new Exception('Failed to create dashboard page: ' . $page_id->get_error_message()); } error_log('[HVAC Activation] Successfully created dashboard page with ID: ' . $page_id); // Save page ID in options $created_pages = get_option('hvac_community_pages', []); $created_pages['hvac_dashboard'] = $page_id; update_option('hvac_community_pages', $created_pages); } /** * Create the HVAC trainer role */ function create_hvac_trainer_role() { error_log('[HVAC Activation] Creating trainer role'); // Check if role already exists if (get_role('hvac_trainer')) { error_log('[HVAC Activation] Trainer role already exists'); return; } // Define trainer capabilities $caps = array( // Basic WordPress capabilities 'read' => true, 'upload_files' => true, // Custom HVAC capabilities 'manage_hvac_events' => true, 'edit_hvac_profile' => true, 'view_hvac_dashboard' => true, 'manage_attendees' => true, 'email_attendees' => true, // The Events Calendar capabilities 'publish_tribe_events' => true, 'edit_tribe_events' => true, 'delete_tribe_events' => true, 'edit_published_tribe_events' => true, 'delete_published_tribe_events' => true, 'read_private_tribe_events' => true, ); // Add the role $result = add_role( 'hvac_trainer', 'HVAC Trainer', $caps ); if ($result === null) { throw new Exception('Failed to create hvac_trainer role'); } error_log('[HVAC Activation] Successfully created hvac_trainer role'); } /** * Create all required pages for the plugin */ function create_hvac_required_pages() { error_log('[HVAC Activation] Creating required pages'); // Define required pages (excluding dashboard, which was created first) $required_pages = [ 'community-login' => [ 'title' => 'Trainer Login', 'content' => '[hvac_community_login]', 'template' => 'page-community-login.php', ], 'trainer-registration' => [ 'title' => 'Trainer Registration', 'content' => '[hvac_trainer_registration]', ], 'manage-event' => [ 'title' => 'Manage Event', 'content' => '[tribe_community_events view="submission_form"]', ], 'my-events' => [ 'title' => 'My Events', 'content' => '[tribe_community_events view="my_events"]', ], 'trainer-profile' => [ 'title' => 'Trainer Profile', 'content' => '[hvac_trainer_profile]', ], 'event-summary' => [ 'title' => 'Event Summary', 'content' => '[hvac_event_summary]', ], 'email-attendees' => [ 'title' => 'Email Attendees', 'content' => '[hvac_email_attendees]', ], ]; // Get existing page IDs from options $created_pages = get_option('hvac_community_pages', []); // Create each page one by one foreach ($required_pages as $slug => $page_data) { try { create_single_hvac_page($slug, $page_data, $created_pages); } catch (Exception $e) { error_log('[HVAC Activation] Error creating page ' . $slug . ': ' . $e->getMessage()); // Continue with other pages even if one fails } } // Update the option with all created pages update_option('hvac_community_pages', $created_pages); } /** * Create a single page safely */ function create_single_hvac_page($slug, $page_data, &$created_pages) { // 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])) { error_log('[HVAC Activation] Page ' . $slug . ' already exists in options with ID: ' . $created_pages[$feature_key]); return; } // 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)) { error_log('[HVAC Activation] Page ' . $slug . ' exists with ID: ' . $existing_page->ID); $created_pages[$feature_key] = $existing_page->ID; return; } // Page doesn't exist, create it error_log('[HVAC Activation] Creating page: ' . $slug); $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)) { throw new Exception('Error creating page ' . $slug . ': ' . $page_id->get_error_message()); } elseif (!empty($page_id)) { error_log('[HVAC Activation] Successfully created page ' . $slug . ' with ID: ' . $page_id); $created_pages[$feature_key] = $page_id; } } /** * Show admin notice for activation status */ function hvac_activation_admin_notice() { // Show only on plugin pages $screen = get_current_screen(); if (!in_array($screen->id, ['plugins', 'dashboard'])) { return; } // Check if activation was successful if (get_option('hvac_activation_successful') === 'yes') { ?>
HVAC Community Events: Plugin activated successfully!
Activation completed at:
Created pages:'; echo 'HVAC Community Events: Plugin activation encountered an error:
Please deactivate and reactivate the plugin to try again.
HVAC Community Events: Plugin activation appears to be in progress.
Started at:
If this message persists, please deactivate and reactivate the plugin.