handle_certificate_download($token); exit; // Stop WordPress from processing further } } /** * Handle certificate download */ protected function handle_certificate_download($token) { // Validate the token $certificate_data = $this->validate_download_token($token); if (!$certificate_data) { if (class_exists('HVAC_Logger')) { HVAC_Logger::error("Invalid or expired certificate token: $token", 'Certificates'); } wp_die(__('Invalid or expired certificate download link.', 'hvac-community-events'), 'Certificate Error', array('response' => 404)); } // Get file path $file_path = $this->get_certificate_file_path($certificate_data); if (!$file_path || !file_exists($file_path)) { if (class_exists('HVAC_Logger')) { HVAC_Logger::error("Certificate file not found for token: $token", 'Certificates'); } wp_die(__('Certificate file not found.', 'hvac-community-events'), 'Certificate Error', array('response' => 404)); } // Log successful access if (class_exists('HVAC_Logger')) { HVAC_Logger::info("Serving certificate file: $file_path", 'Certificates'); } // Serve the file $this->serve_certificate_file($file_path, $certificate_data); } /** * Validate a certificate download token. */ protected function validate_download_token($token) { // Check if token exists in transients $certificate_data = get_transient('hvac_certificate_token_' . $token); if (!$certificate_data) { return false; } // Delete the transient to prevent reuse delete_transient('hvac_certificate_token_' . $token); return $certificate_data; } /** * Get the full file path for a certificate. */ protected function get_certificate_file_path($certificate_data) { if (empty($certificate_data['file_path'])) { return false; } $upload_dir = wp_upload_dir(); $file_path = $upload_dir['basedir'] . '/' . $certificate_data['file_path']; if (file_exists($file_path)) { return $file_path; } return false; } /** * Serve a certificate file for download. */ protected function serve_certificate_file($file_path, $certificate_data) { // Get file information $file_name = basename($file_path); $file_size = filesize($file_path); $file_ext = pathinfo($file_path, PATHINFO_EXTENSION); // Set download filename $event_name = sanitize_title($certificate_data['event_name'] ?? 'event'); $attendee_name = sanitize_title($certificate_data['attendee_name'] ?? 'attendee'); $download_filename = "certificate-{$event_name}-{$attendee_name}.{$file_ext}"; // Send headers nocache_headers(); header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="' . $download_filename . '"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . $file_size); // Disable output buffering if (ob_get_level()) { ob_end_clean(); } // Output the file readfile($file_path); } /** * Parse request fallback method */ public function parse_certificate_request($wp) { // Get the request URI $request_uri = $_SERVER['REQUEST_URI']; $parsed_url = parse_url($request_uri); $path = $parsed_url['path'] ?? ''; // Remove any trailing slash for consistency $path = rtrim($path, '/'); // Check if this is a certificate URL if (preg_match('#^/hvac-certificate/([a-zA-Z0-9]{32})$#', $path, $matches)) { $token = $matches[1]; // Log the request if (class_exists('HVAC_Logger')) { HVAC_Logger::info("Certificate URL detected via parse_request - Token: $token", 'Certificates'); } // Handle the certificate download $this->handle_certificate_download($token); exit; // Stop WordPress from processing further } } }