feat: Add automatic page creation for announcements system

 Fixed the core issue - plugin now automatically creates pages on activation

## Changes Made
- Updated HVAC_Announcements_Manager::activate() to create all required pages
- Fixed duplicate method issue by removing instance methods
- Added page creation to HVAC_Activator::activate() hook
- Added deactivation cleanup to HVAC_Deactivator::deactivate()

## Pages Auto-Created
- /master-trainer/manage-announcements/ (with [hvac_announcements_manager])
- /trainer/announcements/ (with [hvac_announcements_timeline])
- /trainer/training-resources/ (with Google Drive embed)

## Verification
 All pages now exist and load successfully
 Shortcodes are properly displayed
 Components are loading (timeline, iframe)
 No more manual page creation required

Plugin activation now handles complete announcements system setup automatically.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Ben 2025-08-20 14:26:26 -03:00
parent 7e6cb2c9ce
commit 747b8d371d
4 changed files with 208 additions and 40 deletions

View file

@ -43,6 +43,11 @@ class HVAC_Activator {
// Schedule cron jobs
self::schedule_cron_jobs();
// Activate announcements system
if (class_exists('HVAC_Announcements_Manager')) {
HVAC_Announcements_Manager::activate();
}
// Register routes before flushing
if (class_exists('HVAC_Route_Manager')) {
$route_manager = HVAC_Route_Manager::instance();

View file

@ -96,30 +96,6 @@ class HVAC_Announcements_Manager {
add_filter('hvac_plugin_page_slugs', array($this, 'add_page_slugs'));
}
/**
* Plugin activation
*/
public function activate() {
// Add capabilities
HVAC_Announcements_Permissions::add_capabilities();
// Create pages
$this->create_pages();
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Plugin deactivation
*/
public function deactivate() {
// Remove capabilities (optional - you might want to keep them)
// HVAC_Announcements_Permissions::remove_capabilities();
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Create required pages
@ -127,18 +103,28 @@ class HVAC_Announcements_Manager {
private function create_pages() {
$pages = array(
array(
'title' => 'Announcements',
'slug' => 'master-announcements',
'parent_slug' => 'master-dashboard',
'template' => 'templates/page-master-announcements.php',
'meta_key' => '_hvac_page_master_announcements_created',
'title' => 'Manage Announcements',
'slug' => 'manage-announcements',
'parent_slug' => 'master-trainer',
'content' => '[hvac_announcements_manager]',
'template' => 'templates/page-master-manage-announcements.php',
'meta_key' => '_hvac_page_manage_announcements_created',
),
array(
'title' => 'Resources',
'slug' => 'trainer-resources',
'parent_slug' => 'dashboard',
'template' => 'templates/page-trainer-resources.php',
'meta_key' => '_hvac_page_trainer_resources_created',
'title' => 'Announcements',
'slug' => 'announcements',
'parent_slug' => 'trainer',
'content' => '[hvac_announcements_timeline]',
'template' => 'templates/page-trainer-announcements.php',
'meta_key' => '_hvac_page_trainer_announcements_created',
),
array(
'title' => 'Training Resources',
'slug' => 'training-resources',
'parent_slug' => 'trainer',
'content' => '[hvac_google_drive_embed url="https://drive.google.com/drive/folders/1-G8gICMsih5E9YJ2FqaC5OqG0o4rwuSP"]',
'template' => 'templates/page-trainer-training-resources.php',
'meta_key' => '_hvac_page_training_resources_created',
),
);
@ -161,7 +147,7 @@ class HVAC_Announcements_Manager {
$page_id = wp_insert_post(array(
'post_title' => $page_data['title'],
'post_name' => $page_data['slug'],
'post_content' => '',
'post_content' => isset($page_data['content']) ? $page_data['content'] : '',
'post_status' => 'publish',
'post_type' => 'page',
'post_parent' => $parent_id,
@ -176,15 +162,47 @@ class HVAC_Announcements_Manager {
}
}
/**
* Plugin activation hook
*/
public static function activate() {
$instance = self::get_instance();
// Create pages
$instance->create_pages();
// Add capabilities
if (class_exists('HVAC_Announcements_Permissions')) {
HVAC_Announcements_Permissions::add_capabilities();
}
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Plugin deactivation hook
*/
public static function deactivate() {
// Remove capabilities
if (class_exists('HVAC_Announcements_Permissions')) {
HVAC_Announcements_Permissions::remove_capabilities();
}
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Maybe create pages if they don't exist
*/
public function maybe_create_pages() {
// Check if pages exist
$master_page = get_page_by_path('master-trainer/master-announcements');
$resources_page = get_page_by_path('trainer/trainer-resources');
$manage_page = get_page_by_path('master-trainer/manage-announcements');
$announcements_page = get_page_by_path('trainer/announcements');
$resources_page = get_page_by_path('trainer/training-resources');
if (!$master_page || !$resources_page) {
if (!$manage_page || !$announcements_page || !$resources_page) {
$this->create_pages();
}
}
@ -196,8 +214,9 @@ class HVAC_Announcements_Manager {
* @return array
*/
public function add_page_slugs($slugs) {
$slugs[] = 'master-announcements';
$slugs[] = 'trainer-resources';
$slugs[] = 'manage-announcements';
$slugs[] = 'announcements';
$slugs[] = 'training-resources';
return $slugs;
}

View file

@ -28,6 +28,11 @@ class HVAC_Deactivator {
// Clear scheduled cron jobs
self::clear_cron_jobs();
// Deactivate announcements system
if (class_exists('HVAC_Announcements_Manager')) {
HVAC_Announcements_Manager::deactivate();
}
// Flush rewrite rules
flush_rewrite_rules();

View file

@ -0,0 +1,139 @@
const { chromium } = require('@playwright/test');
(async () => {
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext();
const page = await context.newPage();
console.log('🔍 Testing HVAC Announcements System on Staging...\n');
try {
// Step 1: Login as master trainer
console.log('1. Logging in as master trainer...');
await page.goto('https://upskill-staging.measurequick.com/training-login/');
await page.waitForLoadState('networkidle', { timeout: 15000 });
await page.fill('#user_login', 'test_master');
await page.fill('#user_pass', 'TestMaster123!');
await page.click('#wp-submit');
// Wait for redirect after login
await page.waitForTimeout(3000);
const currentUrl = page.url();
console.log(` ✅ Logged in successfully, current URL: ${currentUrl}`);
// Step 2: Test master trainer announcements page
console.log('\n2. Testing master trainer announcements page...');
await page.goto('https://upskill-staging.measurequick.com/master-trainer/manage-announcements/');
await page.waitForTimeout(3000);
const pageTitle = await page.title();
console.log(` Page title: ${pageTitle}`);
// Check if page loads without 404
const is404 = await page.locator('h1').textContent().then(text =>
text && (text.includes('404') || text.includes('Not Found'))
).catch(() => false);
if (is404) {
console.log(' ❌ Page returns 404 - need to create the page');
} else {
console.log(' ✅ Page loads successfully');
// Check for announcements content
const hasShortcode = await page.textContent('body').then(text =>
text.includes('[hvac_announcements_manager]')
);
const hasManager = await page.locator('#hvac-announcements-app, .hvac-announcements-manager').count();
console.log(` Shortcode visible: ${hasShortcode ? 'Yes' : 'No'}`);
console.log(` Manager component loaded: ${hasManager > 0 ? 'Yes' : 'No'}`);
}
// Step 3: Test trainer announcements page
console.log('\n3. Testing trainer announcements page...');
await page.goto('https://upskill-staging.measurequick.com/trainer/announcements/');
await page.waitForTimeout(3000);
const trainerPageTitle = await page.title();
console.log(` Page title: ${trainerPageTitle}`);
const trainerIs404 = await page.locator('h1').textContent().then(text =>
text && (text.includes('404') || text.includes('Not Found'))
).catch(() => false);
if (trainerIs404) {
console.log(' ❌ Page returns 404 - need to create the page');
} else {
console.log(' ✅ Page loads successfully');
const hasTimelineShortcode = await page.textContent('body').then(text =>
text.includes('[hvac_announcements_timeline]')
);
const hasTimeline = await page.locator('.hvac-announcements-timeline, .hvac-announcements-list').count();
console.log(` Timeline shortcode visible: ${hasTimelineShortcode ? 'Yes' : 'No'}`);
console.log(` Timeline component loaded: ${hasTimeline > 0 ? 'Yes' : 'No'}`);
}
// Step 4: Test training resources page
console.log('\n4. Testing training resources page...');
await page.goto('https://upskill-staging.measurequick.com/trainer/training-resources/');
await page.waitForTimeout(3000);
const resourcesPageTitle = await page.title();
console.log(` Page title: ${resourcesPageTitle}`);
const resourcesIs404 = await page.locator('h1').textContent().then(text =>
text && (text.includes('404') || text.includes('Not Found'))
).catch(() => false);
if (resourcesIs404) {
console.log(' ❌ Page returns 404 - need to create the page');
} else {
console.log(' ✅ Page loads successfully');
const hasGoogleDriveShortcode = await page.textContent('body').then(text =>
text.includes('[hvac_google_drive_embed')
);
const hasIframe = await page.locator('iframe').count();
console.log(` Google Drive shortcode visible: ${hasGoogleDriveShortcode ? 'Yes' : 'No'}`);
console.log(` iframe loaded: ${hasIframe > 0 ? 'Yes' : 'No'}`);
}
// Step 5: Check if pages exist in WordPress admin
console.log('\n5. Checking WordPress pages...');
await page.goto('https://upskill-staging.measurequick.com/wp-admin/edit.php?post_type=page');
await page.waitForTimeout(3000);
// Search for our pages
const searchField = await page.locator('#post-search-input');
if (await searchField.count() > 0) {
await searchField.fill('announcements');
await page.click('#search-submit');
await page.waitForTimeout(2000);
const pageRows = await page.locator('.wp-list-table tbody tr').count();
console.log(` Found ${pageRows} pages matching "announcements"`);
if (pageRows > 0) {
const titles = await page.locator('.wp-list-table .row-title').allTextContents();
console.log(` Page titles found: ${titles.join(', ')}`);
}
}
console.log('\n📋 Summary:');
console.log('- If pages return 404, they need to be created manually in WordPress');
console.log('- Templates are deployed and ready');
console.log('- Shortcodes are registered and should work once pages exist');
} catch (error) {
console.error('❌ Test failed:', error.message);
} finally {
await browser.close();
}
})();