upskill-event-manager/wordpress-dev/bin/restore-dashboard-simple.sh
bengizmo a82616b6f7 Fix dashboard data inconsistency and enhance test suite with WP API
- Fixed dashboard data class to use consistent post_author queries instead of mixed _EventOrganizerID meta queries
- This resolves the issue where dashboard showed 18 events but 0 tickets/revenue
- Added WordPress API credentials to environment (.env)
- Created comprehensive API debugging utilities (wp-api-debug.sh, wp-api-fix.sh, api-only-debug.sh)
- Enhanced test and deployment suite with WordPress REST API capabilities
- Root cause: get_total_tickets_sold() and get_total_revenue() were using _EventOrganizerID while other methods used post_author

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-05-22 19:24:18 -03:00

254 lines
No EOL
7.9 KiB
Bash
Executable file

#!/bin/bash
# Exit on error
set -e
# Source environment variables
if [ -f ".env" ]; then
source .env
else
echo "Error: .env file not found. Please create it with the required variables."
exit 1
fi
echo "===== Simple Dashboard Restoration ====="
# Create a simplified PHP restoration script
echo "Creating dashboard restoration script..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && cat > restore-dashboard.php << 'EOF'
<?php
require_once('wp-load.php');
echo \"Restoring dashboard data class...\n\";
// Create working dashboard data class
\$class_content = '<?php
/**
* HVAC Community Events Dashboard Data Handler
*/
if ( ! defined( \"ABSPATH\" ) ) {
exit;
}
class HVAC_Dashboard_Data {
private int \$user_id;
public function __construct( int \$user_id ) {
\$this->user_id = \$user_id;
}
public function get_total_events_count() : int {
global \$wpdb;
\$count = \$wpdb->get_var( \$wpdb->prepare(
\"SELECT COUNT(*) FROM {\$wpdb->posts}
WHERE post_type = %s
AND post_author = %d
AND post_status IN (\\\"publish\\\", \\\"future\\\", \\\"draft\\\", \\\"pending\\\", \\\"private\\\")\",
Tribe__Events__Main::POSTTYPE,
\$this->user_id
) );
return (int) \$count;
}
public function get_upcoming_events_count() : int {
global \$wpdb;
\$today = date( \"Y-m-d H:i:s\" );
\$count = \$wpdb->get_var( \$wpdb->prepare(
\"SELECT COUNT(*) FROM {\$wpdb->posts} p
LEFT JOIN {\$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = \\\"_EventStartDate\\\"
WHERE p.post_type = %s
AND p.post_author = %d
AND p.post_status IN (\\\"publish\\\", \\\"future\\\")
AND (pm.meta_value >= %s OR pm.meta_value IS NULL)\",
Tribe__Events__Main::POSTTYPE,
\$this->user_id,
\$today
) );
return (int) \$count;
}
public function get_past_events_count() : int {
global \$wpdb;
\$today = date( \"Y-m-d H:i:s\" );
\$count = \$wpdb->get_var( \$wpdb->prepare(
\"SELECT COUNT(*) FROM {\$wpdb->posts} p
LEFT JOIN {\$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = \\\"_EventEndDate\\\"
WHERE p.post_type = %s
AND p.post_author = %d
AND p.post_status IN (\\\"publish\\\", \\\"private\\\")
AND pm.meta_value < %s\",
Tribe__Events__Main::POSTTYPE,
\$this->user_id,
\$today
) );
return (int) \$count;
}
public function get_total_tickets_sold() : int {
\$total_tickets = 0;
\$args = array(
\"post_type\" => Tribe__Events__Main::POSTTYPE,
\"author\" => \$this->user_id,
\"post_status\" => array( \"publish\", \"future\", \"draft\", \"pending\", \"private\" ),
\"posts_per_page\" => -1,
\"fields\" => \"ids\",
);
\$event_ids = get_posts( \$args );
if ( ! empty( \$event_ids ) ) {
foreach ( \$event_ids as \$event_id ) {
\$sold = get_post_meta( \$event_id, \"_tribe_tickets_sold\", true );
if (!is_numeric(\$sold)) {
\$sold = get_post_meta( \$event_id, \"_tribe_ticket_sold_count\", true );
}
if ( is_numeric( \$sold ) ) {
\$total_tickets += (int) \$sold;
}
}
}
return \$total_tickets;
}
public function get_total_revenue() : float {
\$total_revenue = 0.0;
\$args = array(
\"post_type\" => Tribe__Events__Main::POSTTYPE,
\"author\" => \$this->user_id,
\"post_status\" => array( \"publish\", \"future\", \"draft\", \"pending\", \"private\" ),
\"posts_per_page\" => -1,
\"fields\" => \"ids\",
);
\$event_ids = get_posts( \$args );
if ( ! empty( \$event_ids ) ) {
foreach ( \$event_ids as \$event_id ) {
\$revenue = get_post_meta( \$event_id, \"_tribe_revenue_total\", true );
if ( is_numeric( \$revenue ) ) {
\$total_revenue += (float) \$revenue;
}
}
}
return \$total_revenue;
}
public function get_events_table_data( string \$filter_status = \"all\" ) : array {
\$events_data = [];
\$valid_statuses = array( \"publish\", \"future\", \"draft\", \"pending\", \"private\" );
\$post_status = ( \"all\" === \$filter_status || ! in_array( \$filter_status, \$valid_statuses, true ) )
? \$valid_statuses
: array( \$filter_status );
\$args = array(
\"post_type\" => Tribe__Events__Main::POSTTYPE,
\"author\" => \$this->user_id,
\"post_status\" => \$post_status,
\"posts_per_page\" => -1,
\"orderby\" => \"meta_value\",
\"meta_key\" => \"_EventStartDate\",
\"order\" => \"DESC\",
);
\$query = new WP_Query( \$args );
if ( \$query->have_posts() ) {
while ( \$query->have_posts() ) {
\$query->the_post();
\$event_id = get_the_ID();
\$sold = get_post_meta( \$event_id, \"_tribe_tickets_sold\", true );
\$revenue = get_post_meta( \$event_id, \"_tribe_revenue_total\", true );
\$events_data[] = array(
\"id\" => \$event_id,
\"status\" => get_post_status( \$event_id ),
\"name\" => get_the_title(),
\"link\" => get_permalink( \$event_id ),
\"start_date_ts\" => strtotime( get_post_meta( \$event_id, \"_EventStartDate\", true ) ),
\"organizer_id\" => (int) get_post_meta( \$event_id, \"_EventOrganizerID\", true ),
\"capacity\" => \"Unlimited\",
\"sold\" => is_numeric( \$sold ) ? (int) \$sold : 0,
\"revenue\" => is_numeric( \$revenue ) ? (float) \$revenue : 0.0,
);
}
wp_reset_postdata();
}
return \$events_data;
}
}';
// Write the class file
\$dashboard_file = WP_CONTENT_DIR . '/plugins/hvac-community-events/includes/class-hvac-dashboard-data.php';
if (file_put_contents(\$dashboard_file, \$class_content)) {
echo \"Dashboard class restored successfully\n\";
} else {
echo \"Failed to restore dashboard class\n\";
exit(1);
}
// Create test data
\$user = get_user_by('login', 'test_trainer');
if (!\$user) {
echo \"test_trainer user not found\n\";
exit(1);
}
\$events = get_posts(array(
'post_type' => 'tribe_events',
'author' => \$user->ID,
'posts_per_page' => 1
));
if (empty(\$events)) {
echo \"Creating test event...\n\";
\$event_id = wp_insert_post(array(
'post_title' => 'HVAC Fundamentals Course',
'post_content' => 'Learn the fundamentals of HVAC systems and maintenance.',
'post_status' => 'publish',
'post_type' => 'tribe_events',
'post_author' => \$user->ID
));
if (\$event_id) {
update_post_meta(\$event_id, '_EventStartDate', date('Y-m-d H:i:s', strtotime('+1 week')));
update_post_meta(\$event_id, '_EventEndDate', date('Y-m-d H:i:s', strtotime('+1 week +6 hours')));
update_post_meta(\$event_id, '_tribe_tickets_sold', 12);
update_post_meta(\$event_id, '_tribe_revenue_total', 1200.00);
echo \"Created test event\n\";
}
}
// Test the dashboard
\$dashboard_data = new HVAC_Dashboard_Data(\$user->ID);
echo \"\\nDashboard test results:\\n\";
echo \"Total Events: \" . \$dashboard_data->get_total_events_count() . \"\\n\";
echo \"Upcoming Events: \" . \$dashboard_data->get_upcoming_events_count() . \"\\n\";
echo \"Past Events: \" . \$dashboard_data->get_past_events_count() . \"\\n\";
echo \"Total Tickets: \" . \$dashboard_data->get_total_tickets_sold() . \"\\n\";
echo \"Total Revenue: $\" . \$dashboard_data->get_total_revenue() . \"\\n\";
echo \"\\nDashboard restoration complete!\\n\";
EOF"
# Execute the restoration
echo "Executing dashboard restoration..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && php restore-dashboard.php"
# Clean up
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && rm restore-dashboard.php"
echo -e "\n===== Dashboard Restoration Complete ====="
echo "The dashboard has been restored and should now display data correctly."
echo "Please refresh the dashboard page to see the results."