send_approval_notification( $user_id ); } elseif ( $new_status === HVAC_Trainer_Status::STATUS_DISABLED ) { $this->send_disabled_notification( $user_id ); } } /** * Send new registration notification to admins * * @param int $user_id User ID * @param array $registration_data Registration form data */ public function send_new_registration_notification( $user_id, $registration_data ) { $user = get_userdata( $user_id ); if ( ! $user ) { return false; } // Get notification emails $options = get_option( 'hvac_ce_options', array() ); $notification_emails = isset( $options['notification_emails'] ) ? $options['notification_emails'] : get_option( 'admin_email' ); // Convert comma-separated list to array if ( is_string( $notification_emails ) ) { $notification_emails = array_map( 'trim', explode( ',', $notification_emails ) ); } // Generate approval token $approval_token = $this->generate_approval_token( $user_id ); update_user_meta( $user_id, 'hvac_approval_token', $approval_token ); // Build approval URL $approval_url = add_query_arg( array( 'hvac_approve_trainer' => $user_id, 'hvac_approval_token' => $approval_token, ), home_url( '/master-trainer/dashboard/' ) ); // Get email template $template = $this->get_email_template( 'new_registration' ); // Replace placeholders $replacements = array( '{trainer_name}' => $user->display_name, '{trainer_email}' => $user->user_email, '{business_name}' => get_user_meta( $user_id, 'business_name', true ), '{business_phone}' => get_user_meta( $user_id, 'business_phone', true ), '{business_email}' => get_user_meta( $user_id, 'business_email', true ), '{registration_date}' => date( 'F j, Y', strtotime( $user->user_registered ) ), '{application_details}' => get_user_meta( $user_id, 'application_details', true ), '{approval_url}' => $approval_url, '{website_name}' => get_bloginfo( 'name' ), '{website_url}' => home_url(), ); $subject = str_replace( array_keys( $replacements ), array_values( $replacements ), $template['subject'] ); $message = str_replace( array_keys( $replacements ), array_values( $replacements ), $template['body'] ); // Send emails $headers = array( 'Content-Type: text/html; charset=UTF-8' ); foreach ( $notification_emails as $email ) { wp_mail( $email, $subject, $message, $headers ); } return true; } /** * Send approval notification to trainer * * @param int $user_id User ID */ public function send_approval_notification( $user_id ) { $user = get_userdata( $user_id ); if ( ! $user ) { return false; } // Get email template $template = $this->get_email_template( 'account_approved' ); // Replace placeholders $replacements = array( '{trainer_name}' => $user->display_name, '{trainer_email}' => $user->user_email, '{business_name}' => get_user_meta( $user_id, 'business_name', true ), '{dashboard_url}' => home_url( '/trainer/dashboard/' ), '{login_url}' => home_url( '/community-login/' ), '{website_name}' => get_bloginfo( 'name' ), '{website_url}' => home_url(), '{current_date}' => date( 'F j, Y' ), ); $subject = str_replace( array_keys( $replacements ), array_values( $replacements ), $template['subject'] ); $message = str_replace( array_keys( $replacements ), array_values( $replacements ), $template['body'] ); // Send email $headers = array( 'Content-Type: text/html; charset=UTF-8' ); return wp_mail( $user->user_email, $subject, $message, $headers ); } /** * Send disabled notification to trainer * * @param int $user_id User ID */ public function send_disabled_notification( $user_id ) { $user = get_userdata( $user_id ); if ( ! $user ) { return false; } // Get email template $template = $this->get_email_template( 'account_disabled' ); // Replace placeholders $replacements = array( '{trainer_name}' => $user->display_name, '{trainer_email}' => $user->user_email, '{business_name}' => get_user_meta( $user_id, 'business_name', true ), '{support_email}' => get_option( 'admin_email' ), '{website_name}' => get_bloginfo( 'name' ), '{website_url}' => home_url(), '{current_date}' => date( 'F j, Y' ), ); $subject = str_replace( array_keys( $replacements ), array_values( $replacements ), $template['subject'] ); $message = str_replace( array_keys( $replacements ), array_values( $replacements ), $template['body'] ); // Send email $headers = array( 'Content-Type: text/html; charset=UTF-8' ); return wp_mail( $user->user_email, $subject, $message, $headers ); } /** * Get email template * * @param string $template_key Template key * @return array Template data with subject and body */ private function get_email_template( $template_key ) { $options = get_option( 'hvac_ce_email_templates', array() ); // Default templates $defaults = array( 'new_registration' => array( 'subject' => 'New HVAC Trainer Registration - {trainer_name}', 'body' => '

New Trainer Registration Pending Approval

A new HVAC trainer has registered and is awaiting approval.

Trainer Details:

Application Details:

{application_details}

Approve This Trainer

Or copy this link: {approval_url}

', ), 'account_approved' => array( 'subject' => 'Your HVAC Trainer Account Has Been Approved!', 'body' => '

Welcome to {website_name}!

Dear {trainer_name},

Great news! Your HVAC trainer account has been approved and you now have full access to create and manage training events.

What You Can Do Now:

Getting Started:

  1. Log in to your account
  2. Visit your Trainer Dashboard
  3. Click "Create New Event" to post your first training event
  4. Share your event with your network to maximize attendance

If you have any questions or need assistance, please don\'t hesitate to reach out to our support team.

We\'re excited to have you as part of our training community!

Best regards,
The {website_name} Team

', ), 'account_disabled' => array( 'subject' => 'Your HVAC Trainer Account Has Been Disabled', 'body' => '

Account Status Update

Dear {trainer_name},

We regret to inform you that your HVAC trainer account on {website_name} has been disabled.

Your account may have been disabled for one of the following reasons:

If you believe this action was taken in error or would like to discuss reactivating your account, please contact our support team at {support_email}.

We appreciate your understanding.

Sincerely,
The {website_name} Team

', ), ); // Return custom template if exists, otherwise default if ( isset( $options[$template_key] ) ) { return $options[$template_key]; } return isset( $defaults[$template_key] ) ? $defaults[$template_key] : array( 'subject' => '', 'body' => '' ); } /** * Generate approval token * * @param int $user_id User ID * @return string Token */ private function generate_approval_token( $user_id ) { return wp_hash( $user_id . time() . wp_rand() ); } /** * Handle approval request from email link */ public function handle_approval_request() { $user_id = get_query_var( 'hvac_approve_trainer' ); $token = get_query_var( 'hvac_approval_token' ); if ( ! $user_id || ! $token ) { return; } // Verify token $stored_token = get_user_meta( $user_id, 'hvac_approval_token', true ); if ( $token !== $stored_token ) { wp_die( __( 'Invalid approval token.', 'hvac-community-events' ) ); } // Check if user is logged in if ( ! is_user_logged_in() ) { // Store approval request in session/transient set_transient( 'hvac_pending_approval_' . $token, $user_id, HOUR_IN_SECONDS ); // Redirect to login with return URL $login_url = add_query_arg( array( 'redirect_to' => urlencode( add_query_arg( array( 'hvac_approve_trainer' => $user_id, 'hvac_approval_token' => $token, ), home_url( '/master-trainer/dashboard/' ) ) ), ), home_url( '/community-login/' ) ); wp_redirect( $login_url ); exit; } // Check if current user can approve trainers if ( ! current_user_can( 'view_master_dashboard' ) && ! current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have permission to approve trainers.', 'hvac-community-events' ) ); } // Approve the trainer if ( ! class_exists( 'HVAC_Trainer_Status' ) ) { require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-trainer-status.php'; } $result = HVAC_Trainer_Status::set_trainer_status( $user_id, HVAC_Trainer_Status::STATUS_APPROVED ); if ( $result ) { // Delete the token delete_user_meta( $user_id, 'hvac_approval_token' ); // Get trainer info for message $trainer = get_userdata( $user_id ); $message = sprintf( __( 'Trainer %s with email %s has been approved!', 'hvac-community-events' ), $trainer->display_name, $trainer->user_email ); // Store success message in transient set_transient( 'hvac_approval_message', $message, 30 ); } // Redirect to master dashboard wp_redirect( home_url( '/master-trainer/dashboard/' ) ); exit; } /** * AJAX handler for bulk status updates */ public function ajax_bulk_update_status() { // Check nonce if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'hvac_master_dashboard_nonce' ) ) { wp_die( 'Security check failed' ); } // Check permissions if ( ! current_user_can( 'view_master_dashboard' ) && ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Insufficient permissions' ) ); } // Get parameters $user_ids = isset( $_POST['user_ids'] ) ? array_map( 'intval', $_POST['user_ids'] ) : array(); $new_status = isset( $_POST['status'] ) ? sanitize_text_field( $_POST['status'] ) : ''; if ( empty( $user_ids ) || empty( $new_status ) ) { wp_send_json_error( array( 'message' => 'Missing required parameters' ) ); } // Load status class if ( ! class_exists( 'HVAC_Trainer_Status' ) ) { require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-trainer-status.php'; } // Perform bulk update $results = HVAC_Trainer_Status::bulk_update_status( $user_ids, $new_status ); if ( $results['success'] > 0 ) { wp_send_json_success( array( 'message' => sprintf( __( 'Successfully updated %d trainer(s). %d failed.', 'hvac-community-events' ), $results['success'], $results['failed'] ), 'results' => $results, ) ); } else { wp_send_json_error( array( 'message' => __( 'Failed to update trainer statuses.', 'hvac-community-events' ), 'results' => $results, ) ); } } }